Add IfOp helpers. (#1870)

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.
This commit is contained in:
Chris Gyurgyik 2021-09-23 09:38:58 -07:00 committed by GitHub
parent 3fd09246c3
commit 815029c851
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 12 deletions

View File

@ -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();
}

View File

@ -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();
}
@ -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();
}
@ -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();

View File

@ -405,10 +405,10 @@ private:
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) {