From 89d8035e36c927bd3cb88dba49b9f97d6c9dbc68 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Fri, 18 Mar 2022 20:05:12 +0100 Subject: [PATCH] Use llvm::append_range where applicable It knows the size, so no need to call reserve beforehand. NFCI. --- mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp | 8 ++------ mlir/lib/Dialect/Affine/Analysis/Utils.cpp | 12 ++---------- mlir/lib/Dialect/Affine/Transforms/LoopFusion.cpp | 6 ++---- mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp | 4 +--- mlir/lib/Dialect/Linalg/Transforms/Detensorize.cpp | 19 +++++-------------- .../Transforms/SparseTensorConversion.cpp | 7 ++----- mlir/lib/Dialect/Utils/ReshapeOpsUtils.cpp | 3 +-- mlir/lib/IR/AffineMap.cpp | 11 ++++------- .../LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp | 4 +--- 9 files changed, 20 insertions(+), 54 deletions(-) diff --git a/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp b/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp index 3b4e7ad..b5c26a8 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 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 7a1064e..540c5bb 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(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(loadOrStoreOpInst) && "Affine read/write op expected"); auto storeOp = cast(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 588dc63..4acc508 100644 --- a/mlir/lib/Dialect/Affine/Transforms/LoopFusion.cpp +++ b/mlir/lib/Dialect/Affine/Transforms/LoopFusion.cpp @@ -527,10 +527,8 @@ public: void addToNode(unsigned id, const SmallVectorImpl &loads, const SmallVectorImpl &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 7500a50..20c2d70 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 loops, FlatAffineValueConstraints cst; SmallVector 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 9a87871..25aa539 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 { SmallVector 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 visitedValues; @@ -310,8 +306,7 @@ struct LinalgDetensorize : public LinalgDetensorizeBase { // 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 { } 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 { 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 f494af5..17a07da 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 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 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 17f449e..03cd3af 100644 --- a/mlir/lib/Dialect/Utils/ReshapeOpsUtils.cpp +++ b/mlir/lib/Dialect/Utils/ReshapeOpsUtils.cpp @@ -116,8 +116,7 @@ Optional> 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 5d0ffd6..32eb07c 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 43f5069d..4d72cdb 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 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) { -- 2.7.4