Skip to content

Commit

Permalink
WIP: code-gen of for-loop and support in ta-dialect #2
Browse files Browse the repository at this point in the history
  • Loading branch information
rizwanashraf committed Nov 29, 2022
1 parent a68e072 commit 5d96c82
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
48 changes: 48 additions & 0 deletions frontends/comet_dsl/mlir/MLIRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,40 @@ namespace
return t;
}

/// Codegen for-loop
mlir::LogicalResult mlirGen(ForLoopExprAST &forLoop)
{
comet_debug() << "codegen: ForLoopExprAST \n";

mlir::Value lo = builder.create<mlir::ConstantIndexOp>(
loc(forLoop.loc()), forLoop.getBegin());
mlir::Value hi = builder.create<mlir::ConstantIndexOp>(loc(forLoop.loc()),
forLoop.getEnd());
mlir::Value step = builder.create<mlir::ConstantIndexOp>(
loc(forLoop.loc()), forLoop.getIncrement());

mlir::Value value = builder.create<IndexLabelStaticOp>(loc(forLoop.loc()), lo, hi, step);

builder.create<ForLoopStartOp>(loc(forLoop.loc()), value, forLoop.getName());

if (failed(declare(forLoop.getName(), value)))
return mlir::failure();

comet_debug() << "codegen: ForLoopExprAST done \n";
return mlir::success();
}

/// Codegen for-loop
mlir::LogicalResult mlirGen(ForLoopEndExprAST &forLoopEnd)
{
comet_debug() << "codegen: ForLoopEndExprAST \n";

builder.create<ForLoopEndOp>(loc(forLoopEnd.loc()));

comet_debug() << "codegen: ForLoopEndExprAST done \n";
return mlir::success();
}

/// Codegen a list of expression, return failure if one of them hit an error.
mlir::LogicalResult mlirGen(ExprASTList &blockAST)
{
Expand Down Expand Up @@ -1727,6 +1761,20 @@ namespace
continue;
}

if (auto *forLoopStart = dyn_cast<ForLoopExprAST>(expr.get()))
{
if (mlir::failed(mlirGen(*forLoopStart)))
return mlir::success();
continue;
}

if (auto *forLoopEnd = dyn_cast<ForLoopEndExprAST>(expr.get()))
{
if (mlir::failed(mlirGen(*forLoopEnd)))
return mlir::success();
continue;
}

if (auto *tensor_op = dyn_cast<TensorOpExprAST>(expr.get()))
{
comet_debug() << " generate ops for TensorOpExprAST\n";
Expand Down
19 changes: 19 additions & 0 deletions include/comet/Dialect/TensorAlgebra/IR/TAOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,25 @@ def PrintElapsedTimeOp : TA_Op<"print_elapsed_time"> {
let arguments = (ins F64:$start, F64:$end);
}

def ForLoopStartOp : TA_Op<"start_for_loop">
{
let summary = "identifies the start of a for-loop body";
let description = [{
The "start_for_loop" identifies the start of a for-loop body
}];

// the inputs are 1) index_label_static (index start, index end, index step)
// and 2) string-attr with info about the iterator
let arguments = (ins Range:$label, StrAttr:$iterator);
}

def ForLoopEndOp : TA_Op<"end_for_loop"> {
let summary = "identifies the end of a for-loop body";
let description = [{
The "end_for_loop" identifies the end of a for-loop body
}];
}

def TAReturnOp : TA_Op<"return", [Terminator, HasParent<"FuncOp">]> {
let summary = "return operation";
let description = [{
Expand Down

0 comments on commit 5d96c82

Please sign in to comment.