Skip to content

Commit

Permalink
Fix non-determinism in order of LLVM attributes
Browse files Browse the repository at this point in the history
We were using array_pod_sort on an array of type 'Attribute', which
wraps a pointer to AttributeImpl. For the most part this didn't matter
because the printing code prints enum attributes in a defined order, but
integer attributes such as 'align' and 'dereferenceable' were not
ordered.

Furthermore, AttributeImpl::operator< was broken for integer attributes.
An integer attribute is a kind and an integer value, and both pieces
need to be compared.

By fixing the comparison operator, we can go back to std::sort, and
things look good now.  This should fix clang arm-swiftcall.c test
failures on Windows.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265361 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
rnk committed Apr 4, 2016
1 parent 2870284 commit 3918651
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/IR/Attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,11 @@ bool AttributeImpl::operator<(const AttributeImpl &AI) const {

if (isIntAttribute()) {
if (AI.isEnumAttribute()) return false;
if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
if (AI.isIntAttribute()) {
if (getKindAsEnum() == AI.getKindAsEnum())
return getValueAsInt() < AI.getValueAsInt();
return getKindAsEnum() < AI.getKindAsEnum();
}
if (AI.isStringAttribute()) return true;
}

Expand Down Expand Up @@ -482,7 +486,7 @@ AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
FoldingSetNodeID ID;

SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
std::sort(SortedAttrs.begin(), SortedAttrs.end());

for (Attribute Attr : SortedAttrs)
Attr.Profile(ID);
Expand Down
9 changes: 9 additions & 0 deletions unittests/IR/AttributesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ TEST(Attributes, Uniquing) {
TEST(Attributes, Ordering) {
LLVMContext C;

Attribute Align4 = Attribute::get(C, Attribute::Alignment, 4);
Attribute Align5 = Attribute::get(C, Attribute::Alignment, 5);
Attribute Deref4 = Attribute::get(C, Attribute::Dereferenceable, 4);
Attribute Deref5 = Attribute::get(C, Attribute::Dereferenceable, 5);
EXPECT_TRUE(Align4 < Align5);
EXPECT_TRUE(Align4 < Deref4);
EXPECT_TRUE(Align4 < Deref5);
EXPECT_TRUE(Align5 < Deref4);

AttributeSet ASs[] = {
AttributeSet::get(C, 2, Attribute::ZExt),
AttributeSet::get(C, 1, Attribute::SExt)
Expand Down

0 comments on commit 3918651

Please sign in to comment.