Issue 241631
Summary [AArch64][GlobalISel] arm64_32: shufflevector of <2 x ptr> asserts in mutationIsSane (bitcast to <2 x i64> changes size)
Labels new issue
Assignees
Reporter nodecoda
    ## Summary

The AArch64 `G_SHUFFLE_VECTOR` rule ends with

```cpp
.bitcastIf(isPointerVector(0), [=](const LegalityQuery &Query) {
  // Bitcast pointers vector to i64.
  const LLT DstTy = Query.Types[0];
  return std::pair(
      0, LLT::vector(DstTy.getElementCount(), LLT::integer(64)));
});
```

which assumes 64-bit pointers.  On `arm64_32` (32-bit pointers) `<2 x p0>` is
64 bits, but the bitcast target `<2 x i64>` is 128 bits, so `mutationIsSane()`
rejects the `Bitcast` and an assertions build aborts; a release build gets a
size-changing bitcast.

This is reachable from valid IR.

## Reproducer

```llvm
target datalayout = "e-p:32:32-i64:64-i128:128-n32:64-S128"
target triple = "arm64_32-apple-ios7.0"

define <2 x ptr> @f(<2 x ptr> %a, <2 x ptr> %b) {
  %r = shufflevector <2 x ptr> %a, <2 x ptr> %b, <2 x i32> <i32 0, i32 3>
  ret <2 x ptr> %r
}
```

```console
$ llc -global-isel -O2 repr.ll -o /dev/null
llc: llvm/lib/CodeGen/GlobalISel/LegalizerInfo.cpp:200:
 llvm::LegalizeActionStep llvm::LegalizeRuleSet::apply(const llvm::LegalityQuery&) const:
  Assertion `mutationIsSane(Rule, Query, Mutation) && "legality mutation invalid for match"' failed.
```

Fails at `-O0`, `-O1`, `-O2` and `-O3`.  The same IR on LP64
(`aarch64-apple-ios7.0` with `e-p:64:64`) compiles fine.

Note that simply bitcasting to a same-width integer vector is not enough: there
is no 32-bit `G_PTRTOINT`/`G_INTTOPTR` vector rule to convert it back
(AArch64 registers only `{i64, p0}` and `{v2i64, v2p0}`), so the legalizer then
fails with `unable to legalize instruction: %4:_(<2 x p0>) = G_INTTOPTR ...`.

## Suggested fix

Only do the bitcast when it preserves the size, i.e. when the pointer element
is 64 bits, and leave the 32-bit case to the fallback:

```cpp
.bitcastIf(
    [](const LegalityQuery &Query) {
 const LLT Ty = Query.Types[0];
      return Ty.isPointerVector() && Ty.getScalarSizeInBits() == 64;
    },
    [=](const LegalityQuery &Query) {
      const LLT DstTy = Query.Types[0];
      return std::pair(0, LLT::vector(DstTy.getElementCount(),
 LLT::integer(64)));
    });
```

With that change on `arm64_32` the query no longer aborts: it reports
`unable to legalize G_SHUFFLE_VECTOR`, and with `-global-isel-abort=0` it falls
back to SelectionDAG and produces the same assembly as `-global-isel=false`.
LP64 codegen is unchanged (`legalize-shuffle-vector.mir` is byte-identical).

## Related

#241336 is the same family on `arm64_32`: `<4 x ptr>` expressions assert
earlier, in `IRTranslator`/call lowering (`mergeVectorRegsToResultRegs`), so
fixing this legalizer rule alone does not make `<4 x ptr>` work.

## Environment

```
LLVM version 24.0.0git
Optimized build with assertions.
Default target: x86_64-unknown-linux-gnu
Host CPU: znver2
```

Recorded on trunk `700e551714cc`; the rule quoted above is identical from
`9bfc850b` through upstream main `0dc8e3c1`.  Built with
`BUILD_SHARED_LIBS=ON`, RelWithDebInfo + assertions, Linux x86_64.

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

Reply via email to