Skip to content

Commit

Permalink
Add IfOp helpers. (#1870)
Browse files Browse the repository at this point in the history
Add helper methods for the IfOp. The IfOp inherits from CalyxContainer, which has the SingleBlock trait, 
so it should be guaranteed that each region has a single block. I've renamed the getters to remain in line 
with the getBody helper for other Calyx containers.
  • Loading branch information
cgyurgyik authored Sep 23, 2021
1 parent 3fd0924 commit 815029c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
11 changes: 8 additions & 3 deletions include/circt/Dialect/Calyx/CalyxControl.td
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,16 @@ def IfOp : CalyxContainer<"if", [
let assemblyFormat = "$cond (`with` $groupName^)? $thenRegion (`else` $elseRegion^)? attr-dict";
let verifier = "return ::verify$cppClass(*this);";
let extraClassDeclaration = [{
Block *getThenRegion() {
/// Checks whether the `then` region exists.
bool thenRegionExists() { return !getOperation()->getRegion(0).empty(); }
/// Checks whether the `else` region exists.
bool elseRegionExists() { return !getOperation()->getRegion(1).empty(); }
/// Gets the single basic block representing the `then` region.
Block *getThenBody() {
return &getOperation()->getRegion(0).front();
}
bool elseRegionExists() { return !getOperation()->getRegion(1).empty(); }
Block *getElseRegion() {
/// Gets the single basic block representing the `else` region.
Block *getElseBody() {
assert(elseRegionExists() && "Else region does not exist.");
return &getOperation()->getRegion(1).front();
}
Expand Down
12 changes: 5 additions & 7 deletions lib/Conversion/SCFToCalyx/SCFToCalyx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ struct EliminateEmptyOpPattern : mlir::OpRewritePattern<TOp> {

LogicalResult matchAndRewrite(TOp op,
PatternRewriter &rewriter) const override {
if (op.getRegion().empty() || op.getRegion().front().empty()) {
if (op.getBody()->empty()) {
rewriter.eraseOp(op);
return success();
}
Expand All @@ -1293,7 +1293,7 @@ struct EliminateEmptyOpPattern<calyx::IfOp>

LogicalResult matchAndRewrite(calyx::IfOp op,
PatternRewriter &rewriter) const override {
if (op.thenRegion().empty() || op.thenRegion().front().empty()) {
if (!op.thenRegionExists()) {
rewriter.eraseOp(op);
return success();
}
Expand Down Expand Up @@ -1432,13 +1432,11 @@ struct CommonIfTailEnablePattern : mlir::OpRewritePattern<calyx::IfOp> {
/// Check if there's anything in the branches; if not,
/// EliminateEmptyOpPattern will eliminate a potentially
/// empty/invalid if statement.
if (ifOp.thenRegion().empty() || ifOp.thenRegion().front().empty())
return failure();
if (ifOp.elseRegion().empty() || ifOp.elseRegion().front().empty())
if (!ifOp.thenRegionExists() || !ifOp.elseRegionExists())
return failure();

auto &thenOpStructureOp = ifOp.thenRegion().front().front();
auto &elseOpStructureOp = ifOp.elseRegion().front().front();
auto &thenOpStructureOp = ifOp.getThenBody()->front();
auto &elseOpStructureOp = ifOp.getElseBody()->front();
if (isa<calyx::ParOp>(thenOpStructureOp) ||
isa<calyx::ParOp>(elseOpStructureOp))
return failure();
Expand Down
4 changes: 2 additions & 2 deletions lib/Dialect/Calyx/Export/CalyxEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,10 @@ struct Emitter {
if (auto groupName = op.groupName(); groupName.hasValue())
os << " with " << groupName.getValue();

emitCalyxBody([&]() { emitCalyxControl(op.getThenRegion()); });
emitCalyxBody([&]() { emitCalyxControl(op.getThenBody()); });
if (op.elseRegionExists())
emitCalyxSection("else",
[&]() { emitCalyxControl(op.getElseRegion()); });
[&]() { emitCalyxControl(op.getElseBody()); });
})
.Case<EnableOp>([&](auto op) { emitEnable(op); })
.Default([&](auto op) {
Expand Down

0 comments on commit 815029c

Please sign in to comment.