[mlir][NFC] Clean up builder usage around constants/non-foldable ops
authorMatthias Springer <me@m-sp.org>
Fri, 30 Jun 2023 10:44:18 +0000 (12:44 +0200)
committerMatthias Springer <me@m-sp.org>
Fri, 30 Jun 2023 11:56:42 +0000 (13:56 +0200)
* Use `create` instead of `createOrFold` for constant ops. Constants cannot be folded any further.
* Use `create` instead of `createOrFold` for ops that do not have a folder.
* Use C++ op builders that take an `int` instead of creating a `ConstantIndexOp`.
* Create `tensor::DimOp` instead of `linalg::createOrFoldDimOp` when it is certain that the operand is a tensor.

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

mlir/lib/Conversion/AMDGPUToROCDL/AMDGPUToROCDL.cpp
mlir/lib/Conversion/BufferizationToMemRef/BufferizationToMemRef.cpp
mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp
mlir/lib/Dialect/GPU/Transforms/MemoryPromotion.cpp
mlir/lib/Dialect/Linalg/Transforms/Promotion.cpp
mlir/lib/Dialect/MemRef/Transforms/ResolveShapedTypeResultDims.cpp

index e764c8d..0fc1aae 100644 (file)
@@ -29,7 +29,7 @@ using namespace mlir::amdgpu;
 static Value createI32Constant(ConversionPatternRewriter &rewriter,
                                Location loc, int32_t value) {
   Type llvmI32 = rewriter.getI32Type();
-  return rewriter.createOrFold<LLVM::ConstantOp>(loc, llvmI32, value);
+  return rewriter.create<LLVM::ConstantOp>(loc, llvmI32, value);
 }
 
 namespace {
@@ -317,14 +317,14 @@ static Value mfmaConcatIfNeeded(ConversionPatternRewriter &rewriter,
       return input;
     int64_t numBytes = vectorType.getNumElements();
     Type destType = rewriter.getIntegerType(numBytes * 8);
-    Value result = rewriter.createOrFold<LLVM::ConstantOp>(
+    Value result = rewriter.create<LLVM::ConstantOp>(
         loc, destType, rewriter.getIntegerAttr(destType, 0));
     for (int64_t i = 0; i < numBytes; ++i) {
       Value idxConst = createI32Constant(rewriter, loc, i);
       Value element =
           rewriter.create<LLVM::ExtractElementOp>(loc, input, idxConst);
       Value extended = rewriter.create<LLVM::ZExtOp>(loc, destType, element);
-      Value shiftConst = rewriter.createOrFold<LLVM::ConstantOp>(
+      Value shiftConst = rewriter.create<LLVM::ConstantOp>(
           loc, destType, rewriter.getIntegerAttr(destType, i * 8));
       Value shifted = rewriter.create<LLVM::ShlOp>(loc, extended, shiftConst);
       result = rewriter.create<LLVM::OrOp>(loc, result, shifted);
index f498d2c..a01fc9f 100644 (file)
@@ -62,9 +62,7 @@ struct CloneOpConversion : public OpConversionPattern<bufferization::CloneOp> {
     for (int i = 0; i < memrefType.getRank(); ++i) {
       if (!memrefType.isDynamicDim(i))
         continue;
-      Value size = rewriter.createOrFold<arith::ConstantIndexOp>(loc, i);
-      Value dim =
-          rewriter.createOrFold<memref::DimOp>(loc, op.getInput(), size);
+      Value dim = rewriter.createOrFold<memref::DimOp>(loc, op.getInput(), i);
       dynamicOperands.push_back(dim);
     }
 
index 3c1a63b..a2dfa0b 100644 (file)
@@ -2124,8 +2124,8 @@ struct RFFT2dConverter final : public OpRewritePattern<RFFT2dOp> {
         affineDimsExpr(rewriter, 0, 1, 2)});
 
     // Width and height dimensions of the original input.
-    auto dimH = linalg::createOrFoldDimOp(rewriter, loc, input, 1);
-    auto dimW = linalg::createOrFoldDimOp(rewriter, loc, input, 2);
+    auto dimH = rewriter.createOrFold<tensor::DimOp>(loc, input, 1);
+    auto dimW = rewriter.createOrFold<tensor::DimOp>(loc, input, 2);
 
     // Constants and dimension sizes
     auto twoPiAttr = rewriter.getFloatAttr(elementType, 6.283185307179586);
index 4eb8231..2984b0f 100644 (file)
@@ -71,8 +71,7 @@ mlir::bufferization::castOrReallocMemRefValue(OpBuilder &b, Value value,
   for (int i = 0; i < destType.getRank(); ++i) {
     if (destType.getShape()[i] != ShapedType::kDynamic)
       continue;
-    auto index = b.createOrFold<arith::ConstantIndexOp>(loc, i);
-    Value size = b.create<memref::DimOp>(loc, value, index);
+    Value size = b.create<memref::DimOp>(loc, value, i);
     dynamicOperands.push_back(size);
   }
   // TODO: Use alloc/memcpy callback from BufferizationOptions if called via
index 21de15e..14c44f2 100644 (file)
@@ -51,8 +51,7 @@ static void insertCopyLoops(ImplicitLocOpBuilder &b, Value from, Value to) {
   ubs.reserve(lbs.size());
   steps.reserve(lbs.size());
   for (auto idx = 0; idx < rank; ++idx) {
-    ubs.push_back(b.createOrFold<memref::DimOp>(
-        from, b.create<arith::ConstantIndexOp>(idx)));
+    ubs.push_back(b.createOrFold<memref::DimOp>(from, idx));
     steps.push_back(one);
   }
 
index e952f94..319f73b 100644 (file)
@@ -59,11 +59,11 @@ static Value allocBuffer(ImplicitLocOpBuilder &b,
     auto staticBufferType =
         MemRefType::get(width * cst.value(), b.getIntegerType(8));
     if (options.useAlloca) {
-      return b.createOrFold<memref::AllocaOp>(staticBufferType, ValueRange{},
-                                              alignmentAttr);
+      return b.create<memref::AllocaOp>(staticBufferType, ValueRange{},
+                                        alignmentAttr);
     }
-    return b.createOrFold<memref::AllocOp>(staticBufferType, ValueRange{},
-                                           alignmentAttr);
+    return b.create<memref::AllocOp>(staticBufferType, ValueRange{},
+                                     alignmentAttr);
   }
 
   // Fallback dynamic buffer.
@@ -86,8 +86,8 @@ static std::optional<Value> defaultAllocBufferCallBack(
     std::optional<unsigned> alignment, DataLayout &layout) {
   ShapedType viewType = subView.getType();
   ImplicitLocOpBuilder b(subView.getLoc(), builder);
-  auto zero = b.createOrFold<arith::ConstantIndexOp>(0);
-  auto one = b.createOrFold<arith::ConstantIndexOp>(1);
+  auto zero = b.create<arith::ConstantIndexOp>(0);
+  auto one = b.create<arith::ConstantIndexOp>(1);
 
   Value allocSize = one;
   for (const auto &size : llvm::enumerate(boundingSubViewSize))
index 9e5fc73..f18ae2c 100644 (file)
@@ -68,7 +68,7 @@ struct DimOfShapedTypeOpInterface : public OpRewritePattern<OpTy> {
     Location loc = dimOp->getLoc();
     rewriter.replaceOpWithNewOp<tensor::ExtractOp>(
         dimOp, resultShape,
-        rewriter.createOrFold<arith::ConstantIndexOp>(loc, *dimIndex));
+        rewriter.create<arith::ConstantIndexOp>(loc, *dimIndex).getResult());
     return success();
   }
 };