Skip to content

Commit 15ff8ca

Browse files
author
Mandeep Singh Grang
committed
[Transforms] Change std::sort to llvm::sort in response to r327219
Summary: r327219 added wrappers to std::sort which randomly shuffle the container before sorting. This will help in uncovering non-determinism caused due to undefined sorting order of objects having the same key. To make use of that infrastructure we need to invoke llvm::sort instead of std::sort. Note: This patch is one of a series of patches to replace *all* std::sort to llvm::sort. Refer the comments section in D44363 for a list of all the required patches. Reviewers: kcc, pcc, danielcdh, jmolloy, sanjoy, dberlin, ruiu Reviewed By: ruiu Subscribers: ruiu, llvm-commits Differential Revision: https://reviews.llvm.org/D45142 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330059 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 648f7ea commit 15ff8ca

23 files changed

+89
-84
lines changed

lib/Transforms/Coroutines/CoroFrame.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class BlockToIndexMapping {
4848
BlockToIndexMapping(Function &F) {
4949
for (BasicBlock &BB : F)
5050
V.push_back(&BB);
51-
std::sort(V.begin(), V.end());
51+
llvm::sort(V.begin(), V.end());
5252
}
5353

5454
size_t blockToIndex(BasicBlock *BB) const {

lib/Transforms/IPO/LowerTypeTests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,11 +1869,11 @@ bool LowerTypeTestsModule::lower() {
18691869
}
18701870
Sets.emplace_back(I, MaxIndex);
18711871
}
1872-
std::sort(Sets.begin(), Sets.end(),
1873-
[](const std::pair<GlobalClassesTy::iterator, unsigned> &S1,
1874-
const std::pair<GlobalClassesTy::iterator, unsigned> &S2) {
1875-
return S1.second < S2.second;
1876-
});
1872+
llvm::sort(Sets.begin(), Sets.end(),
1873+
[](const std::pair<GlobalClassesTy::iterator, unsigned> &S1,
1874+
const std::pair<GlobalClassesTy::iterator, unsigned> &S2) {
1875+
return S1.second < S2.second;
1876+
});
18771877

18781878
// For each disjoint set we found...
18791879
for (const auto &S : Sets) {
@@ -1894,7 +1894,7 @@ bool LowerTypeTestsModule::lower() {
18941894

18951895
// Order type identifiers by global index for determinism. This ordering is
18961896
// stable as there is a one-to-one mapping between metadata and indices.
1897-
std::sort(TypeIds.begin(), TypeIds.end(), [&](Metadata *M1, Metadata *M2) {
1897+
llvm::sort(TypeIds.begin(), TypeIds.end(), [&](Metadata *M1, Metadata *M2) {
18981898
return TypeIdInfo[M1].Index < TypeIdInfo[M2].Index;
18991899
});
19001900

lib/Transforms/IPO/SampleProfile.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -679,10 +679,10 @@ SampleProfileLoader::findIndirectCallFunctionSamples(
679679
Sum += NameFS.second.getEntrySamples();
680680
R.push_back(&NameFS.second);
681681
}
682-
std::sort(R.begin(), R.end(),
683-
[](const FunctionSamples *L, const FunctionSamples *R) {
684-
return L->getEntrySamples() > R->getEntrySamples();
685-
});
682+
llvm::sort(R.begin(), R.end(),
683+
[](const FunctionSamples *L, const FunctionSamples *R) {
684+
return L->getEntrySamples() > R->getEntrySamples();
685+
});
686686
}
687687
return R;
688688
}
@@ -1170,13 +1170,13 @@ static SmallVector<InstrProfValueData, 2> SortCallTargets(
11701170
SmallVector<InstrProfValueData, 2> R;
11711171
for (auto I = M.begin(); I != M.end(); ++I)
11721172
R.push_back({Function::getGUID(I->getKey()), I->getValue()});
1173-
std::sort(R.begin(), R.end(),
1174-
[](const InstrProfValueData &L, const InstrProfValueData &R) {
1175-
if (L.Count == R.Count)
1176-
return L.Value > R.Value;
1177-
else
1178-
return L.Count > R.Count;
1179-
});
1173+
llvm::sort(R.begin(), R.end(),
1174+
[](const InstrProfValueData &L, const InstrProfValueData &R) {
1175+
if (L.Count == R.Count)
1176+
return L.Value > R.Value;
1177+
else
1178+
return L.Count > R.Count;
1179+
});
11801180
return R;
11811181
}
11821182

lib/Transforms/Instrumentation/GCOVProfiling.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ namespace {
272272
write(Len);
273273
write(Number);
274274

275-
std::sort(
275+
llvm::sort(
276276
SortedLinesByFile.begin(), SortedLinesByFile.end(),
277277
[](StringMapEntry<GCOVLines> *LHS, StringMapEntry<GCOVLines> *RHS) {
278278
return LHS->getKey() < RHS->getKey();

lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -659,11 +659,11 @@ void SanitizerCoverageModule::InjectTraceForSwitch(
659659
C = ConstantExpr::getCast(CastInst::ZExt, It.getCaseValue(), Int64Ty);
660660
Initializers.push_back(C);
661661
}
662-
std::sort(Initializers.begin() + 2, Initializers.end(),
663-
[](const Constant *A, const Constant *B) {
664-
return cast<ConstantInt>(A)->getLimitedValue() <
665-
cast<ConstantInt>(B)->getLimitedValue();
666-
});
662+
llvm::sort(Initializers.begin() + 2, Initializers.end(),
663+
[](const Constant *A, const Constant *B) {
664+
return cast<ConstantInt>(A)->getLimitedValue() <
665+
cast<ConstantInt>(B)->getLimitedValue();
666+
});
667667
ArrayType *ArrayOfInt64Ty = ArrayType::get(Int64Ty, Initializers.size());
668668
GlobalVariable *GV = new GlobalVariable(
669669
*CurModule, ArrayOfInt64Ty, false, GlobalVariable::InternalLinkage,

lib/Transforms/Scalar/ConstantHoisting.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,8 @@ void ConstantHoistingPass::findAndMakeBaseConstant(
571571
/// rematerialized with an add from a common base constant.
572572
void ConstantHoistingPass::findBaseConstants() {
573573
// Sort the constants by value and type. This invalidates the mapping!
574-
std::sort(ConstCandVec.begin(), ConstCandVec.end(),
575-
[](const ConstantCandidate &LHS, const ConstantCandidate &RHS) {
574+
llvm::sort(ConstCandVec.begin(), ConstCandVec.end(),
575+
[](const ConstantCandidate &LHS, const ConstantCandidate &RHS) {
576576
if (LHS.ConstInt->getType() != RHS.ConstInt->getType())
577577
return LHS.ConstInt->getType()->getBitWidth() <
578578
RHS.ConstInt->getType()->getBitWidth();

lib/Transforms/Scalar/GVNHoist.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -748,11 +748,11 @@ class GVNHoist {
748748
// TODO: Remove fully-redundant expressions.
749749
// Get instruction from the Map, assume that all the Instructions
750750
// with same VNs have same rank (this is an approximation).
751-
std::sort(Ranks.begin(), Ranks.end(),
752-
[this, &Map](const VNType &r1, const VNType &r2) {
753-
return (rank(*Map.lookup(r1).begin()) <
754-
rank(*Map.lookup(r2).begin()));
755-
});
751+
llvm::sort(Ranks.begin(), Ranks.end(),
752+
[this, &Map](const VNType &r1, const VNType &r2) {
753+
return (rank(*Map.lookup(r1).begin()) <
754+
rank(*Map.lookup(r2).begin()));
755+
});
756756

757757
// - Sort VNs according to their rank, and start with lowest ranked VN
758758
// - Take a VN and for each instruction with same VN

lib/Transforms/Scalar/GVNSink.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class ModelledPHI {
239239
SmallVector<std::pair<BasicBlock *, Value *>, 4> Ops;
240240
for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I)
241241
Ops.push_back({PN->getIncomingBlock(I), PN->getIncomingValue(I)});
242-
std::sort(Ops.begin(), Ops.end());
242+
llvm::sort(Ops.begin(), Ops.end());
243243
for (auto &P : Ops) {
244244
Blocks.push_back(P.first);
245245
Values.push_back(P.second);
@@ -361,7 +361,7 @@ class InstructionUseExpr : public GVNExpression::BasicExpression {
361361

362362
for (auto &U : I->uses())
363363
op_push_back(U.getUser());
364-
std::sort(op_begin(), op_end());
364+
llvm::sort(op_begin(), op_end());
365365
}
366366

367367
void setMemoryUseOrder(unsigned MUO) { MemoryUseOrder = MUO; }
@@ -761,7 +761,7 @@ unsigned GVNSink::sinkBB(BasicBlock *BBEnd) {
761761
}
762762
if (Preds.size() < 2)
763763
return 0;
764-
std::sort(Preds.begin(), Preds.end());
764+
llvm::sort(Preds.begin(), Preds.end());
765765

766766
unsigned NumOrigPreds = Preds.size();
767767
// We can only sink instructions through unconditional branches.

lib/Transforms/Scalar/GuardWidening.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,9 @@ bool GuardWideningImpl::combineRangeChecks(
555555
// CurrentChecks.size() will typically be 3 here, but so far there has been
556556
// no need to hard-code that fact.
557557

558-
std::sort(CurrentChecks.begin(), CurrentChecks.end(),
559-
[&](const GuardWideningImpl::RangeCheck &LHS,
560-
const GuardWideningImpl::RangeCheck &RHS) {
558+
llvm::sort(CurrentChecks.begin(), CurrentChecks.end(),
559+
[&](const GuardWideningImpl::RangeCheck &LHS,
560+
const GuardWideningImpl::RangeCheck &RHS) {
561561
return LHS.getOffsetValue().slt(RHS.getOffsetValue());
562562
});
563563

lib/Transforms/Scalar/LoopSink.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ static bool sinkInstruction(Loop &L, Instruction &I,
200200
SmallVector<BasicBlock *, 2> SortedBBsToSinkInto;
201201
SortedBBsToSinkInto.insert(SortedBBsToSinkInto.begin(), BBsToSinkInto.begin(),
202202
BBsToSinkInto.end());
203-
std::sort(SortedBBsToSinkInto.begin(), SortedBBsToSinkInto.end(),
204-
[&](BasicBlock *A, BasicBlock *B) {
205-
return *LoopBlockNumber.find(A) < *LoopBlockNumber.find(B);
206-
});
203+
llvm::sort(SortedBBsToSinkInto.begin(), SortedBBsToSinkInto.end(),
204+
[&](BasicBlock *A, BasicBlock *B) {
205+
return *LoopBlockNumber.find(A) < *LoopBlockNumber.find(B);
206+
});
207207

208208
BasicBlock *MoveBB = *SortedBBsToSinkInto.begin();
209209
// FIXME: Optimize the efficiency for cloned value replacement. The current

0 commit comments

Comments
 (0)