[Calyx] [NFC] Use stricter Block existence, not Region existence for IfOp.

This commit is contained in:
cgyurgyik 2021-09-28 18:54:31 -07:00
parent 360c4e7a0f
commit ef7bdd252d
3 changed files with 18 additions and 8 deletions

View File

@ -70,17 +70,27 @@ def IfOp : CalyxContainer<"if", [
let verifier = "return ::verify$cppClass(*this);"; let verifier = "return ::verify$cppClass(*this);";
let hasCanonicalizeMethod = true; let hasCanonicalizeMethod = true;
let extraClassDeclaration = [{ let extraClassDeclaration = [{
/// Checks whether the `then` region exists. /// Checks whether the `then` body exists.
bool thenRegionExists() { return !getOperation()->getRegion(0).empty(); } bool thenBodyExists() {
/// Checks whether the `else` region exists. Region* region = &getOperation()->getRegion(0);
bool elseRegionExists() { return !getOperation()->getRegion(1).empty(); } if (region == nullptr)
return false;
return !region->empty();
}
/// Checks whether the `else` body exists.
bool elseBodyExists() {
Region* region = &getOperation()->getRegion(1);
if (region == nullptr)
return false;
return !region->empty();
}
/// Gets the single basic block representing the `then` region. /// Gets the single basic block representing the `then` region.
Block *getThenBody() { Block *getThenBody() {
return &getOperation()->getRegion(0).front(); return &getOperation()->getRegion(0).front();
} }
/// Gets the single basic block representing the `else` region. /// Gets the single basic block representing the `else` region.
Block *getElseBody() { Block *getElseBody() {
assert(elseRegionExists() && "Else region does not exist."); assert(elseBodyExists() && "Else region does not exist.");
return &getOperation()->getRegion(1).front(); return &getOperation()->getRegion(1).front();
} }
}]; }];

View File

@ -1377,7 +1377,7 @@ static LogicalResult verifyIfOp(IfOp ifOp) {
if (ifOp.getThenBody()->empty()) if (ifOp.getThenBody()->empty())
return ifOp.emitError() << "empty 'then' region."; return ifOp.emitError() << "empty 'then' region.";
if (ifOp.elseRegionExists() && ifOp.getElseBody()->empty()) if (ifOp.elseBodyExists() && ifOp.getElseBody()->empty())
return ifOp.emitError() << "empty 'else' region."; return ifOp.emitError() << "empty 'else' region.";
Optional<StringRef> optGroupName = ifOp.groupName(); Optional<StringRef> optGroupName = ifOp.groupName();
@ -1428,7 +1428,7 @@ static EnableOp getLastEnableOp(SeqOp parent) {
static LogicalResult eliminateCommonTailEnable(IfOp ifOp, static LogicalResult eliminateCommonTailEnable(IfOp ifOp,
PatternRewriter &rewriter) { PatternRewriter &rewriter) {
// Check if the branches exist. // Check if the branches exist.
if (!ifOp.thenRegionExists() || !ifOp.elseRegionExists()) if (!ifOp.thenBodyExists() || !ifOp.elseBodyExists())
return failure(); return failure();
auto &thenOpStructureOp = ifOp.getThenBody()->front(); auto &thenOpStructureOp = ifOp.getThenBody()->front();

View File

@ -406,7 +406,7 @@ private:
os << " with " << groupName.getValue(); os << " with " << groupName.getValue();
emitCalyxBody([&]() { emitCalyxControl(op.getThenBody()); }); emitCalyxBody([&]() { emitCalyxControl(op.getThenBody()); });
if (op.elseRegionExists()) if (op.elseBodyExists())
emitCalyxSection("else", emitCalyxSection("else",
[&]() { emitCalyxControl(op.getElseBody()); }); [&]() { emitCalyxControl(op.getElseBody()); });
}) })