Skip to content

Commit 8a41d3a

Browse files
seven-milelanza
authored andcommitted
[CIR][LowerToLLVM] Lowering triple from cir.triple attribute (llvm#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 bd14365 commit 8a41d3a

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

clang/lib/CIR/FrontendAction/CIRGenAction.cpp

Lines changed: 0 additions & 2 deletions
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(llvm::Triple(targetOptions.Triple));
296-
297295
BackendAction backendAction = getBackendActionFromOutputType(action);
298296

299297
emitBackendOutput(compilerInstance, codeGenOptions,

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,8 @@ struct ConvertCIRToLLVMPass
12951295
llvm::StringMap<mlir::LLVM::GlobalOp> &argStringGlobalsMap,
12961296
llvm::MapVector<mlir::ArrayAttr, mlir::LLVM::GlobalOp> &argsVarMap);
12971297

1298+
void processCIRAttrs(mlir::ModuleOp moduleOp);
1299+
12981300
virtual StringRef getArgument() const override { return "cir-flat-to-llvm"; }
12991301
};
13001302

@@ -4789,6 +4791,18 @@ void ConvertCIRToLLVMPass::buildGlobalAnnotationsVar(
47894791
}
47904792
}
47914793

4794+
void ConvertCIRToLLVMPass::processCIRAttrs(mlir::ModuleOp module) {
4795+
// Lower the module attributes to LLVM equivalents.
4796+
if (auto tripleAttr = module->getAttr(cir::CIRDialect::getTripleAttrName()))
4797+
module->setAttr(mlir::LLVM::LLVMDialect::getTargetTripleAttrName(),
4798+
tripleAttr);
4799+
4800+
// Strip the CIR attributes.
4801+
module->removeAttr(cir::CIRDialect::getSOBAttrName());
4802+
module->removeAttr(cir::CIRDialect::getLangAttrName());
4803+
module->removeAttr(cir::CIRDialect::getTripleAttrName());
4804+
}
4805+
47924806
void ConvertCIRToLLVMPass::runOnOperation() {
47934807
llvm::TimeTraceScope scope("Convert CIR to LLVM Pass");
47944808

@@ -4839,8 +4853,7 @@ void ConvertCIRToLLVMPass::runOnOperation() {
48394853
// Allow operations that will be lowered directly to LLVM IR.
48404854
target.addLegalOp<mlir::LLVM::ZeroOp>();
48414855

4842-
getOperation()->removeAttr(cir::CIRDialect::getSOBAttrName());
4843-
getOperation()->removeAttr(cir::CIRDialect::getLangAttrName());
4856+
processCIRAttrs(module);
48444857

48454858
llvm::SmallVector<mlir::Operation *> ops;
48464859
ops.push_back(module);
Lines changed: 13 additions & 0 deletions
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)