| 1 | //===- AffineToStandard.cpp - Lower affine constructs to primitives -------===// |
| 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 lowers affine constructs (If and For statements, AffineApply |
| 10 | // operations) within a function into their standard If and For equivalent ops. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Conversion/AffineToStandard/AffineToStandard.h" |
| 15 | |
| 16 | #include "mlir/Dialect/Affine/IR/AffineOps.h" |
| 17 | #include "mlir/Dialect/Affine/Transforms/Transforms.h" |
| 18 | #include "mlir/Dialect/Affine/Utils.h" |
| 19 | #include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 20 | #include "mlir/Dialect/SCF/IR/SCF.h" |
| 21 | #include "mlir/Dialect/Vector/IR/VectorOps.h" |
| 22 | #include "mlir/IR/IntegerSet.h" |
| 23 | #include "mlir/IR/MLIRContext.h" |
| 24 | #include "mlir/Transforms/DialectConversion.h" |
| 25 | #include "mlir/Transforms/Passes.h" |
| 26 | |
| 27 | namespace mlir { |
| 28 | #define GEN_PASS_DEF_LOWERAFFINEPASS |
| 29 | #include "mlir/Conversion/Passes.h.inc" |
| 30 | } // namespace mlir |
| 31 | |
| 32 | using namespace mlir; |
| 33 | using namespace mlir::affine; |
| 34 | using namespace mlir::vector; |
| 35 | |
| 36 | /// Given a range of values, emit the code that reduces them with "min" or "max" |
| 37 | /// depending on the provided comparison predicate, sgt for max and slt for min. |
| 38 | /// |
| 39 | /// Multiple values are scanned in a linear sequence. This creates a data |
| 40 | /// dependences that wouldn't exist in a tree reduction, but is easier to |
| 41 | /// recognize as a reduction by the subsequent passes. |
| 42 | static Value buildMinMaxReductionSeq(Location loc, |
| 43 | arith::CmpIPredicate predicate, |
| 44 | ValueRange values, OpBuilder &builder) { |
| 45 | assert(!values.empty() && "empty min/max chain" ); |
| 46 | assert(predicate == arith::CmpIPredicate::sgt || |
| 47 | predicate == arith::CmpIPredicate::slt); |
| 48 | |
| 49 | auto valueIt = values.begin(); |
| 50 | Value value = *valueIt++; |
| 51 | for (; valueIt != values.end(); ++valueIt) { |
| 52 | if (predicate == arith::CmpIPredicate::sgt) |
| 53 | value = arith::MaxSIOp::create(builder, location: loc, lhs: value, rhs: *valueIt); |
| 54 | else |
| 55 | value = arith::MinSIOp::create(builder, location: loc, lhs: value, rhs: *valueIt); |
| 56 | } |
| 57 | |
| 58 | return value; |
| 59 | } |
| 60 | |
| 61 | /// Emit instructions that correspond to computing the maximum value among the |
| 62 | /// values of a (potentially) multi-output affine map applied to `operands`. |
| 63 | static Value lowerAffineMapMax(OpBuilder &builder, Location loc, AffineMap map, |
| 64 | ValueRange operands) { |
| 65 | if (auto values = expandAffineMap(builder, loc, affineMap: map, operands)) |
| 66 | return buildMinMaxReductionSeq(loc, predicate: arith::CmpIPredicate::sgt, values: *values, |
| 67 | builder); |
| 68 | return nullptr; |
| 69 | } |
| 70 | |
| 71 | /// Emit instructions that correspond to computing the minimum value among the |
| 72 | /// values of a (potentially) multi-output affine map applied to `operands`. |
| 73 | static Value lowerAffineMapMin(OpBuilder &builder, Location loc, AffineMap map, |
| 74 | ValueRange operands) { |
| 75 | if (auto values = expandAffineMap(builder, loc, affineMap: map, operands)) |
| 76 | return buildMinMaxReductionSeq(loc, predicate: arith::CmpIPredicate::slt, values: *values, |
| 77 | builder); |
| 78 | return nullptr; |
| 79 | } |
| 80 | |
| 81 | /// Emit instructions that correspond to the affine map in the upper bound |
| 82 | /// applied to the respective operands, and compute the minimum value across |
| 83 | /// the results. |
| 84 | Value mlir::lowerAffineUpperBound(AffineForOp op, OpBuilder &builder) { |
| 85 | return lowerAffineMapMin(builder, loc: op.getLoc(), map: op.getUpperBoundMap(), |
| 86 | operands: op.getUpperBoundOperands()); |
| 87 | } |
| 88 | |
| 89 | /// Emit instructions that correspond to the affine map in the lower bound |
| 90 | /// applied to the respective operands, and compute the maximum value across |
| 91 | /// the results. |
| 92 | Value mlir::lowerAffineLowerBound(AffineForOp op, OpBuilder &builder) { |
| 93 | return lowerAffineMapMax(builder, loc: op.getLoc(), map: op.getLowerBoundMap(), |
| 94 | operands: op.getLowerBoundOperands()); |
| 95 | } |
| 96 | |
| 97 | namespace { |
| 98 | class AffineMinLowering : public OpRewritePattern<AffineMinOp> { |
| 99 | public: |
| 100 | using OpRewritePattern<AffineMinOp>::OpRewritePattern; |
| 101 | |
| 102 | LogicalResult matchAndRewrite(AffineMinOp op, |
| 103 | PatternRewriter &rewriter) const override { |
| 104 | Value reduced = |
| 105 | lowerAffineMapMin(builder&: rewriter, loc: op.getLoc(), map: op.getMap(), operands: op.getOperands()); |
| 106 | if (!reduced) |
| 107 | return failure(); |
| 108 | |
| 109 | rewriter.replaceOp(op, newValues: reduced); |
| 110 | return success(); |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | class AffineMaxLowering : public OpRewritePattern<AffineMaxOp> { |
| 115 | public: |
| 116 | using OpRewritePattern<AffineMaxOp>::OpRewritePattern; |
| 117 | |
| 118 | LogicalResult matchAndRewrite(AffineMaxOp op, |
| 119 | PatternRewriter &rewriter) const override { |
| 120 | Value reduced = |
| 121 | lowerAffineMapMax(builder&: rewriter, loc: op.getLoc(), map: op.getMap(), operands: op.getOperands()); |
| 122 | if (!reduced) |
| 123 | return failure(); |
| 124 | |
| 125 | rewriter.replaceOp(op, newValues: reduced); |
| 126 | return success(); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | /// Affine yields ops are removed. |
| 131 | class AffineYieldOpLowering : public OpRewritePattern<AffineYieldOp> { |
| 132 | public: |
| 133 | using OpRewritePattern<AffineYieldOp>::OpRewritePattern; |
| 134 | |
| 135 | LogicalResult matchAndRewrite(AffineYieldOp op, |
| 136 | PatternRewriter &rewriter) const override { |
| 137 | if (isa<scf::ParallelOp, AffineParallelOp>(Val: op->getParentOp())) { |
| 138 | // Terminator is rewritten as part of the "affine.parallel" lowering |
| 139 | // pattern. |
| 140 | return failure(); |
| 141 | } |
| 142 | rewriter.replaceOpWithNewOp<scf::YieldOp>(op, args: op.getOperands()); |
| 143 | return success(); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | class AffineForLowering : public OpRewritePattern<AffineForOp> { |
| 148 | public: |
| 149 | using OpRewritePattern<AffineForOp>::OpRewritePattern; |
| 150 | |
| 151 | LogicalResult matchAndRewrite(AffineForOp op, |
| 152 | PatternRewriter &rewriter) const override { |
| 153 | Location loc = op.getLoc(); |
| 154 | Value lowerBound = lowerAffineLowerBound(op, builder&: rewriter); |
| 155 | Value upperBound = lowerAffineUpperBound(op, builder&: rewriter); |
| 156 | Value step = |
| 157 | arith::ConstantIndexOp::create(builder&: rewriter, location: loc, value: op.getStepAsInt()); |
| 158 | auto scfForOp = scf::ForOp::create(builder&: rewriter, location: loc, lowerBound, upperBound, |
| 159 | step, initArgs: op.getInits()); |
| 160 | rewriter.eraseBlock(block: scfForOp.getBody()); |
| 161 | rewriter.inlineRegionBefore(region&: op.getRegion(), parent&: scfForOp.getRegion(), |
| 162 | before: scfForOp.getRegion().end()); |
| 163 | rewriter.replaceOp(op, newValues: scfForOp.getResults()); |
| 164 | return success(); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | /// Convert an `affine.parallel` (loop nest) operation into a `scf.parallel` |
| 169 | /// operation. |
| 170 | class AffineParallelLowering : public OpRewritePattern<AffineParallelOp> { |
| 171 | public: |
| 172 | using OpRewritePattern<AffineParallelOp>::OpRewritePattern; |
| 173 | |
| 174 | LogicalResult matchAndRewrite(AffineParallelOp op, |
| 175 | PatternRewriter &rewriter) const override { |
| 176 | Location loc = op.getLoc(); |
| 177 | SmallVector<Value, 8> steps; |
| 178 | SmallVector<Value, 8> upperBoundTuple; |
| 179 | SmallVector<Value, 8> lowerBoundTuple; |
| 180 | SmallVector<Value, 8> identityVals; |
| 181 | // Emit IR computing the lower and upper bound by expanding the map |
| 182 | // expression. |
| 183 | lowerBoundTuple.reserve(N: op.getNumDims()); |
| 184 | upperBoundTuple.reserve(N: op.getNumDims()); |
| 185 | for (unsigned i = 0, e = op.getNumDims(); i < e; ++i) { |
| 186 | Value lower = lowerAffineMapMax(builder&: rewriter, loc, map: op.getLowerBoundMap(pos: i), |
| 187 | operands: op.getLowerBoundsOperands()); |
| 188 | if (!lower) |
| 189 | return rewriter.notifyMatchFailure(arg&: op, msg: "couldn't convert lower bounds" ); |
| 190 | lowerBoundTuple.push_back(Elt: lower); |
| 191 | |
| 192 | Value upper = lowerAffineMapMin(builder&: rewriter, loc, map: op.getUpperBoundMap(pos: i), |
| 193 | operands: op.getUpperBoundsOperands()); |
| 194 | if (!upper) |
| 195 | return rewriter.notifyMatchFailure(arg&: op, msg: "couldn't convert upper bounds" ); |
| 196 | upperBoundTuple.push_back(Elt: upper); |
| 197 | } |
| 198 | steps.reserve(N: op.getSteps().size()); |
| 199 | for (int64_t step : op.getSteps()) |
| 200 | steps.push_back(Elt: arith::ConstantIndexOp::create(builder&: rewriter, location: loc, value: step)); |
| 201 | |
| 202 | // Get the terminator op. |
| 203 | auto affineParOpTerminator = |
| 204 | cast<AffineYieldOp>(Val: op.getBody()->getTerminator()); |
| 205 | scf::ParallelOp parOp; |
| 206 | if (op.getResults().empty()) { |
| 207 | // Case with no reduction operations/return values. |
| 208 | parOp = scf::ParallelOp::create(builder&: rewriter, location: loc, lowerBounds: lowerBoundTuple, |
| 209 | upperBounds: upperBoundTuple, steps, |
| 210 | /*bodyBuilderFn=*/nullptr); |
| 211 | rewriter.eraseBlock(block: parOp.getBody()); |
| 212 | rewriter.inlineRegionBefore(region&: op.getRegion(), parent&: parOp.getRegion(), |
| 213 | before: parOp.getRegion().end()); |
| 214 | rewriter.replaceOp(op, newValues: parOp.getResults()); |
| 215 | rewriter.setInsertionPoint(affineParOpTerminator); |
| 216 | rewriter.replaceOpWithNewOp<scf::ReduceOp>(op: affineParOpTerminator); |
| 217 | return success(); |
| 218 | } |
| 219 | // Case with affine.parallel with reduction operations/return values. |
| 220 | // scf.parallel handles the reduction operation differently unlike |
| 221 | // affine.parallel. |
| 222 | ArrayRef<Attribute> reductions = op.getReductions().getValue(); |
| 223 | for (auto pair : llvm::zip(t&: reductions, u: op.getResultTypes())) { |
| 224 | // For each of the reduction operations get the identity values for |
| 225 | // initialization of the result values. |
| 226 | Attribute reduction = std::get<0>(t&: pair); |
| 227 | Type resultType = std::get<1>(t&: pair); |
| 228 | std::optional<arith::AtomicRMWKind> reductionOp = |
| 229 | arith::symbolizeAtomicRMWKind( |
| 230 | static_cast<uint64_t>(cast<IntegerAttr>(Val&: reduction).getInt())); |
| 231 | assert(reductionOp && "Reduction operation cannot be of None Type" ); |
| 232 | arith::AtomicRMWKind reductionOpValue = *reductionOp; |
| 233 | Value identityVal = |
| 234 | arith::getIdentityValue(op: reductionOpValue, resultType, builder&: rewriter, loc); |
| 235 | if (!identityVal) |
| 236 | return rewriter.notifyMatchFailure( |
| 237 | arg&: op, msg: "unsupported reduction kind for identity value" ); |
| 238 | identityVals.push_back(Elt: identityVal); |
| 239 | } |
| 240 | parOp = scf::ParallelOp::create(builder&: rewriter, location: loc, lowerBounds: lowerBoundTuple, |
| 241 | upperBounds: upperBoundTuple, steps, initVals: identityVals, |
| 242 | /*bodyBuilderFn=*/nullptr); |
| 243 | |
| 244 | // Copy the body of the affine.parallel op. |
| 245 | rewriter.eraseBlock(block: parOp.getBody()); |
| 246 | rewriter.inlineRegionBefore(region&: op.getRegion(), parent&: parOp.getRegion(), |
| 247 | before: parOp.getRegion().end()); |
| 248 | assert(reductions.size() == affineParOpTerminator->getNumOperands() && |
| 249 | "Unequal number of reductions and operands." ); |
| 250 | |
| 251 | // Emit new "scf.reduce" terminator. |
| 252 | rewriter.setInsertionPoint(affineParOpTerminator); |
| 253 | auto reduceOp = rewriter.replaceOpWithNewOp<scf::ReduceOp>( |
| 254 | op: affineParOpTerminator, args: affineParOpTerminator->getOperands()); |
| 255 | for (unsigned i = 0, end = reductions.size(); i < end; i++) { |
| 256 | // For each of the reduction operations get the respective mlir::Value. |
| 257 | std::optional<arith::AtomicRMWKind> reductionOp = |
| 258 | arith::symbolizeAtomicRMWKind( |
| 259 | cast<IntegerAttr>(Val: reductions[i]).getInt()); |
| 260 | assert(reductionOp && "Reduction Operation cannot be of None Type" ); |
| 261 | arith::AtomicRMWKind reductionOpValue = *reductionOp; |
| 262 | rewriter.setInsertionPoint(&parOp.getBody()->back()); |
| 263 | Block &reductionBody = reduceOp.getReductions()[i].front(); |
| 264 | rewriter.setInsertionPointToEnd(&reductionBody); |
| 265 | Value reductionResult = arith::getReductionOp( |
| 266 | op: reductionOpValue, builder&: rewriter, loc, lhs: reductionBody.getArgument(i: 0), |
| 267 | rhs: reductionBody.getArgument(i: 1)); |
| 268 | scf::ReduceReturnOp::create(builder&: rewriter, location: loc, result: reductionResult); |
| 269 | } |
| 270 | rewriter.replaceOp(op, newValues: parOp.getResults()); |
| 271 | return success(); |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | class AffineIfLowering : public OpRewritePattern<AffineIfOp> { |
| 276 | public: |
| 277 | using OpRewritePattern<AffineIfOp>::OpRewritePattern; |
| 278 | |
| 279 | LogicalResult matchAndRewrite(AffineIfOp op, |
| 280 | PatternRewriter &rewriter) const override { |
| 281 | auto loc = op.getLoc(); |
| 282 | |
| 283 | // Now we just have to handle the condition logic. |
| 284 | auto integerSet = op.getIntegerSet(); |
| 285 | Value zeroConstant = arith::ConstantIndexOp::create(builder&: rewriter, location: loc, value: 0); |
| 286 | SmallVector<Value, 8> operands(op.getOperands()); |
| 287 | auto operandsRef = llvm::ArrayRef(operands); |
| 288 | |
| 289 | // Calculate cond as a conjunction without short-circuiting. |
| 290 | Value cond = nullptr; |
| 291 | for (unsigned i = 0, e = integerSet.getNumConstraints(); i < e; ++i) { |
| 292 | AffineExpr constraintExpr = integerSet.getConstraint(idx: i); |
| 293 | bool isEquality = integerSet.isEq(idx: i); |
| 294 | |
| 295 | // Build and apply an affine expression |
| 296 | auto numDims = integerSet.getNumDims(); |
| 297 | Value affResult = expandAffineExpr(builder&: rewriter, loc, expr: constraintExpr, |
| 298 | dimValues: operandsRef.take_front(N: numDims), |
| 299 | symbolValues: operandsRef.drop_front(N: numDims)); |
| 300 | if (!affResult) |
| 301 | return failure(); |
| 302 | auto pred = |
| 303 | isEquality ? arith::CmpIPredicate::eq : arith::CmpIPredicate::sge; |
| 304 | Value cmpVal = |
| 305 | arith::CmpIOp::create(builder&: rewriter, location: loc, predicate: pred, lhs: affResult, rhs: zeroConstant); |
| 306 | cond = |
| 307 | cond ? arith::AndIOp::create(builder&: rewriter, location: loc, lhs: cond, rhs: cmpVal).getResult() |
| 308 | : cmpVal; |
| 309 | } |
| 310 | cond = cond ? cond |
| 311 | : arith::ConstantIntOp::create(builder&: rewriter, location: loc, /*value=*/1, |
| 312 | /*width=*/1); |
| 313 | |
| 314 | bool hasElseRegion = !op.getElseRegion().empty(); |
| 315 | auto ifOp = scf::IfOp::create(builder&: rewriter, location: loc, resultTypes: op.getResultTypes(), cond, |
| 316 | withElseRegion: hasElseRegion); |
| 317 | rewriter.inlineRegionBefore(region&: op.getThenRegion(), |
| 318 | before: &ifOp.getThenRegion().back()); |
| 319 | rewriter.eraseBlock(block: &ifOp.getThenRegion().back()); |
| 320 | if (hasElseRegion) { |
| 321 | rewriter.inlineRegionBefore(region&: op.getElseRegion(), |
| 322 | before: &ifOp.getElseRegion().back()); |
| 323 | rewriter.eraseBlock(block: &ifOp.getElseRegion().back()); |
| 324 | } |
| 325 | |
| 326 | // Replace the Affine IfOp finally. |
| 327 | rewriter.replaceOp(op, newValues: ifOp.getResults()); |
| 328 | return success(); |
| 329 | } |
| 330 | }; |
| 331 | |
| 332 | /// Convert an "affine.apply" operation into a sequence of arithmetic |
| 333 | /// operations using the StandardOps dialect. |
| 334 | class AffineApplyLowering : public OpRewritePattern<AffineApplyOp> { |
| 335 | public: |
| 336 | using OpRewritePattern<AffineApplyOp>::OpRewritePattern; |
| 337 | |
| 338 | LogicalResult matchAndRewrite(AffineApplyOp op, |
| 339 | PatternRewriter &rewriter) const override { |
| 340 | auto maybeExpandedMap = |
| 341 | expandAffineMap(builder&: rewriter, loc: op.getLoc(), affineMap: op.getAffineMap(), |
| 342 | operands: llvm::to_vector<8>(Range: op.getOperands())); |
| 343 | if (!maybeExpandedMap) |
| 344 | return failure(); |
| 345 | rewriter.replaceOp(op, newValues: *maybeExpandedMap); |
| 346 | return success(); |
| 347 | } |
| 348 | }; |
| 349 | |
| 350 | /// Apply the affine map from an 'affine.load' operation to its operands, and |
| 351 | /// feed the results to a newly created 'memref.load' operation (which replaces |
| 352 | /// the original 'affine.load'). |
| 353 | class AffineLoadLowering : public OpRewritePattern<AffineLoadOp> { |
| 354 | public: |
| 355 | using OpRewritePattern<AffineLoadOp>::OpRewritePattern; |
| 356 | |
| 357 | LogicalResult matchAndRewrite(AffineLoadOp op, |
| 358 | PatternRewriter &rewriter) const override { |
| 359 | // Expand affine map from 'affineLoadOp'. |
| 360 | SmallVector<Value, 8> indices(op.getMapOperands()); |
| 361 | auto resultOperands = |
| 362 | expandAffineMap(builder&: rewriter, loc: op.getLoc(), affineMap: op.getAffineMap(), operands: indices); |
| 363 | if (!resultOperands) |
| 364 | return failure(); |
| 365 | |
| 366 | // Build memref.load memref[expandedMap.results]. |
| 367 | rewriter.replaceOpWithNewOp<memref::LoadOp>( |
| 368 | op, args: op.getMemRef(), args&: *resultOperands, |
| 369 | /*nontemporal=*/args: false, args: op.getMaybeAlign()); |
| 370 | return success(); |
| 371 | } |
| 372 | }; |
| 373 | |
| 374 | /// Apply the affine map from an 'affine.prefetch' operation to its operands, |
| 375 | /// and feed the results to a newly created 'memref.prefetch' operation (which |
| 376 | /// replaces the original 'affine.prefetch'). |
| 377 | class AffinePrefetchLowering : public OpRewritePattern<AffinePrefetchOp> { |
| 378 | public: |
| 379 | using OpRewritePattern<AffinePrefetchOp>::OpRewritePattern; |
| 380 | |
| 381 | LogicalResult matchAndRewrite(AffinePrefetchOp op, |
| 382 | PatternRewriter &rewriter) const override { |
| 383 | // Expand affine map from 'affinePrefetchOp'. |
| 384 | SmallVector<Value, 8> indices(op.getMapOperands()); |
| 385 | auto resultOperands = |
| 386 | expandAffineMap(builder&: rewriter, loc: op.getLoc(), affineMap: op.getAffineMap(), operands: indices); |
| 387 | if (!resultOperands) |
| 388 | return failure(); |
| 389 | |
| 390 | // Build memref.prefetch memref[expandedMap.results]. |
| 391 | rewriter.replaceOpWithNewOp<memref::PrefetchOp>( |
| 392 | op, args: op.getMemref(), args&: *resultOperands, args: op.getIsWrite(), |
| 393 | args: op.getLocalityHint(), args: op.getIsDataCache()); |
| 394 | return success(); |
| 395 | } |
| 396 | }; |
| 397 | |
| 398 | /// Apply the affine map from an 'affine.store' operation to its operands, and |
| 399 | /// feed the results to a newly created 'memref.store' operation (which replaces |
| 400 | /// the original 'affine.store'). |
| 401 | class AffineStoreLowering : public OpRewritePattern<AffineStoreOp> { |
| 402 | public: |
| 403 | using OpRewritePattern<AffineStoreOp>::OpRewritePattern; |
| 404 | |
| 405 | LogicalResult matchAndRewrite(AffineStoreOp op, |
| 406 | PatternRewriter &rewriter) const override { |
| 407 | // Expand affine map from 'affineStoreOp'. |
| 408 | SmallVector<Value, 8> indices(op.getMapOperands()); |
| 409 | auto maybeExpandedMap = |
| 410 | expandAffineMap(builder&: rewriter, loc: op.getLoc(), affineMap: op.getAffineMap(), operands: indices); |
| 411 | if (!maybeExpandedMap) |
| 412 | return failure(); |
| 413 | |
| 414 | // Build memref.store valueToStore, memref[expandedMap.results]. |
| 415 | rewriter.replaceOpWithNewOp<memref::StoreOp>( |
| 416 | op, args: op.getValueToStore(), args: op.getMemRef(), args&: *maybeExpandedMap, |
| 417 | /*nontemporal=*/args: false, args: op.getMaybeAlign()); |
| 418 | return success(); |
| 419 | } |
| 420 | }; |
| 421 | |
| 422 | /// Apply the affine maps from an 'affine.dma_start' operation to each of their |
| 423 | /// respective map operands, and feed the results to a newly created |
| 424 | /// 'memref.dma_start' operation (which replaces the original |
| 425 | /// 'affine.dma_start'). |
| 426 | class AffineDmaStartLowering : public OpRewritePattern<AffineDmaStartOp> { |
| 427 | public: |
| 428 | using OpRewritePattern<AffineDmaStartOp>::OpRewritePattern; |
| 429 | |
| 430 | LogicalResult matchAndRewrite(AffineDmaStartOp op, |
| 431 | PatternRewriter &rewriter) const override { |
| 432 | SmallVector<Value, 8> operands(op.getOperands()); |
| 433 | auto operandsRef = llvm::ArrayRef(operands); |
| 434 | |
| 435 | // Expand affine map for DMA source memref. |
| 436 | auto maybeExpandedSrcMap = expandAffineMap( |
| 437 | builder&: rewriter, loc: op.getLoc(), affineMap: op.getSrcMap(), |
| 438 | operands: operandsRef.drop_front(N: op.getSrcMemRefOperandIndex() + 1)); |
| 439 | if (!maybeExpandedSrcMap) |
| 440 | return failure(); |
| 441 | // Expand affine map for DMA destination memref. |
| 442 | auto maybeExpandedDstMap = expandAffineMap( |
| 443 | builder&: rewriter, loc: op.getLoc(), affineMap: op.getDstMap(), |
| 444 | operands: operandsRef.drop_front(N: op.getDstMemRefOperandIndex() + 1)); |
| 445 | if (!maybeExpandedDstMap) |
| 446 | return failure(); |
| 447 | // Expand affine map for DMA tag memref. |
| 448 | auto maybeExpandedTagMap = expandAffineMap( |
| 449 | builder&: rewriter, loc: op.getLoc(), affineMap: op.getTagMap(), |
| 450 | operands: operandsRef.drop_front(N: op.getTagMemRefOperandIndex() + 1)); |
| 451 | if (!maybeExpandedTagMap) |
| 452 | return failure(); |
| 453 | |
| 454 | // Build memref.dma_start operation with affine map results. |
| 455 | rewriter.replaceOpWithNewOp<memref::DmaStartOp>( |
| 456 | op, args: op.getSrcMemRef(), args&: *maybeExpandedSrcMap, args: op.getDstMemRef(), |
| 457 | args&: *maybeExpandedDstMap, args: op.getNumElements(), args: op.getTagMemRef(), |
| 458 | args&: *maybeExpandedTagMap, args: op.getStride(), args: op.getNumElementsPerStride()); |
| 459 | return success(); |
| 460 | } |
| 461 | }; |
| 462 | |
| 463 | /// Apply the affine map from an 'affine.dma_wait' operation tag memref, |
| 464 | /// and feed the results to a newly created 'memref.dma_wait' operation (which |
| 465 | /// replaces the original 'affine.dma_wait'). |
| 466 | class AffineDmaWaitLowering : public OpRewritePattern<AffineDmaWaitOp> { |
| 467 | public: |
| 468 | using OpRewritePattern<AffineDmaWaitOp>::OpRewritePattern; |
| 469 | |
| 470 | LogicalResult matchAndRewrite(AffineDmaWaitOp op, |
| 471 | PatternRewriter &rewriter) const override { |
| 472 | // Expand affine map for DMA tag memref. |
| 473 | SmallVector<Value, 8> indices(op.getTagIndices()); |
| 474 | auto maybeExpandedTagMap = |
| 475 | expandAffineMap(builder&: rewriter, loc: op.getLoc(), affineMap: op.getTagMap(), operands: indices); |
| 476 | if (!maybeExpandedTagMap) |
| 477 | return failure(); |
| 478 | |
| 479 | // Build memref.dma_wait operation with affine map results. |
| 480 | rewriter.replaceOpWithNewOp<memref::DmaWaitOp>( |
| 481 | op, args: op.getTagMemRef(), args&: *maybeExpandedTagMap, args: op.getNumElements()); |
| 482 | return success(); |
| 483 | } |
| 484 | }; |
| 485 | |
| 486 | /// Apply the affine map from an 'affine.vector_load' operation to its operands, |
| 487 | /// and feed the results to a newly created 'vector.load' operation (which |
| 488 | /// replaces the original 'affine.vector_load'). |
| 489 | class AffineVectorLoadLowering : public OpRewritePattern<AffineVectorLoadOp> { |
| 490 | public: |
| 491 | using OpRewritePattern<AffineVectorLoadOp>::OpRewritePattern; |
| 492 | |
| 493 | LogicalResult matchAndRewrite(AffineVectorLoadOp op, |
| 494 | PatternRewriter &rewriter) const override { |
| 495 | // Expand affine map from 'affineVectorLoadOp'. |
| 496 | SmallVector<Value, 8> indices(op.getMapOperands()); |
| 497 | auto resultOperands = |
| 498 | expandAffineMap(builder&: rewriter, loc: op.getLoc(), affineMap: op.getAffineMap(), operands: indices); |
| 499 | if (!resultOperands) |
| 500 | return failure(); |
| 501 | |
| 502 | // Build vector.load memref[expandedMap.results]. |
| 503 | rewriter.replaceOpWithNewOp<vector::LoadOp>( |
| 504 | op, args: op.getVectorType(), args: op.getMemRef(), args&: *resultOperands, |
| 505 | /*nontemporal=*/args: false, args: op.getMaybeAlign()); |
| 506 | return success(); |
| 507 | } |
| 508 | }; |
| 509 | |
| 510 | /// Apply the affine map from an 'affine.vector_store' operation to its |
| 511 | /// operands, and feed the results to a newly created 'vector.store' operation |
| 512 | /// (which replaces the original 'affine.vector_store'). |
| 513 | class AffineVectorStoreLowering : public OpRewritePattern<AffineVectorStoreOp> { |
| 514 | public: |
| 515 | using OpRewritePattern<AffineVectorStoreOp>::OpRewritePattern; |
| 516 | |
| 517 | LogicalResult matchAndRewrite(AffineVectorStoreOp op, |
| 518 | PatternRewriter &rewriter) const override { |
| 519 | // Expand affine map from 'affineVectorStoreOp'. |
| 520 | SmallVector<Value, 8> indices(op.getMapOperands()); |
| 521 | auto maybeExpandedMap = |
| 522 | expandAffineMap(builder&: rewriter, loc: op.getLoc(), affineMap: op.getAffineMap(), operands: indices); |
| 523 | if (!maybeExpandedMap) |
| 524 | return failure(); |
| 525 | |
| 526 | rewriter.replaceOpWithNewOp<vector::StoreOp>( |
| 527 | op, args: op.getValueToStore(), args: op.getMemRef(), args&: *maybeExpandedMap, |
| 528 | /*nontemporal=*/args: false, args: op.getMaybeAlign()); |
| 529 | return success(); |
| 530 | } |
| 531 | }; |
| 532 | |
| 533 | } // namespace |
| 534 | |
| 535 | void mlir::populateAffineToStdConversionPatterns(RewritePatternSet &patterns) { |
| 536 | // clang-format off |
| 537 | patterns.add< |
| 538 | AffineApplyLowering, |
| 539 | AffineDmaStartLowering, |
| 540 | AffineDmaWaitLowering, |
| 541 | AffineLoadLowering, |
| 542 | AffineMinLowering, |
| 543 | AffineMaxLowering, |
| 544 | AffineParallelLowering, |
| 545 | AffinePrefetchLowering, |
| 546 | AffineStoreLowering, |
| 547 | AffineForLowering, |
| 548 | AffineIfLowering, |
| 549 | AffineYieldOpLowering>(arg: patterns.getContext()); |
| 550 | // clang-format on |
| 551 | } |
| 552 | |
| 553 | void mlir::populateAffineToVectorConversionPatterns( |
| 554 | RewritePatternSet &patterns) { |
| 555 | // clang-format off |
| 556 | patterns.add< |
| 557 | AffineVectorLoadLowering, |
| 558 | AffineVectorStoreLowering>(arg: patterns.getContext()); |
| 559 | // clang-format on |
| 560 | } |
| 561 | |
| 562 | namespace { |
| 563 | class LowerAffine : public impl::LowerAffinePassBase<LowerAffine> { |
| 564 | void runOnOperation() override { |
| 565 | RewritePatternSet patterns(&getContext()); |
| 566 | populateAffineToStdConversionPatterns(patterns); |
| 567 | populateAffineToVectorConversionPatterns(patterns); |
| 568 | populateAffineExpandIndexOpsPatterns(patterns); |
| 569 | ConversionTarget target(getContext()); |
| 570 | target.addLegalDialect<arith::ArithDialect, memref::MemRefDialect, |
| 571 | scf::SCFDialect, VectorDialect>(); |
| 572 | if (failed(Result: applyPartialConversion(op: getOperation(), target, |
| 573 | patterns: std::move(patterns)))) |
| 574 | signalPassFailure(); |
| 575 | } |
| 576 | }; |
| 577 | } // namespace |
| 578 | |