[all-commits] [llvm/llvm-project] 4d7ba1: [IR] Preserve pointer-byte bitcasts around addrspa...

Drew Kersnar via All-commits all-commits at lists.llvm.org
Tue Jun 9 07:16:18 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 4d7ba1f5ef6402183372e2ea6358c53a9c182868
      https://github.com/llvm/llvm-project/commit/4d7ba1f5ef6402183372e2ea6358c53a9c182868
  Author: Drew Kersnar <dkersnar at nvidia.com>
  Date:   2026-06-09 (Tue, 09 Jun 2026)

  Changed paths:
    M llvm/lib/IR/Instructions.cpp
    M llvm/test/Transforms/InstCombine/addrspacecast.ll

  Log Message:
  -----------
  [IR] Preserve pointer-byte bitcasts around addrspacecast (#202454)

This fixes cast-pair elimination for addrspacecast combined with
pointer/byte bitcast.

The LLVM LangRef defines [bN byte
types](https://llvm.org/docs/LangRef.html#byte-type) as raw memory data
in SSA registers, where each bit may be an integer bit, part of a
pointer value, or poison.

The LangRef permits pointer-to-byte bitcast: the [bitcast .. to
instruction](https://llvm.org/docs/LangRef.html#bitcast-to-instruction)
says that if the source type is a pointer, the destination type must be
a pointer or a byte/vector-of-bytes type of the same size.

The same [bitcast .. to
section](https://llvm.org/docs/LangRef.html#bitcast-to-instruction) also
defines byte-to-pointer behavior: when the destination type is a pointer
type, a byte value whose bits all come from the same correctly ordered
pointer produces that pointer; otherwise it produces a pointer with the
address encoded by the input and no provenance.

By contrast, [addrspacecast ..
to](https://llvm.org/docs/LangRef.html#addrspacecast-to-instruction) is
only valid from a pointer or vector-of-pointers value to a pointer type
in a different address space.

So these two-step sequences are valid:

```
%p = addrspacecast ptr addrspace(1) %x to ptr
%b = bitcast ptr %p to b64
```
```
%p = bitcast b64 %x to ptr
%q = addrspacecast ptr %p to ptr addrspace(1)
```
But they cannot be folded into a single addrspacecast, because that
would require addrspacecast to cast directly to or from bN, which the
LangRef does not allow.

This patch prevents cast-pair elimination from performing those invalid
folds and adds InstCombine regression coverage for both directions. Both
tests crashed before this patch. This patch was created with the
assistance of AI, based on a real case I found while working on
https://github.com/llvm/llvm-project/pull/177908. I'm pretty sure I'm
reading the langref correctly, but please let me know if I am mistaken.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list