[mlir] Remove `materializeOpFoldResult` functions.
authorAlexander Belyaev <pifon@google.com>
Wed, 7 Sep 2022 08:10:48 +0000 (10:10 +0200)
committerAlexander Belyaev <pifon@google.com>
Wed, 7 Sep 2022 08:22:42 +0000 (10:22 +0200)
We can use `getValueOrCreateConstantIndexOp` instead.

Differential Revision: https://reviews.llvm.org/D133403

mlir/include/mlir/Dialect/Linalg/Utils/Utils.h
mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp
mlir/lib/Dialect/Linalg/Transforms/Promotion.cpp
mlir/lib/Dialect/Linalg/Transforms/Tiling.cpp
mlir/lib/Dialect/Linalg/Utils/Utils.cpp

index 510eab3..20164d7 100644 (file)
@@ -187,15 +187,6 @@ SmallVector<Value> insertSlicesBack(OpBuilder &builder, Location loc,
                                     LinalgOp op, ValueRange operands,
                                     ValueRange results);
 
-/// Turns an OpFoldResult into a value, creating an index-typed constant if
-/// necessary.
-Value materializeOpFoldResult(ImplicitLocOpBuilder &builder,
-                              OpFoldResult opFoldResult);
-Value materializeOpFoldResult(OpBuilder &b, Location loc,
-                              OpFoldResult opFoldResult);
-Value materializeOpFoldResult(OpBuilder &b, Location loc,
-                              ArrayRef<OpFoldResult> opFoldResults);
-
 /// A struct containg offsets-sizes-strides arguments of the tiled shape.
 struct SliceParameters {
   SmallVector<OpFoldResult> offsets;
index d63b8f6..97dd0c4 100644 (file)
@@ -13,9 +13,9 @@
 #include "mlir/Dialect/Linalg/Passes.h"
 
 #include "mlir/Dialect/Affine/IR/AffineOps.h"
+#include "mlir/Dialect/Arithmetic/Utils/Utils.h"
 #include "mlir/Dialect/Linalg/IR/Linalg.h"
 #include "mlir/Dialect/Linalg/Transforms/Transforms.h"
-#include "mlir/Dialect/Linalg/Utils/Utils.h"
 #include "mlir/Dialect/SparseTensor/IR/SparseTensor.h"
 #include "mlir/IR/AffineExpr.h"
 #include "mlir/IR/AffineMap.h"
@@ -1453,7 +1453,7 @@ static FailureOr<SmallVector<Value>> collapseGenericOpIterationDims(
     rewriter.setInsertionPoint(collapsedGenericOp);
     SmallVector<Value> loopBound =
         llvm::to_vector(llvm::map_range(loopRanges, [&](Range range) {
-          return materializeOpFoldResult(rewriter, loc, range.size);
+          return getValueOrCreateConstantIndexOp(rewriter, loc, range.size);
         }));
     generateCollapsedIndexingRegion(loc,
                                     &collapsedGenericOp->getRegion(0).front(),
index 79ef14e..4eb4eea 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
+#include "mlir/Dialect/Arithmetic/Utils/Utils.h"
 #include "mlir/Dialect/Complex/IR/Complex.h"
 #include "mlir/Dialect/Linalg/IR/Linalg.h"
 #include "mlir/Dialect/Linalg/Passes.h"
 #include "mlir/Dialect/Linalg/Transforms/Transforms.h"
-#include "mlir/Dialect/Linalg/Utils/Utils.h"
 #include "mlir/Dialect/SCF/IR/SCF.h"
 #include "mlir/IR/AffineExpr.h"
 #include "mlir/IR/AffineExprVisitor.h"
@@ -227,9 +227,10 @@ FailureOr<PromotionInfo> mlir::linalg::promoteSubviewAsNewBuffer(
     LLVM_DEBUG(llvm::dbgs() << "Extract tightest: " << rangeValue.size << "\n");
     Value size;
     if (auto attr = rangeValue.size.dyn_cast<Attribute>()) {
-      size = materializeOpFoldResult(b, loc, rangeValue.size);
+      size = getValueOrCreateConstantIndexOp(b, loc, rangeValue.size);
     } else {
-      Value materializedSize = materializeOpFoldResult(b, loc, rangeValue.size);
+      Value materializedSize =
+          getValueOrCreateConstantIndexOp(b, loc, rangeValue.size);
       FailureOr<int64_t> upperBound =
           getConstantUpperBoundForIndex(materializedSize);
       size = failed(upperBound)
index 3a49dcb..1110b05 100644 (file)
@@ -18,7 +18,6 @@
 #include "mlir/Dialect/Func/IR/FuncOps.h"
 #include "mlir/Dialect/Linalg/IR/Linalg.h"
 #include "mlir/Dialect/Linalg/Transforms/Transforms.h"
-#include "mlir/Dialect/Linalg/Utils/Utils.h"
 #include "mlir/Dialect/MemRef/IR/MemRef.h"
 #include "mlir/Dialect/SCF/Transforms/Transforms.h"
 #include "mlir/Dialect/Tensor/IR/Tensor.h"
@@ -123,13 +122,15 @@ mlir::linalg::computeMultiTileSizes(OpBuilder &builder, LinalgOp op,
     return failure();
 
   // The code below works only on values.
-  ImplicitLocOpBuilder b(op.getLoc(), builder);
+  Location loc = op.getLoc();
+  ImplicitLocOpBuilder b(loc, builder);
   if (emitAssertions) {
     emitIsPositiveIndexAssertion(b, targetSize);
     emitIsPositiveIndexAssertion(b, divisor);
   }
-  Value targetSizeValue = materializeOpFoldResult(b, targetSize);
-  Value divisorValue = materializeOpFoldResult(b, divisor);
+  Value targetSizeValue =
+      getValueOrCreateConstantIndexOp(builder, loc, targetSize);
+  Value divisorValue = getValueOrCreateConstantIndexOp(builder, loc, divisor);
 
   // Find the trip count of the iteration space dimension for which the tile
   // sizes are computed.
@@ -140,7 +141,7 @@ mlir::linalg::computeMultiTileSizes(OpBuilder &builder, LinalgOp op,
       makeComposedFoldedMultiResultAffineApply(b, op.getLoc(), shapesToLoops,
                                                allShapes);
   Value tripCount =
-      materializeOpFoldResult(b, op.getLoc(), loopRanges[dimension]);
+      getValueOrCreateConstantIndexOp(b, op.getLoc(), loopRanges[dimension]);
 
   // Compute the tile sizes and the respective numbers of tiles.
   AffineExpr s0 = b.getAffineSymbolExpr(0);
@@ -245,8 +246,7 @@ static FailureOr<ForeachThreadTilingResult> tileToForeachThreadOpImpl(
       }));
   SmallVector<Value> materializedNonZeroNumThreads =
       llvm::to_vector(llvm::map_range(nonZeroNumThreads, [&](OpFoldResult ofr) {
-        ImplicitLocOpBuilder ilocb(loc, b);
-        return materializeOpFoldResult(ilocb, ofr);
+        return getValueOrCreateConstantIndexOp(b, loc, ofr);
       }));
 
   Operation *tiledOp = nullptr;
@@ -618,9 +618,12 @@ static LogicalResult tilePadOp(RewriterBase &builder, tensor::PadOp op,
   for (int64_t i = 0; i < rank; ++i) {
     allDims.push_back(ranges[i].size);
     if (!isZero(tileSizes[i])) {
-      lbs.push_back(materializeOpFoldResult(builder, loc, ranges[i].offset));
-      dims.push_back(materializeOpFoldResult(builder, loc, ranges[i].size));
-      steps.push_back(materializeOpFoldResult(builder, loc, tileSizes[i]));
+      lbs.push_back(
+          getValueOrCreateConstantIndexOp(builder, loc, ranges[i].offset));
+      dims.push_back(
+          getValueOrCreateConstantIndexOp(builder, loc, ranges[i].size));
+      steps.push_back(
+          getValueOrCreateConstantIndexOp(builder, loc, tileSizes[i]));
     }
   }
   // Generate loop nest: One loop per dimension.
index 19db66a..2c43045 100644 (file)
@@ -140,9 +140,11 @@ static void unpackRanges(OpBuilder &builder, Location loc,
                          SmallVectorImpl<Value> &ubs,
                          SmallVectorImpl<Value> &steps) {
   for (Range range : ranges) {
-    lbs.emplace_back(materializeOpFoldResult(builder, loc, range.offset));
-    ubs.emplace_back(materializeOpFoldResult(builder, loc, range.size));
-    steps.emplace_back(materializeOpFoldResult(builder, loc, range.stride));
+    lbs.emplace_back(
+        getValueOrCreateConstantIndexOp(builder, loc, range.offset));
+    ubs.emplace_back(getValueOrCreateConstantIndexOp(builder, loc, range.size));
+    steps.emplace_back(
+        getValueOrCreateConstantIndexOp(builder, loc, range.stride));
   }
 }
 
@@ -929,33 +931,6 @@ SmallVector<Value> insertSlicesBack(OpBuilder &builder, Location loc,
   return tensorResults;
 }
 
-Value materializeOpFoldResult(ImplicitLocOpBuilder &builder,
-                              OpFoldResult opFoldResult) {
-  if (!opFoldResult)
-    return nullptr;
-
-  if (auto value = opFoldResult.dyn_cast<Value>())
-    return value;
-  auto attr = opFoldResult.get<Attribute>().cast<IntegerAttr>();
-  return builder.create<arith::ConstantIndexOp>(attr.getValue().getSExtValue());
-}
-
-Value materializeOpFoldResult(OpBuilder &builder, Location loc,
-                              OpFoldResult opFoldResult) {
-  ImplicitLocOpBuilder b(loc, builder);
-  return materializeOpFoldResult(b, opFoldResult);
-}
-
-SmallVector<Value>
-materializeOpFoldResults(OpBuilder &builder, Location loc,
-                         ArrayRef<OpFoldResult> opFoldResults) {
-  ImplicitLocOpBuilder b(loc, builder);
-  SmallVector<Value> values;
-  for (const auto &opFoldResult : opFoldResults)
-    values.push_back(materializeOpFoldResult(b, opFoldResult));
-  return values;
-}
-
 SmallVector<Optional<SliceParameters>>
 computeAllSliceParameters(OpBuilder &builder, Location loc, LinalgOp linalgOp,
                           ValueRange valuesToTile, ArrayRef<OpFoldResult> ivs,
@@ -1046,7 +1021,8 @@ void offsetIndices(RewriterBase &b, LinalgOp linalgOp,
     OpFoldResult applied = makeComposedFoldedAffineApply(
         b, indexOp.getLoc(), index + offset,
         {getAsOpFoldResult(indexOp.getResult()), offsets[indexOp.getDim()]});
-    Value materialized = materializeOpFoldResult(b, indexOp.getLoc(), applied);
+    Value materialized =
+        getValueOrCreateConstantIndexOp(b, indexOp.getLoc(), applied);
     b.replaceOpWithIf(indexOp, materialized, [&](OpOperand &use) {
       return use.getOwner() != materialized.getDefiningOp();
     });