Skip to content

Commit 1188105

Browse files
committed
[emitc][mlir] Refactor 'lowerRegion' lambda function
1 parent dfacff1 commit 1188105

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

mlir/lib/Conversion/SCFToEmitC/SCFToEmitC.cpp

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ static void lowerYield(SmallVector<Value> &resultVariables,
9494
rewriter.eraseOp(yield);
9595
}
9696

97+
// Lower the contents of an scf::if/scf::index_switch regions to an
98+
// emitc::if/emitc::switch regions. The contents of the lowering region is
99+
// moved into the respective lowered region, but the scf::yield is replaced not
100+
// only with an emitc::yield, but also with a sequence of emitc::assign ops that
101+
// set the yielded values into the result variables.
102+
static void lowerRegion(SmallVector<Value> &resultVariables,
103+
PatternRewriter &rewriter, Region &region,
104+
Region &loweredRegion) {
105+
rewriter.inlineRegionBefore(region, loweredRegion, loweredRegion.end());
106+
Operation *terminator = loweredRegion.back().getTerminator();
107+
lowerYield(resultVariables, rewriter, cast<scf::YieldOp>(terminator));
108+
}
109+
97110
LogicalResult ForLowering::matchAndRewrite(ForOp forOp,
98111
PatternRewriter &rewriter) const {
99112
Location loc = forOp.getLoc();
@@ -145,18 +158,6 @@ LogicalResult IfLowering::matchAndRewrite(IfOp ifOp,
145158
SmallVector<Value> resultVariables =
146159
createVariablesForResults(ifOp, rewriter);
147160

148-
// Utility function to lower the contents of an scf::if region to an emitc::if
149-
// region. The contents of the scf::if regions is moved into the respective
150-
// emitc::if regions, but the scf::yield is replaced not only with an
151-
// emitc::yield, but also with a sequence of emitc::assign ops that set the
152-
// yielded values into the result variables.
153-
auto lowerRegion = [&resultVariables, &rewriter](Region &region,
154-
Region &loweredRegion) {
155-
rewriter.inlineRegionBefore(region, loweredRegion, loweredRegion.end());
156-
Operation *terminator = loweredRegion.back().getTerminator();
157-
lowerYield(resultVariables, rewriter, cast<scf::YieldOp>(terminator));
158-
};
159-
160161
Region &thenRegion = ifOp.getThenRegion();
161162
Region &elseRegion = ifOp.getElseRegion();
162163

@@ -196,18 +197,6 @@ IndexSwitchOpLowering::matchAndRewrite(IndexSwitchOp indexSwitchOp,
196197
SmallVector<Value> resultVariables =
197198
createVariablesForResults(indexSwitchOp, rewriter);
198199

199-
// Utility function to lower the contents of an scf::index_switch regions to
200-
// an emitc::switch regions. The contents of the scf::index_switch regions is
201-
// moved into the respective emitc::switch regions, but the scf::yield is
202-
// replaced not only with an emitc::yield, but also with a sequence of
203-
// emitc::assign ops that set the yielded values into the result variables.
204-
auto lowerRegion = [&resultVariables, &rewriter](Region &region,
205-
Region &loweredRegion) {
206-
rewriter.inlineRegionBefore(region, loweredRegion, loweredRegion.end());
207-
Operation *terminator = loweredRegion.back().getTerminator();
208-
lowerYield(resultVariables, rewriter, cast<scf::YieldOp>(terminator));
209-
};
210-
211200
auto loweredSwitch = rewriter.create<emitc::SwitchOp>(
212201
loc, indexSwitchOp.getArg(), indexSwitchOp.getCases(),
213202
indexSwitchOp.getNumCases());

0 commit comments

Comments
 (0)