[Mlir-commits] [mlir] [mlir][emitc] Add 'emitc.switch' op to the dialect (PR #102331)

Gil Rapaport llvmlistbot at llvm.org
Sun Aug 11 00:18:51 PDT 2024


================
@@ -177,9 +177,59 @@ LogicalResult IfLowering::matchAndRewrite(IfOp ifOp,
   return success();
 }
 
+// Lower scf::index_switch to emitc::switch, implementing result values as
+// emitc::variable's updated within the case and default regions.
+struct IndexSwitchOpLowering : public OpRewritePattern<IndexSwitchOp> {
+  using OpRewritePattern<IndexSwitchOp>::OpRewritePattern;
+
+  LogicalResult matchAndRewrite(IndexSwitchOp indexSwitchOp,
+                                PatternRewriter &rewriter) const override;
+};
+
+LogicalResult
+IndexSwitchOpLowering::matchAndRewrite(IndexSwitchOp indexSwitchOp,
+                                       PatternRewriter &rewriter) const {
+  Location loc = indexSwitchOp.getLoc();
+
+  // Create an emitc::variable op for each result. These variables will be
+  // assigned to by emitc::assign ops within the case and default regions.
+  SmallVector<Value> resultVariables =
+      createVariablesForResults(indexSwitchOp, rewriter);
+
+  // Utility function to lower the contents of an scf::index_switch regions to
+  // an emitc::switch regions. The contents of the scf::index_switch regions is
+  // moved into the respective emitc::switch regions, but the scf::yield is
+  // replaced not only with an emitc::yield, but also with a sequence of
+  // emitc::assign ops that set the yielded values into the result variables.
+  auto lowerRegion = [&resultVariables, &rewriter](Region &region,
+                                                   Region &loweredRegion) {
+    rewriter.inlineRegionBefore(region, loweredRegion, loweredRegion.end());
+    Operation *terminator = loweredRegion.back().getTerminator();
+    lowerYield(resultVariables, rewriter, cast<scf::YieldOp>(terminator));
+  };
----------------
aniragil wrote:

Can we refactor this function and its equivalent from `scf.if` lowering into a static function (similar to `lowerYield()`)?

https://github.com/llvm/llvm-project/pull/102331


More information about the Mlir-commits mailing list