Skip to content

ILGPU V2.0: Unsafe.NullRef as an Intrinsic + Null Checks of Pointers and References - #1709

Open
m4rs-mt wants to merge 1 commit into
integer_pointer_address_spacesfrom
pointer_null_checks
Open

m4rs-mt wants to merge 1 commit into
integer_pointer_address_spacesfrom
pointer_null_checks

Conversation

@m4rs-mt

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

Copy link
Copy Markdown
Owner

Builds on #1708. Unsafe.NullRef<T>() compiles to the null reference of its type, which
takes the address space of the reference it is compared with or merged into, as null pointers do. At O0, where
the call is not inlined, its BCL body returned a generic null reference that Metal and OpenCL could not merge with
references to locals, so a helper called with both did not compile there. Unsafe.AsPointer already compiles since
#1688. The PR also adds tests for null checks of pointers and references on every backend: pointers to locals, view
elements and shared memory, pointers selected by conditions and loops, and helpers that test their parameters for
null.

[MethodImpl(MethodImplOptions.NoInlining)]
static int ReadRefOrDefault(ref int r, int fallback) =>
    Unsafe.IsNullRef(ref r) ? fallback : r;

int value = data[index] * 2;
data[index] = ReadRefOrDefault(ref value, 0) +
    ReadRefOrDefault(ref Unsafe.NullRef<int>(), 60);  // compiles at O0 on Metal and OpenCL

int* p = value > 15 ? &value : null;
data[index] = p != null ? *p + 1 : -1;               // null checks of local pointers, tested on every backend

A note on authorship

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

What's in this PR

1. Unsafe.NullRef as an intrinsic (Src/ILGPUC/Frontend/Intrinsic/CompilerServices.cs, Src/ILGPUC/Static/Intrinsics/CompilerServices.xml)

  • Unsafe.NullRef<T>() maps to the new Unsafe_NullRef intrinsic, next to ILGPU V2.0: Pointer Constructs in Kernels #1688's Unsafe_AsPointer. It builds
    the null value of the return type instead of calling the BCL body. Like other null pointers, it takes the address
    space of the reference it is compared with or merged into.
  • At O0 the call of the BCL body was kept, and its generic null reference could not be merged with references to
    locals on Metal and OpenCL. Now a non-inlined helper that receives both ref Unsafe.NullRef<int>() and a
    reference to a local takes a pointer to local memory and is called with the null pointer of local memory.

2. Tests (Src/ILGPUC.Tests/)

  • NullPointerExecutionTests.NullChecks_ProducesCorrectOutput, run on every backend with a device, in Debug and
    Release builds at O0, O1 and O2, with the eight kernels of TestPrograms/NullPointer/NullChecks.cs:

    • a pointer to a local merged in a phi (LocalNullCheckKernel) and in a select (LocalNullSelectKernel);
    • pointers to a view element (ElementNullCheckKernel) and to shared memory (SharedNullCheckKernel);
    • inlined and non-inlined helpers testing pointer parameters, called with null and with local and view pointers
      (HelperNullCheckKernel);
    • Unsafe.NullRef and Unsafe.IsNullRef on a view element (NullRefCheckKernel);
    • a pointer selected in a loop (LoopNullCheckKernel);
    • a non-inlined helper testing a reference parameter, called with a local and with Unsafe.NullRef
      (RefHelperNullCheckKernel).

    These kernels also cover the typed null pointers of ILGPU V2.0: Pointers in Views, Struct Fields and Local Memory #1687 and the joins of pointers with null of ILGPU V2.0: Pointer Constructs in Kernels #1688 on every
    backend. On Metal and OpenCL they rely on the comparisons of pointers into local and shared memory with null from
    ILGPU V2.0: Fixed Statements on Arrays, Null Pointer Comparisons + Conditional References to Local Array Elements #1706, without which six of the eight kernels fail there.
    On Metal devices that place the first thread-private object of a kernel at address 0, such as the Apple
    Paravirtual device of macOS VMs, the pointers to locals of these kernels at O0 also rely on ILGPU V2.0: Null Arrays and Null Tests in Kernels + Empty Arrays Distinct from Null + Null-Safe Metal Thread Storage #1707, which keeps
    thread storage away from address 0.

  • NullPointerIRTests: 74 IR snapshots pin the null checks of LocalNullCheckKernel and HelperNullCheckKernel
    after the frontend, after global optimization and after the transforms of every backend, such as the integer
    zero converted to a pointer becoming the null pointer (ILGPU V2.0: IR Snapshots for Null Checks of Pointers ILGPU-Snapshots#13).

  • NullPointerIRTests.RefHelperNullCheck_NullRefPointsToLocalMemory (Metal, OpenCL, O0): ReadRefOrDefault takes
    a pointer to local memory and is called with the null pointer of local memory. Without the intrinsic, this test
    and the Metal execution test at O0 fail.

  • NullPointerKernels for source generation and native compilation on every backend.

  • CompilationTestBase.DumpIRString takes an optional backend whose transforms it applies.

Depends on

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

Testing

Suite This PR
Full test suite, Apple Silicon (CPU, Metal; CUDA, ROCm and OpenCL compile-only) 12794 passed, 1853 skipped, 0 failed
Release build with TreatWarningsAsErrors 0 errors
Every commit of the PR builds (0 warnings)

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

🤖 Generated with Claude Code

…a generic null reference that Metal and OpenCL could not merge with local references, and tested null checks of pointers and references on every backend.

Unsafe.NullRef<T>() now builds the null value of its return type, which
takes the address space of the reference it is compared with or merged
into, like other null pointers. The tests cover null checks of pointers to
locals, view elements and shared memory, pointers selected by conditions
and loops, and helpers testing pointer and reference parameters for null,
called with null and with local and global addresses. They run in Debug and
Release builds at O0 to O2. On Metal and OpenCL they rely on comparisons
of pointers to local and shared memory with null, which type null like the
other operand, and they also cover the null pointers and the joins of
pointers with null that the compiler already supports on every backend. 74
IR snapshots pin the null checks of LocalNullCheckKernel and
HelperNullCheckKernel after the frontend, after the global optimizations
and after the transforms of every backend, such as the integer zero
converted to a pointer becoming the null pointer. An IR test checks that
Unsafe.NullRef passed at O0 to a helper that is also called with a
reference to a local is the null pointer of local memory on Metal and
OpenCL. DumpIRString takes the backend whose transforms it applies.

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