Issue 244426
Summary [AMDGPU][GlobalISel] Inline asm with SGPR constraints produces illegal VGPR to SGPR copies
Labels new issue
Assignees
Reporter nodecoda
    ## Summary

With `-global-isel`, a divergent value passed to an inline asm operand that has
an SGPR constraint is lowered to a direct VGPR to SGPR copy, and the backend
then fails with `illegal VGPR to SGPR copy`. The SelectionDAG path handles this
in `SITargetLowering::LowerINLINEASM` (#176330, which inserts
`llvm.amdgcn.readfirstlane` for divergent SGPR inputs); GlobalISel has no
equivalent.

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

```llvm
define void @f(ptr addrspace(1) %p) {
  %v = load i32, ptr addrspace(1) %p
  call void asm sideeffect "; use $0", "{m0}"(i32 %v)
 ret void
}
```

```
$ llc -mtriple=amdgpu9.50 -global-isel -global-isel-abort=2 t.ll -o /dev/null
error: <unknown>:0:0: in function f void (ptr addrspace(1)): illegal VGPR to SGPR copy
```

The same input is fine with SelectionDAG:

```
v_readfirstlane_b32 s0, v0
s_mov_b32 m0, s0
```

Three shapes reproduce (`i32` `"s"`, `i32` `"{m0}"`, `<4 x i32>` `"s"`); the
`<4 x i32>` case ends up as `%15:sgpr_128 = COPY %9:vreg_128_align2` and needs
four readfirstlanes.

## Root cause

* `InlineAsmLowering::lowerInlineAsm` materializes a register-class operand with
  `buildAnyextOrCopy()`. At that point there is no divergence or register bank
  information, so a divergent value is copied straight into an SGPR-class vreg
  or a physical SGPR such as `$m0`.
* `SIFixSGPRCopies`, which fixes exactly these copies on the SelectionDAG path,
  returns early for GlobalISel-selected functions
  (`if (MF.getProperties().hasSelected()) return false;`, originally from
  d9e8b2cbcccc "AMDGPU/GlobalISel: Skip DAG hack passes on selected
  functions"), and it is not added to the GlobalISel pipeline at all
  (`GCNPassConfig::addGlobalInstructionSelect()` /
  `AMDGPUCodeGenPassBuilder::addGlobalInstructionSelect()` only add
 `InstructionSelect`).

The copy exists before instruction selection, which distinguishes this from
#62035 (`selectImpl()` import) and from the pattern-selection problem in
#61468: `llc -stop-after=ir-translator` already shows

```
%11:_(i32) = G_LOAD %0(p1) :: (load (i32) from %ir.p, addrspace 1)
$m0 = COPY %11(i32)
INLINEASM &"; use $0", sideeffect attdialect, reguse, $m0
```

## Affected upstream tests

Found by compiling the AMDGPU test suite with `-global-isel
-global-isel-abort=2`; all of them pass with SelectionDAG:

* `CodeGen/AMDGPU/inline-asm-vgpr-sgpr-copy.ll` (5 functions)
* `CodeGen/AMDGPU/spill-offset-calculation.ll` (2 functions)
* `CodeGen/AMDGPU/fix-sgpr-copies-readfirstlane-av-register-regression.ll`
 (`av_class_to_m0`)

## Fix

I have a local fix and will send it as a separate patch: run the inline asm
part of `SIFixSGPRCopies` after GlobalISel instruction selection, and for each
SGPR-constrained inline asm input that is materialized by a copy from a VGPR,
insert `v_readfirstlane_b32` once per 32-bit piece (physical destinations such
as `m0` need an `s_mov_b32`, since `m0` is not a valid destination for
`v_readfirstlane_b32`).

Running the whole `SIFixSGPRCopies` for GlobalISel does not work: it rewrites
register classes assuming pre-isel invariants and produces invalid MIR
(`%68:sreg_64 = S_AND_B64 %57:sreg_32, $exec` in
`fix-sgpr-copies-readfirstlane-av-register-regression.ll`), so the fix is
restricted to the inline asm copies.

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

Reply via email to