Skip to content

Commit 5eb7527

Browse files
committed
feat: Set up the MLIR dialect
Add CXX_ENABLE_MLIR option to enable building the MLIR dialect. Signed-off-by: Roberto Raggi <[email protected]>
1 parent 45bc496 commit 5eb7527

File tree

12 files changed

+3329
-0
lines changed

12 files changed

+3329
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.json" package_json)
4040
option(CXX_INSTALL_TOOLS "Install tools" ON)
4141
option(CXX_INSTALL_WASI_SYSROOT "Install wasi sysroot" OFF)
4242
option(CXX_ENABLE_FLATBUFFERS "Enable flatbuffers" ON)
43+
option(CXX_ENABLE_MLIR "Enable MLIR" OFF)
4344
option(CXX_LIBCXX_WITH_CLANG "Link with libc++" OFF)
4445
option(CXX_BUILD_TESTS "Build tests" ON)
4546
option(CXX_INTERPROCEDURAL_OPTIMIZATION "Enable interprocedural optimization" OFF)

src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ add_subdirectory(lib)
2121
add_subdirectory(parser)
2222
add_subdirectory(lsp)
2323
add_subdirectory(js)
24+
25+
if (CXX_ENABLE_MLIR)
26+
add_subdirectory(mlir)
27+
endif()
28+
2429
add_subdirectory(frontend)

src/frontend/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ add_executable(cxx ${SOURCES})
2323

2424
target_link_libraries(cxx PRIVATE cxx-lsp)
2525

26+
if (CXX_ENABLE_MLIR)
27+
target_compile_definitions(cxx PRIVATE CXX_WITH_MLIR)
28+
target_link_libraries(cxx PRIVATE cxx-mlir)
29+
endif()
30+
2631
if(EMSCRIPTEN)
2732
target_link_options(cxx PRIVATE
2833
"SHELL:-s EXIT_RUNTIME=1"

src/frontend/cxx/frontend.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
#include <cxx/wasm32_wasi_toolchain.h>
3939
#include <cxx/windows_toolchain.h>
4040

41+
#ifdef CXX_WITH_MLIR
42+
#include <cxx/mlir/codegen.h>
43+
#include <cxx/mlir/cxx_dialect.h>
44+
#endif
45+
4146
#include <cassert>
4247
#include <format>
4348
#include <fstream>
@@ -289,6 +294,21 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool {
289294
ASTPrinter printAST(&unit, std::cout);
290295
printAST(unit.ast());
291296
}
297+
298+
#ifdef CXX_WITH_MLIR
299+
if (cli.opt_ir_dump) {
300+
mlir::MLIRContext context;
301+
context.loadDialect<mlir::cxx::CxxDialect>();
302+
303+
cxx::Codegen codegen(context, &unit);
304+
305+
auto ir = codegen(unit.ast());
306+
307+
mlir::OpPrintingFlags flags;
308+
flags.enableDebugInfo(true);
309+
ir.module->print(llvm::outs(), flags);
310+
}
311+
#endif
292312
}
293313

294314
diagnosticsClient.verifyExpectedDiagnostics();

src/mlir/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
if(POLICY CMP0116)
3+
cmake_policy(SET CMP0116 NEW)
4+
endif()
5+
6+
find_package(MLIR REQUIRED CONFIG)
7+
8+
LIST(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}" "${LLVM_CMAKE_DIR}")
9+
10+
include(AddMLIR)
11+
include(AddLLVM)
12+
include(TableGen)
13+
include(HandleLLVMOptions)
14+
15+
SET(CXX_MLIR_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
16+
SET(CXX_MLIR_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
17+
18+
add_subdirectory(cxx/mlir)

src/mlir/cxx/mlir/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
include_directories(${MLIR_INCLUDE_DIRS} ${LLVM_INCLUDE_DIRS}) # for tablegen
3+
add_mlir_dialect(CxxOps cxx)
4+
5+
aux_source_directory(. SOURCES)
6+
add_library(cxx-mlir ${SOURCES})
7+
8+
llvm_update_compile_flags(cxx-mlir)
9+
10+
target_include_directories(cxx-mlir
11+
PUBLIC ${CXX_MLIR_SOURCE_DIR} ${CXX_MLIR_BINARY_DIR}
12+
${MLIR_INCLUDE_DIRS}
13+
${LLVM_INCLUDE_DIRS}
14+
)
15+
16+
target_link_libraries(cxx-mlir PUBLIC
17+
cxx-parser
18+
MLIRIR
19+
MLIRFuncDialect
20+
MLIRControlFlowDialect
21+
)
22+
add_dependencies(cxx-mlir MLIRCxxOpsIncGen)
23+
24+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
25+
target_compile_options(cxx-mlir PRIVATE
26+
-Wno-unused-variable
27+
-Wno-covered-switch-default
28+
)
29+
endif()

src/mlir/cxx/mlir/CxxOps.td

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2025 Roberto Raggi <[email protected]>
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
21+
include "mlir/IR/OpBase.td"
22+
include "mlir/IR/AttrTypeBase.td"
23+
24+
def Cxx_Dialect : Dialect {
25+
let name = "cxx";
26+
let cppNamespace = "mlir::cxx";
27+
let useDefaultTypePrinterParser = 1;
28+
let dependentDialects = [
29+
"mlir::func::FuncDialect",
30+
"mlir::cf::ControlFlowDialect",
31+
];
32+
}
33+
34+
class Cxx_Type<string name, string typeMnemonic, list<Trait> traits = []> : TypeDef<Cxx_Dialect, name, traits> {
35+
let mnemonic = typeMnemonic;
36+
}
37+
38+
class Cxx_Op<string mnemonic, list<Trait> traits = []> : Op<Cxx_Dialect, mnemonic, traits> {
39+
}
40+
41+
def Cxx_ExprType : Cxx_Type<"Expr", "expr">;
42+
43+
def Cxx_TodoExprOp : Cxx_Op<"todo.expr"> {
44+
let arguments = (ins StrAttr:$message);
45+
let results = (outs Cxx_ExprType:$result);
46+
let assemblyFormat = "$message attr-dict `:` type($result)";
47+
let builders = [
48+
OpBuilder<(ins "::llvm::StringRef":$message),
49+
[{ build($_builder, $_state, $_builder.getType<ExprType>(), message); }]>,
50+
];
51+
}
52+
53+
def Cxx_TodoStmtOp : Cxx_Op<"todo.stmt"> {
54+
let arguments = (ins StrAttr:$message);
55+
let results = (outs);
56+
let assemblyFormat = "$message attr-dict";
57+
}

0 commit comments

Comments
 (0)