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
8 changes: 8 additions & 0 deletions src/mlir/cxx/mlir/CxxOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ def Cxx_MulIOp : Cxx_Op<"muli"> {
let results = (outs Cxx_IntegerType:$result);
}

def Cxx_LabelOp : Cxx_Op<"label"> {
let arguments = (ins StringProp:$name);
}

def Cxx_GotoOp : Cxx_Op<"goto"> {
let arguments = (ins StringProp:$label);
}

def CondBranchOp : Cxx_Op<"cond_br", [ AttrSizedOperandSegments, Terminator ]> {
let arguments = (ins Cxx_BoolType:$condition, Variadic<AnyType>:$trueDestOperands, Variadic<AnyType>:$falseDestOperands);

Expand Down
4 changes: 3 additions & 1 deletion src/mlir/cxx/mlir/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ auto Codegen::control() const -> Control* { return unit_->control(); }

auto Codegen::currentBlockMightHaveTerminator() -> bool {
auto block = builder_.getInsertionBlock();
if (!block) return true;
if (!block) {
cxx_runtime_error("current block is null");
}
return block->mightHaveTerminator();
}

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 @@ -22,9 +22,12 @@

#include <cxx/ast_fwd.h>
#include <cxx/mlir/cxx_dialect.h>
#include <cxx/names_fwd.h>
#include <cxx/source_location.h>
#include <cxx/symbols_fwd.h>
#include <cxx/types_fwd.h>

// mlir
#include <mlir/Dialect/Func/IR/FuncOps.h>
#include <mlir/IR/Builders.h>
#include <mlir/IR/BuiltinOps.h>
Expand Down
2 changes: 1 addition & 1 deletion src/mlir/cxx/mlir/codegen_declarations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
exitValue = gen.builder_.create<mlir::cxx::AllocaOp>(exitValueLoc, ptrType);
}

// restore state
// function state
std::swap(gen.function_, func);
std::swap(gen.exitBlock_, exitBlock);
std::swap(gen.exitValue_, exitValue);
Expand Down
22 changes: 20 additions & 2 deletions src/mlir/cxx/mlir/codegen_statements.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// cxx
#include <cxx/ast.h>
#include <cxx/control.h>
#include <cxx/names.h>

// mlir
#include <mlir/Dialect/ControlFlow/IR/ControlFlowOps.h>
Expand Down Expand Up @@ -91,7 +92,12 @@ auto Codegen::handler(HandlerAST* ast) -> HandlerResult {
}

void Codegen::StatementVisitor::operator()(LabeledStatementAST* ast) {
(void)gen.emitTodoStmt(ast->firstSourceLocation(), to_string(ast->kind()));
auto targetBlock = gen.newBlock();
gen.branch(gen.getLocation(ast->firstSourceLocation()), targetBlock);
gen.builder_.setInsertionPointToEnd(targetBlock);

gen.builder_.create<mlir::cxx::LabelOp>(
gen.getLocation(ast->firstSourceLocation()), ast->identifier->name());
}

void Codegen::StatementVisitor::operator()(CaseStatementAST* ast) {
Expand Down Expand Up @@ -291,7 +297,19 @@ void Codegen::StatementVisitor::operator()(CoroutineReturnStatementAST* ast) {
}

void Codegen::StatementVisitor::operator()(GotoStatementAST* ast) {
(void)gen.emitTodoStmt(ast->firstSourceLocation(), to_string(ast->kind()));
if (ast->isIndirect) {
(void)gen.emitTodoStmt(ast->firstSourceLocation(), to_string(ast->kind()));
return;
}

gen.builder_.create<mlir::cxx::GotoOp>(
gen.getLocation(ast->firstSourceLocation()), mlir::ValueRange{},
ast->identifier->name());

auto nextBlock = gen.newBlock();
gen.branch(gen.getLocation(ast->firstSourceLocation()), nextBlock);

gen.builder_.setInsertionPointToEnd(nextBlock);
}

void Codegen::StatementVisitor::operator()(DeclarationStatementAST* ast) {
Expand Down
58 changes: 58 additions & 0 deletions src/mlir/cxx/mlir/cxx_dialect_conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,53 @@ class CondBranchOpLowering : public OpConversionPattern<cxx::CondBranchOp> {
}
};

struct LabelConverter {
DenseMap<StringRef, Block *> labels;
};

class GotoOpLowering : public OpConversionPattern<cxx::GotoOp> {
public:
GotoOpLowering(const TypeConverter &typeConverter,
const LabelConverter &labelConverter, MLIRContext *context,
PatternBenefit benefit = 1)
: OpConversionPattern(typeConverter, context, benefit),
labelConverter_(labelConverter) {}

auto matchAndRewrite(cxx::GotoOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const
-> LogicalResult override {
auto context = getContext();

auto targetBlock = labelConverter_.labels.lookup(op.getLabel());

if (auto nextOp = ++op->getIterator(); isa<cf::BranchOp>(&*nextOp)) {
rewriter.eraseOp(&*nextOp);
}

rewriter.replaceOpWithNewOp<cf::BranchOp>(op, targetBlock);

return success();
}

private:
const LabelConverter &labelConverter_;
};

class LabelOpLowering : public OpConversionPattern<cxx::LabelOp> {
public:
using OpConversionPattern::OpConversionPattern;

auto matchAndRewrite(cxx::LabelOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const
-> LogicalResult override {
auto context = getContext();

rewriter.eraseOp(op);

return success();
}
};

class CxxToLLVMLoweringPass
: public PassWrapper<CxxToLLVMLoweringPass, OperationPass<ModuleOp>> {
public:
Expand Down Expand Up @@ -553,6 +600,17 @@ void CxxToLLVMLoweringPass::runOnOperation() {
SubIOpLowering, MulIOpLowering, CondBranchOpLowering>(
typeConverter, context);

LabelConverter labelConverter;

module.walk([&](Operation *op) {
if (auto labelOp = dyn_cast<cxx::LabelOp>(op)) {
labelConverter.labels[labelOp.getName()] = labelOp->getBlock();
}
});

patterns.insert<LabelOpLowering>(typeConverter, context);
patterns.insert<GotoOpLowering>(typeConverter, labelConverter, context);

populateFunctionOpInterfaceTypeConversionPattern<cxx::FuncOp>(patterns,
typeConverter);

Expand Down
Loading