diff options
author | gitoleg <forown@yandex.ru> | 2025-08-07 20:12:58 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-08-07 10:12:58 -0700 |
commit | d97f0e93642722380be9ed190c17ea895817c339 (patch) | |
tree | 44bf7524ed96ca246d40bd7340b2427d1105dc03 | |
parent | ad3196d7595dd53c4021b4bf4cd7bcefd85853df (diff) | |
download | llvm-d97f0e93642722380be9ed190c17ea895817c339.zip llvm-d97f0e93642722380be9ed190c17ea895817c339.tar.gz llvm-d97f0e93642722380be9ed190c17ea895817c339.tar.bz2 |
[CIR] add support for file scope assembly (#152093)
This PR adds a support for file scope assembly in CIR.
-rw-r--r-- | clang/include/clang/CIR/Dialect/IR/CIRDialect.td | 1 | ||||
-rw-r--r-- | clang/lib/CIR/CodeGen/CIRGenModule.cpp | 18 | ||||
-rw-r--r-- | clang/lib/CIR/CodeGen/CIRGenModule.h | 2 | ||||
-rw-r--r-- | clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 5 | ||||
-rw-r--r-- | clang/test/CIR/CodeGen/module-asm.c | 6 | ||||
-rw-r--r-- | clang/test/CIR/Lowering/module-asm.cir | 11 |
6 files changed, 43 insertions, 0 deletions
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td index 3fdbf65..fdba4e4 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td @@ -40,6 +40,7 @@ def CIR_Dialect : Dialect { static llvm::StringRef getCalleeAttrName() { return "callee"; } static llvm::StringRef getNoThrowAttrName() { return "nothrow"; } static llvm::StringRef getSideEffectAttrName() { return "side_effect"; } + static llvm::StringRef getModuleLevelAsmAttrName() { return "cir.module_asm"; } void registerAttributes(); void registerTypes(); diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 425250d..ff6d293 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -1365,6 +1365,21 @@ void CIRGenModule::emitTopLevelDecl(Decl *decl) { assert(!cir::MissingFeatures::generateDebugInfo()); assert(!cir::MissingFeatures::cxxRecordStaticMembers()); break; + + case Decl::FileScopeAsm: + // File-scope asm is ignored during device-side CUDA compilation. + if (langOpts.CUDA && langOpts.CUDAIsDevice) + break; + // File-scope asm is ignored during device-side OpenMP compilation. + if (langOpts.OpenMPIsTargetDevice) + break; + // File-scope asm is ignored during device-side SYCL compilation. + if (langOpts.SYCLIsDevice) + break; + auto *file_asm = cast<FileScopeAsmDecl>(decl); + std::string line = file_asm->getAsmString(); + globalScopeAsm.push_back(builder.getStringAttr(line)); + break; } } @@ -1978,6 +1993,9 @@ void CIRGenModule::release() { emitDeferred(); applyReplacements(); + theModule->setAttr(cir::CIRDialect::getModuleLevelAsmAttrName(), + builder.getArrayAttr(globalScopeAsm)); + // There's a lot of code that is not implemented yet. assert(!cir::MissingFeatures::cgmRelease()); } diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h index 5d07d38..163a0fc 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.h +++ b/clang/lib/CIR/CodeGen/CIRGenModule.h @@ -90,6 +90,8 @@ private: /// for FunctionDecls's. CIRGenFunction *curCGF = nullptr; + llvm::SmallVector<mlir::Attribute> globalScopeAsm; + public: mlir::ModuleOp getModule() const { return theModule; } CIRGenBuilderTy &getBuilder() { return builder; } diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 7e1c9fb..43a1b51 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -2143,6 +2143,11 @@ void ConvertCIRToLLVMPass::processCIRAttrs(mlir::ModuleOp module) { module->getAttr(cir::CIRDialect::getTripleAttrName())) module->setAttr(mlir::LLVM::LLVMDialect::getTargetTripleAttrName(), tripleAttr); + + if (mlir::Attribute asmAttr = + module->getAttr(cir::CIRDialect::getModuleLevelAsmAttrName())) + module->setAttr(mlir::LLVM::LLVMDialect::getModuleLevelAsmAttrName(), + asmAttr); } void ConvertCIRToLLVMPass::runOnOperation() { diff --git a/clang/test/CIR/CodeGen/module-asm.c b/clang/test/CIR/CodeGen/module-asm.c new file mode 100644 index 0000000..e6cec5e --- /dev/null +++ b/clang/test/CIR/CodeGen/module-asm.c @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s + +// CHECK: cir.module_asm = [".globl bar", ".globl foo"] +__asm (".globl bar"); +__asm (".globl foo"); diff --git a/clang/test/CIR/Lowering/module-asm.cir b/clang/test/CIR/Lowering/module-asm.cir new file mode 100644 index 0000000..b802cda --- /dev/null +++ b/clang/test/CIR/Lowering/module-asm.cir @@ -0,0 +1,11 @@ +// RUN: cir-opt %s -cir-to-llvm -o %t.cir +// RUN: FileCheck %s --input-file=%t.cir + +// RUN: cir-translate -cir-to-llvmir --disable-cc-lowering -o %t.ll %s +// RUN: FileCheck -check-prefix=LLVM --input-file=%t.ll %s + +// CHECK: llvm.module_asm = [".globl bar", ".globl foo"] +// LLVM: module asm ".globl bar" +// LLVM: module asm ".globl foo" +module attributes {cir.module_asm = [".globl bar", ".globl foo"]} { +} |