Repository navigation
Conversation
…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 was referenced Oct 31, 2026
m4rs-mt
force-pushed
the
pointer_null_checks
branch
from
October 31, 2026 11:26
c682f9b to
e242090
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Builds on #1708.
Unsafe.NullRef<T>()compiles to the null reference of its type, whichtakes the address space of the reference it is compared with or merged into, as
nullpointers do. AtO0, wherethe 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.AsPointeralready 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.
A note on authorship
This PR was done with AI-assistant pair programming (Claude via Claude Code). The commit carries a
Co-Authored-By: Claudetrailer.What's in this PR
1.
Unsafe.NullRefas an intrinsic (Src/ILGPUC/Frontend/Intrinsic/CompilerServices.cs,Src/ILGPUC/Static/Intrinsics/CompilerServices.xml)Unsafe.NullRef<T>()maps to the newUnsafe_NullRefintrinsic, next to ILGPU V2.0: Pointer Constructs in Kernels #1688'sUnsafe_AsPointer. It buildsthe 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.
O0the call of the BCL body was kept, and its generic null reference could not be merged with references tolocals on Metal and OpenCL. Now a non-inlined helper that receives both
ref Unsafe.NullRef<int>()and areference 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 andRelease builds at
O0,O1andO2, with the eight kernels ofTestPrograms/NullPointer/NullChecks.cs:LocalNullCheckKernel) and in a select (LocalNullSelectKernel);ElementNullCheckKernel) and to shared memory (SharedNullCheckKernel);(
HelperNullCheckKernel);Unsafe.NullRefandUnsafe.IsNullRefon a view element (NullRefCheckKernel);LoopNullCheckKernel);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
O0also rely on ILGPU V2.0: Null Arrays and Null Tests in Kernels + Empty Arrays Distinct from Null + Null-Safe Metal Thread Storage #1707, which keepsthread storage away from address 0.
NullPointerIRTests: 74 IR snapshots pin the null checks ofLocalNullCheckKernelandHelperNullCheckKernelafter 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):ReadRefOrDefaulttakesa 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
O0fail.NullPointerKernelsfor source generation and native compilation on every backend.CompilationTestBase.DumpIRStringtakes 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
TreatWarningsAsErrorsCUDA, ROCm and OpenCL were compiled with nvcc, hipcc and ocloc but not run on a device.
🤖 Generated with Claude Code