[PATCH] D98167: Avoid shuffle self-assignment in EXPENSIVE_CHECKS builds
Alexander Richardson via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 9 11:00:20 PST 2021
arichardson updated this revision to Diff 329403.
arichardson added a comment.
- Add comment explaining != 0 check
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D98167/new/
https://reviews.llvm.org/D98167
Files:
llvm/include/llvm/ADT/STLExtras.h
llvm/tools/bugpoint/FindBugs.cpp
llvm/tools/bugpoint/ListReducer.h
llvm/tools/llvm-exegesis/lib/SerialSnippetGenerator.cpp
llvm/tools/llvm-stress/llvm-stress.cpp
Index: llvm/tools/llvm-stress/llvm-stress.cpp
===================================================================
--- llvm/tools/llvm-stress/llvm-stress.cpp
+++ llvm/tools/llvm-stress/llvm-stress.cpp
@@ -718,7 +718,7 @@
BoolInst.push_back(&Instr);
}
- std::shuffle(BoolInst.begin(), BoolInst.end(), R);
+ llvm::shuffle(BoolInst.begin(), BoolInst.end(), R);
for (auto *Instr : BoolInst) {
BasicBlock *Curr = Instr->getParent();
Index: llvm/tools/llvm-exegesis/lib/SerialSnippetGenerator.cpp
===================================================================
--- llvm/tools/llvm-exegesis/lib/SerialSnippetGenerator.cpp
+++ llvm/tools/llvm-exegesis/lib/SerialSnippetGenerator.cpp
@@ -42,7 +42,7 @@
std::vector<unsigned> Opcodes;
Opcodes.resize(State.getInstrInfo().getNumOpcodes());
std::iota(Opcodes.begin(), Opcodes.end(), 0U);
- std::shuffle(Opcodes.begin(), Opcodes.end(), randomGenerator());
+ llvm::shuffle(Opcodes.begin(), Opcodes.end(), randomGenerator());
std::vector<const Instruction *> AliasingInstructions;
for (const unsigned OtherOpcode : Opcodes) {
Index: llvm/tools/bugpoint/ListReducer.h
===================================================================
--- llvm/tools/bugpoint/ListReducer.h
+++ llvm/tools/bugpoint/ListReducer.h
@@ -92,7 +92,7 @@
// distribution (improving the speed of convergence).
if (ShufflingEnabled && NumOfIterationsWithoutProgress > MaxIterations) {
std::vector<ElTy> ShuffledList(TheList);
- std::shuffle(ShuffledList.begin(), ShuffledList.end(), randomness);
+ llvm::shuffle(ShuffledList.begin(), ShuffledList.end(), randomness);
errs() << "\n\n*** Testing shuffled set...\n\n";
// Check that random shuffle doesn't lose the bug
Expected<TestResult> Result = doTest(ShuffledList, empty);
Index: llvm/tools/bugpoint/FindBugs.cpp
===================================================================
--- llvm/tools/bugpoint/FindBugs.cpp
+++ llvm/tools/bugpoint/FindBugs.cpp
@@ -41,7 +41,7 @@
//
// Step 1: Randomize the order of the optimizer passes.
//
- std::shuffle(PassesToRun.begin(), PassesToRun.end(), randomness);
+ llvm::shuffle(PassesToRun.begin(), PassesToRun.end(), randomness);
//
// Step 2: Run optimizer passes on the program and check for success.
Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -1332,8 +1332,15 @@
void shuffle(Iterator first, Iterator last, RNG &&g) {
// It would be better to use a std::uniform_int_distribution,
// but that would be stdlib dependent.
- for (auto size = last - first; size > 1; ++first, (void)--size)
- std::iter_swap(first, first + g() % size);
+ typedef
+ typename std::iterator_traits<Iterator>::difference_type difference_type;
+ for (auto size = last - first; size > 1; ++first, (void)--size) {
+ difference_type offset = g() % size;
+ // Avoid self-assignment due to incorrect assertions in libstdc++
+ // containers (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85828).
+ if (offset != difference_type(0))
+ std::iter_swap(first, first + offset);
+ }
}
/// Find the length of an array.
@@ -1373,7 +1380,7 @@
template <class IteratorTy>
inline void presortShuffle(IteratorTy Start, IteratorTy End) {
std::mt19937 Generator(presortShuffleEntropy());
- std::shuffle(Start, End, Generator);
+ llvm::shuffle(Start, End, Generator);
}
} // end namespace detail
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98167.329403.patch
Type: text/x-patch
Size: 3588 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210309/c2f3cfd9/attachment.bin>
More information about the llvm-commits
mailing list