Skip to content

Commit d97f0e9

Browse files
authored
[CIR] add support for file scope assembly (#152093)
This PR adds a support for file scope assembly in CIR.
1 parent ad3196d commit d97f0e9

File tree

6 files changed

+43
-0
lines changed

6 files changed

+43
-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 getCalleeAttrName() { return "callee"; }
4141
static llvm::StringRef getNoThrowAttrName() { return "nothrow"; }
4242
static llvm::StringRef getSideEffectAttrName() { return "side_effect"; }
43+
static llvm::StringRef getModuleLevelAsmAttrName() { return "cir.module_asm"; }
4344

4445
void registerAttributes();
4546
void registerTypes();

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,21 @@ void CIRGenModule::emitTopLevelDecl(Decl *decl) {
13651365
assert(!cir::MissingFeatures::generateDebugInfo());
13661366
assert(!cir::MissingFeatures::cxxRecordStaticMembers());
13671367
break;
1368+
1369+
case Decl::FileScopeAsm:
1370+
// File-scope asm is ignored during device-side CUDA compilation.
1371+
if (langOpts.CUDA && langOpts.CUDAIsDevice)
1372+
break;
1373+
// File-scope asm is ignored during device-side OpenMP compilation.
1374+
if (langOpts.OpenMPIsTargetDevice)
1375+
break;
1376+
// File-scope asm is ignored during device-side SYCL compilation.
1377+
if (langOpts.SYCLIsDevice)
1378+
break;
1379+
auto *file_asm = cast<FileScopeAsmDecl>(decl);
1380+
std::string line = file_asm->getAsmString();
1381+
globalScopeAsm.push_back(builder.getStringAttr(line));
1382+
break;
13681383
}
13691384
}
13701385

@@ -1978,6 +1993,9 @@ void CIRGenModule::release() {
19781993
emitDeferred();
19791994
applyReplacements();
19801995

1996+
theModule->setAttr(cir::CIRDialect::getModuleLevelAsmAttrName(),
1997+
builder.getArrayAttr(globalScopeAsm));
1998+
19811999
// There's a lot of code that is not implemented yet.
19822000
assert(!cir::MissingFeatures::cgmRelease());
19832001
}

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class CIRGenModule : public CIRGenTypeCache {
9090
/// for FunctionDecls's.
9191
CIRGenFunction *curCGF = nullptr;
9292

93+
llvm::SmallVector<mlir::Attribute> globalScopeAsm;
94+
9395
public:
9496
mlir::ModuleOp getModule() const { return theModule; }
9597
CIRGenBuilderTy &getBuilder() { return builder; }

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,11 @@ void ConvertCIRToLLVMPass::processCIRAttrs(mlir::ModuleOp module) {
21432143
module->getAttr(cir::CIRDialect::getTripleAttrName()))
21442144
module->setAttr(mlir::LLVM::LLVMDialect::getTargetTripleAttrName(),
21452145
tripleAttr);
2146+
2147+
if (mlir::Attribute asmAttr =
2148+
module->getAttr(cir::CIRDialect::getModuleLevelAsmAttrName()))
2149+
module->setAttr(mlir::LLVM::LLVMDialect::getModuleLevelAsmAttrName(),
2150+
asmAttr);
21462151
}
21472152

21482153
void ConvertCIRToLLVMPass::runOnOperation() {

clang/test/CIR/CodeGen/module-asm.c

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)