Skip to content

Commit f199eb7

Browse files
committed
Convert basic cxx types to MLIR types
Currently map C++ basic types to MLIR built-in types as a temporary solution. Signed-off-by: Roberto Raggi <[email protected]>
1 parent 3fbd7fd commit f199eb7

File tree

3 files changed

+224
-69
lines changed

3 files changed

+224
-69
lines changed

src/mlir/cxx/mlir/CxxOps.td

Lines changed: 15 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,19 @@ class Cxx_Type<string name, string typeMnemonic, list<Trait> traits = []>
4242
class Cxx_Op<string mnemonic, list<Trait> traits = []>
4343
: Op<Cxx_Dialect, mnemonic, traits> {}
4444

45+
46+
// types
47+
4548
def Cxx_ExprType : Cxx_Type<"Expr", "expr">;
4649

50+
def Cxx_PointerType : Cxx_Type<"Pointer", "ptr"> {
51+
let parameters = (ins AnyType:$elementType);
52+
53+
let assemblyFormat = "`<` $elementType `>`";
54+
}
55+
56+
// ops
57+
4758
def Cxx_FuncOp : Cxx_Op<"func", [FunctionOpInterface, IsolatedFromAbove]> {
4859
let arguments = (ins SymbolNameAttr:$sym_name,
4960
TypeAttrOf<FunctionType>:$function_type,
@@ -67,7 +78,7 @@ def Cxx_FuncOp : Cxx_Op<"func", [FunctionOpInterface, IsolatedFromAbove]> {
6778
}
6879

6980
def Cxx_ReturnOp : Cxx_Op<"return", [Pure, HasParent<"FuncOp">, Terminator]> {
70-
let arguments = (ins Variadic<Cxx_ExprType>:$input);
81+
let arguments = (ins Variadic<AnyType>:$input);
7182

7283
let builders = [OpBuilder<(ins), [{ build($_builder, $_state, {}); }]>];
7384

@@ -78,20 +89,7 @@ def Cxx_ReturnOp : Cxx_Op<"return", [Pure, HasParent<"FuncOp">, Terminator]> {
7889
let hasVerifier = 0;
7990
}
8091

81-
def Cxx_CallOp : Cxx_Op<"call"> {
82-
let arguments = (ins Cxx_ExprType:$callee, Variadic<AnyType>:$arguments);
83-
let results = (outs Cxx_ExprType:$result);
84-
let assemblyFormat =
85-
"$callee `(` $arguments `:` type($arguments) `)` attr-dict `:` "
86-
"type($result)";
87-
let builders =
88-
[OpBuilder<
89-
(ins "mlir::Value":$callee, "mlir::ValueRange":$arguments),
90-
[{ build($_builder, $_state, $_builder.getType<ExprType>(), callee, arguments); }]>,
91-
];
92-
}
93-
94-
def TodoExprOp : Cxx_Op<"todo.expr"> {
92+
def Cxx_TodoExprOp : Cxx_Op<"todo.expr"> {
9593
let arguments = (ins StrAttr:$message);
9694
let results = (outs Cxx_ExprType:$result);
9795
let assemblyFormat = "$message attr-dict `:` type($result)";
@@ -102,59 +100,8 @@ def TodoExprOp : Cxx_Op<"todo.expr"> {
102100
];
103101
}
104102

105-
def TodoStmtOp : Cxx_Op<"todo.stmt"> {
103+
def Cxx_TodoStmtOp : Cxx_Op<"todo.stmt"> {
106104
let arguments = (ins StrAttr:$message);
107105
let results = (outs);
108106
let assemblyFormat = "$message attr-dict";
109-
}
110-
111-
def ToBoolOp : Cxx_Op<"to.bool"> {
112-
let arguments = (ins Cxx_ExprType:$value);
113-
let results = (outs I1:$result);
114-
let assemblyFormat = "`(` $value `)` attr-dict `:` type($result)";
115-
}
116-
117-
def ImplicitCastOp : Cxx_Op<"implicit.cast"> {
118-
let arguments = (ins StrAttr:$cast, Cxx_ExprType:$value);
119-
let results = (outs Cxx_ExprType:$result);
120-
let assemblyFormat = "$cast `(` $value `)` attr-dict `:` type($result)";
121-
let builders =
122-
[OpBuilder<
123-
(ins "llvm::StringRef":$cast, "mlir::Value":$value),
124-
[{ build($_builder, $_state, $_builder.getType<ExprType>(), cast, value); }]>,
125-
];
126-
}
127-
128-
def IntLiteralOp : Cxx_Op<"int.literal"> {
129-
let arguments = (ins I64Attr:$value);
130-
let results = (outs Cxx_ExprType:$result);
131-
let assemblyFormat = "$value attr-dict `:` type($result)";
132-
let builders =
133-
[OpBuilder<
134-
(ins "int64_t":$value),
135-
[{ build($_builder, $_state, $_builder.getType<ExprType>(), value); }]>,
136-
];
137-
}
138-
139-
def IdOp : Cxx_Op<"id"> {
140-
let arguments = (ins StrAttr:$name);
141-
let results = (outs Cxx_ExprType:$result);
142-
let assemblyFormat = "$name attr-dict `:` type($result)";
143-
let builders =
144-
[OpBuilder<
145-
(ins "llvm::StringRef":$name),
146-
[{ build($_builder, $_state, $_builder.getType<ExprType>(), name); }]>,
147-
];
148-
}
149-
150-
def BinOp : Cxx_Op<"binary"> {
151-
let arguments = (ins StrAttr:$op, Cxx_ExprType:$lhs, Cxx_ExprType:$rhs);
152-
let results = (outs Cxx_ExprType:$result);
153-
let assemblyFormat =
154-
"`op` $op `(` $lhs `,` $rhs `)` attr-dict `:` type($result)";
155-
let builders =
156-
[OpBuilder<
157-
(ins "llvm::StringRef":$op, "mlir::Value":$lhs, "mlir::Value":$rhs),
158-
[{ build($_builder, $_state, $_builder.getType<ExprType>(), op, lhs, rhs); }]>,
159-
];
160-
}
107+
}

src/mlir/cxx/mlir/codegen.cc

Lines changed: 204 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
#include <cxx/ast_cursor.h>
2626
#include <cxx/control.h>
2727
#include <cxx/literals.h>
28+
#include <cxx/memory_layout.h>
2829
#include <cxx/mlir/cxx_dialect.h>
2930
#include <cxx/names.h>
3031
#include <cxx/symbols.h>
3132
#include <cxx/translation_unit.h>
3233
#include <cxx/types.h>
34+
35+
//
3336
#include <mlir/Dialect/ControlFlow/IR/ControlFlowOps.h>
3437
#include <mlir/Dialect/Func/IR/FuncOps.h>
3538
#include <mlir/Dialect/SCF/IR/SCF.h>
@@ -38,6 +41,197 @@
3841

3942
namespace cxx {
4043

44+
struct Codegen::ConvertType {
45+
Codegen& gen;
46+
47+
[[nodiscard]] auto control() const { return gen.control(); }
48+
[[nodiscard]] auto memoryLayout() const { return control()->memoryLayout(); }
49+
50+
auto getExprType() const -> mlir::Type {
51+
return gen.builder_.getType<mlir::cxx::ExprType>();
52+
}
53+
54+
auto getIntType(const Type* type, bool isSigned) -> mlir::Type {
55+
const auto width = memoryLayout()->sizeOf(type).value() * 8;
56+
return gen.builder_.getIntegerType(width, isSigned);
57+
}
58+
59+
auto operator()(const VoidType* type) -> mlir::Type {
60+
return gen.builder_.getNoneType();
61+
}
62+
63+
auto operator()(const NullptrType* type) -> mlir::Type {
64+
return getExprType();
65+
}
66+
67+
auto operator()(const DecltypeAutoType* type) -> mlir::Type {
68+
return getExprType();
69+
}
70+
71+
auto operator()(const AutoType* type) -> mlir::Type { return getExprType(); }
72+
73+
auto operator()(const BoolType* type) -> mlir::Type {
74+
return gen.builder_.getI1Type();
75+
}
76+
77+
auto operator()(const SignedCharType* type) -> mlir::Type {
78+
return getIntType(type, true);
79+
}
80+
81+
auto operator()(const ShortIntType* type) -> mlir::Type {
82+
return getIntType(type, true);
83+
}
84+
85+
auto operator()(const IntType* type) -> mlir::Type {
86+
return getIntType(type, true);
87+
}
88+
89+
auto operator()(const LongIntType* type) -> mlir::Type {
90+
return getIntType(type, true);
91+
}
92+
93+
auto operator()(const LongLongIntType* type) -> mlir::Type {
94+
return getIntType(type, true);
95+
}
96+
97+
auto operator()(const Int128Type* type) -> mlir::Type {
98+
return getIntType(type, true);
99+
}
100+
101+
auto operator()(const UnsignedCharType* type) -> mlir::Type {
102+
return getIntType(type, false);
103+
}
104+
105+
auto operator()(const UnsignedShortIntType* type) -> mlir::Type {
106+
return getIntType(type, false);
107+
}
108+
109+
auto operator()(const UnsignedIntType* type) -> mlir::Type {
110+
return getIntType(type, false);
111+
}
112+
113+
auto operator()(const UnsignedLongIntType* type) -> mlir::Type {
114+
return getIntType(type, false);
115+
}
116+
117+
auto operator()(const UnsignedLongLongIntType* type) -> mlir::Type {
118+
return getIntType(type, false);
119+
}
120+
121+
auto operator()(const UnsignedInt128Type* type) -> mlir::Type {
122+
return getIntType(type, false);
123+
}
124+
125+
auto operator()(const CharType* type) -> mlir::Type { return getExprType(); }
126+
127+
auto operator()(const Char8Type* type) -> mlir::Type { return getExprType(); }
128+
129+
auto operator()(const Char16Type* type) -> mlir::Type {
130+
return getExprType();
131+
}
132+
133+
auto operator()(const Char32Type* type) -> mlir::Type {
134+
return getExprType();
135+
}
136+
137+
auto operator()(const WideCharType* type) -> mlir::Type {
138+
return getExprType();
139+
}
140+
141+
auto operator()(const FloatType* type) -> mlir::Type {
142+
return gen.builder_.getF32Type();
143+
}
144+
145+
auto operator()(const DoubleType* type) -> mlir::Type {
146+
return gen.builder_.getF64Type();
147+
}
148+
149+
auto operator()(const LongDoubleType* type) -> mlir::Type {
150+
return getExprType();
151+
}
152+
153+
auto operator()(const QualType* type) -> mlir::Type {
154+
return gen.convertType(type->elementType());
155+
}
156+
157+
auto operator()(const BoundedArrayType* type) -> mlir::Type {
158+
return getExprType();
159+
}
160+
161+
auto operator()(const UnboundedArrayType* type) -> mlir::Type {
162+
return getExprType();
163+
}
164+
165+
auto operator()(const PointerType* type) -> mlir::Type {
166+
auto elementType = gen.convertType(type->elementType());
167+
return gen.builder_.getType<mlir::cxx::PointerType>(elementType);
168+
}
169+
170+
auto operator()(const LvalueReferenceType* type) -> mlir::Type {
171+
return getExprType();
172+
}
173+
174+
auto operator()(const RvalueReferenceType* type) -> mlir::Type {
175+
return getExprType();
176+
}
177+
178+
auto operator()(const FunctionType* type) -> mlir::Type {
179+
return getExprType();
180+
}
181+
182+
auto operator()(const ClassType* type) -> mlir::Type { return getExprType(); }
183+
184+
auto operator()(const EnumType* type) -> mlir::Type { return getExprType(); }
185+
186+
auto operator()(const ScopedEnumType* type) -> mlir::Type {
187+
return getExprType();
188+
}
189+
190+
auto operator()(const MemberObjectPointerType* type) -> mlir::Type {
191+
return getExprType();
192+
}
193+
194+
auto operator()(const MemberFunctionPointerType* type) -> mlir::Type {
195+
return getExprType();
196+
}
197+
198+
auto operator()(const NamespaceType* type) -> mlir::Type {
199+
return getExprType();
200+
}
201+
202+
auto operator()(const TypeParameterType* type) -> mlir::Type {
203+
return getExprType();
204+
}
205+
206+
auto operator()(const TemplateTypeParameterType* type) -> mlir::Type {
207+
return getExprType();
208+
}
209+
210+
auto operator()(const UnresolvedNameType* type) -> mlir::Type {
211+
return getExprType();
212+
}
213+
214+
auto operator()(const UnresolvedBoundedArrayType* type) -> mlir::Type {
215+
return getExprType();
216+
}
217+
218+
auto operator()(const UnresolvedUnderlyingType* type) -> mlir::Type {
219+
return getExprType();
220+
}
221+
222+
auto operator()(const OverloadSetType* type) -> mlir::Type {
223+
return getExprType();
224+
}
225+
226+
auto operator()(const BuiltinVaListType* type) -> mlir::Type {
227+
return getExprType();
228+
}
229+
230+
auto operator()(const Type* type) -> mlir::Type {
231+
return gen.builder_.getType<mlir::cxx::ExprType>();
232+
}
233+
};
234+
41235
struct Codegen::UnitVisitor {
42236
Codegen& gen;
43237

@@ -664,6 +858,10 @@ auto Codegen::emitTodoExpr(SourceLocation location, std::string_view message)
664858
return op;
665859
}
666860

861+
auto Codegen::convertType(const Type* type) -> mlir::Type {
862+
return visit(ConvertType{*this}, type);
863+
}
864+
667865
auto Codegen::operator()(UnitAST* ast) -> UnitResult {
668866
if (ast) return visit(UnitVisitor{*this}, ast);
669867
return {};
@@ -1201,7 +1399,7 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
12011399
std::vector<mlir::Type> resultTypes;
12021400

12031401
for (auto paramTy : functionType->parameterTypes()) {
1204-
inputTypes.push_back(exprType);
1402+
inputTypes.push_back(gen.convertType(paramTy));
12051403
}
12061404

12071405
if (!gen.control()->is_void(functionType->returnType())) {
@@ -1619,10 +1817,15 @@ auto Codegen::ExpressionVisitor::operator()(BoolLiteralExpressionAST* ast)
16191817

16201818
auto Codegen::ExpressionVisitor::operator()(IntLiteralExpressionAST* ast)
16211819
-> ExpressionResult {
1820+
auto op =
1821+
gen.emitTodoExpr(ast->firstSourceLocation(), to_string(ast->kind()));
1822+
1823+
#if false
16221824
auto loc = gen.getLocation(ast->literalLoc);
16231825

16241826
auto op = gen.builder_.create<mlir::cxx::IntLiteralOp>(
16251827
loc, ast->literal->integerValue());
1828+
#endif
16261829

16271830
return {op};
16281831
}

src/mlir/cxx/mlir/codegen.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <cxx/ast_fwd.h>
2424
#include <cxx/mlir/cxx_dialect.h>
2525
#include <cxx/source_location.h>
26+
#include <cxx/types_fwd.h>
2627
#include <mlir/Dialect/Func/IR/FuncOps.h>
2728
#include <mlir/IR/Builders.h>
2829
#include <mlir/IR/BuiltinOps.h>
@@ -172,6 +173,8 @@ class Codegen {
172173
[[nodiscard]] auto emitTodoExpr(SourceLocation loc, std::string_view message)
173174
-> mlir::cxx::TodoExprOp;
174175

176+
[[nodiscard]] auto convertType(const Type* type) -> mlir::Type;
177+
175178
struct UnitVisitor;
176179
struct DeclarationVisitor;
177180
struct StatementVisitor;
@@ -194,6 +197,8 @@ class Codegen {
194197
struct AttributeSpecifierVisitor;
195198
struct AttributeTokenVisitor;
196199

200+
struct ConvertType;
201+
197202
mlir::OpBuilder builder_;
198203
mlir::ModuleOp module_;
199204
mlir::cxx::FuncOp function_;

0 commit comments

Comments
 (0)