Issue 239099
Summary [InstCombine] Vector `llvm.ptrmask` gains an invalid `nonnull` return attribute
Labels new issue
Assignees
Reporter Cerulime
    ## Summary

InstCombine can turn a valid vector `llvm.ptrmask` call into invalid IR by adding a `nonnull` return attribute. The `Intrinsic::ptrmask` handling infers non-nullness without checking whether the return type is a scalar pointer. LLVM permits `ptrmask` on pointer vectors, but `nonnull` is only valid on scalar pointers.

The input below passes the verifier. Running only `instcombine` adds `nonnull` to a `<2 x ptr>` result, and the output verifier aborts with `Attribute 'nonnull' applied to incompatible type!`.

This reproducer has no target triple, memory accesses, or external dependencies. No GPU backend or sanitizer pass is required.

## Environment

Reproduced with an unmodified upstream `llvm-project` build:

- Build revision: `04e0db52562678dcd95d6a04af001c70780e9262` (recorded in the build's generated `llvm/Support/VCSRevision.h`).
- `opt --version`: LLVM 24.0.0git, optimized build with assertions.
- Default target: `x86_64-unknown-linux-gnu`.

## Reproducer

Save as `repro.ll`:

```llvm
declare <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr>, <2 x i64>)

define <2 x ptr> @repro(<2 x ptr> %p) {
  %masked = call <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr> %p, <2 x i64> splat (i64 -60))
  %offset = getelementptr i8, <2 x ptr> %masked, i64 33
  %result = call <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr> %offset, <2 x i64> splat (i64 -16))
  ret <2 x ptr> %result
}
```

Run:

```bash
opt repro.ll -passes=verify -disable-output
opt repro.ll -passes=instcombine -S -o repro.optimized.ll
```

### Actual behavior

The first command succeeds. The second aborts with SIGABRT:

```text
Attribute 'nonnull' applied to incompatible type!
  %result = call nonnull align 32 <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr> %offset12, <2 x i64> splat (i64 -32))
LLVM ERROR: Broken module found, compilation aborted!
```

To inspect the IR immediately after InstCombine:

```bash
opt repro.ll -passes=instcombine -S -o repro.optimized.ll -print-after=instcombine
```

The function printed before the verifier aborts is:

```llvm
define <2 x ptr> @repro(<2 x ptr> %p) {
  %masked = call align 4 <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr> %p, <2 x i64> splat (i64 -60))
  %offset12 = getelementptr i8, <2 x ptr> %masked, i64 32
 %result = call nonnull align 32 <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr> %offset12, <2 x i64> splat (i64 -32))
  ret <2 x ptr> %result
}
```

The invalid attribute is introduced by the optimizer; the input contains no `nonnull` attributes.

## Expected behavior

InstCombine should produce valid IR for vector `ptrmask` calls. It may infer alignment and simplify the GEP and mask, but it should not attach the scalar-only `nonnull` return attribute to a pointer-vector result, even when every lane is known to be non-null.

## Controls

Both of the following functions pass `opt controls.ll -passes=instcombine -S -o controls.optimized.ll`:

```llvm
declare ptr @llvm.ptrmask.p0.i64(ptr, i64)
declare <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr>, <2 x i64>)

define ptr @scalar(ptr %p) {
  %masked = call ptr @llvm.ptrmask.p0.i64(ptr %p, i64 -60)
  %offset = getelementptr i8, ptr %masked, i64 33
  %result = call ptr @llvm.ptrmask.p0.i64(ptr %offset, i64 -16)
  ret ptr %result
}

define <2 x ptr> @vector_alignment_only(<2 x ptr> %p) {
  %result = call <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr> %p, <2 x i64> splat (i64 -16))
  ret <2 x ptr> %result
}
```

The scalar version gains a valid `nonnull align 32` return attribute. The vector alignment-only version gains `align 16` and no `nonnull`. This isolates the failure to non-null attribute inference on pointer vectors, rather than vector `ptrmask` or alignment inference in general.

## Root cause

In `llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp`, the `Intrinsic::ptrmask` case contains:

```cpp
// See if we can deduce non-null.
if (!CI.hasRetAttr(Attribute::NonNull) &&
    (Known.isNonZero() ||
     isKnownNonZero(II, getSimplifyQuery().getWithInstruction(II)))) {
 CI.addRetAttr(Attribute::NonNull);
  Changed = true;
}
```

This condition does not distinguish scalar pointers from pointer vectors. In the reproducer, masking with `-60` clears bit 5 and the low two bits of each pointer. Adding 33 makes bit 5 known to be set, and the final mask preserves that bit. Nonzero information is therefore available, but it cannot be represented by a `nonnull` return attribute on this vector type.

The type rules in `AttributeFuncs::typeIncompatible`, in `llvm/lib/IR/Attributes.cpp`, reject `NonNull` when `!Ty->isPointerTy()`. In contrast, they allow `Alignment` on both pointers and pointer vectors using `isPtrOrPtrVectorTy()`. The `ptrmask` verifier in `llvm/lib/IR/Verifier.cpp` explicitly accepts pointer vectors.

## Candidate fix

Restrict `nonnull` inference to scalar pointer results:

```diff
     // See if we can deduce non-null.
-    if (!CI.hasRetAttr(Attribute::NonNull) &&
+    if (CI.getType()->isPointerTy() && !CI.hasRetAttr(Attribute::NonNull) &&
 (Known.isNonZero() ||
          isKnownNonZero(II, getSimplifyQuery().getWithInstruction(II)))) {
 CI.addRetAttr(Attribute::NonNull);
```

This leaves scalar non-null inference, vector alignment inference, and the remaining `ptrmask` combines intact. The candidate fix has not been applied, built, or tested against this upstream checkout.

A regression test could add the reproducer as a vector counterpart to `ptrmask_combine_add_nonnull` in `llvm/test/Transforms/InstCombine/ptrmask.ll`, checking that the output verifies and retains the inferred alignment without adding `nonnull` to the vector call. The existing scalar case should continue to infer `nonnull`.

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

Reply via email to