Skip to content

Commit 3da59b8

Browse files
committed
Add types to represent floats, arrays and structs
Fixes #614
1 parent 58bb151 commit 3da59b8

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

src/mlir/cxx/mlir/CxxOps.td

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,33 @@ def Cxx_IntegerType : Cxx_Type<"Integer", "int"> {
5757
let assemblyFormat = "`<` $width `,` $isSigned `>`";
5858
}
5959

60+
def Cxx_FloatType : Cxx_Type<"Float", "float"> {
61+
let parameters = (ins "unsigned":$width);
62+
63+
let assemblyFormat = "`<` $width `>`";
64+
}
65+
6066
def Cxx_PointerType : Cxx_Type<"Pointer", "ptr"> {
6167
let parameters = (ins "Type":$elementType);
6268

6369
let assemblyFormat = "`<` $elementType `>`";
6470
}
6571

72+
def Cxx_ArrayType : Cxx_Type<"Array", "array"> {
73+
let parameters = (ins "Type":$elementType, "unsigned":$size);
74+
75+
let assemblyFormat = "`<` $elementType `,` $size `>`";
76+
}
77+
78+
def Cxx_ClassType : Cxx_Type<"Class", "class"> {
79+
let parameters = (ins
80+
StringRefParameter<"class name", [{ "" }]>:$name,
81+
OptionalArrayRefParameter<"mlir::Type">:$body
82+
);
83+
84+
let assemblyFormat = "`<` $name `(` $body `)` `>`";
85+
}
86+
6687
// ops
6788

6889
def Cxx_FuncOp : Cxx_Op<"func", [FunctionOpInterface, IsolatedFromAbove]> {

src/mlir/cxx/mlir/codegen.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
#include <cxx/ast_fwd.h>
2424
#include <cxx/mlir/cxx_dialect.h>
2525
#include <cxx/source_location.h>
26+
#include <cxx/symbols_fwd.h>
2627
#include <cxx/types_fwd.h>
2728
#include <mlir/Dialect/Func/IR/FuncOps.h>
2829
#include <mlir/IR/Builders.h>
2930
#include <mlir/IR/BuiltinOps.h>
3031

32+
#include <unordered_map>
33+
3134
namespace mlir::func {
3235
class FuncOp;
3336
}
@@ -271,6 +274,7 @@ class Codegen {
271274
TranslationUnit* unit_ = nullptr;
272275
mlir::Block* exitBlock_ = nullptr;
273276
mlir::cxx::AllocaOp exitValue_;
277+
std::unordered_map<ClassSymbol*, std::string> classNames_;
274278
int count_ = 0;
275279
};
276280

src/mlir/cxx/mlir/convert_type.cc

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
#include <cxx/literals.h>
2727
#include <cxx/memory_layout.h>
2828
#include <cxx/names.h>
29+
#include <cxx/scope.h>
2930
#include <cxx/symbols.h>
3031
#include <cxx/translation_unit.h>
3132
#include <cxx/types.h>
3233

34+
#include <format>
35+
3336
namespace cxx {
3437

3538
struct Codegen::ConvertType {
@@ -40,6 +43,7 @@ struct Codegen::ConvertType {
4043

4144
auto getExprType() const -> mlir::Type;
4245
auto getIntType(const Type* type, bool isSigned) -> mlir::Type;
46+
auto getFloatType(const Type* type) -> mlir::Type;
4347

4448
auto operator()(const VoidType* type) -> mlir::Type;
4549
auto operator()(const NullptrType* type) -> mlir::Type;
@@ -106,6 +110,11 @@ auto Codegen::ConvertType::getIntType(const Type* type, bool isSigned)
106110
return gen.builder_.getType<mlir::cxx::IntegerType>(width, isSigned);
107111
}
108112

113+
auto Codegen::ConvertType::getFloatType(const Type* type) -> mlir::Type {
114+
return gen.builder_.getType<mlir::cxx::FloatType>(
115+
memoryLayout()->sizeOf(type).value() * 8);
116+
}
117+
109118
auto Codegen::ConvertType::operator()(const VoidType* type) -> mlir::Type {
110119
return gen.builder_.getType<mlir::cxx::VoidType>();
111120
}
@@ -205,16 +214,16 @@ auto Codegen::ConvertType::operator()(const WideCharType* type) -> mlir::Type {
205214
}
206215

207216
auto Codegen::ConvertType::operator()(const FloatType* type) -> mlir::Type {
208-
return gen.builder_.getF32Type();
217+
return getFloatType(type);
209218
}
210219

211220
auto Codegen::ConvertType::operator()(const DoubleType* type) -> mlir::Type {
212-
return gen.builder_.getF64Type();
221+
return getFloatType(type);
213222
}
214223

215224
auto Codegen::ConvertType::operator()(const LongDoubleType* type)
216225
-> mlir::Type {
217-
return getExprType();
226+
return getFloatType(type);
218227
}
219228

220229
auto Codegen::ConvertType::operator()(const QualType* type) -> mlir::Type {
@@ -223,12 +232,14 @@ auto Codegen::ConvertType::operator()(const QualType* type) -> mlir::Type {
223232

224233
auto Codegen::ConvertType::operator()(const BoundedArrayType* type)
225234
-> mlir::Type {
226-
return getExprType();
235+
auto elementType = gen.convertType(type->elementType());
236+
return gen.builder_.getType<mlir::cxx::ArrayType>(elementType, type->size());
227237
}
228238

229239
auto Codegen::ConvertType::operator()(const UnboundedArrayType* type)
230240
-> mlir::Type {
231-
return getExprType();
241+
auto elementType = gen.convertType(type->elementType());
242+
return gen.builder_.getType<mlir::cxx::PointerType>(elementType);
232243
}
233244

234245
auto Codegen::ConvertType::operator()(const PointerType* type) -> mlir::Type {
@@ -251,7 +262,38 @@ auto Codegen::ConvertType::operator()(const FunctionType* type) -> mlir::Type {
251262
}
252263

253264
auto Codegen::ConvertType::operator()(const ClassType* type) -> mlir::Type {
254-
return getExprType();
265+
auto classSymbol = type->symbol();
266+
267+
auto ctx = gen.builder_.getContext();
268+
269+
if (auto it = gen.classNames_.find(classSymbol);
270+
it != gen.classNames_.end()) {
271+
return mlir::cxx::ClassType::get(ctx, it->second, {});
272+
}
273+
274+
auto name = to_string(classSymbol->name());
275+
if (name.empty()) {
276+
auto loc = type->symbol()->location();
277+
name = std::format("$class_{}", loc.index());
278+
}
279+
280+
gen.classNames_[classSymbol] = name;
281+
282+
// todo: layout of parent classes, anonymous nested fields, etc.
283+
284+
std::vector<mlir::Type> memberTypes;
285+
286+
for (auto member : classSymbol->scope()->symbols()) {
287+
auto field = symbol_cast<FieldSymbol>(member);
288+
if (!field) continue;
289+
if (field->isStatic()) continue;
290+
auto memberType = gen.convertType(member->type());
291+
memberTypes.push_back(memberType);
292+
}
293+
294+
auto classType = mlir::cxx::ClassType::get(ctx, name, memberTypes);
295+
296+
return classType;
255297
}
256298

257299
auto Codegen::ConvertType::operator()(const EnumType* type) -> mlir::Type {

src/mlir/cxx/mlir/cxx_dialect.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ struct CxxGenerateAliases : public OpAsmDialectInterface {
4444
return AliasResult::FinalAlias;
4545
}
4646

47+
if (auto floatType = mlir::dyn_cast<mlir::cxx::FloatType>(type)) {
48+
os << 'f' << floatType.getWidth();
49+
return AliasResult::FinalAlias;
50+
}
51+
52+
if (auto classType = mlir::dyn_cast<mlir::cxx::ClassType>(type)) {
53+
if (!classType.getBody().empty()) {
54+
os << "class_" << classType.getName();
55+
return AliasResult::FinalAlias;
56+
}
57+
}
58+
4759
if (mlir::isa<VoidType>(type)) {
4860
os << "void";
4961
return AliasResult::FinalAlias;

0 commit comments

Comments
 (0)