Repository navigation
Conversation
…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>
m4rs-mt
force-pushed
the
fixed_array_statements
branch
from
October 31, 2026 05:04
751dc7a to
2eb6c1f
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 #1705.
fixedstatements on arrays now compile on every backend. Pinning an array createdin 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. Areference that selects one of two elements of a local array, such as
c ? ref a[1] : ref a[2], compiles atO0.A note on authorship
This PR was done with AI-assistant pair programming (Claude via Claude Code). Every commit carries a
Co-Authored-By: Claudetrailer.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)ldc.i4.0; conv.u). This became an integer cast to ageneric 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.
pointers compare as equal.
local or shared memory instead of being converted to a generic one.
OpenCLSourceTests.IntegersAsPointers_AreCastToPointerTypesnow 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)default, which has no type to infer a temporary from. The temporaries ofuniform phis with a null argument are now declared with the type of the phi. Before,
O0kernels with such phisfailed to build with CS0266 (
ninttoint*).3. References to elements of local arrays at O0 (
Src/ILGPUC/IR/Transformations/LowerArrays.cs,Optimizer.cs)O0,c ? ref a[1] : ref a[2]merges two generic element addresses in a phi. LowerArrays turned each of theminto 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".
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.spaces once more, which turns the merged addresses back into pointers to local memory. Kernels without such casts
skip this pass.
4.
fixedstatements on arrays (Src/ILGPUC/Frontend/CodeGenerator/ControlFlow.cs,Variables.cs,CodeGenerator.cs,Src/ILGPUC/IR/PureValues/Construction/Predicate.cs)fixed (int* p = array)asp = array == null || array.Length == 0 ? null : &array[0]. It tests the arraywith a
brfalseon the array itself, and at the end of the statement it unpins the array by storingnullto thepinned local. Both failed with "Not supported compare or arithmetic value of type 'None'".
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.
O1andO2the pointer to the first element and the null pointer become a select. The select now gives thenull pointer the type of the other pointer, so the result stays a pointer to local memory.
O0the 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.mdnow coversfixedstatements on arrays: the pointer of an empty array isnull,and leaving such a statement through
break,continueorgotois not supported.5. Tests (
Src/ILGPUC.Tests/)AddressSpaceExecutionTests.NullPointerCompare_ProducesCorrectOutput(TestPrograms/AddressSpace/NullPointerCompare.cs),at
O0,O1andO2and 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 ahelper that is not inlined.
NullPointerPhisKernel: a uniform and a per-thread pointer phi with null arguments.SharedNullPointerKernel:q = even ? p : null; q == nullon a pointer into shared memory, which Metal rejectedbefore.
LocalArrayExecutionTests.RefSelect_ProducesCorrectOutput(TestPrograms/LocalArray/RefSelect.cs), atO0,O1and
O2.RefSelectKernel,RefSelectIndexKernelandRefFieldSelectKernelwrite through a reference to one oftwo 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), atO0andO1, with Release- and Debug-compiled programs. The kernels pin:int*, through abyte*cast from it, and throughvoid*.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 phiwith null.
LocalPointerBackendTestsandFixedArrayBackendTestscompileAddressSpaceKernels.LocalAddressNullKernel,NullPointerPhiKernel,NullPointerPhisKernel,SharedNullPointerKernel,LocalArrayKernels.RefSelectKernel,RefSelectIndexKernel,RefFieldSelectKernel,FixedKernel,FixedNullKernel,FixedSelectKernelandFixedVoidKernelwith the native compilers of Metal, CUDA, ROCm and OpenCL atO0andO2.LocalArrayIRTestscoversFixedKernel,FixedNullKernelandFixedSelectKernelafter the frontend and afterglobal optimization. It also covers
FixedKernelafter the backend transforms of every backend, andFixedSelectKernelafter theO0backend transforms of Metal and OpenCL. The IR shows the null test fallingthrough 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
TreatWarningsAsErrorsCUDA, ROCm and OpenCL were compiled with nvcc, hipcc and ocloc but not run on a device.
🤖 Generated with Claude Code