-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[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
[mlir][NFC] Use llvm::sort
#140261
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-mlir-bufferization @llvm/pr-subscribers-mlir-linalg Author: Iris Shi (el-ev) ChangesFull diff: https://github.com/llvm/llvm-project/pull/140261.diff 8 Files Affected:
diff --git a/mlir/lib/Dialect/ArmSME/Transforms/TileAllocation.cpp b/mlir/lib/Dialect/ArmSME/Transforms/TileAllocation.cpp
index 84556fbefbc9f..72a05ffe97ac0 100644
--- a/mlir/lib/Dialect/ArmSME/Transforms/TileAllocation.cpp
+++ b/mlir/lib/Dialect/ArmSME/Transforms/TileAllocation.cpp
@@ -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);
}
@@ -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);
}
diff --git a/mlir/lib/Dialect/Bufferization/IR/BufferDeallocationOpInterface.cpp b/mlir/lib/Dialect/Bufferization/IR/BufferDeallocationOpInterface.cpp
index 51dfd84d9ac60..eed7a56fff8af 100644
--- a/mlir/lib/Dialect/Bufferization/IR/BufferDeallocationOpInterface.cpp
+++ b/mlir/lib/Dialect/Bufferization/IR/BufferDeallocationOpInterface.cpp
@@ -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);
}
diff --git a/mlir/lib/Dialect/Linalg/Transforms/DecomposeGenericByUnfoldingPermutation.cpp b/mlir/lib/Dialect/Linalg/Transforms/DecomposeGenericByUnfoldingPermutation.cpp
index ae8cb94661c76..daeae2c9d947d 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/DecomposeGenericByUnfoldingPermutation.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/DecomposeGenericByUnfoldingPermutation.cpp
@@ -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());
diff --git a/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenEnv.cpp b/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenEnv.cpp
index 86c13d03c7ec6..b94091cfa5f58 100644
--- a/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenEnv.cpp
+++ b/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenEnv.cpp
@@ -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
diff --git a/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenUtils.cpp b/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenUtils.cpp
index ffa06bc0e2071..57291064eba22 100644
--- a/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenUtils.cpp
+++ b/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenUtils.cpp
@@ -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);
diff --git a/mlir/lib/Dialect/Utils/StaticValueUtils.cpp b/mlir/lib/Dialect/Utils/StaticValueUtils.cpp
index fcb736aa031f3..fac836ebd7a36 100644
--- a/mlir/lib/Dialect/Utils/StaticValueUtils.cpp
+++ b/mlir/lib/Dialect/Utils/StaticValueUtils.cpp
@@ -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)
diff --git a/mlir/lib/Pass/Pass.cpp b/mlir/lib/Pass/Pass.cpp
index 8d1fbcdb19a9d..e0e9b5f54042a 100644
--- a/mlir/lib/Pass/Pass.cpp
+++ b/mlir/lib/Pass/Pass.cpp
@@ -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();
}
diff --git a/mlir/tools/mlir-tblgen/OpDocGen.cpp b/mlir/tools/mlir-tblgen/OpDocGen.cpp
index 077f9d1ea2b13..f2b269e3a4542 100644
--- a/mlir/tools/mlir-tblgen/OpDocGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDocGen.cpp
@@ -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,
|
@llvm/pr-subscribers-mlir Author: Iris Shi (el-ev) ChangesFull diff: https://github.com/llvm/llvm-project/pull/140261.diff 8 Files Affected:
diff --git a/mlir/lib/Dialect/ArmSME/Transforms/TileAllocation.cpp b/mlir/lib/Dialect/ArmSME/Transforms/TileAllocation.cpp
index 84556fbefbc9f..72a05ffe97ac0 100644
--- a/mlir/lib/Dialect/ArmSME/Transforms/TileAllocation.cpp
+++ b/mlir/lib/Dialect/ArmSME/Transforms/TileAllocation.cpp
@@ -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);
}
@@ -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);
}
diff --git a/mlir/lib/Dialect/Bufferization/IR/BufferDeallocationOpInterface.cpp b/mlir/lib/Dialect/Bufferization/IR/BufferDeallocationOpInterface.cpp
index 51dfd84d9ac60..eed7a56fff8af 100644
--- a/mlir/lib/Dialect/Bufferization/IR/BufferDeallocationOpInterface.cpp
+++ b/mlir/lib/Dialect/Bufferization/IR/BufferDeallocationOpInterface.cpp
@@ -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);
}
diff --git a/mlir/lib/Dialect/Linalg/Transforms/DecomposeGenericByUnfoldingPermutation.cpp b/mlir/lib/Dialect/Linalg/Transforms/DecomposeGenericByUnfoldingPermutation.cpp
index ae8cb94661c76..daeae2c9d947d 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/DecomposeGenericByUnfoldingPermutation.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/DecomposeGenericByUnfoldingPermutation.cpp
@@ -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());
diff --git a/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenEnv.cpp b/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenEnv.cpp
index 86c13d03c7ec6..b94091cfa5f58 100644
--- a/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenEnv.cpp
+++ b/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenEnv.cpp
@@ -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
diff --git a/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenUtils.cpp b/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenUtils.cpp
index ffa06bc0e2071..57291064eba22 100644
--- a/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenUtils.cpp
+++ b/mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenUtils.cpp
@@ -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);
diff --git a/mlir/lib/Dialect/Utils/StaticValueUtils.cpp b/mlir/lib/Dialect/Utils/StaticValueUtils.cpp
index fcb736aa031f3..fac836ebd7a36 100644
--- a/mlir/lib/Dialect/Utils/StaticValueUtils.cpp
+++ b/mlir/lib/Dialect/Utils/StaticValueUtils.cpp
@@ -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)
diff --git a/mlir/lib/Pass/Pass.cpp b/mlir/lib/Pass/Pass.cpp
index 8d1fbcdb19a9d..e0e9b5f54042a 100644
--- a/mlir/lib/Pass/Pass.cpp
+++ b/mlir/lib/Pass/Pass.cpp
@@ -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();
}
diff --git a/mlir/tools/mlir-tblgen/OpDocGen.cpp b/mlir/tools/mlir-tblgen/OpDocGen.cpp
index 077f9d1ea2b13..f2b269e3a4542 100644
--- a/mlir/tools/mlir-tblgen/OpDocGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDocGen.cpp
@@ -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,
|
MacDue
approved these changes
May 16, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
mlir:bufferization
Bufferization infrastructure
mlir:core
MLIR Core Infrastructure
mlir:linalg
mlir:sme
mlir:sparse
Sparse compiler in MLIR
mlir
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.