@@ -494,145 +494,6 @@ LogicalResult mlir::linalg::applyStagedPatterns(
494
494
return success ();
495
495
}
496
496
497
- // / Traverse the `dims` and substitute known min or max expressions returned by
498
- // / the lambda |getMinMaxExpr|.
499
- static AffineMap substitute (AffineMap map, SmallVectorImpl<Value> &dims,
500
- SmallVectorImpl<Value> &symbols,
501
- GetMinMaxExprFn getMinMaxExpr) {
502
- auto exprs = llvm::to_vector<4 >(map.getResults ());
503
- for (AffineExpr &expr : exprs) {
504
- bool substituted = true ;
505
- while (substituted) {
506
- substituted = false ;
507
- for (unsigned dimIdx = 0 ; dimIdx < dims.size (); ++dimIdx) {
508
- Value dim = dims[dimIdx];
509
- auto minMax = getMinMaxExpr (dim, dims, symbols);
510
- if (!minMax)
511
- continue ;
512
- AffineExpr dimExpr = getAffineDimExpr (dimIdx, expr.getContext ());
513
- LLVM_DEBUG (DBGS () << " Subst: " << dim << " @ " << dimExpr << " \n " );
514
- LLVM_DEBUG (DBGS () << " Before: " << expr << " \n " );
515
- // Substitute occurrences of `dimExpr` by either the min expression or
516
- // the max expression depending on whether the value is used with a
517
- // positive or negative coefficient.
518
- AffineExpr substitutedExpr =
519
- substWithMin (expr, dimExpr, minMax->first , minMax->second );
520
- LLVM_DEBUG (DBGS () << " After: " << substitutedExpr << " \n " );
521
- substituted = (substitutedExpr != expr);
522
- expr = substitutedExpr;
523
- }
524
- }
525
-
526
- // Cleanup and simplify the results.
527
- // This needs to happen outside of the loop iterating on dims.size() since
528
- // it modifies dims.
529
- SmallVector<Value, 4 > operands (dims.begin (), dims.end ());
530
- operands.append (symbols.begin (), symbols.end ());
531
- auto map = AffineMap::get (dims.size (), symbols.size (), exprs,
532
- exprs.front ().getContext ());
533
-
534
- LLVM_DEBUG ({
535
- DBGS () << " Map to simplify: " << map << " \n " ;
536
- DBGS () << " Operands:\n " ;
537
- for (Value v : operands)
538
- DBGS () << v << " \n " ;
539
- });
540
-
541
- // Pull in affine.apply operations and compose them fully into the
542
- // result.
543
- fullyComposeAffineMapAndOperands (&map, &operands);
544
- canonicalizeMapAndOperands (&map, &operands);
545
- map = simplifyAffineMap (map);
546
- // Assign the results.
547
- exprs.assign (map.getResults ().begin (), map.getResults ().end ());
548
- dims.assign (operands.begin (), operands.begin () + map.getNumDims ());
549
- symbols.assign (operands.begin () + map.getNumDims (), operands.end ());
550
-
551
- LLVM_DEBUG (DBGS () << " Map simplified: " << map << " \n " );
552
- }
553
-
554
- assert (!exprs.empty () && " Unexpected empty exprs" );
555
- return AffineMap::get (dims.size (), symbols.size (), exprs, map.getContext ());
556
- }
557
-
558
- // / Traverse the dims of the AffineMap of `affineMinOp` and substitute
559
- // / dimensions with known range by new expressions involving the min or max
560
- // / expression:
561
- // / - If the AffineDimExpr mapped to a known value has a positive sign, it
562
- // / is replaced by the min expression.
563
- // / - If the AffineDimExpr mapped to a known value has a negative sign, it is
564
- // / replaced by the max expression.
565
- // / All known values are iteratively replaced.
566
- // / This is used as an intermediate step in computing bounding boxes and
567
- // / canonicalize AffineMinOps. All dim and symbol operands are assumed to have
568
- // / positive values (positive orthant assumptions).
569
- // / Return a new AffineMap, dims and symbols that have been canonicalized and
570
- // / simplified.
571
- AffineMapAndOperands
572
- mlir::linalg::substituteMin (AffineMinOp affineMinOp,
573
- GetMinMaxExprFn getMinMaxExpr) {
574
- AffineMapAndOperands res{affineMinOp.getAffineMap (),
575
- SmallVector<Value>(affineMinOp.getDimOperands ()),
576
- SmallVector<Value>(affineMinOp.getSymbolOperands ())};
577
- res.map = substitute (affineMinOp.getAffineMap (), res.dims , res.symbols ,
578
- getMinMaxExpr);
579
- return res;
580
- }
581
-
582
- LogicalResult AffineMinRangeCanonicalizationPattern::matchAndRewrite (
583
- AffineMinOp minOp, PatternRewriter &rewriter) const {
584
- LLVM_DEBUG (DBGS () << " Canonicalize AffineMinSCF: " << *minOp.getOperation ()
585
- << " \n " );
586
-
587
- auto affineMapAndOperands = substituteMin (minOp, getMinMaxFn);
588
- AffineMap map = affineMapAndOperands.map ;
589
-
590
- LLVM_DEBUG (DBGS () << " Resulting map: " << map << " \n " );
591
-
592
- // Check whether any of the expressions, when subtracted from all other
593
- // expressions, produces only >= 0 constants. If so, it is the min.
594
- for (auto e : minOp.getAffineMap ().getResults ()) {
595
- LLVM_DEBUG (DBGS () << " Candidate min: " << e << " \n " );
596
- if (!e.isSymbolicOrConstant ())
597
- continue ;
598
-
599
- auto isNonPositive = [](AffineExpr e) {
600
- if (auto cst = e.dyn_cast <AffineConstantExpr>())
601
- return cst.getValue () < 0 ;
602
- return true ;
603
- };
604
-
605
- // Build the subMap and check everything is statically known to be
606
- // positive.
607
- SmallVector<AffineExpr, 4 > subExprs;
608
- subExprs.reserve (map.getNumResults ());
609
- for (auto ee : map.getResults ())
610
- subExprs.push_back (ee - e);
611
- MLIRContext *ctx = minOp.getContext ();
612
- AffineMap subMap = simplifyAffineMap (
613
- AffineMap::get (map.getNumDims (), map.getNumSymbols (), subExprs, ctx));
614
- LLVM_DEBUG (DBGS () << " simplified subMap: " << subMap << " \n " );
615
- if (llvm::any_of (subMap.getResults (), isNonPositive))
616
- continue ;
617
-
618
- // Static min found.
619
- if (auto cst = e.dyn_cast <AffineConstantExpr>()) {
620
- rewriter.replaceOpWithNewOp <ConstantIndexOp>(minOp, cst.getValue ());
621
- } else {
622
- auto resultMap = AffineMap::get (0 , map.getNumSymbols (), {e}, ctx);
623
- SmallVector<Value> resultOperands = affineMapAndOperands.dims ;
624
- llvm::append_range (resultOperands, affineMapAndOperands.symbols );
625
- canonicalizeMapAndOperands (&resultMap, &resultOperands);
626
- resultMap = simplifyAffineMap (resultMap);
627
- rewriter.replaceOpWithNewOp <AffineApplyOp>(minOp, resultMap,
628
- resultOperands);
629
- }
630
- return success ();
631
- }
632
-
633
- return failure ();
634
- }
635
-
636
497
static SmallVector<StringRef> getNParallelLoopsAttrs (unsigned nParallelLoops) {
637
498
return SmallVector<StringRef>(nParallelLoops, getParallelIteratorTypeName ());
638
499
}
0 commit comments