Skip to content

ILGPU V2.0: Fixed Statements on Arrays, Null Pointer Comparisons + Conditional References to Local Array Elements - #1706

Open
m4rs-mt wants to merge 3 commits into
undecodable_il_errorsfrom
fixed_array_statements
Open

m4rs-mt wants to merge 3 commits into
undecodable_il_errorsfrom
fixed_array_statements

Conversation

@m4rs-mt

@m4rs-mt m4rs-mt commented Oct 31, 2026

Copy link
Copy Markdown
Owner

Builds on #1705. fixed statements on arrays now compile on every backend. Pinning an array created
in a kernel gives a pointer to its first element, and pinning an empty or null array gives null, as in .NET.
Pointers into local and shared memory compared with null, such as &x == null, compile on Metal and OpenCL. A
reference that selects one of two elements of a local array, such as c ? ref a[1] : ref a[2], compiles at O0.

int[] local = new int[4];
fixed (int* p = local)                    // compiles; was "Not supported compare or arithmetic value of type 'None'"
    p[index.X & 3] = index.X * 10;

fixed (int* e = new int[0])
    output[index] = e == null ? -7 : 1;   // -7: the pointer of an empty array is null

int* q = &value;
data[index] = q == null ? -1 : *q;        // compiles on Metal and OpenCL

ref int t = ref c ? ref local[1] : ref local[2];  // compiles at O0

A note on authorship

This PR was done with AI-assistant pair programming (Claude via Claude Code). Every commit carries a
Co-Authored-By: Claude trailer.

What's in this PR

1. Pointers compared with null (Src/ILGPUC/IR/PureValues/Construction/Cast.cs, Compare.cs, Src/ILGPUC/IR/Transformations/InferAddressSpaces.cs)

  • C# compares a pointer with null through the integer zero (ldc.i4.0; conv.u). This became an integer cast to a
    generic pointer. Since ILGPU V2.0: Pointers in Views, Struct Fields and Local Memory #1687 (67b3bb7) the CPU, CUDA and ROCm backends emit it as a typed null pointer, and
    ILGPU V2.0: Pointer Constructs in Kernels #1688 joins pointers with null in phis. Metal and OpenCL still rejected the comparison of a pointer into local or
    shared memory with the generic null pointer as a comparison of distinct pointer types.
  • An integer zero converted to a pointer is now the null pointer, which ILGPU V2.0: Pointer Constructs in Kernels #1688's casts of null pointers keep null.
  • A comparison gives the null pointer the type of the other pointer operand, including its address space. Two null
    pointers compare as equal.
  • Address space inference retypes the null pointer of a comparison, so the other operand can stay a pointer into
    local or shared memory instead of being converted to a generic one.
  • Upstream's OpenCLSourceTests.IntegersAsPointers_AreCastToPointerTypes now expects the typed null pointer
    ((global int*)0) instead of the cast of the integer zero (global void*)0L.

2. Null pointers on the CPU backend (Src/ILGPUC/Backends/CPU/CPUMethodEmitter.Scheduled.cs)

  • The CPU backend emits null pointers as default, which has no type to infer a temporary from. The temporaries of
    uniform phis with a null argument are now declared with the type of the phi. Before, O0 kernels with such phis
    failed to build with CS0266 (nint to int*).

3. References to elements of local arrays at O0 (Src/ILGPUC/IR/Transformations/LowerArrays.cs, Optimizer.cs)

  • At O0, c ? ref a[1] : ref a[2] merges two generic element addresses in a phi. LowerArrays turned each of them
    into an address into the local view of the array, so the phi arguments no longer had the phi's type. Every
    backend failed with "Phi argument ... does not have the phi type".
  • A lowered element address that flows into a phi is now cast back to the address space of the original address.
    This applies to direct uses and to addresses derived from it, such as the field addresses of
    c ? ref a[0].A : ref a[1].B.
  • Metal and OpenCL have no generic pointers. When such a cast remains after the lowering, they infer the address
    spaces once more, which turns the merged addresses back into pointers to local memory. Kernels without such casts
    skip this pass.

4. fixed statements on arrays (Src/ILGPUC/Frontend/CodeGenerator/ControlFlow.cs, Variables.cs, CodeGenerator.cs, Src/ILGPUC/IR/PureValues/Construction/Predicate.cs)

  • C# emits fixed (int* p = array) as p = array == null || array.Length == 0 ? null : &array[0]. It tests the array
    with a brfalse on the array itself, and at the end of the statement it unpins the array by storing null to the
    pinned local. Both failed with "Not supported compare or arithmetic value of type 'None'".
  • Arrays in kernels have no null value of their own, and a null array is empty. The frontend therefore recognizes the
    null test of a pinned array (dup; stloc pinned; brfalse) and lets it fall through to the test for an empty array.
    That test gives the same null pointer and reads the length only once.
  • Unpinning stores the null value of the array type.
  • At O1 and O2 the pointer to the first element and the null pointer become a select. The select now gives the
    null pointer the type of the other pointer, so the result stays a pointer to local memory.
  • At O0 the two pointers meet in a phi, which section 3 handles. This covers arrays whose length is not a constant,
    and fixed (void* p = array), whose pointer reaches the phi through a cast.
  • Docs/03_Advanced/02_Kernels.md now covers fixed statements on arrays: the pointer of an empty array is null,
    and leaving such a statement through break, continue or goto is not supported.

5. Tests (Src/ILGPUC.Tests/)

  • AddressSpaceExecutionTests.NullPointerCompare_ProducesCorrectOutput (TestPrograms/AddressSpace/NullPointerCompare.cs),
    at O0, O1 and O2 and with a Debug-compiled program. It uses four kernels:

    • LocalAddressNullKernel: the address of a local and a pointer local that holds null, compared with null.
    • NullPointerPhiKernel: a phi of null and the address of a local, compared with null in the kernel and in a
      helper that is not inlined.
    • NullPointerPhisKernel: a uniform and a per-thread pointer phi with null arguments.
    • SharedNullPointerKernel: q = even ? p : null; q == null on a pointer into shared memory, which Metal rejected
      before.
  • LocalArrayExecutionTests.RefSelect_ProducesCorrectOutput (TestPrograms/LocalArray/RefSelect.cs), at O0, O1
    and O2. RefSelectKernel, RefSelectIndexKernel and RefFieldSelectKernel write through a reference to one of
    two elements of a local array, or to one of two fields of its elements. The element indices are constant or
    per-thread.

  • LocalArrayExecutionTests.FixedArray_ProducesCorrectOutput (TestPrograms/LocalArray/FixedArray.cs), at O0 and
    O1, with Release- and Debug-compiled programs. The kernels pin:

    • local arrays inside a loop;
    • empty arrays and the null array of a default structure in helpers;
    • arrays whose length is not a constant, through int*, through a byte* cast from it, and through void*.

    They also compare the pointer pinned on an empty array with null.

  • LocalArrayExecutionTests.FixedNullPointers_ProducesCorrectOutput (TestPrograms/LocalArray/FixedNullPointers.cs),
    at O0: a null array and a local array pinned together, whose pointers reach a uniform and a per-thread pointer phi
    with null.

  • LocalPointerBackendTests and FixedArrayBackendTests compile AddressSpaceKernels.LocalAddressNullKernel,
    NullPointerPhiKernel, NullPointerPhisKernel, SharedNullPointerKernel, LocalArrayKernels.RefSelectKernel,
    RefSelectIndexKernel, RefFieldSelectKernel, FixedKernel, FixedNullKernel, FixedSelectKernel and
    FixedVoidKernel with the native compilers of Metal, CUDA, ROCm and OpenCL at O0 and O2.

  • LocalArrayIRTests covers FixedKernel, FixedNullKernel and FixedSelectKernel after the frontend and after
    global optimization. It also covers FixedKernel after the backend transforms of every backend, and
    FixedSelectKernel after the O0 backend transforms of Metal and OpenCL. The IR shows the null test falling
    through to the length test. The 55 new snapshots are in ILGPU V2.0: IR Snapshots for Fixed Statements on Local Arrays ILGPU-Snapshots#11.

  • The execution tests run on every backend with a device.

Depends on

#1705 (based on its head; the base is retargeted once it merges) and
m4rs-mt/ILGPU-Snapshots#11.

Testing

Suite This PR
Full test suite, Apple Silicon (CPU, Metal; CUDA, ROCm and OpenCL compile-only) 12138 passed, 1778 skipped, 0 failed
IR snapshot tests against m4rs-mt/ILGPU-Snapshots#11 (55 new snapshots) all pass (part of the full suite)
Release build with TreatWarningsAsErrors 0 errors
Every commit of the PR build (3/3, 0 warnings)

CUDA, ROCm and OpenCL were compiled with nvcc, hipcc and ocloc but not run on a device.

🤖 Generated with Claude Code

m4rs-mt and others added 3 commits September 21, 2026 22:01
…such as &x == null, which Metal and OpenCL rejected as comparisons of distinct pointer types, by typing null like the other operand.

C# compares a pointer with null through the integer zero (ldc.i4.0;
conv.u), which became an integer cast to a generic pointer. Every integer
zero that is converted to a pointer now becomes the null pointer.
Comparisons give a null pointer the type of the other pointer operand,
two null pointers compare as equal, and address space inference retypes
the null pointer of a comparison instead of keeping the other operand
generic. Metal and OpenCL have no generic pointers to compare a pointer
into local or shared memory with. The OpenCL test of integers cast to
pointers now expects the typed null pointer, ((global int*)0), instead
of a cast of the integer zero, (global void*)0L.

Since the CPU backend emits null pointers as default, which has no type
to infer a temporary from, the temporaries of uniform pointer phis are
now declared with the pointer type.

Co-Authored-By: Claude <noreply@anthropic.com>
… at O0, such as c ? ref a[1] : ref a[2], which failed on every backend with "Phi argument ... does not have the phi type" after the arrays were lowered.

At O0 the element addresses flow into a phi as generic pointers, while
LowerArrays turned them into addresses into the local view of the array.
The lowered element address is now cast back to the address space of the
original one if it flows into a phi, directly or through addresses
derived from it, such as the field addresses of c ? ref a[0].A :
ref a[1].B, since a phi keeps its type.

On Metal and OpenCL, which have no generic pointers, address spaces are
inferred once more after LowerArrays if such a cast of an element address,
of the global of the array or of the pointer of its lowered view remains,
which is rare and cheap.

Co-Authored-By: Claude <noreply@anthropic.com>
…d compare or arithmetic value of type 'None'", by testing the length of the pinned array instead of the array and unpinning it with the null array.

C# tests the array for null with a brfalse on the array itself, and unpins
it by storing null to the pinned local. Arrays have no null value of their
own, and a null array is empty, so the null test now always falls through
to the test for an empty array that follows, which gives the same null
pointer and reads the length once. The frontend stores the null value of
the array type to unpin the array. The ldc.i4.0; conv.u pointer of an
empty array is the null pointer, like every integer zero converted to a
pointer.

At O1 and O2 the pointer to the first element and the null pointer become
a select, which now gives the null pointer the type of the other pointer,
so that address space inference keeps the pointer local instead of
generic. At O0 the pointer flows into a phi with the null pointer, like
the element references of a conditional ref, also for arrays whose length
is not a constant and for fixed (void* p = array), whose pointer reaches
the phi through a pointer cast. The kernel documentation mentions fixed
statements on arrays and that leaving them through break, continue or
goto is not supported.

Co-Authored-By: Claude <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant