Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/mlir/cxx/mlir/CxxOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ class Cxx_Op<string mnemonic, list<Trait> traits = []>

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

def Cxx_VoidType : Cxx_Type<"Void", "void">;

def Cxx_BoolType : Cxx_Type<"Bool", "bool">;

def Cxx_IntegerType : Cxx_Type<"Integer", "int"> {
let parameters = (ins "unsigned":$width, "bool":$isSigned);

let assemblyFormat = "`<` $width `,` $isSigned `>`";
}

def Cxx_PointerType : Cxx_Type<"Pointer", "ptr"> {
let parameters = (ins "Type":$elementType);

Expand Down
6 changes: 4 additions & 2 deletions src/mlir/cxx/mlir/codegen_declarations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
-> DeclarationResult {
auto functionSymbol = ast->symbol;
auto functionType = type_cast<FunctionType>(functionSymbol->type());
auto returnType = functionType->returnType();

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

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

if (!gen.control()->is_void(functionType->returnType())) {
resultTypes.push_back(exprType);
resultTypes.push_back(gen.convertType(returnType));
}

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

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

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

auto result =
gen.builder_.create<mlir::cxx::ReturnOp>(endLoc, r->getResults());
}
Expand Down
6 changes: 3 additions & 3 deletions src/mlir/cxx/mlir/convert_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ auto Codegen::ConvertType::getExprType() const -> mlir::Type {
auto Codegen::ConvertType::getIntType(const Type* type, bool isSigned)
-> mlir::Type {
const auto width = memoryLayout()->sizeOf(type).value() * 8;
return gen.builder_.getIntegerType(width, isSigned);
return gen.builder_.getType<mlir::cxx::IntegerType>(width, isSigned);
}

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

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

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

auto Codegen::ConvertType::operator()(const SignedCharType* type)
Expand Down
32 changes: 31 additions & 1 deletion src/mlir/cxx/mlir/cxx_dialect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@

namespace mlir::cxx {

namespace {

struct CxxGenerateAliases : public OpAsmDialectInterface {
public:
using OpAsmDialectInterface::OpAsmDialectInterface;

auto getAlias(Type type, raw_ostream &os) const -> AliasResult override {
if (auto intType = mlir::dyn_cast<mlir::cxx::IntegerType>(type)) {
os << 'i' << intType.getWidth() << (intType.getIsSigned() ? 's' : 'u');
return AliasResult::FinalAlias;
}

if (mlir::isa<VoidType>(type)) {
os << "void";
return AliasResult::FinalAlias;
}

if (mlir::isa<BoolType>(type)) {
os << "bool";
return AliasResult::FinalAlias;
}

return AliasResult::NoAlias;
}
};
} // namespace

void CxxDialect::initialize() {
addOperations<
#define GET_OP_LIST
Expand All @@ -42,6 +69,8 @@ void CxxDialect::initialize() {
#define GET_TYPEDEF_LIST
#include <cxx/mlir/CxxOpsTypes.cpp.inc>
>();

addInterface<CxxGenerateAliases>();
}

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

} // namespace mlir::cxx

#include <cxx/mlir/CxxOpsDialect.cpp.inc>

#define GET_TYPEDEF_CLASSES
#include <cxx/mlir/CxxOpsTypes.cpp.inc>

#define GET_OP_CLASSES
#include <cxx/mlir/CxxOps.cpp.inc>
#include <cxx/mlir/CxxOpsDialect.cpp.inc>