Issue 162490
Summary [AMDGPU] `sort` in `AMDGPURewriteAGPRCopyMFMAImpl::eliminateSpillsOfReassignedVGPRs()` needs to have strictly weak ordering
Labels backend:AMDGPU
Assignees weiweichen
Reporter weiweichen
    We ran into an `std::sort` crash at 
https://github.com/llvm/llvm-project/blob/d0da85745dac153705bf1e2c465a8f487068085e/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp#L484-L494
 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 to change 

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to