Skip to content

Commit 5128bf2

Browse files
committed
feat: Add SCF dialect and generate IR for a few basic expressions and statements
Also, add the option `-emit-ir` to the CLI to generate the IR to the standard output. Signed-off-by: Roberto Raggi <[email protected]>
1 parent 9c0693a commit 5128bf2

File tree

8 files changed

+294
-251
lines changed

8 files changed

+294
-251
lines changed

src/frontend/cxx/frontend.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool {
296296
}
297297

298298
#ifdef CXX_WITH_MLIR
299-
if (cli.opt_ir_dump) {
299+
if (cli.opt_emit_ir) {
300300
mlir::MLIRContext context;
301301
context.loadDialect<mlir::cxx::CxxDialect>();
302302

@@ -305,7 +305,7 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool {
305305
auto ir = codegen(unit.ast());
306306

307307
mlir::OpPrintingFlags flags;
308-
flags.enableDebugInfo(true);
308+
flags.enableDebugInfo(true, true);
309309
ir.module->print(llvm::outs(), flags);
310310
}
311311
#endif

src/mlir/cxx/mlir/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ target_link_libraries(cxx-mlir PUBLIC
1818
MLIRIR
1919
MLIRFuncDialect
2020
MLIRControlFlowDialect
21+
MLIRSCFDialect
2122
)
2223
add_dependencies(cxx-mlir MLIRCxxOpsIncGen)
2324

src/mlir/cxx/mlir/CxxOps.td

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def Cxx_Dialect : Dialect {
2828
let dependentDialects = [
2929
"mlir::func::FuncDialect",
3030
"mlir::cf::ControlFlowDialect",
31+
"mlir::scf::SCFDialect",
3132
];
3233
}
3334

@@ -40,7 +41,7 @@ class Cxx_Op<string mnemonic, list<Trait> traits = []> : Op<Cxx_Dialect, mnemoni
4041

4142
def Cxx_ExprType : Cxx_Type<"Expr", "expr">;
4243

43-
def Cxx_TodoExprOp : Cxx_Op<"todo.expr"> {
44+
def TodoExprOp : Cxx_Op<"todo.expr"> {
4445
let arguments = (ins StrAttr:$message);
4546
let results = (outs Cxx_ExprType:$result);
4647
let assemblyFormat = "$message attr-dict `:` type($result)";
@@ -50,8 +51,69 @@ def Cxx_TodoExprOp : Cxx_Op<"todo.expr"> {
5051
];
5152
}
5253

53-
def Cxx_TodoStmtOp : Cxx_Op<"todo.stmt"> {
54+
def TodoStmtOp : Cxx_Op<"todo.stmt"> {
5455
let arguments = (ins StrAttr:$message);
5556
let results = (outs);
5657
let assemblyFormat = "$message attr-dict";
5758
}
59+
60+
def ToBoolOp : Cxx_Op<"to.bool"> {
61+
let arguments = (ins Cxx_ExprType:$value);
62+
let results = (outs I1:$result);
63+
let assemblyFormat = "`(` $value `)` attr-dict `:` type($result)";
64+
}
65+
66+
def ImplicitCastOp : Cxx_Op<"implicit.cast"> {
67+
let arguments = (ins StrAttr:$cast, Cxx_ExprType:$value);
68+
let results = (outs Cxx_ExprType:$result);
69+
let assemblyFormat = "$cast `(` $value `)` attr-dict `:` type($result)";
70+
let builders = [
71+
OpBuilder<(ins "llvm::StringRef":$cast, "mlir::Value":$value),
72+
[{ build($_builder, $_state, $_builder.getType<ExprType>(), cast, value); }]>,
73+
];
74+
}
75+
76+
def IntLiteralOp : Cxx_Op<"int.literal"> {
77+
let arguments = (ins I64Attr:$value);
78+
let results = (outs Cxx_ExprType:$result);
79+
let assemblyFormat = "$value attr-dict `:` type($result)";
80+
let builders = [
81+
OpBuilder<(ins "int64_t":$value),
82+
[{ build($_builder, $_state, $_builder.getType<ExprType>(), value); }]>,
83+
];
84+
}
85+
86+
def IdOp : Cxx_Op<"id"> {
87+
let arguments = (ins StrAttr:$name);
88+
let results = (outs Cxx_ExprType:$result);
89+
let assemblyFormat = "$name attr-dict `:` type($result)";
90+
let builders = [
91+
OpBuilder<(ins "llvm::StringRef":$name),
92+
[{ build($_builder, $_state, $_builder.getType<ExprType>(), name); }]>,
93+
];
94+
}
95+
96+
def BinOp : Cxx_Op<"binary"> {
97+
let arguments = (ins StrAttr:$op, Cxx_ExprType:$lhs, Cxx_ExprType:$rhs);
98+
let results = (outs Cxx_ExprType:$result);
99+
let assemblyFormat = "`op` $op `(` $lhs `,` $rhs `)` attr-dict `:` type($result)";
100+
let builders = [
101+
OpBuilder<(ins "llvm::StringRef":$op, "mlir::Value":$lhs, "mlir::Value":$rhs),
102+
[{ build($_builder, $_state, $_builder.getType<ExprType>(), op, lhs, rhs); }]>,
103+
];
104+
}
105+
106+
def CallOp : Cxx_Op<"call"> {
107+
let arguments = (ins Cxx_ExprType:$callee, Variadic<AnyType>:$arguments);
108+
let results = (outs Cxx_ExprType:$result);
109+
let assemblyFormat = "$callee `(` $arguments `:` type($arguments) `)` attr-dict `:` type($result)";
110+
let builders = [
111+
OpBuilder<(ins "mlir::Value":$callee, "mlir::ValueRange":$arguments),
112+
[{ build($_builder, $_state, $_builder.getType<ExprType>(), callee, arguments); }]>,
113+
];
114+
}
115+
116+
def ReturnOp : Cxx_Op<"return"> {
117+
let arguments = (ins Optional<Cxx_ExprType>:$value);
118+
let results = (outs);
119+
}

0 commit comments

Comments
 (0)