-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Closed
Labels
Description
We ran into an std::sort
crash at
llvm-project/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
Lines 484 to 494 in d0da857
sort(StackIntervals, [](const LiveInterval *A, const LiveInterval *B) { | |
/// Sort heaviest intervals first to prioritize their unspilling | |
if (A->weight() > B->weight()) | |
return true; | |
if (A->getSize() > B->getSize()) | |
return true; | |
// Tie breaker by number to avoid need for stable sort | |
return A->reg().stackSlotIndex() < B->reg().stackSlotIndex(); | |
}); |
with input data
sort 0x561ecd3d3db0,0x561eaba91d10 25
weight 0.000000e+00,0.000000e+00
size 650370,662754
slot 732,733
The logic for the current comparator of the sort is not strict weak order for this case, because both cmp(a, b)
(a.slot < b.slot) and cmp(b, a)
(b.size > a.size) are true
.
However, std::sort
requires strict weak ordering, otherwise it will access out of range / crash with memory corruption:
https://stackoverflow.com/questions/24048022/what-causes-stdsort-to-access-address-out-of-range
Proposing fix with this change
https://github.com/llvm/llvm-project/pull/162493/files