Skip to content

Commit 5548546

Browse files
authored
[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 c644ee7 commit 5548546

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
@@ -289,8 +289,6 @@ class CIRGenConsumer : public clang::ASTConsumer {
289289
feOptions.ClangIRDisableCIRVerifier,
290290
!feOptions.ClangIRCallConvLowering);
291291

292-
llvmModule->setTargetTriple(targetOptions.Triple);
293-
294292
BackendAction backendAction = getBackendActionFromOutputType(action);
295293

296294
EmitBackendOutput(

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

@@ -4784,6 +4786,18 @@ void ConvertCIRToLLVMPass::buildGlobalAnnotationsVar(
47844786
}
47854787
}
47864788

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

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

4837-
getOperation()->removeAttr(cir::CIRDialect::getSOBAttrName());
4838-
getOperation()->removeAttr(cir::CIRDialect::getLangAttrName());
4851+
processCIRAttrs(module);
48394852

48404853
llvm::SmallVector<mlir::Operation *> ops;
48414854
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)