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
6 changes: 6 additions & 0 deletions src/mlir/cxx/mlir/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ auto Codegen::findOrCreateLocal(Symbol* symbol) -> std::optional<mlir::Value> {
return allocaOp;
}

auto Codegen::newTemp(const Type* type, SourceLocation loc)
-> mlir::cxx::AllocaOp {
auto ptrType = builder_.getType<mlir::cxx::PointerType>(convertType(type));
return builder_.create<mlir::cxx::AllocaOp>(getLocation(loc), ptrType);
}

auto Codegen::findOrCreateFunction(FunctionSymbol* functionSymbol)
-> mlir::cxx::FuncOp {
if (auto it = funcOps_.find(functionSymbol); it != funcOps_.end()) {
Expand Down
3 changes: 3 additions & 0 deletions src/mlir/cxx/mlir/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ class Codegen {
[[nodiscard]] auto findOrCreateFunction(FunctionSymbol* functionSymbol)
-> mlir::cxx::FuncOp;

[[nodiscard]] auto newTemp(const Type* type, SourceLocation loc)
-> mlir::cxx::AllocaOp;

[[nodiscard]] auto findOrCreateLocal(Symbol* symbol)
-> std::optional<mlir::Value>;

Expand Down
92 changes: 92 additions & 0 deletions src/mlir/cxx/mlir/codegen_expressions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,98 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast)
return gen.expression(ast->rightExpression, format);
}

if (ast->op == TokenKind::T_BAR_BAR) {
auto t = gen.newTemp(control()->getBoolType(), ast->opLoc);

auto trueBlock = gen.newBlock();
auto continueBlock = gen.newBlock();
auto falseBlock = gen.newBlock();
auto endBlock = gen.newBlock();

gen.condition(ast->leftExpression, trueBlock, continueBlock);

gen.builder_.setInsertionPointToEnd(continueBlock);
gen.condition(ast->rightExpression, trueBlock, falseBlock);

// build the true block
gen.builder_.setInsertionPointToEnd(trueBlock);

auto i1type = gen.convertType(control()->getBoolType());

auto trueValue = gen.builder_.create<mlir::cxx::BoolConstantOp>(
gen.getLocation(ast->opLoc), i1type, true);

gen.builder_.create<mlir::cxx::StoreOp>(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<mlir::cxx::BoolConstantOp>(
gen.getLocation(ast->opLoc), i1type, false);
gen.builder_.create<mlir::cxx::StoreOp>(gen.getLocation(ast->opLoc),
falseValue, t);
gen.branch(gen.getLocation(ast->lastSourceLocation()), endBlock);

// place the end block
gen.builder_.setInsertionPointToEnd(endBlock);

if (format == ExpressionFormat::kSideEffect) return {};

auto resultType = gen.convertType(ast->type);
auto loadOp = gen.builder_.create<mlir::cxx::LoadOp>(
gen.getLocation(ast->opLoc), resultType, t);
return {loadOp};
}

if (ast->op == TokenKind::T_AMP_AMP) {
auto t = gen.newTemp(control()->getBoolType(), ast->opLoc);

auto trueBlock = gen.newBlock();
auto continueBlock = gen.newBlock();
auto falseBlock = gen.newBlock();
auto endBlock = gen.newBlock();

gen.condition(ast->leftExpression, continueBlock, falseBlock);

gen.builder_.setInsertionPointToEnd(continueBlock);
gen.condition(ast->rightExpression, trueBlock, falseBlock);

// build the true block
gen.builder_.setInsertionPointToEnd(trueBlock);

auto i1type = gen.convertType(control()->getBoolType());

auto trueValue = gen.builder_.create<mlir::cxx::BoolConstantOp>(
gen.getLocation(ast->opLoc), i1type, true);

gen.builder_.create<mlir::cxx::StoreOp>(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<mlir::cxx::BoolConstantOp>(
gen.getLocation(ast->opLoc), i1type, false);
gen.builder_.create<mlir::cxx::StoreOp>(gen.getLocation(ast->opLoc),
falseValue, t);
gen.branch(gen.getLocation(ast->lastSourceLocation()), endBlock);

// place the end block
gen.builder_.setInsertionPointToEnd(endBlock);

if (format == ExpressionFormat::kSideEffect) return {};

auto resultType = gen.convertType(ast->type);
auto loadOp = gen.builder_.create<mlir::cxx::LoadOp>(
gen.getLocation(ast->opLoc), resultType, t);
return {loadOp};
}

auto loc = gen.getLocation(ast->opLoc);
auto leftExpressionResult = gen.expression(ast->leftExpression);
auto rightExpressionResult = gen.expression(ast->rightExpression);
Expand Down
Loading