@@ -271,20 +271,41 @@ Value mlir::lowerAffineLowerBound(AffineForOp op, OpBuilder &builder) {
271
271
builder);
272
272
}
273
273
274
+ // Emit instructions that correspond to computing the minimum value amoung the
275
+ // values of a (potentially) multi-output affine map applied to `operands`.
276
+ static Value lowerAffineMapMin (OpBuilder &builder, Location loc, AffineMap map,
277
+ ValueRange operands) {
278
+ if (auto values =
279
+ expandAffineMap (builder, loc, map, llvm::to_vector<4 >(operands)))
280
+ return buildMinMaxReductionSeq (loc, CmpIPredicate::slt, *values, builder);
281
+ return nullptr ;
282
+ }
283
+
274
284
// Emit instructions that correspond to the affine map in the upper bound
275
285
// applied to the respective operands, and compute the minimum value across
276
286
// the results.
277
287
Value mlir::lowerAffineUpperBound (AffineForOp op, OpBuilder &builder) {
278
- SmallVector<Value, 8 > boundOperands (op.getUpperBoundOperands ());
279
- auto ubValues = expandAffineMap (builder, op.getLoc (), op.getUpperBoundMap (),
280
- boundOperands);
281
- if (!ubValues)
282
- return nullptr ;
283
- return buildMinMaxReductionSeq (op.getLoc (), CmpIPredicate::slt, *ubValues,
284
- builder);
288
+ return lowerAffineMapMin (builder, op.getLoc (), op.getUpperBoundMap (),
289
+ op.getUpperBoundOperands ());
285
290
}
286
291
287
292
namespace {
293
+ class AffineMinLowering : public OpRewritePattern <AffineMinOp> {
294
+ public:
295
+ using OpRewritePattern<AffineMinOp>::OpRewritePattern;
296
+
297
+ PatternMatchResult matchAndRewrite (AffineMinOp op,
298
+ PatternRewriter &rewriter) const override {
299
+ Value reduced =
300
+ lowerAffineMapMin (rewriter, op.getLoc (), op.map (), op.operands ());
301
+ if (!reduced)
302
+ return matchFailure ();
303
+
304
+ rewriter.replaceOp (op, reduced);
305
+ return matchSuccess ();
306
+ }
307
+ };
308
+
288
309
// Affine terminators are removed.
289
310
class AffineTerminatorLowering : public OpRewritePattern <AffineTerminatorOp> {
290
311
public:
@@ -520,10 +541,19 @@ class AffineDmaWaitLowering : public OpRewritePattern<AffineDmaWaitOp> {
520
541
521
542
void mlir::populateAffineToStdConversionPatterns (
522
543
OwningRewritePatternList &patterns, MLIRContext *ctx) {
544
+ // clang-format off
523
545
patterns.insert <
524
- AffineApplyLowering, AffineDmaStartLowering, AffineDmaWaitLowering,
525
- AffineLoadLowering, AffinePrefetchLowering, AffineStoreLowering,
526
- AffineForLowering, AffineIfLowering, AffineTerminatorLowering>(ctx);
546
+ AffineApplyLowering,
547
+ AffineDmaStartLowering,
548
+ AffineDmaWaitLowering,
549
+ AffineLoadLowering,
550
+ AffineMinLowering,
551
+ AffinePrefetchLowering,
552
+ AffineStoreLowering,
553
+ AffineForLowering,
554
+ AffineIfLowering,
555
+ AffineTerminatorLowering>(ctx);
556
+ // clang-format on
527
557
}
528
558
529
559
namespace {
0 commit comments