Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BranchFolding] Remove dubious assert from operator< #71639

Merged
merged 1 commit into from
Nov 9, 2023

Conversation

nikic
Copy link
Contributor

@nikic nikic commented Nov 8, 2023

MergePotentialElts::operator< asserts that the two elements being compared are not equal. However, sorting functions are allowed to invoke the comparison function with equal arguments (though they usually don't for efficiency reasons).

There is an existing special-case that disables the assert if _GLIBCXX_DEBUG is used, which may invoke the comparator with equal args to verify strict weak ordering. I believe libc++ also has strict weak ordering checks under some options nowadays.

Recently, #71312 was reported, where a change to glibc's qsort_r implementation can also result in comparison between equal elements. From what I understoo,d this is an inefficiency that will be fixed on the glibc side as well, but I think at this point we should just remove this assertion.

Fixes #71312.

`MergePotentialElts::operator<` asserts that the two elements being
compared are not equal. However, sorting functions are allowed
to invoke the comparison function with equal arguments (though they
usually don't for efficiency reasons).

There is an existing special-case that disables the assert if
_GLIBCXX_DEBUG is used, which may invoke the comparator with equal
args to verify strict weak ordering. I believe libc++ also has
strict weak ordering checks under some options nowadays.

Most recently llvm#71312 was reported, where a change to glibc's qsort_r
implementation can also result in comparison between equal elements.
From what I understood this is an inefficiency that will be fixed
on the glibc side as well, but I think at this point we should just
remove this assertion.

Fixes llvm#71312.
Comment on lines -488 to -494
// _GLIBCXX_DEBUG checks strict weak ordering, which involves comparing
// an object with itself.
#ifndef _GLIBCXX_DEBUG
llvm_unreachable("Predecessor appears twice");
#else
return false;
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really see the point of asserting this in the first place

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the point is to ensure that the array being sorted does not contain a pair of elements that compare equal, in which case it's more efficient to verify that by inspecting the sorted array:

for (MPIterator i = MergePotentials.begin(), n = std::next(i);
     n != MergePotentials.end();
     i = n, n++)
    assert(*i < *n);

@tstellar
Copy link
Collaborator

tstellar commented Nov 8, 2023

Do we need to backport this to release/17.x ?

@nikic nikic merged commit 74a76a2 into llvm:main Nov 9, 2023
3 checks passed
@fweimer-rh
Copy link
Contributor

Do we need to backport this to release/17.x ?

We don't know yet whether the glibc workaround is effective. In any case, it's something that is likely to impact other systems as well.

@amonakov
Copy link

amonakov commented Nov 9, 2023

Unfortunate that this was merged without acknowledging my response to @arsenm about the logic of the assert.

tru pushed a commit that referenced this pull request Nov 14, 2023
`MergePotentialElts::operator<` asserts that the two elements being
compared are not equal. However, sorting functions are allowed to invoke
the comparison function with equal arguments (though they usually don't
for efficiency reasons).

There is an existing special-case that disables the assert if
_GLIBCXX_DEBUG is used, which may invoke the comparator with equal args
to verify strict weak ordering. I believe libc++ also has strict weak
ordering checks under some options nowadays.

Recently, #71312 was reported, where a change to glibc's qsort_r
implementation can also result in comparison between equal elements.
From what I understood, this is an inefficiency that will be fixed on
the glibc side as well, but I think at this point we should just remove
this assertion.

Fixes #71312.

(cherry picked from commit 74a76a2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SIGILL with new libc qsort_r implementation in BranchFolder::MergePotentialsElt
5 participants