Skip to content

[CIR] add support for file scope assembly #152093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,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;
}
}

Expand Down Expand Up @@ -1975,6 +1990,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());
}
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class CIRGenModule : public CIRGenTypeCache {
/// for FunctionDecls's.
CIRGenFunction *curCGF = nullptr;

llvm::SmallVector<mlir::Attribute> globalScopeAsm;

public:
mlir::ModuleOp getModule() const { return theModule; }
CIRGenBuilderTy &getBuilder() { return builder; }
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 6 additions & 0 deletions clang/test/CIR/CodeGen/module-asm.c
Original file line number Diff line number Diff line change
@@ -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");
11 changes: 11 additions & 0 deletions clang/test/CIR/Lowering/module-asm.cir
Original file line number Diff line number Diff line change
@@ -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"]} {
}