[llvm] [MIPS] Sign-extend subwords when expanding atomic max/min (PR #89246)

Jovan Dmitrović via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 19 00:48:02 PDT 2024


================
@@ -1118,6 +1118,8 @@ define i16 @test_max_16(ptr nocapture %ptr, i16 signext %val) {
 ; MIPSEL-NEXT:    srav $7, $7, $10
 ; MIPSEL-NEXT:    seh $2, $2
 ; MIPSEL-NEXT:    seh $7, $7
+; MIPSEL-NEXT:    sllv $2, $2, $10
----------------
jdmitrovic-syrmia wrote:

> I am sorry that I don't understand it well. `seh` does be sign-extended. The result of `seh` is good enough for `slt`. Why do we need to extend them to 32bit value?

Because `slt` compares signed integers. When comparing subwords, we need to take the sign of the subwords into consideration. When the subword isn't at the MSB spot, we get the result we didn't expect.

> And sllv here may make the result incorrect.
The return value should be a signed int16, while with sllv it will be (sign int16)<<$10.
Note, $10 here contains the offset of a int16 in the a word, it may be 0 or 16.

Correct, $10 contains the offset. The code _after_ my changes needs the subwords to be placed with the provided offset inside a word. That is what #77072 didn't do: the subword was shifted to the LSB spot and left there, causing unexpected behavior in the subsequent code.

https://github.com/llvm/llvm-project/pull/89246


More information about the llvm-commits mailing list