Skip to content

Commit

Permalink
Revert "Transform SCF IndexSwitch to nested If-Else (#7670)"
Browse files Browse the repository at this point in the history
This reverts commit d379a35.

This patch failed valgrind nightly regressions.  I'm blamelessly reverting
this to keep nightly clean.
  • Loading branch information
seldridge committed Nov 26, 2024
1 parent 74598ea commit b987492
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 277 deletions.
1 change: 0 additions & 1 deletion include/circt/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ std::unique_ptr<mlir::Pass> createInsertMergeBlocksPass();
std::unique_ptr<mlir::Pass> createPrintOpCountPass();
std::unique_ptr<mlir::Pass>
createMemoryBankingPass(std::optional<unsigned> bankingFactor = std::nullopt);
std::unique_ptr<mlir::Pass> createIndexSwitchToIfPass();

//===----------------------------------------------------------------------===//
// Utility functions.
Expand Down
31 changes: 0 additions & 31 deletions include/circt/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -133,35 +133,4 @@ def MemoryBanking : Pass<"memory-banking", "::mlir::func::FuncOp"> {
let dependentDialects = ["mlir::memref::MemRefDialect, mlir::scf::SCFDialect, mlir::affine::AffineDialect"];
}

def IndexSwitchToIf : Pass<"switch-to-if", "::mlir::ModuleOp"> {
let summary = "Index switch to if";
let description = [{
Transform `scf.index_switch` to a series of `scf.if` operations.
This is necessary for dialects that don't support switch statements, e.g., Calyx.
An example:
```
%0 = scf.index_switch %cond -> i32
case 0 { ... }
case 1 { ... }
...

=>

%c0 = arith.cmpi eq %0, 0
%c1 = arith.cmpi eq %0, 1
%0 = scf.if %c0 {
...
} else {
%1 = scf.if %c1 {
...
} else {
...
}
}
```
}];
let constructor = "circt::createIndexSwitchToIfPass()";
let dependentDialects = ["mlir::scf::SCFDialect"];
}

#endif // CIRCT_TRANSFORMS_PASSES
68 changes: 0 additions & 68 deletions lib/Conversion/SCFToCalyx/SCFToCalyx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,70 +1625,6 @@ class BuildParGroups : public calyx::FuncOpPartialLoweringPattern {
}
};

class BuildSwitchGroups : public calyx::FuncOpPartialLoweringPattern {
using FuncOpPartialLoweringPattern::FuncOpPartialLoweringPattern;

LogicalResult
partiallyLowerFuncToComp(FuncOp funcOp,
PatternRewriter &rewriter) const override {
LogicalResult res = success();
funcOp.walk([&](Operation *op) {
if (!isa<scf::IndexSwitchOp>(op))
return WalkResult::advance();

auto switchOp = cast<scf::IndexSwitchOp>(op);
auto loc = switchOp.getLoc();

Region &defaultRegion = switchOp.getDefaultRegion();
Operation *yieldOp = defaultRegion.front().getTerminator();
Value defaultResult = yieldOp->getOperand(0);

Value finalResult = defaultResult;
scf::IfOp prevIfOp = nullptr;

rewriter.setInsertionPointAfter(switchOp);
for (size_t i = 0; i < switchOp.getCases().size(); i++) {
auto caseValueInt = switchOp.getCases()[i];
if (prevIfOp)
rewriter.setInsertionPointToStart(&prevIfOp.getElseRegion().front());

Value caseValue = rewriter.create<ConstantIndexOp>(loc, caseValueInt);
Value cond = rewriter.create<CmpIOp>(
loc, CmpIPredicate::eq, *switchOp.getODSOperands(0).begin(),
caseValue);

auto ifOp = rewriter.create<scf::IfOp>(loc, switchOp.getResultTypes(),
cond, /*hasElseRegion=*/true);

Region &caseRegion = switchOp.getCaseRegions()[i];
IRMapping mapping;
Block &emptyThenBlock = ifOp.getThenRegion().front();
emptyThenBlock.erase();
caseRegion.cloneInto(&ifOp.getThenRegion(), mapping);

if (i == switchOp.getCases().size() - 1) {
rewriter.setInsertionPointToEnd(&ifOp.getElseRegion().front());
rewriter.create<scf::YieldOp>(loc, defaultResult);
}

if (prevIfOp) {
rewriter.setInsertionPointToEnd(&prevIfOp.getElseRegion().front());
rewriter.create<scf::YieldOp>(loc, ifOp.getResult(0));
}

if (i == 0)
finalResult = ifOp.getResult(0);
prevIfOp = ifOp;
}

rewriter.replaceOp(switchOp, finalResult);

return WalkResult::advance();
});
return res;
}
};

/// Builds a control schedule by traversing the CFG of the function and
/// associating this with the previously created groups.
/// For simplicity, the generated control flow is expanded for all possible
Expand Down Expand Up @@ -2465,10 +2401,6 @@ void SCFToCalyxPass::runOnOperation() {
addOncePattern<BuildParGroups>(loweringPatterns, patternState, funcMap,
*loweringState);

/// This pattern converts all scf.IndexSwitchOps to nested if-elses.
addOncePattern<BuildSwitchGroups>(loweringPatterns, patternState, funcMap,
*loweringState);

/// This pattern converts all index typed values to an i32 integer.
addOncePattern<calyx::ConvertIndexTypes>(loweringPatterns, patternState,
funcMap, *loweringState);
Expand Down
2 changes: 0 additions & 2 deletions lib/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ add_circt_library(CIRCTTransforms
InsertMergeBlocks.cpp
PrintOpCount.cpp
MemoryBanking.cpp
IndexSwitchToIf.cpp

ADDITIONAL_HEADER_DIRS
${CIRCT_MAIN_INCLUDE_DIR}/circt/Transforms
Expand All @@ -20,7 +19,6 @@ add_circt_library(CIRCTTransforms
MLIRFuncDialect
MLIRIR
MLIRMemRefDialect
MLIRSCFDialect
MLIRSupport
MLIRTransforms
MLIRAffineDialect
Expand Down
118 changes: 0 additions & 118 deletions lib/Transforms/IndexSwitchToIf.cpp

This file was deleted.

57 changes: 0 additions & 57 deletions test/Transforms/switch-to-if.mlir

This file was deleted.

0 comments on commit b987492

Please sign in to comment.