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
21 changes: 21 additions & 0 deletions src/mlir/cxx/mlir/CxxOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,33 @@ def Cxx_IntegerType : Cxx_Type<"Integer", "int"> {
let assemblyFormat = "`<` $width `,` $isSigned `>`";
}

def Cxx_FloatType : Cxx_Type<"Float", "float"> {
let parameters = (ins "unsigned":$width);

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

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

let assemblyFormat = "`<` $elementType `>`";
}

def Cxx_ArrayType : Cxx_Type<"Array", "array"> {
let parameters = (ins "Type":$elementType, "unsigned":$size);

let assemblyFormat = "`<` $elementType `,` $size `>`";
}

def Cxx_ClassType : Cxx_Type<"Class", "class"> {
let parameters = (ins
StringRefParameter<"class name", [{ "" }]>:$name,
OptionalArrayRefParameter<"mlir::Type">:$body
);

let assemblyFormat = "`<` $name `(` $body `)` `>`";
}

// ops

def Cxx_FuncOp : Cxx_Op<"func", [FunctionOpInterface, IsolatedFromAbove]> {
Expand Down
4 changes: 4 additions & 0 deletions src/mlir/cxx/mlir/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
#include <cxx/ast_fwd.h>
#include <cxx/mlir/cxx_dialect.h>
#include <cxx/source_location.h>
#include <cxx/symbols_fwd.h>
#include <cxx/types_fwd.h>
#include <mlir/Dialect/Func/IR/FuncOps.h>
#include <mlir/IR/Builders.h>
#include <mlir/IR/BuiltinOps.h>

#include <unordered_map>

namespace mlir::func {
class FuncOp;
}
Expand Down Expand Up @@ -271,6 +274,7 @@ class Codegen {
TranslationUnit* unit_ = nullptr;
mlir::Block* exitBlock_ = nullptr;
mlir::cxx::AllocaOp exitValue_;
std::unordered_map<ClassSymbol*, std::string> classNames_;
int count_ = 0;
};

Expand Down
54 changes: 48 additions & 6 deletions src/mlir/cxx/mlir/convert_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
#include <cxx/literals.h>
#include <cxx/memory_layout.h>
#include <cxx/names.h>
#include <cxx/scope.h>
#include <cxx/symbols.h>
#include <cxx/translation_unit.h>
#include <cxx/types.h>

#include <format>

namespace cxx {

struct Codegen::ConvertType {
Expand All @@ -40,6 +43,7 @@ struct Codegen::ConvertType {

auto getExprType() const -> mlir::Type;
auto getIntType(const Type* type, bool isSigned) -> mlir::Type;
auto getFloatType(const Type* type) -> mlir::Type;

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

auto Codegen::ConvertType::getFloatType(const Type* type) -> mlir::Type {
return gen.builder_.getType<mlir::cxx::FloatType>(
memoryLayout()->sizeOf(type).value() * 8);
}

auto Codegen::ConvertType::operator()(const VoidType* type) -> mlir::Type {
return gen.builder_.getType<mlir::cxx::VoidType>();
}
Expand Down Expand Up @@ -205,16 +214,16 @@ auto Codegen::ConvertType::operator()(const WideCharType* type) -> mlir::Type {
}

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

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

auto Codegen::ConvertType::operator()(const LongDoubleType* type)
-> mlir::Type {
return getExprType();
return getFloatType(type);
}

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

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

auto Codegen::ConvertType::operator()(const UnboundedArrayType* type)
-> mlir::Type {
return getExprType();
auto elementType = gen.convertType(type->elementType());
return gen.builder_.getType<mlir::cxx::PointerType>(elementType);
}

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

auto Codegen::ConvertType::operator()(const ClassType* type) -> mlir::Type {
return getExprType();
auto classSymbol = type->symbol();

auto ctx = gen.builder_.getContext();

if (auto it = gen.classNames_.find(classSymbol);
it != gen.classNames_.end()) {
return mlir::cxx::ClassType::get(ctx, it->second, {});
}

auto name = to_string(classSymbol->name());
if (name.empty()) {
auto loc = type->symbol()->location();
name = std::format("$class_{}", loc.index());
}

gen.classNames_[classSymbol] = name;

// todo: layout of parent classes, anonymous nested fields, etc.

std::vector<mlir::Type> memberTypes;

for (auto member : classSymbol->scope()->symbols()) {
auto field = symbol_cast<FieldSymbol>(member);
if (!field) continue;
if (field->isStatic()) continue;
auto memberType = gen.convertType(member->type());
memberTypes.push_back(memberType);
}

auto classType = mlir::cxx::ClassType::get(ctx, name, memberTypes);

return classType;
}

auto Codegen::ConvertType::operator()(const EnumType* type) -> mlir::Type {
Expand Down
12 changes: 12 additions & 0 deletions src/mlir/cxx/mlir/cxx_dialect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ struct CxxGenerateAliases : public OpAsmDialectInterface {
return AliasResult::FinalAlias;
}

if (auto floatType = mlir::dyn_cast<mlir::cxx::FloatType>(type)) {
os << 'f' << floatType.getWidth();
return AliasResult::FinalAlias;
}

if (auto classType = mlir::dyn_cast<mlir::cxx::ClassType>(type)) {
if (!classType.getBody().empty()) {
os << "class_" << classType.getName();
return AliasResult::FinalAlias;
}
}

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