Issue 244251
Summary [GlobalISel][AMDGPU] GFX12: mul->shl combine builds an illegal shift amount type and llc aborts
Labels new issue
Assignees
Reporter nodecoda
    
## Summary

On GFX12, `llc -global-isel` aborts in the register bank legalizer for a plain
`getelementptr` with a `<2 x i64>` index vector. `applyCombineMulToShl` builds
the shift amount constant with the type of the multiply, but AMDGPU legalizes a
shift of an `i64` with an `s32` amount, so the combine leaves behind a `G_SHL`
that the legalizer would have rejected.

## Reproducer (IR, not hand-written MIR)

```llvm
define <2 x ptr> @f(<2 x ptr> %p, <2 x i64> %a) {
  %gep = getelementptr i32, <2 x ptr> %p, <2 x i64> %a
  ret <2 x ptr> %gep
}
```

```
$ llc -mtriple=amdgpu12.00 -global-isel -global-isel-abort=2 -O0 t.ll -o /dev/null
llc: AMDGPURegBankLegalizeHelper.cpp:2393:
     bool ...::applyMappingSrc(...): Assertion `Ty == getTyFromID(MethodIDs[i])' failed.
```

`amdgpu12.00`, `amdgpu12.01` and `amdgpu12.51` all fail; `amdgpu6.00`,
`amdgpu8.03`, `amdgpu9.00`, `amdgpu9.0a`, `amdgpu10.30` and `amdgpu11.00` are
fine. The upstream test `llvm/test/CodeGen/AMDGPU/pk-lshl-add-u64.ll`
(`pk_lshl_add_u64_gep`) reproduces it as soon as `-global-isel` is added.

## Reduced to the combine

```sh
$ llc -mtriple=amdgpu12.00 -run-pass=amdgpu-postlegalizer-combiner t.mir -o -
; before
  %3:_(i64) = G_CONSTANT i64 2
  %2:_(i64) = G_SHL %0, %3(i64)
; expected
  %3:_(i32) = G_CONSTANT i32 2
  %2:_(i64) = G_SHL %0, %3(i32)
```

## Why only GFX12

1. Since GFX12 an `i64` `G_MUL` is legal (`GCNSubtarget::hasScalarSMulU64()`,
   used in `AMDGPULegalizerInfo.cpp`), so an `i64` multiply survives the
   legalizer - earlier subtargets split it and the combine never sees it.
2. AMDGPU legalizes shifts with `Shifts.clampScalar(1, S32, S32)`, i.e. the
   amount of an `i64` shift is `s32`.
3. `CombinerHelper::applyCombineMulToShl` uses the multiplicand type for the
   constant, so it emits `G_SHL %0, %3(i64)`.
4. `AMDGPURegBankLegalizeRules.cpp` models the amount as `Vgpr32`
 (`.Div(S64, {{Vgpr64}, {Vgpr64, Vgpr32}})`) and `applyMappingSrc` asserts on
   the mismatch.

## Fix

Use the target's preferred shift amount type, and only apply the combine when
the resulting `G_SHL` is legal (`isLegalOrBeforeLegalizer` keeps the
pre-legalizer behaviour unchanged):

```cpp
  LLT Ty = MRI.getType(MI.getOperand(0).getReg());
 LLT ShiftAmtTy = getTargetLowering().getPreferredShiftAmountTy(Ty);
  return isLegalOrBeforeLegalizer({TargetOpcode::G_SHL, {Ty, ShiftAmtTy}});
...
 LLT ShiftAmtTy = getTargetLowering().getPreferredShiftAmountTy(ShiftTy);
 auto ShiftCst = MIB.buildConstant(ShiftAmtTy, ShiftVal);
```

Same class of fix as `93e71907d8d2` (`[AArch64][GlobalISel] Use
PreferredShiftAmountTy in TruncOfShift combine`).

## Verification

* The IR above and `pk-lshl-add-u64.ll -global-isel` no longer assert
  (amdgpu12.00 / 12.01 / 12.51).
* New MIR test: fails without the fix, passes with it.
* `CodeGen/{AMDGPU,AArch64,X86,RISCV}/GlobalISel` under lit: 1163 + 825
  (217 pass, 1 XFAIL) + 500, identical to the unpatched build.
* Byte-for-byte A/B of `llc -run-pass` output over all 1163 AMDGPU GlobalISel
  `.mir`/`.ll` tests: no differences other than one vreg renumbering.

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

Reply via email to