Subzero: Deterministically sort local var stack offsets.
Currently, TargetLowering::sortVarsByAlignment() uses the variable's required alignment as the primary key, but then std::sort() breaks ties arbitrarily. This can give different results across different STL implementations, for example when building against LLVM trunk versus building in the PNaCl environment.
The fix is to use Variable::Number as the secondary key.
BUG= none
R=kschimpf@google.com
Review URL: https://codereview.chromium.org/2295393002 .
diff --git a/src/IceTargetLowering.cpp b/src/IceTargetLowering.cpp
index ff93e70..0ac6e8b 100644
--- a/src/IceTargetLowering.cpp
+++ b/src/IceTargetLowering.cpp
@@ -737,8 +737,11 @@
// the buckets, if performance is an issue.
std::sort(Dest.begin(), Dest.end(),
[this](const Variable *V1, const Variable *V2) {
- return typeWidthInBytesOnStack(V1->getType()) >
- typeWidthInBytesOnStack(V2->getType());
+ const size_t WidthV1 = typeWidthInBytesOnStack(V1->getType());
+ const size_t WidthV2 = typeWidthInBytesOnStack(V2->getType());
+ if (WidthV1 == WidthV2)
+ return V1->getIndex() < V2->getIndex();
+ return WidthV1 > WidthV2;
});
}