Skip to content

[mlir][NFC] Use llvm::sort #140261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions mlir/lib/Dialect/ArmSME/Transforms/TileAllocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ coalesceTileLiveRanges(DenseMap<Value, LiveRange> &initialLiveRanges) {

// Sort the new live ranges by starting point (ready for tile allocation).
auto coalescedLiveRanges = uniqueLiveRanges.takeVector();
std::sort(coalescedLiveRanges.begin(), coalescedLiveRanges.end(),
[](LiveRange *a, LiveRange *b) { return *a < *b; });
llvm::sort(coalescedLiveRanges,
[](LiveRange *a, LiveRange *b) { return *a < *b; });
return std::move(coalescedLiveRanges);
}

Expand Down Expand Up @@ -824,8 +824,8 @@ LogicalResult mlir::arm_sme::allocateSMETiles(FunctionOpInterface function,
[&](LiveRange const &liveRange) { return !liveRange.empty(); });
auto initialRanges = llvm::to_vector(llvm::map_range(
nonEmpty, [](LiveRange const &liveRange) { return &liveRange; }));
std::sort(initialRanges.begin(), initialRanges.end(),
[](LiveRange const *a, LiveRange const *b) { return *a < *b; });
llvm::sort(initialRanges,
[](LiveRange const *a, LiveRange const *b) { return *a < *b; });
llvm::errs() << "\n========== Initial Live Ranges:\n";
dumpLiveRanges(operationToIndexMap, initialRanges, function);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ void DeallocationState::getMemrefsToRetain(
// liveOut has non-deterministic order because it was constructed by iterating
// over a hash-set.
SmallVector<Value> retainedByLiveness(liveOut.begin(), liveOut.end());
std::sort(retainedByLiveness.begin(), retainedByLiveness.end(),
ValueComparator());
llvm::sort(retainedByLiveness, ValueComparator());
toRetain.append(retainedByLiveness);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ computeTransposeBroadcast(AffineMap &map) {

// If dims are not monotonically increasing then transpose is present.
SmallVector<int64_t> sortedResMap(minorResult);
std::sort(sortedResMap.begin(), sortedResMap.end());
llvm::sort(sortedResMap);
bool hasTranspose = !std::equal(minorResult.begin(), minorResult.end(),
sortedResMap.begin(), sortedResMap.end());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ static bool isMaterializing(Value val) {
/// Sorts the dependent loops such that it is ordered in the same sequence in
/// which loops will be generated.
static void sortDependentLoops(std::vector<LoopCoeffPair> &target) {
std::sort(target.begin(), target.end(),
[](const LoopCoeffPair &l, const LoopCoeffPair &r) {
assert(std::addressof(l) == std::addressof(r) || l != r);
return l.first < r.first;
});
llvm::sort(target, [](const LoopCoeffPair &l, const LoopCoeffPair &r) {
assert(std::addressof(l) == std::addressof(r) || l != r);
return l.first < r.first;
});
}
//===----------------------------------------------------------------------===//
// Code generation environment constructor and general methods
Expand Down
39 changes: 19 additions & 20 deletions mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,26 +459,25 @@ void sparse_tensor::foreachInSparseConstant(
}

// Sorts the sparse element attribute based on coordinates.
std::sort(elems.begin(), elems.end(),
[order](const ElementAttr &lhs, const ElementAttr &rhs) {
if (std::addressof(lhs) == std::addressof(rhs))
return false;

auto lhsCoords = llvm::map_to_vector(
lhs.first, [](IntegerAttr i) { return i.getInt(); });
auto rhsCoords = llvm::map_to_vector(
rhs.first, [](IntegerAttr i) { return i.getInt(); });

SmallVector<int64_t, 4> lhsLvlCrds = order.compose(lhsCoords);
SmallVector<int64_t, 4> rhsLvlCrds = order.compose(rhsCoords);
// Sort the element based on the lvl coordinates.
for (Level l = 0; l < order.getNumResults(); l++) {
if (lhsLvlCrds[l] == rhsLvlCrds[l])
continue;
return lhsLvlCrds[l] < rhsLvlCrds[l];
}
llvm_unreachable("no equal coordinate in sparse element attr");
});
llvm::sort(elems, [order](const ElementAttr &lhs, const ElementAttr &rhs) {
if (std::addressof(lhs) == std::addressof(rhs))
return false;

auto lhsCoords = llvm::map_to_vector(
lhs.first, [](IntegerAttr i) { return i.getInt(); });
auto rhsCoords = llvm::map_to_vector(
rhs.first, [](IntegerAttr i) { return i.getInt(); });

SmallVector<int64_t, 4> lhsLvlCrds = order.compose(lhsCoords);
SmallVector<int64_t, 4> rhsLvlCrds = order.compose(rhsCoords);
// Sort the element based on the lvl coordinates.
for (Level l = 0; l < order.getNumResults(); l++) {
if (lhsLvlCrds[l] == rhsLvlCrds[l])
continue;
return lhsLvlCrds[l] < rhsLvlCrds[l];
}
llvm_unreachable("no equal coordinate in sparse element attr");
});

SmallVector<Value> cvs;
cvs.reserve(dimRank);
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Utils/StaticValueUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ getValuesSortedByKeyImpl(ArrayRef<K> keys, ArrayRef<V> values,
return SmallVector<V>{values};
assert(keys.size() == values.size() && "unexpected mismatching sizes");
auto indices = llvm::to_vector(llvm::seq<int64_t>(0, values.size()));
std::sort(indices.begin(), indices.end(),
[&](int64_t i, int64_t j) { return compare(keys[i], keys[j]); });
llvm::sort(indices,
[&](int64_t i, int64_t j) { return compare(keys[i], keys[j]); });
SmallVector<V> res;
res.reserve(values.size());
for (int64_t i = 0, e = indices.size(); i < e; ++i)
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Pass/Pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ LogicalResult OpToOpPassAdaptor::tryMergeInto(MLIRContext *ctx,
}
return false; // lhs(op-agnostic) > rhs(op-specific)
};
std::sort(rhs.mgrs.begin(), rhs.mgrs.end(), compareFn);
llvm::sort(rhs.mgrs, compareFn);
return success();
}

Expand Down
17 changes: 8 additions & 9 deletions mlir/tools/mlir-tblgen/OpDocGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,15 +583,14 @@ static bool emitDialectDoc(const RecordKeeper &records, raw_ostream &os) {
// sections.
// TODO: The sorting order could be revised, currently attempting to sort of
// keep in alphabetical order.
std::sort(dialectOps.begin(), dialectOps.end(),
[](const OpDocGroup &lhs, const OpDocGroup &rhs) {
auto getDesc = [](const OpDocGroup &arg) -> StringRef {
if (!arg.summary.empty())
return arg.summary;
return arg.ops.front().getDef().getValueAsString("opName");
};
return getDesc(lhs).compare_insensitive(getDesc(rhs)) < 0;
});
llvm::sort(dialectOps, [](const OpDocGroup &lhs, const OpDocGroup &rhs) {
auto getDesc = [](const OpDocGroup &arg) -> StringRef {
if (!arg.summary.empty())
return arg.summary;
return arg.ops.front().getDef().getValueAsString("opName");
};
return getDesc(lhs).compare_insensitive(getDesc(rhs)) < 0;
});

os << "<!-- Autogenerated by mlir-tblgen; don't manually edit -->\n";
emitDialectDoc(*dialect, records.getInputFilename(), dialectAttrs,
Expand Down