Skip to content

[AMDGPU] sort in AMDGPURewriteAGPRCopyMFMAImpl::eliminateSpillsOfReassignedVGPRs() needs to have strict weak ordering #162490

@weiweichen

Description

@weiweichen

We ran into an std::sort crash at

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

https://schneide.blog/2010/11/01/bug-hunting-fun-with-stdsort/#:~:text=Ok%2C%20the%20problem%20is%20that,havoc%20inside%20std::sort.&text=The%20really%20annoying%20thing%20about,give%20you%20lot's%20of%20headaches.

Proposing fix with this change
https://github.com/llvm/llvm-project/pull/162493/files

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions