[llvm-branch-commits] [llvm] [AMDGPU] Treat a bitcast between integer and FP types as free (PR #238412)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Nov 3 07:47:06 PST 2026


michaelselehov wrote:

I found this while I studied the register pressure of a rocFFT kernel on gfx950. The SLP vectorizer built a tree with a cost of -1, and two of its bundles were bitcasts from i32 to float, each priced as a saving of 2. Without that phantom saving the tree costs +3 and the vectorizer leaves it scalar.

More details generated by AI below:

<details>
<summary>Details</summary>

### Where the cost comes from

`BasicTTIImpl::getCastInstrCost` returns 0 for a bitcast only if `IntOrPtrSrc == IntOrPtrDst` and the legalized sizes match. A scalar bitcast between an integer and a floating-point type fails the first check and gets the default cost of 1. A vector bitcast passes it, because a vector type is not an integer type. So the costs on AMDGPU before this patch are not consistent:

| bitcast | before | after |
|---|---|---|
| `i16 <-> half`, `i16 <-> bfloat`, `i32 <-> float`, `i64 <-> double` | 1 | 0 |
| `i32 -> <2 x i16>`, `i32 <-> <2 x half>`, `i64 <-> <2 x float>`, `i64 -> <2 x i32>` | 1 | 0 |
| `<4 x i8> -> i32`, `float -> <2 x i16>`, `double -> <2 x float>`, `i128 -> <4 x float>` | 0 | 0 |
| `<2 x i32> <-> <2 x float>`, `<4 x float> -> <2 x i64>`, `<2 x double> -> <4 x i32>` | 0 | 0 |

All of these are moves between values in the same registers.

### Scope

The change is in `GCNTTIImpl::getCastInstrCost`, so it affects only AMDGPU. Pointer bitcasts keep the generic cost. Bitcasts that involve i1 keep their costs too, because a vector of i1 is a lane mask and `bitcast-mask.ll` already prices the packing and unpacking.

### Tests

- `Analysis/CostModel/AMDGPU/bitcast-int-fp.ll` (new in the NFC PR): scalar, scalar to vector and vector bitcasts on gfx900, gfx950 and gfx1201, with and without real true16, and the code-size cost kind.
- `Transforms/SLPVectorizer/AMDGPU/bitcast-int-fp.ll` (new in the NFC PR): in `bitcast_only`, the stored pair is built only from two bitcasts. Before this patch, the vectorizer replaced the two scalar bitcasts with two `insertelement` of i32 and one vector bitcast, at a cost of -2. Now the pair stays scalar. In `bitcast_fmul` the pair also has an fmul each, and it is still vectorized as a packed fmul.

### Testing

- `check-llvm-analysis`, `check-llvm-transforms` and `check-llvm-codegen-amdgpu` pass.
- Performance: I compared the same compiler with and without this patch on gfx942 and gfx950, on llama.cpp, xpu-perf, HIP-Perf, rocFFT, rocRAND, rocSPARSE and rocBLAS. The suites show no significant change caused by the patch.

</details>


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


More information about the llvm-branch-commits mailing list