[llvm] [AMDGPU] Change the representation of double literals in operands (PR #68740)

Stanislav Mekhanoshin via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 11 11:32:25 PDT 2023


================
@@ -2241,7 +2242,10 @@ void AMDGPUOperand::addLiteralImmOperand(MCInst &Inst, int64_t Val, bool ApplyMo
       return;
     }
 
-    Inst.addOperand(MCOperand::createImm(Lo_32(Val)));
+    if (isInt<32>(Val) || isUInt<32>(Val))
+      Val = AMDGPU::isSISrcFPOperand(InstDesc, OpNum) ? Val << 32 : Lo_32(Val);
----------------
rampitec wrote:

Lo_32 for integers is used because inst printer would just use ` O << formatHex(static_cast<uint64_t>(Imm));` and since we have full 64-bits there the output will look like this if the value is unmasked:
```
llvm-mc -arch=amdgcn -mcpu=tonga -show-encoding  <<< 's_mov_b64_e32 s[0:1], -54321'
        s_mov_b64 s[0:1], 0xffffffffffff2bcf    ; encoding: [0xff,0x01,0x80,0xbe,0xcf,0x2b,0xff,0xff]
```
This is technically correct but inconvenient and not what we are expecting in our tests too:
```
// SICI: s_mov_b64 s[0:1], 0xffff2bcf ; encoding: [0xff,0x04,0x80,0xbe,0xcf,0x2b,0xff,0xff]
// GFX89: s_mov_b64 s[0:1], 0xffff2bcf ; encoding: [0xff,0x01,0x80,0xbe,0xcf,0x2b,0xff,0xff]
s_mov_b64_e32 s[0:1], -54321
```

For the large FP literals we do the validation:
```
llvm-mc -arch=amdgcn -mcpu=gfx1030 -show-encoding  <<< 'v_add_f64 v[0:1], 100.1, v[0:1]'
<unknown>:0: warning: Can't encode literal as exact 64-bit floating-point operand. Low 32-bits will be set to zero
        v_add_f64 v[0:1], 0x40590666, v[0:1]    ; encoding: [0x00,0x00,0x64,0xd5,0xff,0x00,0x02,0x00,0x66,0x06,0x59,0x40]
```
But we need to truncate the the value as in the warning. If we don't there will be an error following the warning and this would change current behavior:
```
<unknown>:0: warning: Can't encode literal as exact 64-bit floating-point operand. Low 32-bits will be set to zero
<stdin>:1:19: error: invalid operand for instruction
v_add_f64 v[0:1], 100.1, v[0:1]
```
This is for the FP literal. For the hex string the situation just does not happen and we are getting the error instead because the constant does not pass `isLiteralImm()` check:
```
llvm-mc -arch=amdgcn -mcpu=gfx1030 -show-encoding  <<< 'v_add_f64 v[0:1], 0xa12345678, v[0:1]'
<stdin>:1:19: error: invalid operand for instruction
v_add_f64 v[0:1], 0xa12345678, v[0:1]
                  ^
```
Note that I have no intent to introduce any visible changes, just to change the internal operand representation and keep all existing tests passing.

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


More information about the llvm-commits mailing list