[llvm] r247201 - LowerBitSets: Fix non-determinism bug.
Peter Collingbourne via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 9 15:30:32 PDT 2015
Author: pcc
Date: Wed Sep 9 17:30:32 2015
New Revision: 247201
URL: http://llvm.org/viewvc/llvm-project?rev=247201&view=rev
Log:
LowerBitSets: Fix non-determinism bug.
Visit disjoint sets in a deterministic order based on the maximum BitSetNM
index, otherwise the order in which we visit them will depend on pointer
comparisons. This was being exposed by MSan.
Modified:
llvm/trunk/lib/Transforms/IPO/LowerBitSets.cpp
llvm/trunk/test/Transforms/LowerBitSets/nonstring.ll
llvm/trunk/test/Transforms/LowerBitSets/simple.ll
Modified: llvm/trunk/lib/Transforms/IPO/LowerBitSets.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/LowerBitSets.cpp?rev=247201&r1=247200&r2=247201&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/LowerBitSets.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/LowerBitSets.cpp Wed Sep 9 17:30:32 2015
@@ -938,7 +938,7 @@ bool LowerBitSets::buildBitSets() {
for (unsigned I = 0, E = BitSetNM->getNumOperands(); I != E; ++I) {
MDNode *Op = BitSetNM->getOperand(I);
verifyBitSetMDNode(Op);
- BitSetIdIndices[Op] = I;
+ BitSetIdIndices[Op->getOperand(0)] = I;
}
}
@@ -988,18 +988,36 @@ bool LowerBitSets::buildBitSets() {
if (GlobalClasses.empty())
return false;
- // For each disjoint set we found...
+ // Build a list of disjoint sets ordered by their maximum BitSetNM index
+ // for determinism.
+ std::vector<std::pair<GlobalClassesTy::iterator, unsigned>> Sets;
for (GlobalClassesTy::iterator I = GlobalClasses.begin(),
E = GlobalClasses.end();
I != E; ++I) {
if (!I->isLeader()) continue;
-
++NumBitSetDisjointSets;
+ unsigned MaxIndex = 0;
+ for (GlobalClassesTy::member_iterator MI = GlobalClasses.member_begin(I);
+ MI != GlobalClasses.member_end(); ++MI) {
+ if ((*MI).is<Metadata *>())
+ MaxIndex = std::max(MaxIndex, BitSetIdIndices[MI->get<Metadata *>()]);
+ }
+ Sets.emplace_back(I, MaxIndex);
+ }
+ std::sort(Sets.begin(), Sets.end(),
+ [](const std::pair<GlobalClassesTy::iterator, unsigned> &S1,
+ const std::pair<GlobalClassesTy::iterator, unsigned> &S2) {
+ return S1.second < S2.second;
+ });
+
+ // For each disjoint set we found...
+ for (const auto &S : Sets) {
// Build the list of bitsets in this disjoint set.
std::vector<Metadata *> BitSets;
std::vector<GlobalObject *> Globals;
- for (GlobalClassesTy::member_iterator MI = GlobalClasses.member_begin(I);
+ for (GlobalClassesTy::member_iterator MI =
+ GlobalClasses.member_begin(S.first);
MI != GlobalClasses.member_end(); ++MI) {
if ((*MI).is<Metadata *>())
BitSets.push_back(MI->get<Metadata *>());
Modified: llvm/trunk/test/Transforms/LowerBitSets/nonstring.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LowerBitSets/nonstring.ll?rev=247201&r1=247200&r2=247201&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/LowerBitSets/nonstring.ll (original)
+++ llvm/trunk/test/Transforms/LowerBitSets/nonstring.ll Wed Sep 9 17:30:32 2015
@@ -4,8 +4,8 @@
target datalayout = "e-p:32:32"
-; CHECK: @[[BNAME:.*]] = private constant { [2 x i32] }
; CHECK: @[[ANAME:.*]] = private constant { i32 }
+; CHECK: @[[BNAME:.*]] = private constant { [2 x i32] }
@a = constant i32 1
@b = constant [2 x i32] [i32 2, i32 3]
Modified: llvm/trunk/test/Transforms/LowerBitSets/simple.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LowerBitSets/simple.ll?rev=247201&r1=247200&r2=247201&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/LowerBitSets/simple.ll (original)
+++ llvm/trunk/test/Transforms/LowerBitSets/simple.ll Wed Sep 9 17:30:32 2015
@@ -26,16 +26,16 @@ target datalayout = "e-p:32:32"
!4 = !{!"bitset2", i32* @c, i32 0}
; CHECK-NODISCARD-DAG: !{!"bitset2", i32* @c, i32 0}
+; Entries whose second operand is null (the result of a global being DCE'd)
+; should be ignored.
+!5 = !{!"bitset2", null, i32 0}
+
; Offset 0, 4 byte alignment
-!5 = !{!"bitset3", i32* @a, i32 0}
+!6 = !{!"bitset3", i32* @a, i32 0}
; CHECK-NODISCARD-DAG: !{!"bitset3", i32* @a, i32 0}
-!6 = !{!"bitset3", i32* @c, i32 0}
+!7 = !{!"bitset3", i32* @c, i32 0}
; CHECK-NODISCARD-DAG: !{!"bitset3", i32* @c, i32 0}
-; Entries whose second operand is null (the result of a global being DCE'd)
-; should be ignored.
-!7 = !{!"bitset2", null, i32 0}
-
!llvm.bitsets = !{ !0, !1, !2, !3, !4, !5, !6, !7 }
; CHECK: @bits_use{{[0-9]*}} = private alias i8* @bits{{[0-9]*}}
More information about the llvm-commits
mailing list