| 1 | //===- SCFToAffine.cpp - SCF to Affine conversion -------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements a pass to raise scf ops to affine ops. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Conversion/SCFToAffine/SCFToAffine.h" |
| 14 | #include "mlir/Dialect/Affine/IR/AffineOps.h" |
| 15 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 16 | #include "mlir/Dialect/SCF/IR/SCF.h" |
| 17 | #include "mlir/IR/AffineExpr.h" |
| 18 | #include "mlir/IR/AffineMap.h" |
| 19 | #include "mlir/IR/Value.h" |
| 20 | #include "mlir/Interfaces/DataLayoutInterfaces.h" |
| 21 | #include "mlir/Pass/Pass.h" |
| 22 | #include "mlir/Support/LLVM.h" |
| 23 | #include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 24 | #include "llvm/ADT/SmallVector.h" |
| 25 | |
| 26 | namespace mlir { |
| 27 | #define GEN_PASS_DEF_RAISESCFTOAFFINEPASS |
| 28 | #include "mlir/Conversion/Passes.h.inc" |
| 29 | } // namespace mlir |
| 30 | |
| 31 | using namespace mlir; |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // SCFToAffinePass |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
| 39 | struct SCFToAffinePass |
| 40 | : public impl::RaiseSCFToAffinePassBase<SCFToAffinePass> { |
| 41 | void runOnOperation() override; |
| 42 | }; |
| 43 | |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | // ForOpRewrite |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
| 48 | /// Raise an `scf.for` to an equivalent `affine.for` if lb, ub and step satisfy |
| 49 | /// certain constraints making this possible. |
| 50 | struct ForOpRewrite : public OpRewritePattern<scf::ForOp> { |
| 51 | using OpRewritePattern<scf::ForOp>::OpRewritePattern; |
| 52 | |
| 53 | LogicalResult matchAndRewrite(scf::ForOp op, |
| 54 | PatternRewriter &rewriter) const override; |
| 55 | |
| 56 | private: |
| 57 | /// Definitively decide whether we are going to raise or not. |
| 58 | /// |
| 59 | /// An `scf.for` can trivially be raised if lb, ub are dimensions and step is |
| 60 | /// a constant. With some more work one can raise under relaxed constraints as |
| 61 | /// expressed by this function. |
| 62 | bool canRaiseToAffine(scf::ForOp op) const; |
| 63 | |
| 64 | /// Cast lb, ub, step and the induction variable of an integer-typed `op` to |
| 65 | /// `index`, in place. The bound and step casts are placed at the top level of |
| 66 | /// the affine scope so they are valid affine symbols; the induction variable |
| 67 | /// is cast back to its original type at the start of the body so the body is |
| 68 | /// left unchanged. Assumes `canRaiseToAffine(op) == true`. |
| 69 | void castBoundsToIndex(scf::ForOp op, PatternRewriter &rewriter) const; |
| 70 | |
| 71 | /// Returns an equivalent `affine.for` skeleton and the *old* induction |
| 72 | /// variable for use by the body that is inlined later. The affine loop body |
| 73 | /// is left empty except for an operation computing the old induction variable |
| 74 | /// from the new one *iff* it differs from the new one. |
| 75 | /// |
| 76 | /// Assumes `canRaiseToAffine(op) == true` and index casts were performed (if |
| 77 | /// necessary). |
| 78 | /// |
| 79 | /// There are two cases: |
| 80 | /// |
| 81 | /// 1. step is constant |
| 82 | /// 2. step is dynamic (not constant) |
| 83 | /// |
| 84 | /// In case (1) and if lb, ub are (valid) dimensions `scf.for` is trivially |
| 85 | /// raised (leaving lb, ub, iv as is). If lb is an `affine.max` we "inline" it |
| 86 | /// into the loop's lower bound map. Similarly if ub is an `affine.min`. |
| 87 | /// |
| 88 | /// In case (2) we *normalize* the loop to run from 0 with step 1: the new |
| 89 | /// upper bound is `ceil((ub - lb) / step)` and the original induction |
| 90 | /// variable is recovered in the body as `lb + step * new_iv`. Here we require |
| 91 | /// lb to be a dimension; ub may still be an `affine.min`, which is rescaled |
| 92 | /// accordingly. |
| 93 | std::pair<affine::AffineForOp, Value> |
| 94 | createAffineFor(scf::ForOp op, PatternRewriter &rewriter) const; |
| 95 | |
| 96 | std::pair<affine::AffineForOp, Value> |
| 97 | createAffineForWithConstantStep(scf::ForOp op, int64_t step, |
| 98 | PatternRewriter &rewriter) const; |
| 99 | |
| 100 | std::pair<affine::AffineForOp, Value> |
| 101 | createAffineForWithDynamicStep(scf::ForOp op, |
| 102 | PatternRewriter &rewriter) const; |
| 103 | }; |
| 104 | |
| 105 | static bool areValidAffineMapOperands(AffineMap map, ValueRange operands, |
| 106 | Region *scope) { |
| 107 | assert(map.getNumInputs() == operands.size() && |
| 108 | "expected one operand per affine map input" ); |
| 109 | return llvm::all_of( |
| 110 | Range: operands.take_front(n: map.getNumDims()), |
| 111 | P: [&](Value value) { return affine::isValidDim(value, region: scope); }) && |
| 112 | llvm::all_of(Range: operands.drop_front(n: map.getNumDims()), P: [&](Value value) { |
| 113 | return affine::isValidSymbol(value, region: scope); |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | bool indexBoundsRaisable(scf::ForOp op) { |
| 118 | Value lb = op.getLowerBound(); |
| 119 | Value ub = op.getUpperBound(); |
| 120 | IntegerAttr constAttr; |
| 121 | Region *scope = affine::getAffineScope(op); |
| 122 | if (!scope) |
| 123 | return false; |
| 124 | |
| 125 | // The asymmetry between lb and ub comes from the fact that the step |
| 126 | // normalization (for non-constant (dynamic) steps) does not work with |
| 127 | // multiple *lower* bounds (max). |
| 128 | auto lbMaxOp = lb.getDefiningOp<affine::AffineMaxOp>(); |
| 129 | bool lbOK = affine::isValidDim(value: lb, region: scope) || |
| 130 | (lbMaxOp && matchPattern(value: op.getStep(), pattern: m_Constant(bind_value: &constAttr)) && |
| 131 | areValidAffineMapOperands(map: lbMaxOp.getAffineMap(), |
| 132 | operands: lbMaxOp->getOperands(), scope)); |
| 133 | auto ubMinOp = ub.getDefiningOp<affine::AffineMinOp>(); |
| 134 | bool ubOK = |
| 135 | affine::isValidDim(value: ub, region: scope) || |
| 136 | (ubMinOp && areValidAffineMapOperands(map: ubMinOp.getAffineMap(), |
| 137 | operands: ubMinOp->getOperands(), scope)); |
| 138 | bool stepOK = affine::isValidSymbol(value: op.getStep(), region: scope); |
| 139 | |
| 140 | return lbOK && ubOK && stepOK; |
| 141 | } |
| 142 | |
| 143 | /// Decide whether an integer-typed loop can be raised by first casting its |
| 144 | /// bounds (lb, ub, step) to `index`. Requires the cast to be lossless under |
| 145 | /// affine's *signed* `index` interpretation, and every bound to be available at |
| 146 | /// the top level of the affine scope (so the inserted casts are valid symbols). |
| 147 | bool intBoundsRaisable(scf::ForOp op, IntegerType intType) { |
| 148 | uint64_t indexWidth = DataLayout::closest(op) |
| 149 | .getTypeSizeInBits(t: IndexType::get(context: op.getContext())) |
| 150 | .getFixedValue(); |
| 151 | // Lossless under signed index: sign-extend needs width <= indexWidth; |
| 152 | // zero-extend (unsigned) needs a spare sign bit, i.e. width < indexWidth. |
| 153 | uint64_t requiredWidth = intType.getWidth() + (op.getUnsignedCmp() ? 1 : 0); |
| 154 | if (requiredWidth > indexWidth) |
| 155 | return false; |
| 156 | |
| 157 | Region *scope = affine::getAffineScope(op); |
| 158 | if (!scope) |
| 159 | return false; |
| 160 | |
| 161 | // Being top-level implies the value is a symbol once it is casted to index. |
| 162 | return affine::isTopLevelValue(value: op.getLowerBound(), region: scope) && |
| 163 | affine::isTopLevelValue(value: op.getUpperBound(), region: scope) && |
| 164 | affine::isTopLevelValue(value: op.getStep(), region: scope); |
| 165 | } |
| 166 | |
| 167 | bool ForOpRewrite::canRaiseToAffine(scf::ForOp op) const { |
| 168 | Type type = op.getInductionVar().getType(); |
| 169 | if (isa<IndexType>(Val: type)) |
| 170 | return indexBoundsRaisable(op); |
| 171 | if (auto intType = dyn_cast<IntegerType>(Val&: type)) |
| 172 | return intBoundsRaisable(op, intType); |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | LogicalResult ForOpRewrite::matchAndRewrite(scf::ForOp op, |
| 177 | PatternRewriter &rewriter) const { |
| 178 | if (!canRaiseToAffine(op)) { |
| 179 | return rewriter.notifyMatchFailure(arg&: op, msg: "cannot raise scf op to affine" ); |
| 180 | } |
| 181 | |
| 182 | if (!isa<IndexType>(Val: op.getInductionVar().getType())) |
| 183 | castBoundsToIndex(op, rewriter); |
| 184 | |
| 185 | auto [affineFor, oldIV] = createAffineFor(op, rewriter); |
| 186 | Block *affineBody = affineFor.getBody(); |
| 187 | |
| 188 | if (affineBody->mightHaveTerminator()) { |
| 189 | // No unregistered ops in the body, so this is definitive. |
| 190 | Operation *terminator = affineBody->getTerminator(); |
| 191 | assert(isa<affine::AffineYieldOp>(terminator) && |
| 192 | "expected affine.yield if there *might* be terminator" ); |
| 193 | rewriter.eraseOp(op: terminator); |
| 194 | } |
| 195 | |
| 196 | SmallVector<Value> argValues; |
| 197 | argValues.push_back(Elt: oldIV); |
| 198 | llvm::append_range(C&: argValues, R: affineFor.getRegionIterArgs()); |
| 199 | rewriter.inlineBlockBefore(source: op.getBody(), dest: affineBody, before: affineBody->end(), |
| 200 | argValues); |
| 201 | |
| 202 | auto scfYieldOp = cast<scf::YieldOp>(Val: affineBody->getTerminator()); |
| 203 | rewriter.setInsertionPointToEnd(affineBody); |
| 204 | rewriter.replaceOpWithNewOp<affine::AffineYieldOp>(op: scfYieldOp, |
| 205 | args: scfYieldOp->getOperands()); |
| 206 | |
| 207 | rewriter.replaceOp(op, newOp: affineFor); |
| 208 | return success(); |
| 209 | } |
| 210 | |
| 211 | std::pair<affine::AffineForOp, Value> |
| 212 | ForOpRewrite::createAffineFor(scf::ForOp op, PatternRewriter &rewriter) const { |
| 213 | IntegerAttr constAttr; |
| 214 | if (matchPattern(value: op.getStep(), pattern: m_Constant(bind_value: &constAttr))) { |
| 215 | int64_t step = constAttr.getInt(); |
| 216 | assert(step > 0 && "scf.for has positive step" ); |
| 217 | return createAffineForWithConstantStep(op, step, rewriter); |
| 218 | } |
| 219 | return createAffineForWithDynamicStep(op, rewriter); |
| 220 | } |
| 221 | |
| 222 | std::pair<affine::AffineForOp, Value> |
| 223 | ForOpRewrite::createAffineForWithConstantStep(scf::ForOp op, int64_t step, |
| 224 | PatternRewriter &rewriter) const { |
| 225 | Value lb = op.getLowerBound(); |
| 226 | Value ub = op.getUpperBound(); |
| 227 | |
| 228 | auto lbOperands = ValueRange(lb); |
| 229 | auto ubOperands = ValueRange(ub); |
| 230 | |
| 231 | auto lbMap = AffineMap::getMultiDimIdentityMap(numDims: 1, context: rewriter.getContext()); |
| 232 | auto ubMap = AffineMap::getMultiDimIdentityMap(numDims: 1, context: rewriter.getContext()); |
| 233 | |
| 234 | if (auto ubMinOp = ub.getDefiningOp<affine::AffineMinOp>()) { |
| 235 | ubOperands = ubMinOp->getOperands(); |
| 236 | ubMap = ubMinOp.getAffineMap(); |
| 237 | } |
| 238 | |
| 239 | if (auto lbMaxOp = lb.getDefiningOp<affine::AffineMaxOp>()) { |
| 240 | lbOperands = lbMaxOp->getOperands(); |
| 241 | lbMap = lbMaxOp.getAffineMap(); |
| 242 | } |
| 243 | |
| 244 | auto affineFor = |
| 245 | affine::AffineForOp::create(builder&: rewriter, location: op.getLoc(), lbOperands, lbMap, |
| 246 | ubOperands, ubMap, step, iterArgs: op.getInits()); |
| 247 | |
| 248 | return std::make_pair(x&: affineFor, y: affineFor.getInductionVar()); |
| 249 | } |
| 250 | |
| 251 | std::pair<affine::AffineForOp, Value> |
| 252 | ForOpRewrite::createAffineForWithDynamicStep(scf::ForOp op, |
| 253 | PatternRewriter &rewriter) const { |
| 254 | Value lb = op.getLowerBound(); |
| 255 | Value ub = op.getUpperBound(); |
| 256 | Value step = op.getStep(); |
| 257 | |
| 258 | assert(affine::isValidDim(lb) && |
| 259 | "dynamic-step lower bound must be a valid affine dim" ); |
| 260 | |
| 261 | AffineExpr d0 = rewriter.getAffineDimExpr(position: 0); |
| 262 | AffineExpr d1 = rewriter.getAffineDimExpr(position: 1); |
| 263 | AffineExpr s0 = rewriter.getAffineSymbolExpr(position: 0); |
| 264 | AffineMap zeroMap = rewriter.getConstantAffineMap(val: 0); |
| 265 | |
| 266 | llvm::SmallVector<Value, 3> ubOperands = {lb, ub, step}; |
| 267 | |
| 268 | // ub is transformed with (x - lb + step - 1) floorDiv step where x ranges |
| 269 | // over all ub_i. lb is transformed to zero. |
| 270 | |
| 271 | AffineMap ubMap = AffineMap::get(dimCount: 2, symbolCount: 1, result: (d1 - d0 + s0 - 1).floorDiv(other: s0)); |
| 272 | |
| 273 | if (auto ubMinOp = ub.getDefiningOp<affine::AffineMinOp>()) { |
| 274 | AffineMap origUbMap = ubMinOp.getAffineMap(); |
| 275 | unsigned ubDims = origUbMap.getNumDims(); |
| 276 | unsigned ubSyms = origUbMap.getNumSymbols(); |
| 277 | |
| 278 | AffineExpr lbDim = rewriter.getAffineDimExpr(position: ubDims); |
| 279 | AffineExpr stepSym = rewriter.getAffineSymbolExpr(position: ubSyms); |
| 280 | |
| 281 | SmallVector<AffineExpr> ubExprs; |
| 282 | ubExprs.reserve(N: origUbMap.getNumResults()); |
| 283 | for (AffineExpr ubI : origUbMap.getResults()) { |
| 284 | ubExprs.push_back(Elt: (ubI - lbDim + stepSym - 1).floorDiv(other: stepSym)); |
| 285 | } |
| 286 | |
| 287 | // Combined space: dims = [ub dims, lb] |
| 288 | // syms = [ub syms, step] |
| 289 | ubMap = |
| 290 | AffineMap::get(dimCount: ubDims + 1, symbolCount: ubSyms + 1, results: ubExprs, context: rewriter.getContext()); |
| 291 | |
| 292 | // Operand order consistent with "combined space" above: |
| 293 | ValueRange ubOps = ubMinOp->getOperands(); |
| 294 | SmallVector<Value> combined; |
| 295 | combined.append(in_start: ubOps.begin(), in_end: ubOps.begin() + ubDims); // ub dims |
| 296 | combined.push_back(Elt: lb); // lb (single dim) |
| 297 | combined.append(in_start: ubOps.begin() + ubDims, in_end: ubOps.end()); // ub syms |
| 298 | combined.push_back(Elt: op.getStep()); // step (single sym) |
| 299 | ubOperands = std::move(combined); |
| 300 | } |
| 301 | |
| 302 | auto affineFor = affine::AffineForOp::create( |
| 303 | builder&: rewriter, location: op.getLoc(), lbOperands: {}, lbMap: zeroMap, ubOperands, ubMap, step: 1, iterArgs: op.getInits()); |
| 304 | |
| 305 | // old_iv = old_lb + new_iv * step |
| 306 | AffineMap ivMap = AffineMap::get(dimCount: 2, symbolCount: 1, result: d0 + d1 * s0); |
| 307 | |
| 308 | llvm::SmallVector<Value, 3> ivOperands = {lb, affineFor.getInductionVar(), |
| 309 | step}; |
| 310 | |
| 311 | rewriter.setInsertionPointToStart(affineFor.getBody()); |
| 312 | auto oldIV = |
| 313 | affine::AffineApplyOp::create(builder&: rewriter, location: op.getLoc(), map: ivMap, mapOperands: ivOperands); |
| 314 | |
| 315 | return std::make_pair(x&: affineFor, y&: oldIV); |
| 316 | } |
| 317 | |
| 318 | void ForOpRewrite::castBoundsToIndex(scf::ForOp loop, |
| 319 | PatternRewriter &rewriter) const { |
| 320 | OpBuilder::InsertionGuard guard(rewriter); |
| 321 | |
| 322 | Value lb = loop.getLowerBound(); |
| 323 | Value ub = loop.getUpperBound(); |
| 324 | Value step = loop.getStep(); |
| 325 | Type originalType = step.getType(); |
| 326 | |
| 327 | assert(lb.getType() == originalType && ub.getType() == originalType && |
| 328 | "expected lb, ub, and step to have the same type" ); |
| 329 | |
| 330 | auto createIndexCast = [&](Type out, Value in) -> Value { |
| 331 | Location loc = loop.getLoc(); |
| 332 | if (loop.getUnsignedCmp()) |
| 333 | return arith::IndexCastUIOp::create(builder&: rewriter, location: loc, out, in); |
| 334 | return arith::IndexCastOp::create(builder&: rewriter, location: loc, out, in); |
| 335 | }; |
| 336 | |
| 337 | // We place the bound casts at the top level of the affine scope so that they |
| 338 | // are identified as valid affine symbols. |
| 339 | |
| 340 | Region *scope = affine::getAffineScope(op: loop); |
| 341 | Operation *anchor = loop; |
| 342 | while (anchor->getParentRegion() != scope) |
| 343 | anchor = anchor->getParentOp(); |
| 344 | rewriter.setInsertionPoint(anchor); |
| 345 | |
| 346 | Value newLb = createIndexCast(rewriter.getIndexType(), lb); |
| 347 | Value newUb = createIndexCast(rewriter.getIndexType(), ub); |
| 348 | Value newStep = createIndexCast(rewriter.getIndexType(), step); |
| 349 | |
| 350 | rewriter.modifyOpInPlace(root: loop, callable: [&] { |
| 351 | loop.setLowerBound(newLb); |
| 352 | loop.setUpperBound(newUb); |
| 353 | loop.setStep(newStep); |
| 354 | |
| 355 | Value originalIV = loop.getInductionVar(); |
| 356 | Value iv = loop.getBody()->insertArgument( |
| 357 | index: (unsigned)0, type: rewriter.getIndexType(), loc: loop.getLoc()); |
| 358 | |
| 359 | rewriter.setInsertionPointToStart(loop.getBody()); |
| 360 | Value castIV = createIndexCast(originalType, iv); |
| 361 | rewriter.replaceAllUsesWith(from: originalIV, to: castIV); |
| 362 | |
| 363 | // Original induction var is now at index 1. |
| 364 | loop.getBody()->eraseArgument(index: 1); |
| 365 | }); |
| 366 | } |
| 367 | |
| 368 | //===----------------------------------------------------------------------===// |
| 369 | // Pass implementation |
| 370 | //===----------------------------------------------------------------------===// |
| 371 | |
| 372 | void SCFToAffinePass::runOnOperation() { |
| 373 | MLIRContext &ctx = getContext(); |
| 374 | RewritePatternSet patterns(&ctx); |
| 375 | populateSCFToAffineConversionPatterns(patterns); |
| 376 | |
| 377 | (void)applyPatternsGreedily(op: getOperation(), patterns: std::move(patterns)); |
| 378 | } |
| 379 | |
| 380 | } // namespace |
| 381 | |
| 382 | //===----------------------------------------------------------------------===// |
| 383 | // API |
| 384 | //===----------------------------------------------------------------------===// |
| 385 | |
| 386 | void mlir::populateSCFToAffineConversionPatterns(RewritePatternSet &patterns) { |
| 387 | patterns.add<ForOpRewrite>(arg: patterns.getContext()); |
| 388 | } |
| 389 | |