Skip to content

Commit a90b91b

Browse files
[CIR] Backport FileScopeAsm support from upstream (#2055)
1 parent 6384e63 commit a90b91b

File tree

6 files changed

+44
-0
lines changed

6 files changed

+44
-0
lines changed

clang/include/clang/CIR/Dialect/IR/CIRDialect.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def CIR_Dialect : Dialect {
4040
static llvm::StringRef getTripleAttrName() { return "cir.triple"; }
4141
static llvm::StringRef getOptInfoAttrName() { return "cir.opt_info"; }
4242
static llvm::StringRef getUWTableAttrName() { return "cir.uwtable"; }
43+
static llvm::StringRef getModuleLevelAsmAttrName() { return "cir.module_asm"; }
4344

4445
static llvm::StringRef getGlobalCtorsAttrName() { return "cir.global_ctors"; }
4546
static llvm::StringRef getGlobalDtorsAttrName() { return "cir.global_dtors"; }

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,6 +2190,20 @@ void CIRGenModule::emitTopLevelDecl(Decl *decl) {
21902190
case Decl::Enum:
21912191
assert(!cir::MissingFeatures::generateDebugInfo() && "NYI");
21922192
break;
2193+
case Decl::FileScopeAsm:
2194+
// File-scope asm is ignored during device-side CUDA compilation.
2195+
if (langOpts.CUDA && langOpts.CUDAIsDevice)
2196+
break;
2197+
// File-scope asm is ignored during device-side OpenMP compilation.
2198+
if (langOpts.OpenMPIsTargetDevice)
2199+
break;
2200+
// File-scope asm is ignored during device-side SYCL compilation.
2201+
if (langOpts.SYCLIsDevice)
2202+
break;
2203+
auto *file_asm = cast<FileScopeAsmDecl>(decl);
2204+
std::string line = file_asm->getAsmString();
2205+
globalScopeAsm.push_back(builder.getStringAttr(line));
2206+
break;
21932207
}
21942208
}
21952209

@@ -3603,6 +3617,11 @@ void CIRGenModule::Release() {
36033617
emitVTablesOpportunistically();
36043618
assert(!MissingFeatures::applyGlobalValReplacements());
36053619
applyReplacements();
3620+
3621+
// Set module-level assembly attribute
3622+
theModule->setAttr(cir::CIRDialect::getModuleLevelAsmAttrName(),
3623+
builder.getArrayAttr(globalScopeAsm));
3624+
36063625
assert(!MissingFeatures::emitMultiVersionFunctions());
36073626

36083627
assert(!MissingFeatures::incrementalExtensions());

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,8 @@ class CIRGenModule : public CIRGenTypeCache {
993993
/// one of them.
994994
cir::AnnotationAttr emitAnnotateAttr(const clang::AnnotateAttr *aa);
995995

996+
llvm::SmallVector<mlir::Attribute> globalScopeAsm;
997+
996998
private:
997999
// An ordered map of canonical GlobalDecls to their mangled names.
9981000
llvm::MapVector<clang::GlobalDecl, llvm::StringRef> MangledDeclNames;

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5425,6 +5425,11 @@ void ConvertCIRToLLVMPass::processCIRAttrs(mlir::ModuleOp module) {
54255425
module->setAttr(mlir::LLVM::LLVMDialect::getTargetTripleAttrName(),
54265426
tripleAttr);
54275427

5428+
if (mlir::Attribute asmAttr =
5429+
module->getAttr(cir::CIRDialect::getModuleLevelAsmAttrName()))
5430+
module->setAttr(mlir::LLVM::LLVMDialect::getModuleLevelAsmAttrName(),
5431+
asmAttr);
5432+
54285433
// Strip the CIR attributes.
54295434
module->removeAttr(cir::CIRDialect::getSOBAttrName());
54305435
module->removeAttr(cir::CIRDialect::getSourceLanguageAttrName());
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s
3+
4+
// CHECK: cir.module_asm = [".globl bar", ".globl foo"]
5+
__asm (".globl bar");
6+
__asm (".globl foo");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: cir-opt %s -cir-to-llvm -o %t.cir
2+
// RUN: FileCheck %s --input-file=%t.cir
3+
4+
// RUN: cir-translate -cir-to-llvmir --disable-cc-lowering -o %t.ll %s
5+
// RUN: FileCheck -check-prefix=LLVM --input-file=%t.ll %s
6+
7+
// CHECK: llvm.module_asm = [".globl bar", ".globl foo"]
8+
// LLVM: module asm ".globl bar"
9+
// LLVM: module asm ".globl foo"
10+
module attributes {cir.module_asm = [".globl bar", ".globl foo"]} {
11+
}

0 commit comments

Comments
 (0)