Skip to content

Commit b3c2b94

Browse files
seven-milelanza
authored andcommitted
[CIR][LowerToLLVM] Lowering triple from cir.triple attribute (#1125)
Currently, the final `target triple` in LLVM IR is set in `CIRGenAction`, which is not executed by cir tools like `cir-translate`. This PR delay its assignment to LLVM lowering, enabling sharing the emitting of `target triple` between different invoking paths.
1 parent 5ed9127 commit b3c2b94

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

clang/lib/CIR/FrontendAction/CIRGenAction.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,6 @@ class CIRGenConsumer : public clang::ASTConsumer {
292292
feOptions.ClangIRDisableCIRVerifier,
293293
!feOptions.ClangIRCallConvLowering);
294294

295-
llvmModule->setTargetTriple(targetOptions.Triple);
296-
297295
BackendAction backendAction = getBackendActionFromOutputType(action);
298296

299297
emitBackendOutput(compilerInstance, codeGenOptions,

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,8 @@ struct ConvertCIRToLLVMPass
12901290
llvm::StringMap<mlir::LLVM::GlobalOp> &argStringGlobalsMap,
12911291
llvm::MapVector<mlir::ArrayAttr, mlir::LLVM::GlobalOp> &argsVarMap);
12921292

1293+
void processCIRAttrs(mlir::ModuleOp moduleOp);
1294+
12931295
virtual StringRef getArgument() const override { return "cir-flat-to-llvm"; }
12941296
};
12951297

@@ -4785,6 +4787,18 @@ void ConvertCIRToLLVMPass::buildGlobalAnnotationsVar(
47854787
}
47864788
}
47874789

4790+
void ConvertCIRToLLVMPass::processCIRAttrs(mlir::ModuleOp module) {
4791+
// Lower the module attributes to LLVM equivalents.
4792+
if (auto tripleAttr = module->getAttr(cir::CIRDialect::getTripleAttrName()))
4793+
module->setAttr(mlir::LLVM::LLVMDialect::getTargetTripleAttrName(),
4794+
tripleAttr);
4795+
4796+
// Strip the CIR attributes.
4797+
module->removeAttr(cir::CIRDialect::getSOBAttrName());
4798+
module->removeAttr(cir::CIRDialect::getLangAttrName());
4799+
module->removeAttr(cir::CIRDialect::getTripleAttrName());
4800+
}
4801+
47884802
void ConvertCIRToLLVMPass::runOnOperation() {
47894803
llvm::TimeTraceScope scope("Convert CIR to LLVM Pass");
47904804

@@ -4835,8 +4849,7 @@ void ConvertCIRToLLVMPass::runOnOperation() {
48354849
// Allow operations that will be lowered directly to LLVM IR.
48364850
target.addLegalOp<mlir::LLVM::ZeroOp>();
48374851

4838-
getOperation()->removeAttr(cir::CIRDialect::getSOBAttrName());
4839-
getOperation()->removeAttr(cir::CIRDialect::getLangAttrName());
4852+
processCIRAttrs(module);
48404853

48414854
llvm::SmallVector<mlir::Operation *> ops;
48424855
ops.push_back(module);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: cir-translate --cir-to-llvmir --disable-cc-lowering %s -o %t.ll
2+
// RUN: FileCheck %s -input-file %t.ll -check-prefix=LLVM
3+
4+
module attributes {
5+
cir.triple = "x86_64-unknown-linux-gnu",
6+
llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
7+
} {
8+
cir.func @foo() {
9+
cir.return
10+
}
11+
}
12+
13+
// LLVM-DAG: target triple = "x86_64-unknown-linux-gnu"

0 commit comments

Comments
 (0)