-
Notifications
You must be signed in to change notification settings - Fork 182
[CIR] Add support for FileScopeAsm #1995
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| #include "mlir/IR/OperationSupport.h" | ||
| #include "mlir/IR/SymbolTable.h" | ||
| #include "mlir/IR/Verifier.h" | ||
| #include "clang/AST/Decl.h" | ||
| #include "clang/AST/Expr.h" | ||
| #include "clang/Basic/Cuda.h" | ||
| #include "clang/CIR/Dialect/IR/CIRTypes.h" | ||
|
|
@@ -2072,6 +2073,9 @@ void CIRGenModule::emitTopLevelDecl(Decl *decl) { | |
| emitTopLevelDecl(childDecl); | ||
| break; | ||
| } | ||
| case Decl::FileScopeAsm: | ||
| buildFileScopeAsm(cast<FileScopeAsmDecl>(decl)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow OG skeleton:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just call |
||
| break; | ||
| case Decl::PragmaComment: { | ||
| const auto *PCD = cast<PragmaCommentDecl>(decl); | ||
| switch (PCD->getCommentKind()) { | ||
|
|
@@ -3776,6 +3780,9 @@ void CIRGenModule::Release() { | |
| if (getTriple().isPPC() && !MissingFeatures::mustTailCallUndefinedGlobals()) { | ||
| llvm_unreachable("NYI"); | ||
| } | ||
|
|
||
| // Finalize module attributes (including inline assembly) | ||
| finalizeModuleAttributes(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OG doesn't do this right here, why are we? There's already other model level attributes being handled, the content of this function should be there |
||
| } | ||
|
|
||
| namespace { | ||
|
|
@@ -4430,3 +4437,28 @@ cir::LabelOp | |
| CIRGenModule::lookupBlockAddressInfo(cir::BlockAddrInfoAttr blockInfo) { | ||
| return blockAddressInfoToLabel.lookup(blockInfo); | ||
| } | ||
|
|
||
| void CIRGenModule::buildFileScopeAsm(const FileScopeAsmDecl *D) { | ||
| // Get assembly string from declaration | ||
| appendModuleInlineAsm(D->getAsmString()); | ||
| } | ||
|
|
||
| void CIRGenModule::finalizeModuleAttributes() { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Name might be too generic. Should I rename it to something more specific? |
||
| if (moduleAsm.empty()) | ||
| return; | ||
|
|
||
| // Build array of string attributes | ||
| llvm::SmallVector<mlir::Attribute> asmStrings; | ||
| for (const auto &asmStr : moduleAsm) { | ||
| asmStrings.push_back(builder.getStringAttr(asmStr)); | ||
| } | ||
|
|
||
| // Create array attribute | ||
| mlir::ArrayAttr asmArray = builder.getArrayAttr(asmStrings); | ||
|
|
||
| // Create ModuleAsmAttr | ||
| auto moduleAsmAttr = cir::ModuleAsmAttr::get(builder.getContext(), asmArray); | ||
|
|
||
| // Set as module attribute | ||
| theModule->setAttr("cir.module_asm", moduleAsmAttr); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
| #include "mlir/IR/Attributes.h" | ||
| #include "mlir/IR/DialectRegistry.h" | ||
| #include "mlir/Target/LLVMIR/LLVMTranslationInterface.h" | ||
| #include "mlir/Target/LLVMIR/ModuleTranslation.h" | ||
|
|
@@ -86,6 +87,17 @@ class CIRDialectLLVMIRTranslationInterface | |
| mlir::dyn_cast<cir::UWTableAttr>(attribute.getValue())) | ||
| llvmModule->setUwtable(convertUWTableKind(uwTableAttr.getValue())); | ||
|
|
||
| // Handle module-level inline assembly | ||
| if (auto moduleAsmAttr = | ||
| mlir::dyn_cast<cir::ModuleAsmAttr>(attribute.getValue())) { | ||
| mlir::ArrayAttr asmStrings = moduleAsmAttr.getAsmStrings(); | ||
| for (mlir::Attribute attr : asmStrings) { | ||
| if (auto strAttr = mlir::dyn_cast<mlir::StringAttr>(attr)) { | ||
| llvmModule->appendModuleInlineAsm(strAttr.getValue()); | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+90
to
+99
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to place the lowering here or is there another place you guys prefer? |
||
|
|
||
| // Drop ammended CIR attribute from LLVM op. | ||
| module->removeAttr(attribute.getName()); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir \ | ||
| // RUN: -emit-cir %s -o %t.cir | ||
| // RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR | ||
|
|
||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir \ | ||
| // RUN: -emit-llvm %s -o %t.ll | ||
| // RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM | ||
|
|
||
| // Test basic file-scope assembly | ||
| asm(".section .text"); | ||
| asm(".global my_func"); | ||
| asm("my_func: ret"); | ||
|
|
||
| // Test file-scope assembly with functions | ||
| int foo() { | ||
| return 42; | ||
| } | ||
|
|
||
| asm(".data"); | ||
| asm(".align 8"); | ||
|
|
||
| int bar() { | ||
| return 24; | ||
| } | ||
|
|
||
| // Test file-scope assembly at end of file | ||
| asm(".section .rodata"); | ||
| asm(".string \"hello\""); | ||
|
|
||
| int main() { | ||
| return foo() + bar(); | ||
| } | ||
|
|
||
| // CIR: cir.module_asm = #cir.module_asm<[".section .text", ".global my_func", "my_func: ret", ".data", ".align 8", ".section .rodata", ".string {{\\22}}hello{{\\22}}" | ||
|
|
||
| // CIR: cir.func {{.*}}@foo | ||
| // CIR: cir.func {{.*}}@bar | ||
| // CIR: cir.func {{.*}}@main | ||
|
|
||
| // LLVM: module asm ".section .text" | ||
| // LLVM: module asm ".global my_func" | ||
| // LLVM: module asm "my_func: ret" | ||
| // LLVM: module asm ".data" | ||
| // LLVM: module asm ".align 8" | ||
| // LLVM: module asm ".section .rodata" | ||
| // LLVM: module asm ".string {{\\22}}hello{{\\22}}" | ||
|
|
||
| // LLVM: define{{.*}} i32 @foo() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look for |
||
| // LLVM: define{{.*}} i32 @bar() | ||
| // LLVM: define{{.*}} i32 @main() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
->
CIR_ModuleAsmAttr?