Skip to content

Commit a4c11da

Browse files
committed
Add MLIR types for integers
Fixes #613 Signed-off-by: Roberto Raggi <[email protected]>
1 parent 41f9e08 commit a4c11da

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

src/mlir/cxx/mlir/CxxOps.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ class Cxx_Op<string mnemonic, list<Trait> traits = []>
4747

4848
def Cxx_ExprType : Cxx_Type<"Expr", "expr">;
4949

50+
def Cxx_VoidType : Cxx_Type<"Void", "void">;
51+
52+
def Cxx_BoolType : Cxx_Type<"Bool", "bool">;
53+
54+
def Cxx_IntegerType : Cxx_Type<"Integer", "int"> {
55+
let parameters = (ins "unsigned":$width, "bool":$isSigned);
56+
57+
let assemblyFormat = "`<` $width `,` $isSigned `>`";
58+
}
59+
5060
def Cxx_PointerType : Cxx_Type<"Pointer", "ptr"> {
5161
let parameters = (ins "Type":$elementType);
5262

src/mlir/cxx/mlir/codegen_declarations.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
268268
-> DeclarationResult {
269269
auto functionSymbol = ast->symbol;
270270
auto functionType = type_cast<FunctionType>(functionSymbol->type());
271+
auto returnType = functionType->returnType();
271272

272273
auto exprType = gen.builder_.getType<mlir::cxx::ExprType>();
273274

@@ -279,7 +280,7 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
279280
}
280281

281282
if (!gen.control()->is_void(functionType->returnType())) {
282-
resultTypes.push_back(exprType);
283+
resultTypes.push_back(gen.convertType(returnType));
283284
}
284285

285286
auto funcType = gen.builder_.getFunctionType(inputTypes, resultTypes);
@@ -337,12 +338,13 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
337338

338339
auto endLoc = gen.getLocation(ast->lastSourceLocation());
339340

340-
if (gen.control()->is_void(functionType->returnType())) {
341+
if (gen.control()->is_void(returnType)) {
341342
// If the function returns void, we don't need to return anything.
342343
gen.builder_.create<mlir::cxx::ReturnOp>(endLoc);
343344
} else {
344345
// Otherwise, we need to return a value of the correct type.
345346
auto r = gen.emitTodoExpr(ast->lastSourceLocation(), "result value");
347+
346348
auto result =
347349
gen.builder_.create<mlir::cxx::ReturnOp>(endLoc, r->getResults());
348350
}

src/mlir/cxx/mlir/convert_type.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ auto Codegen::ConvertType::getExprType() const -> mlir::Type {
9999
auto Codegen::ConvertType::getIntType(const Type* type, bool isSigned)
100100
-> mlir::Type {
101101
const auto width = memoryLayout()->sizeOf(type).value() * 8;
102-
return gen.builder_.getIntegerType(width, isSigned);
102+
return gen.builder_.getType<mlir::cxx::IntegerType>(width, isSigned);
103103
}
104104

105105
auto Codegen::ConvertType::operator()(const VoidType* type) -> mlir::Type {
106-
return gen.builder_.getNoneType();
106+
return gen.builder_.getType<mlir::cxx::VoidType>();
107107
}
108108

109109
auto Codegen::ConvertType::operator()(const NullptrType* type) -> mlir::Type {
@@ -120,7 +120,7 @@ auto Codegen::ConvertType::operator()(const AutoType* type) -> mlir::Type {
120120
}
121121

122122
auto Codegen::ConvertType::operator()(const BoolType* type) -> mlir::Type {
123-
return gen.builder_.getI1Type();
123+
return gen.builder_.getType<mlir::cxx::BoolType>();
124124
}
125125

126126
auto Codegen::ConvertType::operator()(const SignedCharType* type)

src/mlir/cxx/mlir/cxx_dialect.cc

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,33 @@
3232

3333
namespace mlir::cxx {
3434

35+
namespace {
36+
37+
struct CxxGenerateAliases : public OpAsmDialectInterface {
38+
public:
39+
using OpAsmDialectInterface::OpAsmDialectInterface;
40+
41+
auto getAlias(Type type, raw_ostream &os) const -> AliasResult override {
42+
if (auto intType = mlir::dyn_cast<mlir::cxx::IntegerType>(type)) {
43+
os << 'i' << intType.getWidth() << (intType.getIsSigned() ? 's' : 'u');
44+
return AliasResult::FinalAlias;
45+
}
46+
47+
if (mlir::isa<VoidType>(type)) {
48+
os << "void";
49+
return AliasResult::FinalAlias;
50+
}
51+
52+
if (mlir::isa<BoolType>(type)) {
53+
os << "bool";
54+
return AliasResult::FinalAlias;
55+
}
56+
57+
return AliasResult::NoAlias;
58+
}
59+
};
60+
} // namespace
61+
3562
void CxxDialect::initialize() {
3663
addOperations<
3764
#define GET_OP_LIST
@@ -42,6 +69,8 @@ void CxxDialect::initialize() {
4269
#define GET_TYPEDEF_LIST
4370
#include <cxx/mlir/CxxOpsTypes.cpp.inc>
4471
>();
72+
73+
addInterface<CxxGenerateAliases>();
4574
}
4675

4776
void FuncOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
@@ -72,9 +101,10 @@ auto FuncOp::parse(mlir::OpAsmParser &parser, mlir::OperationState &result)
72101

73102
} // namespace mlir::cxx
74103

104+
#include <cxx/mlir/CxxOpsDialect.cpp.inc>
105+
75106
#define GET_TYPEDEF_CLASSES
76107
#include <cxx/mlir/CxxOpsTypes.cpp.inc>
77108

78109
#define GET_OP_CLASSES
79110
#include <cxx/mlir/CxxOps.cpp.inc>
80-
#include <cxx/mlir/CxxOpsDialect.cpp.inc>

0 commit comments

Comments
 (0)