[Mlir-commits] [mlir] [SCFToAffine] Add a pass to raise scf to affine ops. (PR #152925)
Oleksandr Alex Zinenko
llvmlistbot at llvm.org
Mon Aug 11 05:58:46 PDT 2025
================
@@ -0,0 +1,100 @@
+//===- SCFToAffine.cpp - SCF to Affine conversion -------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements a pass to raise scf.for, scf.if and loop.terminator
+// ops into affine ops.
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Conversion/SCFToAffine/SCFToAffine.h"
+#include "mlir/Dialect/Affine/IR/AffineOps.h"
+#include "mlir/Dialect/SCF/IR/SCF.h"
+#include "mlir/IR/Verifier.h"
+#include "mlir/Transforms/DialectConversion.h"
+#include "mlir/Transforms/Passes.h"
+
+namespace mlir {
+#define GEN_PASS_DEF_RAISESCFTOAFFINEPASS
+#include "mlir/Conversion/Passes.h.inc"
+} // namespace mlir
+
+using namespace mlir;
+
+namespace {
+
+struct SCFToAffinePass
+ : public impl::RaiseSCFToAffinePassBase<SCFToAffinePass> {
+ void runOnOperation() override;
+};
+
+struct ForOpRewrite : public OpRewritePattern<scf::ForOp> {
+ using OpRewritePattern<scf::ForOp>::OpRewritePattern;
+
+ LogicalResult matchAndRewrite(scf::ForOp op,
+ PatternRewriter &rewriter) const override {
+ auto loc = op.getLoc();
+ auto lower = op.getLowerBound();
+ auto upper = op.getUpperBound();
+ auto step = op.getStep();
+
+ if (!affine::isValidDim(lower) || !affine::isValidDim(upper) ||
+ !affine::isValidSymbol(step))
+ return llvm::failure();
+
+ auto lowerDim = rewriter.getAffineDimExpr(0);
+ auto upperDim = rewriter.getAffineDimExpr(1);
+ auto stepSym = rewriter.getAffineSymbolExpr(0);
+ auto affineFor = affine::AffineForOp::create(
+ rewriter, loc, ValueRange(), rewriter.getConstantAffineMap(0),
+ ValueRange({lower, upper, step}),
+ AffineMap::get(2, 1,
+ (upperDim - lowerDim + stepSym - 1).floorDiv(stepSym)),
+ 1, op.getInits());
+ auto affineBody = affineFor.getBody();
+
+ if (affineBody->mightHaveTerminator())
+ rewriter.eraseOp(affineBody->getTerminator());
+
+ rewriter.setInsertionPointToStart(affineBody);
+ auto actualIndexMap =
+ AffineMap::get(2, 1, lowerDim + rewriter.getAffineDimExpr(1) * stepSym);
+ Value newIndVar =
+ affine::AffineApplyOp::create(
+ rewriter, op.getLoc(), actualIndexMap,
+ ValueRange({lower, affineFor.getInductionVar(), step}))
+ .getResult();
+
+ SmallVector<Value> argValues;
+ argValues.push_back(newIndVar);
+ llvm::append_range(argValues, affineFor.getRegionIterArgs());
+ rewriter.inlineBlockBefore(op.getBody(), affineBody, affineBody->end(),
+ argValues);
+
+ auto scfYieldOp = cast<scf::YieldOp>(affineBody->getTerminator());
+ rewriter.setInsertionPointToEnd(affineBody);
+ rewriter.replaceOpWithNewOp<affine::AffineYieldOp>(
+ scfYieldOp, scfYieldOp->getOperands());
+
+ rewriter.replaceOp(op, affineFor);
+ return success();
+ }
+};
+
+} // namespace
+
+void mlir::populateSCFToAffineConversionPatterns(RewritePatternSet &patterns) {
+ patterns.add<ForOpRewrite>(patterns.getContext());
+}
+
+void SCFToAffinePass::runOnOperation() {
+ RewritePatternSet patterns(&getContext());
+ populateSCFToAffineConversionPatterns(patterns);
+
+ if (failed(applyPatternsGreedily(getOperation(), std::move(patterns))))
+ signalPassFailure();
----------------
ftynse wrote:
Do we actually need the greedy rewriter with its overhead here or would a simple walk calling a function suffice?
https://github.com/llvm/llvm-project/pull/152925
More information about the Mlir-commits
mailing list