From ef7bdd252dbaa1f4d95d90fb6d6f5826944078d3 Mon Sep 17 00:00:00 2001 From: cgyurgyik Date: Tue, 28 Sep 2021 18:54:31 -0700 Subject: [PATCH] [Calyx] [NFC] Use stricter Block existence, not Region existence for IfOp. --- include/circt/Dialect/Calyx/CalyxControl.td | 20 +++++++++++++++----- lib/Dialect/Calyx/CalyxOps.cpp | 4 ++-- lib/Dialect/Calyx/Export/CalyxEmitter.cpp | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/include/circt/Dialect/Calyx/CalyxControl.td b/include/circt/Dialect/Calyx/CalyxControl.td index 8468c0e829..e28d6e371d 100644 --- a/include/circt/Dialect/Calyx/CalyxControl.td +++ b/include/circt/Dialect/Calyx/CalyxControl.td @@ -70,17 +70,27 @@ def IfOp : CalyxContainer<"if", [ let verifier = "return ::verify$cppClass(*this);"; let hasCanonicalizeMethod = true; let extraClassDeclaration = [{ - /// 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(); } + /// Checks whether the `then` body exists. + bool thenBodyExists() { + Region* region = &getOperation()->getRegion(0); + 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. Block *getThenBody() { return &getOperation()->getRegion(0).front(); } /// Gets the single basic block representing the `else` region. Block *getElseBody() { - assert(elseRegionExists() && "Else region does not exist."); + assert(elseBodyExists() && "Else region does not exist."); return &getOperation()->getRegion(1).front(); } }]; diff --git a/lib/Dialect/Calyx/CalyxOps.cpp b/lib/Dialect/Calyx/CalyxOps.cpp index 53fab6aea1..cb514ff623 100644 --- a/lib/Dialect/Calyx/CalyxOps.cpp +++ b/lib/Dialect/Calyx/CalyxOps.cpp @@ -1377,7 +1377,7 @@ static LogicalResult verifyIfOp(IfOp ifOp) { if (ifOp.getThenBody()->empty()) return ifOp.emitError() << "empty 'then' region."; - if (ifOp.elseRegionExists() && ifOp.getElseBody()->empty()) + if (ifOp.elseBodyExists() && ifOp.getElseBody()->empty()) return ifOp.emitError() << "empty 'else' region."; Optional optGroupName = ifOp.groupName(); @@ -1428,7 +1428,7 @@ static EnableOp getLastEnableOp(SeqOp parent) { static LogicalResult eliminateCommonTailEnable(IfOp ifOp, PatternRewriter &rewriter) { // Check if the branches exist. - if (!ifOp.thenRegionExists() || !ifOp.elseRegionExists()) + if (!ifOp.thenBodyExists() || !ifOp.elseBodyExists()) return failure(); auto &thenOpStructureOp = ifOp.getThenBody()->front(); diff --git a/lib/Dialect/Calyx/Export/CalyxEmitter.cpp b/lib/Dialect/Calyx/Export/CalyxEmitter.cpp index 97e8cb800b..0a32efde9e 100644 --- a/lib/Dialect/Calyx/Export/CalyxEmitter.cpp +++ b/lib/Dialect/Calyx/Export/CalyxEmitter.cpp @@ -406,7 +406,7 @@ private: os << " with " << groupName.getValue(); emitCalyxBody([&]() { emitCalyxControl(op.getThenBody()); }); - if (op.elseRegionExists()) + if (op.elseBodyExists()) emitCalyxSection("else", [&]() { emitCalyxControl(op.getElseBody()); }); })