Skip to content

Commit f31f4cc

Browse files
committed
Add PtrAddOp to the cxx dialect and require MLIR 21
1 parent b331648 commit f31f4cc

File tree

8 files changed

+142
-81
lines changed

8 files changed

+142
-81
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ jobs:
7272
run: |
7373
wget https://apt.llvm.org/llvm.sh
7474
chmod +x llvm.sh
75-
sudo ./llvm.sh 20
75+
sudo ./llvm.sh 21
7676
rm llvm.sh
7777
78-
sudo apt-get install -y libmlir-20-dev mlir-20-tools
78+
sudo apt-get install -y libmlir-21-dev mlir-21-tools
7979
8080
- name: Install unit tests requirements
8181
run: |
@@ -86,7 +86,7 @@ jobs:
8686
run: |
8787
. .venv/bin/activate
8888
89-
PATH=/usr/lib/llvm-20/bin:$PATH
89+
PATH=/usr/lib/llvm-21/bin:$PATH
9090
9191
cmake --preset default-mlir
9292

scripts/build-mlir.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ async function main() {
7373
"-DCMAKE_CXX_FLAGS=-DLLVM_BUILD_STATIC",
7474
"-DCMAKE_EXE_LINKER_FLAGS=-sNODERAWFS -sEXIT_RUNTIME -sALLOW_MEMORY_GROWTH",
7575
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
76+
"-DCMAKE_CXX_FLAGS=-Wno-c2y-extensions",
7677
"-DLLVM_BUILD_EXTERNAL_COMPILER_RT=OFF",
7778
"-DLLVM_BUILD_TOOLS=OFF",
7879
"-DLLVM_ENABLE_EH=OFF",
@@ -81,11 +82,12 @@ async function main() {
8182
"-DLLVM_ENABLE_RTTI=OFF",
8283
"-DLLVM_ENABLE_RUNTIMES=",
8384
"-DLLVM_ENABLE_Z3_SOLVER=OFF",
85+
"-DLLVM_ENABLE_WERROR=OFF",
8486
"-DLLVM_INCLUDE_DOCS=OFF",
8587
"-DLLVM_INCLUDE_TESTS=OFF",
8688
"-DLLVM_INSTALL_UTILS=OFF",
8789
"-DLLVM_LINK_LLVM_DYLIB=OFF",
88-
"-DLLVM_OPTIMIZED_TABLEGEN=OFF",
90+
"-DLLVM_OPTIMIZED_TABLEGEN=ON",
8991
"-DLLVM_TARGETS_TO_BUILD=WebAssembly",
9092
];
9193

@@ -164,4 +166,11 @@ require("./${app}.js");`
164166
}
165167
}
166168

167-
await await main();
169+
main().catch((err) => {
170+
if (err instanceof Error) {
171+
console.error(err.message);
172+
} else {
173+
console.error(err);
174+
}
175+
process.exit(1);
176+
});

src/frontend/cxx/frontend.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,14 +549,14 @@ void Frontend::Private::emitCode() {
549549
#ifdef CXX_WITH_MLIR
550550
llvm::InitializeAllAsmPrinters();
551551

552-
auto triple = toolchain_->memoryLayout()->triple();
552+
auto triple = llvm::Triple{toolchain_->memoryLayout()->triple()};
553553

554554
std::string error;
555555
auto target = llvm::TargetRegistry::lookupTarget(triple, error);
556556

557557
if (!target) {
558558
std::cerr << std::format("cxx: cannot find target for triple '{}': {}\n",
559-
triple, error);
559+
triple.getTriple(), error);
560560
shouldExit_ = true;
561561
exitStatus_ = EXIT_FAILURE;
562562
return;
@@ -567,11 +567,11 @@ void Frontend::Private::emitCode() {
567567
auto RM = std::optional<llvm::Reloc::Model>();
568568

569569
auto targetMachine =
570-
target->createTargetMachine(triple, "generic", "", opt, RM);
570+
target->createTargetMachine(llvm::Triple{triple}, "generic", "", opt, RM);
571571

572572
if (!targetMachine) {
573573
std::cerr << std::format("cxx: cannot create target machine for '{}': {}\n",
574-
triple, error);
574+
triple.getTriple(), error);
575575
shouldExit_ = true;
576576
exitStatus_ = EXIT_FAILURE;
577577
return;

src/mlir/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ endif()
55

66
find_package(MLIR REQUIRED CONFIG)
77

8-
if (MLIR_VERSION VERSION_LESS "20.0")
9-
message(FATAL_ERROR "MLIR >= 20.0 is required")
8+
if (MLIR_VERSION VERSION_LESS "21.0")
9+
message(FATAL_ERROR "MLIR >= 21.0 is required")
1010
endif()
1111

1212
LIST(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}" "${LLVM_CMAKE_DIR}")

src/mlir/cxx/mlir/CxxOps.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ def Cxx_MemberOp : Cxx_Op<"member"> {
186186
let results = (outs Cxx_PointerType:$result);
187187
}
188188

189+
def Cxx_PtrAddOp : Cxx_Op<"ptr_add"> {
190+
let arguments = (ins Cxx_PointerType:$base, Cxx_IntegerType:$offset);
191+
192+
let results = (outs Cxx_PointerType:$result);
193+
}
194+
189195
def Cxx_AddressOfOp : Cxx_Op<"addressof"> {
190196
let arguments = (ins FlatSymbolRefAttr:$sym_name);
191197

src/mlir/cxx/mlir/codegen_expressions.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,14 @@ auto Codegen::ExpressionVisitor::operator()(SubscriptExpressionAST* ast)
482482

483483
auto resultType = gen.convertType(control()->add_pointer(ast->type));
484484

485+
if (control()->is_pointer(ast->baseExpression->type)) {
486+
auto op = gen.builder_.create<mlir::cxx::PtrAddOp>(
487+
loc, resultType, baseExpressionResult.value,
488+
indexExpressionResult.value);
489+
490+
return {op};
491+
}
492+
485493
auto op = gen.builder_.create<mlir::cxx::SubscriptOp>(
486494
loc, resultType, baseExpressionResult.value, indexExpressionResult.value);
487495

src/mlir/cxx/mlir/codegen_units.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ auto Codegen::UnitVisitor::operator()(TranslationUnitAST* ast) -> UnitResult {
115115

116116
auto memoryLayout = gen.control()->memoryLayout();
117117

118-
auto triple = gen.control()->memoryLayout()->triple();
118+
auto triple = llvm::Triple{gen.control()->memoryLayout()->triple()};
119119

120120
std::string error;
121121
auto target = llvm::TargetRegistry::lookupTarget(triple, error);
122122

123123
if (!target) {
124124
cxx_runtime_error(std::format("failed to find target for triple '{}': {}",
125-
triple, error));
125+
triple.getTriple(), error));
126126
}
127127

128128
llvm::TargetOptions opt;

0 commit comments

Comments
 (0)