-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transform scf index switch to if-else
- Loading branch information
1 parent
12785bb
commit 577722d
Showing
5 changed files
with
127 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
//===- IndexSwitchToIf.cpp - Index switch to if-else pass ---*-C++-*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Contains the definitions of the SCF IndexSwitch to If-Else pass. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "circt/Transforms/Passes.h" | ||
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h" | ||
#include "mlir/Conversion/LLVMCommon/Pattern.h" | ||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/Dialect/SCF/IR/SCF.h" | ||
#include "mlir/IR/OperationSupport.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
|
||
namespace circt { | ||
#define GEN_PASS_DEF_INDEXSWITCHTOIF | ||
#include "circt/Transforms/Passes.h.inc" | ||
} // namespace circt | ||
|
||
using namespace mlir; | ||
using namespace circt; | ||
|
||
struct SwitchToIfConversion : public OpConversionPattern<scf::IndexSwitchOp> { | ||
using OpConversionPattern::OpConversionPattern; | ||
|
||
LogicalResult | ||
matchAndRewrite(scf::IndexSwitchOp switchOp, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
auto loc = switchOp.getLoc(); | ||
|
||
Region &defaultRegion = switchOp.getDefaultRegion(); | ||
|
||
Value finalResult; | ||
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<arith::ConstantIndexOp>(loc, caseValueInt); | ||
Value cond = rewriter.create<arith::CmpIOp>( | ||
loc, arith::CmpIPredicate::eq, switchOp.getOperand(), 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()); | ||
Block &elseBlock = ifOp.getElseRegion().front(); | ||
elseBlock.erase(); | ||
defaultRegion.cloneInto(&ifOp.getElseRegion(), mapping); | ||
} | ||
|
||
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 success(); | ||
} | ||
}; | ||
|
||
namespace { | ||
|
||
struct IndexSwitchToIfPass | ||
: public circt::impl::IndexSwitchToIfBase<IndexSwitchToIfPass> { | ||
public: | ||
void runOnOperation() override { | ||
auto *ctx = &getContext(); | ||
RewritePatternSet patterns(ctx); | ||
ConversionTarget target(*ctx); | ||
|
||
target.addLegalDialect<scf::SCFDialect>(); | ||
target.addLegalDialect<arith::ArithDialect>(); | ||
target.addLegalOp<ModuleOp, func::FuncOp, func::ReturnOp>(); | ||
target.addIllegalOp<scf::IndexSwitchOp>(); | ||
|
||
patterns.add<SwitchToIfConversion>(ctx); | ||
|
||
if (applyPartialConversion(getOperation(), target, std::move(patterns)) | ||
.failed()) { | ||
signalPassFailure(); | ||
return; | ||
} | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
namespace circt { | ||
std::unique_ptr<mlir::Pass> createIndexSwitchToIfPass() { | ||
return std::make_unique<IndexSwitchToIfPass>(); | ||
} | ||
} // namespace circt |