[Mlir-commits] [mlir] 89d8035 - Use llvm::append_range where applicable

Benjamin Kramer llvmlistbot at llvm.org
Fri Mar 18 12:07:05 PDT 2022


Author: Benjamin Kramer
Date: 2022-03-18T20:05:48+01:00
New Revision: 89d8035e36c927bd3cb88dba49b9f97d6c9dbc68

URL: https://github.com/llvm/llvm-project/commit/89d8035e36c927bd3cb88dba49b9f97d6c9dbc68
DIFF: https://github.com/llvm/llvm-project/commit/89d8035e36c927bd3cb88dba49b9f97d6c9dbc68.diff

LOG: Use llvm::append_range where applicable

It knows the size, so no need to call reserve beforehand. NFCI.

Added: 
    

Modified: 
    mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
    mlir/lib/Dialect/Affine/Analysis/Utils.cpp
    mlir/lib/Dialect/Affine/Transforms/LoopFusion.cpp
    mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp
    mlir/lib/Dialect/Linalg/Transforms/Detensorize.cpp
    mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp
    mlir/lib/Dialect/Utils/ReshapeOpsUtils.cpp
    mlir/lib/IR/AffineMap.cpp
    mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp b/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
index 3b4e7adcebcba..b5c26a8fefa60 100644
--- a/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
+++ b/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
@@ -220,11 +220,6 @@ Type LLVMTypeConverter::convertFunctionSignature(
     result.addInputs(en.index(), converted);
   }
 
-  SmallVector<Type, 8> argTypes;
-  argTypes.reserve(llvm::size(result.getConvertedTypes()));
-  for (Type type : result.getConvertedTypes())
-    argTypes.push_back(type);
-
   // If function does not return anything, create the void result type,
   // if it returns on element, convert it, otherwise pack the result types into
   // a struct.
@@ -233,7 +228,8 @@ Type LLVMTypeConverter::convertFunctionSignature(
                         : packFunctionResults(funcTy.getResults());
   if (!resultType)
     return {};
-  return LLVM::LLVMFunctionType::get(resultType, argTypes, isVariadic);
+  return LLVM::LLVMFunctionType::get(resultType, result.getConvertedTypes(),
+                                     isVariadic);
 }
 
 /// Converts the function type to a C-compatible format, in particular using

diff  --git a/mlir/lib/Dialect/Affine/Analysis/Utils.cpp b/mlir/lib/Dialect/Affine/Analysis/Utils.cpp
index 7a1064e3698f6..540c5bba8984f 100644
--- a/mlir/lib/Dialect/Affine/Analysis/Utils.cpp
+++ b/mlir/lib/Dialect/Affine/Analysis/Utils.cpp
@@ -1218,22 +1218,14 @@ MemRefAccess::MemRefAccess(Operation *loadOrStoreOpInst) {
   if (auto loadOp = dyn_cast<AffineReadOpInterface>(loadOrStoreOpInst)) {
     memref = loadOp.getMemRef();
     opInst = loadOrStoreOpInst;
-    auto loadMemrefType = loadOp.getMemRefType();
-    indices.reserve(loadMemrefType.getRank());
-    for (auto index : loadOp.getMapOperands()) {
-      indices.push_back(index);
-    }
+    llvm::append_range(indices, loadOp.getMapOperands());
   } else {
     assert(isa<AffineWriteOpInterface>(loadOrStoreOpInst) &&
            "Affine read/write op expected");
     auto storeOp = cast<AffineWriteOpInterface>(loadOrStoreOpInst);
     opInst = loadOrStoreOpInst;
     memref = storeOp.getMemRef();
-    auto storeMemrefType = storeOp.getMemRefType();
-    indices.reserve(storeMemrefType.getRank());
-    for (auto index : storeOp.getMapOperands()) {
-      indices.push_back(index);
-    }
+    llvm::append_range(indices, storeOp.getMapOperands());
   }
 }
 

diff  --git a/mlir/lib/Dialect/Affine/Transforms/LoopFusion.cpp b/mlir/lib/Dialect/Affine/Transforms/LoopFusion.cpp
index 588dc63fbbff0..4acc508f3e45b 100644
--- a/mlir/lib/Dialect/Affine/Transforms/LoopFusion.cpp
+++ b/mlir/lib/Dialect/Affine/Transforms/LoopFusion.cpp
@@ -527,10 +527,8 @@ struct MemRefDependenceGraph {
   void addToNode(unsigned id, const SmallVectorImpl<Operation *> &loads,
                  const SmallVectorImpl<Operation *> &stores) {
     Node *node = getNode(id);
-    for (auto *loadOpInst : loads)
-      node->loads.push_back(loadOpInst);
-    for (auto *storeOpInst : stores)
-      node->stores.push_back(storeOpInst);
+    llvm::append_range(node->loads, loads);
+    llvm::append_range(node->stores, stores);
   }
 
   void clearNodeLoadAndStores(unsigned id) {

diff  --git a/mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp b/mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp
index 7500a507967f8..20c2d70123025 100644
--- a/mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp
+++ b/mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp
@@ -2660,9 +2660,7 @@ static AffineIfOp createSeparationCondition(MutableArrayRef<AffineForOp> loops,
 
   FlatAffineValueConstraints cst;
   SmallVector<Operation *, 8> ops;
-  ops.reserve(loops.size());
-  for (AffineForOp forOp : loops)
-    ops.push_back(forOp);
+  llvm::append_range(ops, loops);
   (void)getIndexSet(ops, &cst);
 
   // Remove constraints that are independent of these loop IVs.

diff  --git a/mlir/lib/Dialect/Linalg/Transforms/Detensorize.cpp b/mlir/lib/Dialect/Linalg/Transforms/Detensorize.cpp
index 9a87871478cf1..25aa5396686e1 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/Detensorize.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/Detensorize.cpp
@@ -256,15 +256,11 @@ struct LinalgDetensorize : public LinalgDetensorizeBase<LinalgDetensorize> {
       SmallVector<Value> workList;
 
       func->walk([&](cf::CondBranchOp condBr) {
-        for (auto operand : condBr.getOperands()) {
-          workList.push_back(operand);
-        }
+        llvm::append_range(workList, condBr.getOperands());
       });
 
       func->walk([&](cf::BranchOp br) {
-        for (auto operand : br.getOperands()) {
-          workList.push_back(operand);
-        }
+        llvm::append_range(workList, br.getOperands());
       });
 
       DenseSet<Value> visitedValues;
@@ -310,8 +306,7 @@ struct LinalgDetensorize : public LinalgDetensorizeBase<LinalgDetensorize> {
         // detensorable and if so, their operands will be added to workList to
         // potentially discover other parts of the detensorable component.
         for (auto *user : currentItem.getUsers())
-          for (Value result : user->getResults())
-            workList.push_back(result);
+          llvm::append_range(workList, user->getResults());
 
         // 2   - Look backward:
         // 2.1 - The current item is defined by a block argument. If the owner
@@ -383,10 +378,7 @@ struct LinalgDetensorize : public LinalgDetensorizeBase<LinalgDetensorize> {
           }
 
           opsToDetensor.insert(genericOp);
-
-          for (Value genericOpOperand : genericOp.inputs())
-            workList.push_back(genericOpOperand);
-
+          llvm::append_range(workList, genericOp.inputs());
           continue;
         }
 
@@ -405,8 +397,7 @@ struct LinalgDetensorize : public LinalgDetensorizeBase<LinalgDetensorize> {
         if (llvm::all_of(
                 currentItemDefiningOp->getResultTypes(),
                 [&](Type resultType) { return resultType.isIntOrFloat(); }))
-          for (Value scalarOpOperand : currentItemDefiningOp->getOperands())
-            workList.push_back(scalarOpOperand);
+          llvm::append_range(workList, currentItemDefiningOp->getOperands());
       }
 
       // Since the cost model gives up on some ops (see the details of step 2.2

diff  --git a/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp b/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp
index f494af5d14c16..17a07da564e25 100644
--- a/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp
+++ b/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp
@@ -177,7 +177,7 @@ static Value genAllocaScalar(ConversionPatternRewriter &rewriter, Location loc,
 
 /// Generates a temporary buffer of the given type and given contents.
 static Value genBuffer(ConversionPatternRewriter &rewriter, Location loc,
-                       ArrayRef<Value> values) {
+                       ValueRange values) {
   unsigned sz = values.size();
   assert(sz >= 1);
   Value buffer = genAlloca(rewriter, loc, sz, values[0].getType());
@@ -205,10 +205,7 @@ static void newParams(ConversionPatternRewriter &rewriter,
   params.push_back(genBuffer(rewriter, loc, attrs));
   // Dimension sizes array of the enveloping tensor. Useful for either
   // verification of external data, or for construction of internal data.
-  SmallVector<Value, 4> sizes;
-  for (Value s : szs)
-    sizes.push_back(s);
-  params.push_back(genBuffer(rewriter, loc, sizes));
+  params.push_back(genBuffer(rewriter, loc, szs));
   // Dimension order permutation array. This is the "identity" permutation by
   // default, or otherwise the "reverse" permutation of a given ordering, so
   // that indices can be mapped quickly to the right position.

diff  --git a/mlir/lib/Dialect/Utils/ReshapeOpsUtils.cpp b/mlir/lib/Dialect/Utils/ReshapeOpsUtils.cpp
index 17f449e489e38..03cd3af2e7bec 100644
--- a/mlir/lib/Dialect/Utils/ReshapeOpsUtils.cpp
+++ b/mlir/lib/Dialect/Utils/ReshapeOpsUtils.cpp
@@ -116,8 +116,7 @@ Optional<SmallVector<ReassociationIndices>> mlir::composeReassociationIndices(
   for (ReassociationIndicesRef consumerIndices : consumerReassociations) {
     ReassociationIndices reassociations;
     for (int64_t consumerIndex : consumerIndices) {
-      for (int64_t producerIndex : producerReassociations[consumerIndex])
-        reassociations.push_back(producerIndex);
+      llvm::append_range(reassociations, producerReassociations[consumerIndex]);
     }
     composedIndices.push_back(std::move(reassociations));
   }

diff  --git a/mlir/lib/IR/AffineMap.cpp b/mlir/lib/IR/AffineMap.cpp
index 5d0ffd6fb5c6a..32eb07cf1a2f4 100644
--- a/mlir/lib/IR/AffineMap.cpp
+++ b/mlir/lib/IR/AffineMap.cpp
@@ -727,19 +727,16 @@ AffineMap mlir::getProjectedMap(AffineMap map,
 //===----------------------------------------------------------------------===//
 
 MutableAffineMap::MutableAffineMap(AffineMap map)
-    : numDims(map.getNumDims()), numSymbols(map.getNumSymbols()),
-      context(map.getContext()) {
-  for (auto result : map.getResults())
-    results.push_back(result);
-}
+    : results(map.getResults().begin(), map.getResults().end()),
+      numDims(map.getNumDims()), numSymbols(map.getNumSymbols()),
+      context(map.getContext()) {}
 
 void MutableAffineMap::reset(AffineMap map) {
   results.clear();
   numDims = map.getNumDims();
   numSymbols = map.getNumSymbols();
   context = map.getContext();
-  for (auto result : map.getResults())
-    results.push_back(result);
+  llvm::append_range(results, map.getResults());
 }
 
 bool MutableAffineMap::isMultipleOf(unsigned idx, int64_t factor) const {

diff  --git a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
index 43f5069ddd2e8..4d72cdbf13a10 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
@@ -303,9 +303,7 @@ convertOperationImpl(Operation &opInst, llvm::IRBuilderBase &builder,
     // TODO: refactor function type creation which usually occurs in std-LLVM
     // conversion.
     SmallVector<Type, 8> operandTypes;
-    operandTypes.reserve(inlineAsmOp.getOperands().size());
-    for (auto t : inlineAsmOp.getOperands().getTypes())
-      operandTypes.push_back(t);
+    llvm::append_range(operandTypes, inlineAsmOp.getOperands().getTypes());
 
     Type resultType;
     if (inlineAsmOp.getNumResults() == 0) {


        


More information about the Mlir-commits mailing list