diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a0fcc9a4..1ebaaf3f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -72,9 +72,9 @@ jobs: run: | sudo apt-get -y update sudo apt-get install -y \ - llvm-18-dev \ - libmlir-18-dev \ - mlir-18-tools + llvm-19-dev \ + libmlir-19-dev \ + mlir-19-tools - name: Install unit tests requirements run: | @@ -85,11 +85,9 @@ jobs: run: | . .venv/bin/activate - cmake . \ - -Bbuild \ - -DCMAKE_BUILD_TYPE=Release \ - -DCXX_ENABLE_MLIR=ON \ - -DMLIR_DIR=/usr/lib/llvm-18/lib/cmake/mlir + PATH=/usr/lib/llvm-19/bin:$PATH + + cmake --preset default-mlir cmake --build build --parallel diff --git a/src/mlir/cxx/mlir/CMakeLists.txt b/src/mlir/cxx/mlir/CMakeLists.txt index e1d12fc1..bc7c1dc3 100644 --- a/src/mlir/cxx/mlir/CMakeLists.txt +++ b/src/mlir/cxx/mlir/CMakeLists.txt @@ -28,7 +28,13 @@ add_dependencies(cxx-mlir MLIRCxxOpsIncGen) if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") target_compile_options(cxx-mlir PRIVATE - $<$:-Wno-all -Wno-extra -Wno-covered-switch-default> + $<$:-Wno-all -Wno-extra> ) + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + target_compile_options(cxx-mlir PRIVATE + $<$:-Wno-covered-switch-default> + ) + endif() + endif() diff --git a/src/mlir/cxx/mlir/CxxOps.td b/src/mlir/cxx/mlir/CxxOps.td index 1e5ffbcc..285a6b9a 100644 --- a/src/mlir/cxx/mlir/CxxOps.td +++ b/src/mlir/cxx/mlir/CxxOps.td @@ -42,8 +42,19 @@ class Cxx_Type traits = []> class Cxx_Op traits = []> : Op {} + +// types + def Cxx_ExprType : Cxx_Type<"Expr", "expr">; +def Cxx_PointerType : Cxx_Type<"Pointer", "ptr"> { + let parameters = (ins "Type":$elementType); + + let assemblyFormat = "`<` $elementType `>`"; +} + +// ops + def Cxx_FuncOp : Cxx_Op<"func", [FunctionOpInterface, IsolatedFromAbove]> { let arguments = (ins SymbolNameAttr:$sym_name, TypeAttrOf:$function_type, @@ -67,7 +78,7 @@ def Cxx_FuncOp : Cxx_Op<"func", [FunctionOpInterface, IsolatedFromAbove]> { } def Cxx_ReturnOp : Cxx_Op<"return", [Pure, HasParent<"FuncOp">, Terminator]> { - let arguments = (ins Variadic:$input); + let arguments = (ins Variadic:$input); let builders = [OpBuilder<(ins), [{ build($_builder, $_state, {}); }]>]; @@ -78,20 +89,7 @@ def Cxx_ReturnOp : Cxx_Op<"return", [Pure, HasParent<"FuncOp">, Terminator]> { let hasVerifier = 0; } -def Cxx_CallOp : Cxx_Op<"call"> { - let arguments = (ins Cxx_ExprType:$callee, Variadic:$arguments); - let results = (outs Cxx_ExprType:$result); - let assemblyFormat = - "$callee `(` $arguments `:` type($arguments) `)` attr-dict `:` " - "type($result)"; - let builders = - [OpBuilder< - (ins "mlir::Value":$callee, "mlir::ValueRange":$arguments), - [{ build($_builder, $_state, $_builder.getType(), callee, arguments); }]>, - ]; -} - -def TodoExprOp : Cxx_Op<"todo.expr"> { +def Cxx_TodoExprOp : Cxx_Op<"todo.expr"> { let arguments = (ins StrAttr:$message); let results = (outs Cxx_ExprType:$result); let assemblyFormat = "$message attr-dict `:` type($result)"; @@ -102,59 +100,8 @@ def TodoExprOp : Cxx_Op<"todo.expr"> { ]; } -def TodoStmtOp : Cxx_Op<"todo.stmt"> { +def Cxx_TodoStmtOp : Cxx_Op<"todo.stmt"> { let arguments = (ins StrAttr:$message); let results = (outs); let assemblyFormat = "$message attr-dict"; -} - -def ToBoolOp : Cxx_Op<"to.bool"> { - let arguments = (ins Cxx_ExprType:$value); - let results = (outs I1:$result); - let assemblyFormat = "`(` $value `)` attr-dict `:` type($result)"; -} - -def ImplicitCastOp : Cxx_Op<"implicit.cast"> { - let arguments = (ins StrAttr:$cast, Cxx_ExprType:$value); - let results = (outs Cxx_ExprType:$result); - let assemblyFormat = "$cast `(` $value `)` attr-dict `:` type($result)"; - let builders = - [OpBuilder< - (ins "llvm::StringRef":$cast, "mlir::Value":$value), - [{ build($_builder, $_state, $_builder.getType(), cast, value); }]>, - ]; -} - -def IntLiteralOp : Cxx_Op<"int.literal"> { - let arguments = (ins I64Attr:$value); - let results = (outs Cxx_ExprType:$result); - let assemblyFormat = "$value attr-dict `:` type($result)"; - let builders = - [OpBuilder< - (ins "int64_t":$value), - [{ build($_builder, $_state, $_builder.getType(), value); }]>, - ]; -} - -def IdOp : Cxx_Op<"id"> { - let arguments = (ins StrAttr:$name); - let results = (outs Cxx_ExprType:$result); - let assemblyFormat = "$name attr-dict `:` type($result)"; - let builders = - [OpBuilder< - (ins "llvm::StringRef":$name), - [{ build($_builder, $_state, $_builder.getType(), name); }]>, - ]; -} - -def BinOp : Cxx_Op<"binary"> { - let arguments = (ins StrAttr:$op, Cxx_ExprType:$lhs, Cxx_ExprType:$rhs); - let results = (outs Cxx_ExprType:$result); - let assemblyFormat = - "`op` $op `(` $lhs `,` $rhs `)` attr-dict `:` type($result)"; - let builders = - [OpBuilder< - (ins "llvm::StringRef":$op, "mlir::Value":$lhs, "mlir::Value":$rhs), - [{ build($_builder, $_state, $_builder.getType(), op, lhs, rhs); }]>, - ]; -} +} \ No newline at end of file diff --git a/src/mlir/cxx/mlir/codegen.cc b/src/mlir/cxx/mlir/codegen.cc index 7f86c483..e7ace4d6 100644 --- a/src/mlir/cxx/mlir/codegen.cc +++ b/src/mlir/cxx/mlir/codegen.cc @@ -25,11 +25,14 @@ #include #include #include +#include #include #include #include #include #include + +// #include #include #include @@ -38,6 +41,197 @@ namespace cxx { +struct Codegen::ConvertType { + Codegen& gen; + + [[nodiscard]] auto control() const { return gen.control(); } + [[nodiscard]] auto memoryLayout() const { return control()->memoryLayout(); } + + auto getExprType() const -> mlir::Type { + return gen.builder_.getType(); + } + + auto getIntType(const Type* type, bool isSigned) -> mlir::Type { + const auto width = memoryLayout()->sizeOf(type).value() * 8; + return gen.builder_.getIntegerType(width, isSigned); + } + + auto operator()(const VoidType* type) -> mlir::Type { + return gen.builder_.getNoneType(); + } + + auto operator()(const NullptrType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const DecltypeAutoType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const AutoType* type) -> mlir::Type { return getExprType(); } + + auto operator()(const BoolType* type) -> mlir::Type { + return gen.builder_.getI1Type(); + } + + auto operator()(const SignedCharType* type) -> mlir::Type { + return getIntType(type, true); + } + + auto operator()(const ShortIntType* type) -> mlir::Type { + return getIntType(type, true); + } + + auto operator()(const IntType* type) -> mlir::Type { + return getIntType(type, true); + } + + auto operator()(const LongIntType* type) -> mlir::Type { + return getIntType(type, true); + } + + auto operator()(const LongLongIntType* type) -> mlir::Type { + return getIntType(type, true); + } + + auto operator()(const Int128Type* type) -> mlir::Type { + return getIntType(type, true); + } + + auto operator()(const UnsignedCharType* type) -> mlir::Type { + return getIntType(type, false); + } + + auto operator()(const UnsignedShortIntType* type) -> mlir::Type { + return getIntType(type, false); + } + + auto operator()(const UnsignedIntType* type) -> mlir::Type { + return getIntType(type, false); + } + + auto operator()(const UnsignedLongIntType* type) -> mlir::Type { + return getIntType(type, false); + } + + auto operator()(const UnsignedLongLongIntType* type) -> mlir::Type { + return getIntType(type, false); + } + + auto operator()(const UnsignedInt128Type* type) -> mlir::Type { + return getIntType(type, false); + } + + auto operator()(const CharType* type) -> mlir::Type { return getExprType(); } + + auto operator()(const Char8Type* type) -> mlir::Type { return getExprType(); } + + auto operator()(const Char16Type* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const Char32Type* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const WideCharType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const FloatType* type) -> mlir::Type { + return gen.builder_.getF32Type(); + } + + auto operator()(const DoubleType* type) -> mlir::Type { + return gen.builder_.getF64Type(); + } + + auto operator()(const LongDoubleType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const QualType* type) -> mlir::Type { + return gen.convertType(type->elementType()); + } + + auto operator()(const BoundedArrayType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const UnboundedArrayType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const PointerType* type) -> mlir::Type { + auto elementType = gen.convertType(type->elementType()); + return gen.builder_.getType(elementType); + } + + auto operator()(const LvalueReferenceType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const RvalueReferenceType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const FunctionType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const ClassType* type) -> mlir::Type { return getExprType(); } + + auto operator()(const EnumType* type) -> mlir::Type { return getExprType(); } + + auto operator()(const ScopedEnumType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const MemberObjectPointerType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const MemberFunctionPointerType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const NamespaceType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const TypeParameterType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const TemplateTypeParameterType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const UnresolvedNameType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const UnresolvedBoundedArrayType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const UnresolvedUnderlyingType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const OverloadSetType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const BuiltinVaListType* type) -> mlir::Type { + return getExprType(); + } + + auto operator()(const Type* type) -> mlir::Type { + return gen.builder_.getType(); + } +}; + struct Codegen::UnitVisitor { Codegen& gen; @@ -664,6 +858,10 @@ auto Codegen::emitTodoExpr(SourceLocation location, std::string_view message) return op; } +auto Codegen::convertType(const Type* type) -> mlir::Type { + return visit(ConvertType{*this}, type); +} + auto Codegen::operator()(UnitAST* ast) -> UnitResult { if (ast) return visit(UnitVisitor{*this}, ast); return {}; @@ -1201,7 +1399,7 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast) std::vector resultTypes; for (auto paramTy : functionType->parameterTypes()) { - inputTypes.push_back(exprType); + inputTypes.push_back(gen.convertType(paramTy)); } if (!gen.control()->is_void(functionType->returnType())) { @@ -1619,10 +1817,15 @@ auto Codegen::ExpressionVisitor::operator()(BoolLiteralExpressionAST* ast) auto Codegen::ExpressionVisitor::operator()(IntLiteralExpressionAST* ast) -> ExpressionResult { + auto op = + gen.emitTodoExpr(ast->firstSourceLocation(), to_string(ast->kind())); + +#if false auto loc = gen.getLocation(ast->literalLoc); auto op = gen.builder_.create( loc, ast->literal->integerValue()); +#endif return {op}; } diff --git a/src/mlir/cxx/mlir/codegen.h b/src/mlir/cxx/mlir/codegen.h index 3f0513ba..4f9cc141 100644 --- a/src/mlir/cxx/mlir/codegen.h +++ b/src/mlir/cxx/mlir/codegen.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -172,6 +173,8 @@ class Codegen { [[nodiscard]] auto emitTodoExpr(SourceLocation loc, std::string_view message) -> mlir::cxx::TodoExprOp; + [[nodiscard]] auto convertType(const Type* type) -> mlir::Type; + struct UnitVisitor; struct DeclarationVisitor; struct StatementVisitor; @@ -194,6 +197,8 @@ class Codegen { struct AttributeSpecifierVisitor; struct AttributeTokenVisitor; + struct ConvertType; + mlir::OpBuilder builder_; mlir::ModuleOp module_; mlir::cxx::FuncOp function_;