Skip to content

Commit 78734ef

Browse files
committed
Remove deprecate calls to MLIR OpBuilder::create<Op>
1 parent f31f4cc commit 78734ef

File tree

6 files changed

+325
-196
lines changed

6 files changed

+325
-196
lines changed

src/mlir/cxx/mlir/codegen.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ auto Codegen::newUniqueSymbolName(std::string_view prefix) -> std::string {
6868
void Codegen::branch(mlir::Location loc, mlir::Block* block,
6969
mlir::ValueRange operands) {
7070
if (currentBlockMightHaveTerminator()) return;
71-
builder_.create<mlir::cf::BranchOp>(loc, block, operands);
71+
mlir::cf::BranchOp::create(builder_, loc, block, operands);
7272
}
7373

7474
auto Codegen::findOrCreateLocal(Symbol* symbol) -> std::optional<mlir::Value> {
@@ -83,7 +83,7 @@ auto Codegen::findOrCreateLocal(Symbol* symbol) -> std::optional<mlir::Value> {
8383
auto ptrType = builder_.getType<mlir::cxx::PointerType>(type);
8484

8585
auto loc = getLocation(var->location());
86-
auto allocaOp = builder_.create<mlir::cxx::AllocaOp>(loc, ptrType);
86+
auto allocaOp = mlir::cxx::AllocaOp::create(builder_, loc, ptrType);
8787

8888
locals_.emplace(var, allocaOp);
8989

@@ -93,7 +93,7 @@ auto Codegen::findOrCreateLocal(Symbol* symbol) -> std::optional<mlir::Value> {
9393
auto Codegen::newTemp(const Type* type, SourceLocation loc)
9494
-> mlir::cxx::AllocaOp {
9595
auto ptrType = builder_.getType<mlir::cxx::PointerType>(convertType(type));
96-
return builder_.create<mlir::cxx::AllocaOp>(getLocation(loc), ptrType);
96+
return mlir::cxx::AllocaOp::create(builder_, getLocation(loc), ptrType);
9797
}
9898

9999
auto Codegen::findOrCreateFunction(FunctionSymbol* functionSymbol)
@@ -145,8 +145,8 @@ auto Codegen::findOrCreateFunction(FunctionSymbol* functionSymbol)
145145

146146
builder_.setInsertionPointToStart(module_.getBody());
147147

148-
auto func = builder_.create<mlir::cxx::FuncOp>(
149-
loc, name, funcType, mlir::ArrayAttr{}, mlir::ArrayAttr{});
148+
auto func = mlir::cxx::FuncOp::create(builder_, loc, name, funcType,
149+
mlir::ArrayAttr{}, mlir::ArrayAttr{});
150150

151151
funcOps_.insert_or_assign(functionSymbol, func);
152152

@@ -165,14 +165,14 @@ auto Codegen::getLocation(SourceLocation location) -> mlir::Location {
165165
auto Codegen::emitTodoStmt(SourceLocation location, std::string_view message)
166166
-> mlir::cxx::TodoStmtOp {
167167
const auto loc = getLocation(location);
168-
auto op = builder_.create<mlir::cxx::TodoStmtOp>(loc, message);
168+
auto op = mlir::cxx::TodoStmtOp::create(builder_, loc, message);
169169
return op;
170170
}
171171

172172
auto Codegen::emitTodoExpr(SourceLocation location, std::string_view message)
173173
-> mlir::cxx::TodoExprOp {
174174
const auto loc = getLocation(location);
175-
auto op = builder_.create<mlir::cxx::TodoExprOp>(loc, message);
175+
auto op = mlir::cxx::TodoExprOp::create(builder_, loc, message);
176176
return op;
177177
}
178178

src/mlir/cxx/mlir/codegen_declarations.cc

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <cxx/ast.h>
2525
#include <cxx/control.h>
2626
#include <cxx/external_name_encoder.h>
27+
#include <cxx/names.h>
2728
#include <cxx/symbols.h>
2829
#include <cxx/translation_unit.h>
2930
#include <cxx/types.h>
@@ -37,6 +38,17 @@
3738

3839
namespace cxx {
3940

41+
namespace {
42+
43+
[[nodiscard]] auto is_global_namespace(Symbol* symbol) -> bool {
44+
if (!symbol) return false;
45+
if (!symbol->isNamespace()) return false;
46+
if (symbol->parent()) return false;
47+
return true;
48+
}
49+
50+
} // namespace
51+
4052
struct Codegen::DeclarationVisitor {
4153
Codegen& gen;
4254

@@ -205,8 +217,8 @@ auto Codegen::DeclarationVisitor::operator()(SimpleDeclarationAST* ast)
205217

206218
const auto elementType = gen.convertType(var->type());
207219

208-
gen.builder_.create<mlir::cxx::StoreOp>(loc, expressionResult.value,
209-
local.value());
220+
mlir::cxx::StoreOp::create(gen.builder_, loc, expressionResult.value,
221+
local.value());
210222
}
211223

212224
return {};
@@ -365,7 +377,17 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
365377
gen.getLocation(ast->functionBody->firstSourceLocation());
366378
auto exitValueType = gen.convertType(returnType);
367379
auto ptrType = gen.builder_.getType<mlir::cxx::PointerType>(exitValueType);
368-
exitValue = gen.builder_.create<mlir::cxx::AllocaOp>(exitValueLoc, ptrType);
380+
exitValue =
381+
mlir::cxx::AllocaOp::create(gen.builder_, exitValueLoc, ptrType);
382+
383+
auto id = name_cast<Identifier>(functionSymbol->name());
384+
if (id && id->name() == "main" &&
385+
is_global_namespace(functionSymbol->parent())) {
386+
auto zeroOp = mlir::cxx::IntConstantOp::create(
387+
gen.builder_, loc, gen.convertType(gen.control()->getIntType()), 0);
388+
389+
mlir::cxx::StoreOp::create(gen.builder_, exitValueLoc, zeroOp, exitValue);
390+
}
369391
}
370392

371393
std::unordered_map<Symbol*, mlir::Value> locals;
@@ -389,8 +411,8 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
389411
thisValue = gen.newTemp(classSymbol->type(), ast->firstSourceLocation());
390412

391413
// store the `this` pointer in the entry block
392-
gen.builder_.create<mlir::cxx::StoreOp>(
393-
loc, gen.entryBlock_->getArgument(0), thisValue);
414+
mlir::cxx::StoreOp::create(gen.builder_, loc,
415+
gen.entryBlock_->getArgument(0), thisValue);
394416
}
395417

396418
FunctionParametersSymbol* params = nullptr;
@@ -408,11 +430,11 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
408430
auto ptrType = gen.builder_.getType<mlir::cxx::PointerType>(type);
409431

410432
auto loc = gen.getLocation(arg->location());
411-
auto allocaOp = gen.builder_.create<mlir::cxx::AllocaOp>(loc, ptrType);
433+
auto allocaOp = mlir::cxx::AllocaOp::create(gen.builder_, loc, ptrType);
412434

413435
auto value = args[argc];
414436
++argc;
415-
gen.builder_.create<mlir::cxx::StoreOp>(loc, value, allocaOp);
437+
mlir::cxx::StoreOp::create(gen.builder_, loc, value, allocaOp);
416438

417439
gen.locals_.emplace(arg, allocaOp);
418440
}
@@ -430,7 +452,7 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
430452
const auto endLoc = gen.getLocation(ast->lastSourceLocation());
431453

432454
if (!gen.builder_.getBlock()->mightHaveTerminator()) {
433-
gen.builder_.create<mlir::cf::BranchOp>(endLoc, gen.exitBlock_);
455+
mlir::cf::BranchOp::create(gen.builder_, endLoc, gen.exitBlock_);
434456
}
435457

436458
gen.builder_.setInsertionPointToEnd(gen.exitBlock_);
@@ -439,13 +461,13 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
439461
// We need to return a value of the correct type.
440462
auto elementType = gen.exitValue_.getType().getElementType();
441463

442-
auto value = gen.builder_.create<mlir::cxx::LoadOp>(endLoc, elementType,
443-
gen.exitValue_);
464+
auto value = mlir::cxx::LoadOp::create(gen.builder_, endLoc, elementType,
465+
gen.exitValue_);
444466

445-
gen.builder_.create<mlir::cxx::ReturnOp>(endLoc, value->getResults());
467+
mlir::cxx::ReturnOp::create(gen.builder_, endLoc, value->getResults());
446468
} else {
447469
// If the function returns void, we don't need to return anything.
448-
gen.builder_.create<mlir::cxx::ReturnOp>(endLoc);
470+
mlir::cxx::ReturnOp::create(gen.builder_, endLoc);
449471
}
450472

451473
// restore the state

0 commit comments

Comments
 (0)