Skip to content

Commit 1cc07a0

Browse files
[mlir] Use *Set::insert_range (NFC) (llvm#133043)
We can use *Set::insert_range to collapse: for (auto Elem : Range) Set.insert(E); down to: Set.insert_range(Range); In some cases, we can further fold that into the set declaration.
1 parent 40d251d commit 1cc07a0

File tree

15 files changed

+23
-51
lines changed

15 files changed

+23
-51
lines changed

mlir/include/mlir/Dialect/Tosa/IR/TargetEnv.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ class TargetEnv {
2727
TargetEnv() {}
2828
explicit TargetEnv(const SmallVectorImpl<Profile> &profiles,
2929
const SmallVectorImpl<Extension> &extensions) {
30-
for (Profile prof : profiles)
31-
enabledProfiles.insert(prof);
30+
enabledProfiles.insert_range(profiles);
3231

33-
for (Extension ext : extensions)
34-
enabledExtensions.insert(ext);
32+
enabledExtensions.insert_range(extensions);
3533
}
3634

3735
void addProfile(Profile p) { enabledProfiles.insert(p); }

mlir/lib/Analysis/Liveness.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,11 @@ struct BlockInfoBuilder {
6868
// operands as used. All defined value will be removed from the used set
6969
// at the end.
7070
block->walk([&](Operation *op) {
71-
for (Value result : op->getResults())
72-
defValues.insert(result);
73-
for (Value operand : op->getOperands())
74-
useValues.insert(operand);
71+
defValues.insert_range(op->getResults());
72+
useValues.insert_range(op->getOperands());
7573
for (Region &region : op->getRegions())
7674
for (Block &child : region.getBlocks())
77-
for (BlockArgument arg : child.getArguments())
78-
defValues.insert(arg);
75+
defValues.insert_range(child.getArguments());
7976
});
8077
llvm::set_subtract(useValues, defValues);
8178
}

mlir/lib/Analysis/TopologicalSortUtils.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,8 @@ bool mlir::computeTopologicalSorting(
116116
return true;
117117

118118
// The set of operations that have not yet been scheduled.
119-
DenseSet<Operation *> unscheduledOps;
120-
121119
// Mark all operations as unscheduled.
122-
for (Operation *op : ops)
123-
unscheduledOps.insert(op);
120+
DenseSet<Operation *> unscheduledOps(llvm::from_range, ops);
124121

125122
unsigned nextScheduledOp = 0;
126123

mlir/lib/Dialect/Func/TransformOps/FuncTransformOps.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ transform::CastAndCallOp::apply(transform::TransformRewriter &rewriter,
5151

5252
SetVector<Value> outputs;
5353
if (getOutputs()) {
54-
for (auto output : state.getPayloadValues(getOutputs()))
55-
outputs.insert(output);
54+
outputs.insert_range(state.getPayloadValues(getOutputs()));
5655

5756
// Verify that the set of output values to be replaced is unique.
5857
if (outputs.size() !=

mlir/lib/Dialect/LLVMIR/IR/LLVMMemorySlot.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,8 +1345,7 @@ static bool memcpyCanRewire(MemcpyLike op, const DestructurableMemorySlot &slot,
13451345
return false;
13461346

13471347
if (op.getSrc() == slot.ptr)
1348-
for (Attribute index : llvm::make_first_range(slot.subelementTypes))
1349-
usedIndices.insert(index);
1348+
usedIndices.insert_range(llvm::make_first_range(slot.subelementTypes));
13501349

13511350
return true;
13521351
}

mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4390,9 +4390,7 @@ static bool isInvalidPackingPosSpecification(ArrayRef<int64_t> dimsPos,
43904390
size_t dimsPosSize = dimsPos.size();
43914391
if (dimsPosSize > rank)
43924392
return true;
4393-
DenseSet<int64_t> uniqued;
4394-
for (int64_t dim : dimsPos)
4395-
uniqued.insert(dim);
4393+
DenseSet<int64_t> uniqued(llvm::from_range, dimsPos);
43964394
if (dimsPosSize != uniqued.size())
43974395
return true;
43984396
return llvm::any_of(dimsPos, [rank](int64_t dimPos) {

mlir/lib/Dialect/Linalg/Transforms/Detensorize.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,7 @@ struct LinalgDetensorize
458458
});
459459

460460
for (Block &block : llvm::drop_begin(func.getFunctionBody(), 1))
461-
for (BlockArgument blockArgument : block.getArguments())
462-
blockArgsToDetensor.insert(blockArgument);
461+
blockArgsToDetensor.insert_range(block.getArguments());
463462
}
464463
};
465464

mlir/lib/Dialect/MLProgram/Transforms/PipelineGlobalOps.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,9 @@ LogicalResult MLProgramPipelineGlobals::buildGlobalMap(ModuleOp module) {
104104
work.push_back(symbol);
105105
});
106106

107-
for (auto load : opLoadSymbols[work[i]])
108-
loadSymbols.insert(load);
107+
loadSymbols.insert_range(opLoadSymbols[work[i]]);
109108

110-
for (auto store : opStoreSymbols[work[i]])
111-
storeSymbols.insert(store);
109+
storeSymbols.insert_range(opStoreSymbols[work[i]]);
112110
}
113111

114112
loadSymbolsMap[thisSymbol] = std::move(loadSymbols);

mlir/lib/Dialect/SCF/IR/SCF.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4052,9 +4052,7 @@ struct WhileRemoveDuplicatedResults : public OpRewritePattern<WhileOp> {
40524052
ConditionOp condOp = op.getConditionOp();
40534053
ValueRange condOpArgs = condOp.getArgs();
40544054

4055-
llvm::SmallPtrSet<Value, 8> argsSet;
4056-
for (Value arg : condOpArgs)
4057-
argsSet.insert(arg);
4055+
llvm::SmallPtrSet<Value, 8> argsSet(llvm::from_range, condOpArgs);
40584056

40594057
if (argsSet.size() == condOpArgs.size())
40604058
return rewriter.notifyMatchFailure(op, "No results to remove");

mlir/lib/Dialect/SCF/Transforms/LoopPipelining.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ bool LoopPipelinerInternal::initializeLoopInfo(
207207
static SetVector<Value> getNestedOperands(Operation *op) {
208208
SetVector<Value> operands;
209209
op->walk([&](Operation *nestedOp) {
210-
for (Value operand : nestedOp->getOperands()) {
211-
operands.insert(operand);
212-
}
210+
operands.insert_range(nestedOp->getOperands());
213211
});
214212
return operands;
215213
}

mlir/lib/Dialect/SPIRV/IR/SPIRVEnums.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ spirv::getRecursiveImpliedCapabilities(spirv::Capability cap) {
9999
// TODO: This is insufficient; find a better way to handle this
100100
// (e.g., using static lists) if this turns out to be a bottleneck.
101101
for (unsigned i = 0; i < allCaps.size(); ++i)
102-
for (Capability c : getDirectImpliedCapabilities(allCaps[i]))
103-
allCaps.insert(c);
102+
allCaps.insert_range(getDirectImpliedCapabilities(allCaps[i]));
104103

105104
return allCaps.takeVector();
106105
}

mlir/lib/Dialect/SPIRV/IR/TargetAndABI.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,17 @@ using namespace mlir;
2323

2424
spirv::TargetEnv::TargetEnv(spirv::TargetEnvAttr targetAttr)
2525
: targetAttr(targetAttr) {
26-
for (spirv::Extension ext : targetAttr.getExtensions())
27-
givenExtensions.insert(ext);
26+
givenExtensions.insert_range(targetAttr.getExtensions());
2827

2928
// Add extensions implied by the current version.
30-
for (spirv::Extension ext :
31-
spirv::getImpliedExtensions(targetAttr.getVersion()))
32-
givenExtensions.insert(ext);
29+
givenExtensions.insert_range(
30+
spirv::getImpliedExtensions(targetAttr.getVersion()));
3331

3432
for (spirv::Capability cap : targetAttr.getCapabilities()) {
3533
givenCapabilities.insert(cap);
3634

3735
// Add capabilities implied by the current capability.
38-
for (spirv::Capability c : spirv::getRecursiveImpliedCapabilities(cap))
39-
givenCapabilities.insert(c);
36+
givenCapabilities.insert_range(spirv::getRecursiveImpliedCapabilities(cap));
4037
}
4138
}
4239

mlir/lib/Transforms/SROA.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,8 @@ static void destructureSlot(
154154
statistics.maxSubelementAmount->updateMax(slot.subelementTypes.size());
155155

156156
SetVector<Operation *> usersToRewire;
157-
for (Operation *user : llvm::make_first_range(info.userToBlockingUses))
158-
usersToRewire.insert(user);
159-
for (DestructurableAccessorOpInterface accessor : info.accessors)
160-
usersToRewire.insert(accessor);
157+
usersToRewire.insert_range(llvm::make_first_range(info.userToBlockingUses));
158+
usersToRewire.insert_range(info.accessors);
161159
usersToRewire = mlir::topologicalSort(usersToRewire);
162160

163161
llvm::SmallVector<Operation *> toErase;

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ struct ConversionValueMapping {
154154
next = it->second;
155155
}
156156
});
157-
for (Value v : newVal)
158-
mappedTo.insert(v);
157+
mappedTo.insert_range(newVal);
159158

160159
mapping[std::forward<OldVal>(oldVal)] = std::forward<NewVal>(newVal);
161160
}

mlir/lib/Transforms/Utils/InliningUtils.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,7 @@ static void handleResultImpl(InlinerInterface &interface, OpBuilder &builder,
251251
SmallVector<DictionaryAttr> resultAttributes;
252252
for (auto [result, resAttr] : llvm::zip(results, resAttrs)) {
253253
// Store the original result users before running the handler.
254-
DenseSet<Operation *> resultUsers;
255-
for (Operation *user : result.getUsers())
256-
resultUsers.insert(user);
254+
DenseSet<Operation *> resultUsers(llvm::from_range, result.getUsers());
257255

258256
Value newResult =
259257
interface.handleResult(builder, call, callable, result, resAttr);

0 commit comments

Comments
 (0)