Skip to content

Commit 5847660

Browse files
committed
[CIR] Add cir-translate tool
1 parent e61859f commit 5847660

10 files changed

+310
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: cir-translate --cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering %s -o %t.ll
2+
// RUN: FileCheck %s -input-file %t.ll -check-prefix=LLVM
3+
4+
module {
5+
cir.func @foo() {
6+
cir.return
7+
}
8+
}
9+
10+
// LLVM-DAG: target triple = "x86_64-unknown-linux-gnu"
11+
// LLVM-DAG: target datalayout = "{{.*}}"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: cir-translate --cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering %s -o %t.x86.ll
2+
// RUN: FileCheck %s -input-file %t.x86.ll -check-prefix=X86
3+
// RUN: cir-translate --cir-to-llvmir --target spirv64-unknown-unknown --disable-cc-lowering %s -o %t.spirv64.ll
4+
// RUN: FileCheck %s -input-file %t.spirv64.ll -check-prefix=SPIRV64
5+
// RUN: cir-translate --cir-to-llvmir --disable-cc-lowering %s -o %t.default.ll
6+
// RUN: FileCheck %s -input-file %t.default.ll -check-prefix=DEFAULT
7+
8+
module attributes {
9+
cir.triple = "spirv64-unknown-unknown",
10+
dlti.dl_spec = #dlti.dl_spec<"dlti.global_memory_space" = 7 : ui64>
11+
} {
12+
cir.func @foo() {
13+
cir.return
14+
}
15+
}
16+
17+
// X86-NOT: target datalayout = "G7"
18+
// X86-DAG: target triple = "x86_64-unknown-linux-gnu"
19+
20+
// SPIRV64-NOT: target datalayout = "G7"
21+
// SPIRV64-DAG: target triple = "spirv64-unknown-unknown"
22+
23+
// DEFAULT-DAG: target datalayout = "G7"
24+
// DEFAULT-DAG: target triple = "spirv64-unknown-unknown"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: cir-translate --cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering %s -o %t.x86.ll
2+
// RUN: FileCheck %s -input-file %t.x86.ll -check-prefix=X86
3+
// RUN: cir-translate --cir-to-llvmir --target spirv64-unknown-unknown --disable-cc-lowering %s -o %t.spirv64.ll
4+
// RUN: FileCheck %s -input-file %t.spirv64.ll -check-prefix=SPIRV64
5+
// RUN: cir-translate --cir-to-llvmir --disable-cc-lowering %s -o %t.default.ll
6+
// RUN: FileCheck %s -input-file %t.default.ll -check-prefix=DEFAULT
7+
8+
module attributes {
9+
cir.triple = "spirv64-unknown-unknown"
10+
} {
11+
cir.func @foo() {
12+
cir.return
13+
}
14+
}
15+
16+
// X86-DAG: target triple = "x86_64-unknown-linux-gnu"
17+
// X86-DAG: target datalayout = "{{.*}}"
18+
19+
// SPIRV64-DAG: target triple = "spirv64-unknown-unknown"
20+
// SPIRV64-DAG: target datalayout = "{{.*}}"
21+
22+
// DEFAULT-DAG: target triple = "spirv64-unknown-unknown"
23+
// DEFAULT-DAG: target datalayout = "{{.*}}"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: cir-translate -verify-diagnostics --cir-to-llvmir --target foobar --disable-cc-lowering %s 2>&1
2+
3+
// expected-error@below {{invalid target triple 'foobar'}}
4+
module {
5+
cir.func @foo() {
6+
cir.return
7+
}
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: cir-translate --cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering %s -o %t.x86.ll
2+
// RUN: FileCheck %s -input-file %t.x86.ll -check-prefix=X86
3+
// RUN: cir-translate --cir-to-llvmir --target spirv64-unknown-unknown --disable-cc-lowering %s -o %t.spirv64.ll
4+
// RUN: FileCheck %s -input-file %t.spirv64.ll -check-prefix=SPIRV64
5+
6+
module attributes {
7+
dlti.dl_spec = #dlti.dl_spec<"dlti.global_memory_space" = 7 : ui64>
8+
} {
9+
cir.func @foo() {
10+
cir.return
11+
}
12+
}
13+
14+
// X86-NOT: target datalayout = "G7"
15+
// X86-DAG: target triple = "x86_64-unknown-linux-gnu"
16+
17+
// SPIRV64-NOT: target datalayout = "G7"
18+
// SPIRV64-DAG: target triple = "spirv64-unknown-unknown"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: cir-translate --cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering %s -o %t.x86.ll
2+
// RUN: FileCheck %s -input-file %t.x86.ll -check-prefix=X86
3+
// RUN: cir-translate --cir-to-llvmir --target spirv64-unknown-unknown --disable-cc-lowering %s -o %t.spirv64.ll
4+
// RUN: FileCheck %s -input-file %t.spirv64.ll -check-prefix=SPIRV64
5+
6+
module {
7+
cir.func @foo() {
8+
cir.return
9+
}
10+
}
11+
12+
// X86-DAG: target triple = "x86_64-unknown-linux-gnu"
13+
// X86-DAG: target datalayout = "{{.*}}"
14+
15+
// SPIRV64-DAG: target triple = "spirv64-unknown-unknown"
16+
// SPIRV64-DAG: target datalayout = "{{.*}}"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: cir-translate -verify-diagnostics --cir-to-llvmir --disable-cc-lowering %s
2+
3+
// expected-warning@below {{no target triple provided, assuming}}
4+
module {
5+
cir.func @foo() {
6+
cir.return
7+
}
8+
}

clang/tools/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_clang_subdirectory(driver)
55
add_clang_subdirectory(apinotes-test)
66
if(CLANG_ENABLE_CIR)
77
add_clang_subdirectory(cir-opt)
8+
add_clang_subdirectory(cir-translate)
89
endif()
910
add_clang_subdirectory(clang-diff)
1011
add_clang_subdirectory(clang-format)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2+
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
3+
get_property(translation_libs GLOBAL PROPERTY MLIR_TRANSLATION_LIBS)
4+
5+
include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include)
6+
include_directories(${CMAKE_BINARY_DIR}/tools/mlir/include)
7+
8+
add_clang_tool(cir-translate
9+
cir-translate.cpp
10+
)
11+
12+
clang_target_link_libraries(cir-translate
13+
PRIVATE
14+
clangCIR
15+
clangCIRLoweringDirectToLLVM
16+
MLIRCIR
17+
MLIRCIRTransforms
18+
)
19+
20+
target_link_libraries(cir-translate
21+
PRIVATE
22+
${dialect_libs}
23+
${conversion_libs}
24+
${translation_libs}
25+
MLIRAnalysis
26+
MLIRDialect
27+
MLIRIR
28+
MLIROptLib
29+
MLIRParser
30+
MLIRPass
31+
MLIRTransforms
32+
MLIRTransformUtils
33+
MLIRTranslateLib
34+
MLIRSupport
35+
)
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Converts CIR directly to LLVM IR, similar to mlir-translate or LLVM llc.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "mlir/Dialect/DLTI/DLTI.h"
14+
#include "mlir/Dialect/Func/IR/FuncOps.h"
15+
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
16+
#include "mlir/IR/BuiltinOps.h"
17+
#include "mlir/IR/MLIRContext.h"
18+
#include "mlir/InitAllTranslations.h"
19+
#include "mlir/Support/LogicalResult.h"
20+
#include "mlir/Target/LLVMIR/Dialect/All.h"
21+
#include "mlir/Target/LLVMIR/Import.h"
22+
#include "mlir/Tools/mlir-translate/MlirTranslateMain.h"
23+
#include "mlir/Tools/mlir-translate/Translation.h"
24+
25+
#include "llvm/IR/Module.h"
26+
#include "llvm/TargetParser/Host.h"
27+
28+
#include "clang/Basic/TargetInfo.h"
29+
#include "clang/CIR/Dialect/IR/CIRDialect.h"
30+
#include "clang/CIR/Dialect/Passes.h"
31+
#include "clang/CIR/LowerToLLVM.h"
32+
#include "clang/CIR/MissingFeatures.h"
33+
34+
namespace cir {
35+
namespace direct {
36+
extern void registerCIRDialectTranslation(mlir::DialectRegistry &registry);
37+
} // namespace direct
38+
39+
namespace {
40+
41+
/// The goal of this option is to ensure that the triple and data layout specs
42+
/// are always available in the ClangIR module. With this requirement met, the
43+
/// behavior of this option is designed to be as intuitive as possible, as shown
44+
/// in the table below:
45+
///
46+
/// +--------+--------+-------------+-----------------+-----------------------+
47+
/// | Option | Triple | Data Layout | Behavior Triple | Behavior Data Layout |
48+
/// +========+========+=============+=================+=======================+
49+
/// | T | T | T | Overwrite | Derive from triple |
50+
/// | T | T | F | Overwrite | Derive from triple |
51+
/// | T | F | T | Overwrite | Derive from triple |
52+
/// | T | F | F | Overwrite | Derive from triple |
53+
/// | F | T | T | | |
54+
/// | F | T | F | | Derive from triple |
55+
/// | F | F | T | Set default | Derive from triple |
56+
/// | F | F | F | Set default | Derive from triple |
57+
/// +--------+--------+-------------+-----------------+-----------------------+
58+
llvm::cl::opt<std::string>
59+
targetTripleOption("target",
60+
llvm::cl::desc("Specify a default target triple when "
61+
"it's not available in the module"),
62+
llvm::cl::init(""));
63+
64+
std::string prepareCIRModuleTriple(mlir::ModuleOp mod) {
65+
std::string triple = targetTripleOption;
66+
67+
// Treat "" as the default target machine.
68+
if (triple.empty()) {
69+
triple = llvm::sys::getDefaultTargetTriple();
70+
71+
mod.emitWarning() << "no target triple provided, assuming " << triple;
72+
}
73+
74+
mod->setAttr(cir::CIRDialect::getTripleAttrName(),
75+
mlir::StringAttr::get(mod.getContext(), triple));
76+
return triple;
77+
}
78+
79+
llvm::LogicalResult prepareCIRModuleDataLayout(mlir::ModuleOp mod,
80+
llvm::StringRef rawTriple) {
81+
auto *context = mod.getContext();
82+
83+
// Data layout is fully determined by the target triple. Here we only pass the
84+
// triple to get the data layout.
85+
llvm::Triple triple(rawTriple);
86+
clang::TargetOptions targetOptions;
87+
targetOptions.Triple = rawTriple;
88+
// FIXME: AllocateTarget is a big deal. Better make it a global state.
89+
auto targetInfo =
90+
clang::targets::AllocateTarget(llvm::Triple(rawTriple), targetOptions);
91+
if (!targetInfo) {
92+
mod.emitError() << "error: invalid target triple '" << rawTriple << "'\n";
93+
return llvm::failure();
94+
}
95+
std::string layoutString = targetInfo->getDataLayoutString();
96+
97+
// Registered dialects may not be loaded yet, ensure they are.
98+
context->loadDialect<mlir::DLTIDialect, mlir::LLVM::LLVMDialect>();
99+
100+
mlir::DataLayoutSpecInterface dlSpec =
101+
mlir::translateDataLayout(llvm::DataLayout(layoutString), context);
102+
mod->setAttr(mlir::DLTIDialect::kDataLayoutAttrName, dlSpec);
103+
104+
return llvm::success();
105+
}
106+
107+
/// Prepare requirements like cir.triple and data layout.
108+
llvm::LogicalResult prepareCIRModuleForTranslation(mlir::ModuleOp mod) {
109+
auto modTriple = mod->getAttrOfType<mlir::StringAttr>(
110+
cir::CIRDialect::getTripleAttrName());
111+
auto modDataLayout = mod->getAttr(mlir::DLTIDialect::kDataLayoutAttrName);
112+
bool hasTargetOption = targetTripleOption.getNumOccurrences() > 0;
113+
114+
// Skip the situation where nothing should be done.
115+
if (!hasTargetOption && modTriple && modDataLayout)
116+
return llvm::success();
117+
118+
std::string triple;
119+
120+
if (!hasTargetOption && modTriple) {
121+
// Do nothing if it's already set.
122+
triple = modTriple.getValue();
123+
} else {
124+
// Otherwise, overwrite or set default.
125+
triple = prepareCIRModuleTriple(mod);
126+
}
127+
128+
// If the data layout is not set, derive it from the triple.
129+
return prepareCIRModuleDataLayout(mod, triple);
130+
}
131+
} // namespace
132+
} // namespace cir
133+
134+
void registerToLLVMTranslation() {
135+
static llvm::cl::opt<bool> disableCCLowering(
136+
"disable-cc-lowering",
137+
llvm::cl::desc("Disable calling convention lowering pass"),
138+
llvm::cl::init(false));
139+
140+
mlir::TranslateFromMLIRRegistration registration(
141+
"cir-to-llvmir", "Translate CIR to LLVMIR",
142+
[](mlir::Operation *op, mlir::raw_ostream &output) {
143+
auto cirModule = llvm::dyn_cast<mlir::ModuleOp>(op);
144+
145+
if (mlir::failed(cir::prepareCIRModuleForTranslation(cirModule)))
146+
return mlir::failure();
147+
148+
llvm::LLVMContext llvmContext;
149+
auto llvmModule =
150+
cir::direct::lowerDirectlyFromCIRToLLVMIR(cirModule, llvmContext);
151+
if (!llvmModule)
152+
return mlir::failure();
153+
llvmModule->print(output, nullptr);
154+
return mlir::success();
155+
},
156+
[](mlir::DialectRegistry &registry) {
157+
registry.insert<mlir::DLTIDialect, mlir::func::FuncDialect>();
158+
mlir::registerAllToLLVMIRTranslations(registry);
159+
cir::direct::registerCIRDialectTranslation(registry);
160+
});
161+
}
162+
163+
int main(int argc, char **argv) {
164+
registerToLLVMTranslation();
165+
return failed(mlir::mlirTranslateMain(argc, argv, "CIR Translation Tool"));
166+
}

0 commit comments

Comments
 (0)