diff --git a/src/mlir/cxx/mlir/codegen.cc b/src/mlir/cxx/mlir/codegen.cc index 6cf16c2e..29af04be 100644 --- a/src/mlir/cxx/mlir/codegen.cc +++ b/src/mlir/cxx/mlir/codegen.cc @@ -68,7 +68,7 @@ auto Codegen::newUniqueSymbolName(std::string_view prefix) -> std::string { void Codegen::branch(mlir::Location loc, mlir::Block* block, mlir::ValueRange operands) { if (currentBlockMightHaveTerminator()) return; - builder_.create(loc, block, operands); + mlir::cf::BranchOp::create(builder_, loc, block, operands); } auto Codegen::findOrCreateLocal(Symbol* symbol) -> std::optional { @@ -83,7 +83,7 @@ auto Codegen::findOrCreateLocal(Symbol* symbol) -> std::optional { auto ptrType = builder_.getType(type); auto loc = getLocation(var->location()); - auto allocaOp = builder_.create(loc, ptrType); + auto allocaOp = mlir::cxx::AllocaOp::create(builder_, loc, ptrType); locals_.emplace(var, allocaOp); @@ -93,7 +93,7 @@ auto Codegen::findOrCreateLocal(Symbol* symbol) -> std::optional { auto Codegen::newTemp(const Type* type, SourceLocation loc) -> mlir::cxx::AllocaOp { auto ptrType = builder_.getType(convertType(type)); - return builder_.create(getLocation(loc), ptrType); + return mlir::cxx::AllocaOp::create(builder_, getLocation(loc), ptrType); } auto Codegen::findOrCreateFunction(FunctionSymbol* functionSymbol) @@ -145,8 +145,8 @@ auto Codegen::findOrCreateFunction(FunctionSymbol* functionSymbol) builder_.setInsertionPointToStart(module_.getBody()); - auto func = builder_.create( - loc, name, funcType, mlir::ArrayAttr{}, mlir::ArrayAttr{}); + auto func = mlir::cxx::FuncOp::create(builder_, loc, name, funcType, + mlir::ArrayAttr{}, mlir::ArrayAttr{}); funcOps_.insert_or_assign(functionSymbol, func); @@ -165,14 +165,14 @@ auto Codegen::getLocation(SourceLocation location) -> mlir::Location { auto Codegen::emitTodoStmt(SourceLocation location, std::string_view message) -> mlir::cxx::TodoStmtOp { const auto loc = getLocation(location); - auto op = builder_.create(loc, message); + auto op = mlir::cxx::TodoStmtOp::create(builder_, loc, message); return op; } auto Codegen::emitTodoExpr(SourceLocation location, std::string_view message) -> mlir::cxx::TodoExprOp { const auto loc = getLocation(location); - auto op = builder_.create(loc, message); + auto op = mlir::cxx::TodoExprOp::create(builder_, loc, message); return op; } diff --git a/src/mlir/cxx/mlir/codegen_declarations.cc b/src/mlir/cxx/mlir/codegen_declarations.cc index ff7f43e8..42c69795 100644 --- a/src/mlir/cxx/mlir/codegen_declarations.cc +++ b/src/mlir/cxx/mlir/codegen_declarations.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -37,6 +38,17 @@ namespace cxx { +namespace { + +[[nodiscard]] auto is_global_namespace(Symbol* symbol) -> bool { + if (!symbol) return false; + if (!symbol->isNamespace()) return false; + if (symbol->parent()) return false; + return true; +} + +} // namespace + struct Codegen::DeclarationVisitor { Codegen& gen; @@ -205,8 +217,8 @@ auto Codegen::DeclarationVisitor::operator()(SimpleDeclarationAST* ast) const auto elementType = gen.convertType(var->type()); - gen.builder_.create(loc, expressionResult.value, - local.value()); + mlir::cxx::StoreOp::create(gen.builder_, loc, expressionResult.value, + local.value()); } return {}; @@ -365,7 +377,17 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast) gen.getLocation(ast->functionBody->firstSourceLocation()); auto exitValueType = gen.convertType(returnType); auto ptrType = gen.builder_.getType(exitValueType); - exitValue = gen.builder_.create(exitValueLoc, ptrType); + exitValue = + mlir::cxx::AllocaOp::create(gen.builder_, exitValueLoc, ptrType); + + auto id = name_cast(functionSymbol->name()); + if (id && id->name() == "main" && + is_global_namespace(functionSymbol->parent())) { + auto zeroOp = mlir::cxx::IntConstantOp::create( + gen.builder_, loc, gen.convertType(gen.control()->getIntType()), 0); + + mlir::cxx::StoreOp::create(gen.builder_, exitValueLoc, zeroOp, exitValue); + } } std::unordered_map locals; @@ -389,8 +411,8 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast) thisValue = gen.newTemp(classSymbol->type(), ast->firstSourceLocation()); // store the `this` pointer in the entry block - gen.builder_.create( - loc, gen.entryBlock_->getArgument(0), thisValue); + mlir::cxx::StoreOp::create(gen.builder_, loc, + gen.entryBlock_->getArgument(0), thisValue); } FunctionParametersSymbol* params = nullptr; @@ -408,11 +430,11 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast) auto ptrType = gen.builder_.getType(type); auto loc = gen.getLocation(arg->location()); - auto allocaOp = gen.builder_.create(loc, ptrType); + auto allocaOp = mlir::cxx::AllocaOp::create(gen.builder_, loc, ptrType); auto value = args[argc]; ++argc; - gen.builder_.create(loc, value, allocaOp); + mlir::cxx::StoreOp::create(gen.builder_, loc, value, allocaOp); gen.locals_.emplace(arg, allocaOp); } @@ -430,7 +452,7 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast) const auto endLoc = gen.getLocation(ast->lastSourceLocation()); if (!gen.builder_.getBlock()->mightHaveTerminator()) { - gen.builder_.create(endLoc, gen.exitBlock_); + mlir::cf::BranchOp::create(gen.builder_, endLoc, gen.exitBlock_); } gen.builder_.setInsertionPointToEnd(gen.exitBlock_); @@ -439,13 +461,13 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast) // We need to return a value of the correct type. auto elementType = gen.exitValue_.getType().getElementType(); - auto value = gen.builder_.create(endLoc, elementType, - gen.exitValue_); + auto value = mlir::cxx::LoadOp::create(gen.builder_, endLoc, elementType, + gen.exitValue_); - gen.builder_.create(endLoc, value->getResults()); + mlir::cxx::ReturnOp::create(gen.builder_, endLoc, value->getResults()); } else { // If the function returns void, we don't need to return anything. - gen.builder_.create(endLoc); + mlir::cxx::ReturnOp::create(gen.builder_, endLoc); } // restore the state diff --git a/src/mlir/cxx/mlir/codegen_expressions.cc b/src/mlir/cxx/mlir/codegen_expressions.cc index 1a488618..65dc6fa8 100644 --- a/src/mlir/cxx/mlir/codegen_expressions.cc +++ b/src/mlir/cxx/mlir/codegen_expressions.cc @@ -153,9 +153,9 @@ void Codegen::condition(ExpressionAST* ast, mlir::Block* trueBlock, const auto loc = getLocation(ast->firstSourceLocation()); auto value = expression(ast); - builder_.create(loc, value.value, mlir::ValueRange{}, - mlir::ValueRange{}, trueBlock, - falseBlock); + mlir::cxx::CondBranchOp::create(builder_, loc, value.value, + mlir::ValueRange{}, mlir::ValueRange{}, + trueBlock, falseBlock); } auto Codegen::newInitializer(NewInitializerAST* ast) -> NewInitializerResult { @@ -179,7 +179,7 @@ auto Codegen::ExpressionVisitor::operator()(CharLiteralExpressionAST* ast) auto type = gen.convertType(ast->type); auto value = std::int64_t(ast->literal->charValue()); - auto op = gen.builder_.create(loc, type, value); + auto op = mlir::cxx::IntConstantOp::create(gen.builder_, loc, type, value); return {op}; } @@ -191,7 +191,7 @@ auto Codegen::ExpressionVisitor::operator()(BoolLiteralExpressionAST* ast) auto type = gen.convertType(ast->type); auto op = - gen.builder_.create(loc, type, ast->isTrue); + mlir::cxx::BoolConstantOp::create(gen.builder_, loc, type, ast->isTrue); return {op}; } @@ -203,7 +203,7 @@ auto Codegen::ExpressionVisitor::operator()(IntLiteralExpressionAST* ast) auto type = gen.convertType(ast->type); auto value = ast->literal->integerValue(); - auto op = gen.builder_.create(loc, type, value); + auto op = mlir::cxx::IntConstantOp::create(gen.builder_, loc, type, value); return {op}; } @@ -233,7 +233,7 @@ auto Codegen::ExpressionVisitor::operator()(FloatLiteralExpressionAST* ast) return {op}; } - auto op = gen.builder_.create(loc, type, value); + auto op = mlir::cxx::FloatConstantOp::create(gen.builder_, loc, type, value); return {op}; } @@ -264,13 +264,13 @@ auto Codegen::ExpressionVisitor::operator()(StringLiteralExpressionAST* ast) auto x = mlir::OpBuilder(gen.module_->getContext()); x.setInsertionPointToEnd(gen.module_.getBody()); - x.create(loc, type, true, name, initializer); + mlir::cxx::GlobalOp::create(x, loc, type, true, name, initializer); it = gen.stringLiterals_.insert_or_assign(ast->literal, name).first; } auto op = - gen.builder_.create(loc, resultType, it->second); + mlir::cxx::AddressOfOp::create(gen.builder_, loc, resultType, it->second); return {op}; } @@ -295,11 +295,9 @@ auto Codegen::ExpressionVisitor::operator()(ThisExpressionAST* ast) auto loc = gen.getLocation(ast->firstSourceLocation()); auto loadOp = - gen.builder_.create(loc, type, gen.thisValue_); + mlir::cxx::LoadOp::create(gen.builder_, loc, type, gen.thisValue_); + return {loadOp}; - // auto op = - // gen.emitTodoExpr(ast->firstSourceLocation(), to_string(ast->kind())); - // return {op}; } auto Codegen::ExpressionVisitor::operator()(GenericSelectionExpressionAST* ast) @@ -342,7 +340,7 @@ auto Codegen::ExpressionVisitor::operator()(IdExpressionAST* ast) auto loc = gen.getLocation(ast->firstSourceLocation()); auto type = gen.convertType(enumerator->type()); auto op = - gen.builder_.create(loc, type, *value); + mlir::cxx::IntConstantOp::create(gen.builder_, loc, type, *value); return {op}; } } @@ -358,7 +356,7 @@ auto Codegen::ExpressionVisitor::operator()(IdExpressionAST* ast) id && !ast->nestedNameSpecifier) { auto loc = gen.getLocation(ast->firstSourceLocation()); auto name = id->identifier->name(); - auto op = gen.builder_.create(loc, name); + auto op = mlir::cxx::IdOp::create(gen.builder_, loc, name); return {op}; } @@ -483,15 +481,16 @@ auto Codegen::ExpressionVisitor::operator()(SubscriptExpressionAST* ast) auto resultType = gen.convertType(control()->add_pointer(ast->type)); if (control()->is_pointer(ast->baseExpression->type)) { - auto op = gen.builder_.create( - loc, resultType, baseExpressionResult.value, - indexExpressionResult.value); + auto op = mlir::cxx::PtrAddOp::create(gen.builder_, loc, resultType, + baseExpressionResult.value, + indexExpressionResult.value); return {op}; } - auto op = gen.builder_.create( - loc, resultType, baseExpressionResult.value, indexExpressionResult.value); + auto op = mlir::cxx::SubscriptOp::create(gen.builder_, loc, resultType, + baseExpressionResult.value, + indexExpressionResult.value); return {op}; } @@ -526,8 +525,9 @@ auto Codegen::ExpressionVisitor::operator()(CallExpressionAST* ast) resultTypes.push_back(gen.convertType(functionType->returnType())); } - auto op = gen.builder_.create( - loc, resultTypes, funcOp.getSymName(), arguments, mlir::TypeAttr{}); + auto op = mlir::cxx::CallOp::create(gen.builder_, loc, resultTypes, + funcOp.getSymName(), arguments, + mlir::TypeAttr{}); if (functionType->isVariadic()) { op.setVarCalleeType( @@ -560,8 +560,9 @@ auto Codegen::ExpressionVisitor::operator()(CallExpressionAST* ast) resultTypes.push_back(gen.convertType(functionType->returnType())); } - auto op = gen.builder_.create( - loc, resultTypes, funcOp.getSymName(), arguments, mlir::TypeAttr{}); + auto op = mlir::cxx::CallOp::create(gen.builder_, loc, resultTypes, + funcOp.getSymName(), arguments, + mlir::TypeAttr{}); if (functionType->isVariadic()) { op.setVarCalleeType( @@ -590,7 +591,7 @@ auto Codegen::ExpressionVisitor::operator()(CallExpressionAST* ast) auto loc = gen.getLocation(ast->lparenLoc); - auto op = gen.builder_.create( + auto op = mlir::cxx::CallOp::create(gen.builder_, loc, baseExpressionResult.value, arguments); #endif return {op}; @@ -660,8 +661,8 @@ auto Codegen::ExpressionVisitor::operator()(MemberExpressionAST* ast) auto resultType = gen.convertType(control()->add_pointer(ast->type)); - auto op = gen.builder_.create( - loc, resultType, baseExpressionResult.value, fieldIndex); + auto op = mlir::cxx::MemberOp::create( + gen.builder_, loc, resultType, baseExpressionResult.value, fieldIndex); return {op}; } @@ -813,8 +814,8 @@ auto Codegen::ExpressionVisitor::operator()(UnaryExpressionAST* ast) auto loc = gen.getLocation(ast->opLoc); auto expressionResult = gen.expression(ast->expression); auto resultType = gen.convertType(ast->type); - auto op = gen.builder_.create(loc, resultType, - expressionResult.value); + auto op = mlir::cxx::NotOp::create(gen.builder_, loc, resultType, + expressionResult.value); return {op}; } break; @@ -835,9 +836,9 @@ auto Codegen::ExpressionVisitor::operator()(UnaryExpressionAST* ast) if (control()->is_integral_or_unscoped_enum(ast->type)) { auto zero = - gen.builder_.create(loc, resultType, 0); - auto op = gen.builder_.create( - loc, resultType, zero, expressionResult.value); + mlir::cxx::IntConstantOp::create(gen.builder_, loc, resultType, 0); + auto op = mlir::cxx::SubIOp::create(gen.builder_, loc, resultType, zero, + expressionResult.value); return {op}; } @@ -863,10 +864,10 @@ auto Codegen::ExpressionVisitor::operator()(UnaryExpressionAST* ast) return {op}; } - auto zero = gen.builder_.create( - loc, resultType, value); - auto op = gen.builder_.create( - loc, resultType, zero, expressionResult.value); + auto zero = mlir::cxx::FloatConstantOp::create(gen.builder_, loc, + resultType, value); + auto op = mlir::cxx::SubFOp::create(gen.builder_, loc, resultType, zero, + expressionResult.value); return {op}; } @@ -880,8 +881,8 @@ auto Codegen::ExpressionVisitor::operator()(UnaryExpressionAST* ast) auto resultType = gen.convertType(ast->type); auto loc = gen.getLocation(ast->opLoc); - auto op = gen.builder_.create(loc, resultType, - expressionResult.value); + auto op = mlir::cxx::NotOp::create(gen.builder_, loc, resultType, + expressionResult.value); return {op}; } @@ -896,6 +897,103 @@ auto Codegen::ExpressionVisitor::operator()(UnaryExpressionAST* ast) return expressionResult; } + case TokenKind::T_MINUS_MINUS: + case TokenKind::T_PLUS_PLUS: { + auto expressionResult = gen.expression(ast->expression); + + if (control()->is_floating_point(ast->expression->type)) { + mlir::Value one; + + switch (control()->remove_cvref(ast->expression->type)->kind()) { + case TypeKind::kFloat: + one = mlir::cxx::FloatConstantOp::create( + gen.builder_, gen.getLocation(ast->opLoc), + gen.convertType(ast->expression->type), + gen.builder_.getF32FloatAttr(1.0)); + break; + + case TypeKind::kDouble: + one = mlir::cxx::FloatConstantOp::create( + gen.builder_, gen.getLocation(ast->opLoc), + gen.convertType(ast->expression->type), + gen.builder_.getF64FloatAttr(1.0)); + break; + + case TypeKind::kLongDouble: + one = mlir::cxx::FloatConstantOp::create( + gen.builder_, gen.getLocation(ast->opLoc), + gen.convertType(ast->expression->type), + gen.builder_.getF64FloatAttr(1.0)); + break; + + default: + // Handle other float types if necessary + auto op = gen.emitTodoExpr(ast->firstSourceLocation(), + "unsupported float type"); + return {op}; + } + + auto loc = gen.getLocation(ast->opLoc); + + auto resultType = gen.convertType(ast->type); + + auto loadOp = mlir::cxx::LoadOp::create(gen.builder_, loc, resultType, + expressionResult.value); + + mlir::Value addOp; + + if (ast->op == TokenKind::T_MINUS_MINUS) + addOp = mlir::cxx::SubFOp::create(gen.builder_, loc, resultType, + loadOp, one); + else + addOp = mlir::cxx::AddFOp::create(gen.builder_, loc, resultType, + loadOp, one); + + auto storeOp = mlir::cxx::StoreOp::create(gen.builder_, loc, addOp, + expressionResult.value); + + auto op = mlir::cxx::LoadOp::create(gen.builder_, loc, resultType, + expressionResult.value); + + return {op}; + } else if (control()->is_arithmetic(ast->expression->type)) { + auto loc = gen.getLocation(ast->opLoc); + + auto oneOp = mlir::cxx::IntConstantOp::create( + gen.builder_, loc, gen.convertType(control()->getIntType()), 1); + + auto castOneOp = mlir::cxx::IntegralCastOp::create( + gen.builder_, loc, gen.convertType(ast->expression->type), oneOp); + + auto resultType = gen.convertType(ast->type); + + auto loadOp = mlir::cxx::LoadOp::create(gen.builder_, loc, resultType, + expressionResult.value); + + mlir::Value addOp; + + if (ast->op == TokenKind::T_MINUS_MINUS) + addOp = mlir::cxx::SubIOp::create(gen.builder_, loc, resultType, + loadOp, castOneOp); + else + addOp = mlir::cxx::AddIOp::create(gen.builder_, loc, resultType, + loadOp, castOneOp); + + auto storeOp = mlir::cxx::StoreOp::create(gen.builder_, loc, addOp, + expressionResult.value); + + auto op = mlir::cxx::LoadOp::create(gen.builder_, loc, resultType, + expressionResult.value); + + return {op}; + } + + auto op = + gen.emitTodoExpr(ast->firstSourceLocation(), to_string(ast->kind())); + + return {op}; + } + default: break; } // switch @@ -1034,8 +1132,8 @@ auto Codegen::ExpressionVisitor::operator()(ImplicitCastExpressionAST* ast) auto expressionResult = gen.expression(ast->expression); auto resultType = gen.convertType(ast->type); - auto op = gen.builder_.create(loc, resultType, - expressionResult.value); + auto op = mlir::cxx::LoadOp::create(gen.builder_, loc, resultType, + expressionResult.value); return {op}; } @@ -1047,21 +1145,21 @@ auto Codegen::ExpressionVisitor::operator()(ImplicitCastExpressionAST* ast) if (is_bool(ast->type)) { // If the result type is a boolean, we can use a specialized cast - auto op = gen.builder_.create( - loc, resultType, expressionResult.value); + auto op = mlir::cxx::IntToBoolOp::create(gen.builder_, loc, resultType, + expressionResult.value); return {op}; } if (is_bool(ast->expression->type)) { // If the expression type is a boolean, we can use a specialized cast - auto op = gen.builder_.create( - loc, resultType, expressionResult.value); + auto op = mlir::cxx::BoolToIntOp::create(gen.builder_, loc, resultType, + expressionResult.value); return {op}; } // generate an integral cast - auto op = gen.builder_.create( - loc, resultType, expressionResult.value); + auto op = mlir::cxx::IntegralCastOp::create(gen.builder_, loc, resultType, + expressionResult.value); return {op}; } @@ -1072,8 +1170,8 @@ auto Codegen::ExpressionVisitor::operator()(ImplicitCastExpressionAST* ast) auto resultType = gen.convertType(ast->type); // generate a floating point cast - auto op = gen.builder_.create( - loc, resultType, expressionResult.value); + auto op = mlir::cxx::FloatingPointCastOp::create( + gen.builder_, loc, resultType, expressionResult.value); return {op}; } @@ -1083,16 +1181,18 @@ auto Codegen::ExpressionVisitor::operator()(ImplicitCastExpressionAST* ast) auto resultType = gen.convertType(ast->type); if (control()->is_floating_point(ast->type)) { - // If the result type is a floating point, we can use a specialized cast - auto op = gen.builder_.create( - loc, resultType, expressionResult.value); + // If the result type is a floating point, we can use a specialized + // cast + auto op = mlir::cxx::IntToFloatOp::create(gen.builder_, loc, resultType, + expressionResult.value); return {op}; } if (control()->is_integral(ast->type)) { - // If the expression type is an integral, we can use a specialized cast - auto op = gen.builder_.create( - loc, resultType, expressionResult.value); + // If the expression type is an integral, we can use a specialized + // cast + auto op = mlir::cxx::FloatToIntOp::create(gen.builder_, loc, resultType, + expressionResult.value); return {op}; } @@ -1104,8 +1204,8 @@ auto Codegen::ExpressionVisitor::operator()(ImplicitCastExpressionAST* ast) auto expressionResult = gen.expression(ast->expression); auto resultType = gen.convertType(ast->type); - auto op = gen.builder_.create( - loc, resultType, expressionResult.value); + auto op = mlir::cxx::ArrayToPointerOp::create( + gen.builder_, loc, resultType, expressionResult.value); return {op}; } @@ -1128,7 +1228,7 @@ auto Codegen::ExpressionVisitor::operator()(ImplicitCastExpressionAST* ast) auto loc = gen.getLocation(ast->firstSourceLocation()); - auto op = gen.builder_.create( + auto op = mlir::cxx::ImplicitCastOp::create(gen.builder_, loc, to_string(ast->castKind), expressionResult.value); #endif @@ -1162,21 +1262,21 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) auto i1type = gen.convertType(control()->getBoolType()); - auto trueValue = gen.builder_.create( - gen.getLocation(ast->opLoc), i1type, true); + auto trueValue = mlir::cxx::BoolConstantOp::create( + gen.builder_, gen.getLocation(ast->opLoc), i1type, true); - gen.builder_.create(gen.getLocation(ast->opLoc), - trueValue, t); + mlir::cxx::StoreOp::create(gen.builder_, gen.getLocation(ast->opLoc), + trueValue, t); auto endLoc = gen.getLocation(ast->lastSourceLocation()); gen.branch(endLoc, endBlock); // build the false block gen.builder_.setInsertionPointToEnd(falseBlock); - auto falseValue = gen.builder_.create( - gen.getLocation(ast->opLoc), i1type, false); - gen.builder_.create(gen.getLocation(ast->opLoc), - falseValue, t); + auto falseValue = mlir::cxx::BoolConstantOp::create( + gen.builder_, gen.getLocation(ast->opLoc), i1type, false); + mlir::cxx::StoreOp::create(gen.builder_, gen.getLocation(ast->opLoc), + falseValue, t); gen.branch(gen.getLocation(ast->lastSourceLocation()), endBlock); // place the end block @@ -1185,8 +1285,8 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) if (format == ExpressionFormat::kSideEffect) return {}; auto resultType = gen.convertType(ast->type); - auto loadOp = gen.builder_.create( - gen.getLocation(ast->opLoc), resultType, t); + auto loadOp = mlir::cxx::LoadOp::create( + gen.builder_, gen.getLocation(ast->opLoc), resultType, t); return {loadOp}; } @@ -1208,21 +1308,21 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) auto i1type = gen.convertType(control()->getBoolType()); - auto trueValue = gen.builder_.create( - gen.getLocation(ast->opLoc), i1type, true); + auto trueValue = mlir::cxx::BoolConstantOp::create( + gen.builder_, gen.getLocation(ast->opLoc), i1type, true); - gen.builder_.create(gen.getLocation(ast->opLoc), - trueValue, t); + mlir::cxx::StoreOp::create(gen.builder_, gen.getLocation(ast->opLoc), + trueValue, t); auto endLoc = gen.getLocation(ast->lastSourceLocation()); gen.branch(endLoc, endBlock); // build the false block gen.builder_.setInsertionPointToEnd(falseBlock); - auto falseValue = gen.builder_.create( - gen.getLocation(ast->opLoc), i1type, false); - gen.builder_.create(gen.getLocation(ast->opLoc), - falseValue, t); + auto falseValue = mlir::cxx::BoolConstantOp::create( + gen.builder_, gen.getLocation(ast->opLoc), i1type, false); + mlir::cxx::StoreOp::create(gen.builder_, gen.getLocation(ast->opLoc), + falseValue, t); gen.branch(gen.getLocation(ast->lastSourceLocation()), endBlock); // place the end block @@ -1231,8 +1331,8 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) if (format == ExpressionFormat::kSideEffect) return {}; auto resultType = gen.convertType(ast->type); - auto loadOp = gen.builder_.create( - gen.getLocation(ast->opLoc), resultType, t); + auto loadOp = mlir::cxx::LoadOp::create( + gen.builder_, gen.getLocation(ast->opLoc), resultType, t); return {loadOp}; } @@ -1244,16 +1344,16 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) switch (ast->op) { case TokenKind::T_PLUS: { if (control()->is_integral(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::AddIOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } if (control()->is_floating_point(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::AddFOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } @@ -1262,16 +1362,16 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) case TokenKind::T_MINUS: { if (control()->is_integral(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::SubIOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } if (control()->is_floating_point(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::SubFOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } @@ -1280,16 +1380,16 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) case TokenKind::T_STAR: { if (control()->is_integral(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::MulIOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } if (control()->is_floating_point(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::MulFOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } @@ -1298,16 +1398,16 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) case TokenKind::T_SLASH: { if (control()->is_integral(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::DivIOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } if (control()->is_floating_point(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::DivFOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } @@ -1316,9 +1416,9 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) case TokenKind::T_PERCENT: { if (control()->is_integral(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::ModIOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } @@ -1327,9 +1427,9 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) case TokenKind::T_LESS_LESS: { if (control()->is_integral(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::ShiftLeftOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } @@ -1338,9 +1438,9 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) case TokenKind::T_GREATER_GREATER: { if (control()->is_integral(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::ShiftRightOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } @@ -1349,16 +1449,16 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) case TokenKind::T_EQUAL_EQUAL: { if (control()->is_integral(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::EqualOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } if (control()->is_floating_point(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::EqualFOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } @@ -1367,16 +1467,16 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) case TokenKind::T_EXCLAIM_EQUAL: { if (control()->is_integral(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::NotEqualOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } if (control()->is_floating_point(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::NotEqualFOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } @@ -1385,16 +1485,16 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) case TokenKind::T_LESS: { if (control()->is_integral(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::LessThanOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } if (control()->is_floating_point(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::LessThanFOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } @@ -1403,16 +1503,16 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) case TokenKind::T_LESS_EQUAL: { if (control()->is_integral(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::LessEqualOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } if (control()->is_floating_point(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, - rightExpressionResult.value); + auto op = mlir::cxx::LessEqualFOp::create(gen.builder_, loc, resultType, + leftExpressionResult.value, + rightExpressionResult.value); return {op}; } @@ -1421,15 +1521,15 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) case TokenKind::T_GREATER: { if (control()->is_integral(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, + auto op = mlir::cxx::GreaterThanOp::create( + gen.builder_, loc, resultType, leftExpressionResult.value, rightExpressionResult.value); return {op}; } if (control()->is_floating_point(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, + auto op = mlir::cxx::GreaterThanFOp::create( + gen.builder_, loc, resultType, leftExpressionResult.value, rightExpressionResult.value); return {op}; } @@ -1439,15 +1539,15 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast) case TokenKind::T_GREATER_EQUAL: { if (control()->is_integral(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, + auto op = mlir::cxx::GreaterEqualOp::create( + gen.builder_, loc, resultType, leftExpressionResult.value, rightExpressionResult.value); return {op}; } if (control()->is_floating_point(ast->type)) { - auto op = gen.builder_.create( - loc, resultType, leftExpressionResult.value, + auto op = mlir::cxx::GreaterEqualFOp::create( + gen.builder_, loc, resultType, leftExpressionResult.value, rightExpressionResult.value); return {op}; } @@ -1480,15 +1580,17 @@ auto Codegen::ExpressionVisitor::operator()(ConditionalExpressionAST* ast) // place the true block gen.builder_.setInsertionPointToEnd(trueBlock); auto trueExpressionResult = gen.expression(ast->iftrueExpression); - auto trueValue = gen.builder_.create( - gen.getLocation(ast->questionLoc), trueExpressionResult.value, t); + auto trueValue = mlir::cxx::StoreOp::create(gen.builder_, + gen.getLocation(ast->questionLoc), + trueExpressionResult.value, t); gen.branch(endLoc, endBlock); // place the false block gen.builder_.setInsertionPointToEnd(falseBlock); auto falseExpressionResult = gen.expression(ast->iffalseExpression); - auto falseValue = gen.builder_.create( - gen.getLocation(ast->colonLoc), falseExpressionResult.value, t); + auto falseValue = + mlir::cxx::StoreOp::create(gen.builder_, gen.getLocation(ast->colonLoc), + falseExpressionResult.value, t); gen.branch(endLoc, endBlock); // place the end block @@ -1497,8 +1599,8 @@ auto Codegen::ExpressionVisitor::operator()(ConditionalExpressionAST* ast) if (format == ExpressionFormat::kSideEffect) return {}; auto resultType = gen.convertType(ast->type); - auto loadOp = gen.builder_.create( - gen.getLocation(ast->colonLoc), resultType, t); + auto loadOp = mlir::cxx::LoadOp::create( + gen.builder_, gen.getLocation(ast->colonLoc), resultType, t); return {loadOp}; } @@ -1535,8 +1637,8 @@ auto Codegen::ExpressionVisitor::operator()(AssignmentExpressionAST* ast) // Generate a store operation const auto loc = gen.getLocation(ast->opLoc); - gen.builder_.create(loc, rightExpressionResult.value, - leftExpressionResult.value); + mlir::cxx::StoreOp::create(gen.builder_, loc, rightExpressionResult.value, + leftExpressionResult.value); if (format == ExpressionFormat::kSideEffect) { return {}; @@ -1548,8 +1650,8 @@ auto Codegen::ExpressionVisitor::operator()(AssignmentExpressionAST* ast) auto resultType = gen.convertType(ast->leftExpression->type); // generate a load - auto op = gen.builder_.create( - resultLoc, resultType, leftExpressionResult.value); + auto op = mlir::cxx::LoadOp::create(gen.builder_, resultLoc, resultType, + leftExpressionResult.value); return {op}; } diff --git a/src/mlir/cxx/mlir/codegen_statements.cc b/src/mlir/cxx/mlir/codegen_statements.cc index 4ffe6738..89e79e2e 100644 --- a/src/mlir/cxx/mlir/codegen_statements.cc +++ b/src/mlir/cxx/mlir/codegen_statements.cc @@ -92,12 +92,15 @@ auto Codegen::handler(HandlerAST* ast) -> HandlerResult { } void Codegen::StatementVisitor::operator()(LabeledStatementAST* ast) { + auto loc = gen.getLocation(ast->firstSourceLocation()); + auto targetBlock = gen.newBlock(); - gen.branch(gen.getLocation(ast->firstSourceLocation()), targetBlock); + + gen.branch(loc, targetBlock); gen.builder_.setInsertionPointToEnd(targetBlock); - gen.builder_.create( - gen.getLocation(ast->firstSourceLocation()), ast->identifier->name()); + mlir::cxx::LabelOp::create(gen.builder_, loc, + mlir::StringRef{ast->identifier->name()}); } void Codegen::StatementVisitor::operator()(CaseStatementAST* ast) { @@ -263,8 +266,8 @@ void Codegen::StatementVisitor::operator()(ForStatementAST* ast) { void Codegen::StatementVisitor::operator()(BreakStatementAST* ast) { if (auto target = gen.loop_.breakBlock) { - gen.builder_.create( - gen.getLocation(ast->firstSourceLocation()), target); + auto loc = gen.getLocation(ast->firstSourceLocation()); + mlir::cf::BranchOp::create(gen.builder_, loc, target, {}); return; } @@ -273,8 +276,8 @@ void Codegen::StatementVisitor::operator()(BreakStatementAST* ast) { void Codegen::StatementVisitor::operator()(ContinueStatementAST* ast) { if (auto target = gen.loop_.continueBlock) { - gen.builder_.create( - gen.getLocation(ast->firstSourceLocation()), target); + mlir::cf::BranchOp::create( + gen.builder_, gen.getLocation(ast->firstSourceLocation()), target); return; } @@ -287,11 +290,11 @@ void Codegen::StatementVisitor::operator()(ReturnStatementAST* ast) { auto loc = gen.getLocation(ast->firstSourceLocation()); if (gen.exitValue_) { - gen.builder_.create(loc, value.value, - gen.exitValue_.getResult()); + mlir::cxx::StoreOp::create(gen.builder_, loc, value.value, + gen.exitValue_.getResult()); } - gen.builder_.create(loc, gen.exitBlock_); + mlir::cf::BranchOp::create(gen.builder_, loc, gen.exitBlock_); } void Codegen::StatementVisitor::operator()(CoroutineReturnStatementAST* ast) { @@ -309,9 +312,9 @@ void Codegen::StatementVisitor::operator()(GotoStatementAST* ast) { return; } - gen.builder_.create( - gen.getLocation(ast->firstSourceLocation()), mlir::ValueRange{}, - ast->identifier->name()); + mlir::cxx::GotoOp::create(gen.builder_, + gen.getLocation(ast->firstSourceLocation()), + mlir::ValueRange{}, ast->identifier->name()); auto nextBlock = gen.newBlock(); gen.branch(gen.getLocation(ast->firstSourceLocation()), nextBlock); diff --git a/src/mlir/cxx/mlir/codegen_units.cc b/src/mlir/cxx/mlir/codegen_units.cc index 0819d711..c3488312 100644 --- a/src/mlir/cxx/mlir/codegen_units.cc +++ b/src/mlir/cxx/mlir/codegen_units.cc @@ -110,7 +110,7 @@ auto Codegen::UnitVisitor::operator()(TranslationUnitAST* ast) -> UnitResult { auto loc = gen.builder_.getUnknownLoc(); auto name = gen.unit_->fileName(); - auto module = gen.builder_.create(loc, name); + auto module = mlir::ModuleOp::create(gen.builder_, loc, name); gen.builder_.setInsertionPointToStart(module.getBody()); auto memoryLayout = gen.control()->memoryLayout(); @@ -173,7 +173,7 @@ auto Codegen::UnitVisitor::operator()(TranslationUnitAST* ast) -> UnitResult { auto Codegen::UnitVisitor::operator()(ModuleUnitAST* ast) -> UnitResult { auto loc = gen.builder_.getUnknownLoc(); auto name = gen.unit_->fileName(); - auto module = gen.builder_.create(loc, name); + auto module = mlir::ModuleOp::create(gen.builder_, loc, name); gen.builder_.setInsertionPointToStart(module.getBody()); std::swap(gen.module_, module); diff --git a/src/mlir/cxx/mlir/cxx_dialect_conversions.cc b/src/mlir/cxx/mlir/cxx_dialect_conversions.cc index f260b4ed..23cc30a5 100644 --- a/src/mlir/cxx/mlir/cxx_dialect_conversions.cc +++ b/src/mlir/cxx/mlir/cxx_dialect_conversions.cc @@ -60,8 +60,8 @@ class FuncOpLowering : public OpConversionPattern { auto funcType = op.getFunctionType(); auto llvmFuncType = typeConverter->convertType(funcType); - auto func = rewriter.create(op.getLoc(), op.getSymName(), - llvmFuncType); + auto func = LLVM::LLVMFuncOp::create(rewriter, op.getLoc(), op.getSymName(), + llvmFuncType); if (op.getBody().empty()) { func.setLinkage(LLVM::linkage::Linkage::External); @@ -154,8 +154,9 @@ class CallOpLowering : public OpConversionPattern { "failed to convert call result types"); } - auto llvmCallOp = rewriter.create( - op.getLoc(), resultTypes, adaptor.getCallee(), adaptor.getInputs()); + auto llvmCallOp = + LLVM::CallOp::create(rewriter, op.getLoc(), resultTypes, + adaptor.getCallee(), adaptor.getInputs()); if (op.getVarCalleeType().has_value()) { auto varCalleeType = @@ -218,8 +219,9 @@ class AllocaOpLowering : public OpConversionPattern { op, "failed to convert element type of alloca"); } - auto size = rewriter.create( - op.getLoc(), typeConverter->convertType(rewriter.getIndexType()), + auto size = LLVM::ConstantOp::create( + rewriter, op.getLoc(), + typeConverter->convertType(rewriter.getIndexType()), rewriter.getIntegerAttr(rewriter.getIndexType(), 1)); auto x = rewriter.replaceOpWithNewOp(op, resultType, @@ -543,8 +545,8 @@ class IntToBoolOpLowering : public OpConversionPattern { "failed to convert int to bool type"); } - auto c0 = rewriter.create( - op.getLoc(), adaptor.getValue().getType(), 0); + auto c0 = LLVM::ConstantOp::create(rewriter, op.getLoc(), + adaptor.getValue().getType(), 0); rewriter.replaceOpWithNewOp( op, resultType, LLVM::ICmpPredicate::ne, adaptor.getValue(), c0); @@ -630,8 +632,8 @@ class NotOpLowering : public OpConversionPattern { return rewriter.notifyMatchFailure(op, "failed to convert not operation"); } - auto c1 = rewriter.create( - op.getLoc(), adaptor.getValue().getType(), -1); + auto c1 = LLVM::ConstantOp::create(rewriter, op.getLoc(), + adaptor.getValue().getType(), -1); rewriter.replaceOpWithNewOp(op, resultType, adaptor.getValue(), c1);