Issue 241630
Summary [PowerPC][GlobalISel] G_AND/G_OR/G_XOR: bitcastIf(typeIsNot(0, V4S32)) asserts on non-128-bit vector types
Labels new issue
Assignees
Reporter nodecoda
    ## Summary

The PowerPC GlobalISel `G_AND`/`G_OR`/`G_XOR` rule ends with

```cpp
getActionDefinitionsBuilder({G_AND, G_OR, G_XOR})
 .legalFor({S64, V4S32})
    .clampScalar(0, S64, S64)
 .bitcastIf(typeIsNot(0, V4S32), changeTo(0, V4S32));
```

The predicate only says "not v4s32"; it does not require the type to be 128 bits
wide. `bitcastIf` then asks for a `Bitcast` to `v4s32`, and `mutationIsSane()`
rejects any `Bitcast` whose total size changes.  So every vector type other than
`v4s32` that reaches this rule aborts an assertions build.  In a release build
the assert is compiled out and the size-changing mutation is handed to the
legalizer.

This is reachable from valid IR, not just hand-written MIR.

## Reproducer

```llvm
define <16 x i32> @f(<16 x i32> %a, <16 x i32> %b) {
  %r = and <16 x i32> %a, %b
  ret <16 x i32> %r
}
```

```console
$ llc -mtriple=powerpc64le-unknown-linux-gnu -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`, with and without `-mcpu=pwr8/pwr9`.
`<4 x i32>` is fine, and the same IR is fine with `-global-isel=false`.

Expected: legalize the wide `and` (split/expand), or report the type as
unhandled.  Crashing on valid IR is not one of the options.

## Suggested fix

Require the width in the predicate, which is what the bitcast actually needs:

```cpp
.bitcastIf(all(typeIsNot(0, V4S32), sizeIs(0, 128)),
           changeTo(0, V4S32));
```

`sizeIs` already exists in `LegalizerPredicates.h`.  X86's equivalent
`bitcastIf` is written with a size check for the same reason.

## Environment

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

Recorded on trunk `700e551714cc`; `PPCLegalizerInfo.cpp` is byte-identical to
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