Skip to content

ILGPU V2.0: Pointer Constructs in Kernels - #1688

Open
m4rs-mt wants to merge 6 commits into
pointer_type_codegenfrom
frontend_pointer_fixes
Open

m4rs-mt wants to merge 6 commits into
pointer_type_codegenfrom
frontend_pointer_fixes

Conversation

@m4rs-mt

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

Copy link
Copy Markdown
Owner

Builds on #1687. Structures that point to their own types compile, native integers and pointer/integer conversions
compute on integer values, Unsafe.AsPointer yields element addresses, and pointer arithmetic subtracts offsets,
addresses elements of any size and joins pointers with null.

unsafe struct Node { public Node* Next; public int Value; }

Node a, b; b.Next = null; b.Value = 10; a.Next = &b; a.Value = index;
int sum = 0;
for (Node* n = &a; n != null; n = n->Next) sum += n->Value;   // index + 10
nint v = index * 3 + 1; long bits = (long)(v >> 1);            // native integers
int* p = (int*)Unsafe.AsPointer(ref data[index]);              // element address
int* q = first + 3 - index;                                    // element arithmetic

A note on authorship

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

What's in this PR

1. Structures pointing to themselves (Src/ILGPUC/IR/TypeInformationManager.cs, Src/ILGPUC/IR/ModuleValues/Construction/ModuleBuilder.cs, Src/ILGPUC/Frontend/CodeGenerator/Fields.cs)

  • Type information and IR types track the structures being created. A pointer to one of them, directly or through
    other structures, is an opaque pointer field in the structure type. Loads from such fields convert their values
    back to the pointer type, and stores convert them to the field type.
  • Structures holding arrays of themselves report ILGPU1200 at the kernel that uses them.

2. Native integers and pointer/integer conversions (Src/ILGPUC/Frontend/CodeGenerator/{Convert,Arithmetic,Compare}.cs, Src/ILGPUC/Frontend/DisassemblerDriver.cs, Src/ILGPUC/IR/PureValues/Construction/{Cast,Compare}.cs)

  • Pointers convert to integers and floats through their native integer values; conv.u zero-extends unsigned
    integers.
  • Arithmetic, shifts, negation and complement of nint/nuint compute on integer values. Signed comparisons and
    comparisons with integers compare integer values. Address strides keep their element addressing.
  • The IR builder folds pointer/integer cast pairs, computes native-integer arithmetic on integer operands, and
    compares integers cast to pointers as integers.

3. Unsafe.AsPointer (Src/ILGPUC/Static/Intrinsics/CompilerServices.xml, Src/ILGPUC/Frontend/Intrinsic/CompilerServices.cs)

  • Unsafe.AsPointer(ref x) converts the reference to a pointer to the same element.

4. Pointer arithmetic and null pointers (Src/ILGPUC/Frontend/CodeGenerator/Arithmetic.cs, Src/ILGPUC/IR/PureValues/Construction/Cast.cs, Src/ILGPUC/IR/Transformations/SSABuilder.cs)

  • Offsets subtracted from pointers are element addresses with negated indices.
  • Pointers to structures of any size are offset by their elements, or by bytes when the pointer's element type
    differs from the stride.
  • Phis joining pointers of different types, such as an element address and a null pointer, cast them to a common
    pointer type. Pointers compare with null pointers as pointers.

5. CPU kernels (Src/ILGPUC/Backends/CPU/CPUMethodEmitter.cs, Src/ILGPUC/Backends/CPU/VectorBufferAllocator.cs)

  • Per-lane field addresses use the field offsets of the structure type, which include the padding that aligns each
    field. Ordinary padded structures, such as ones mixing ints and longs, previously read wrong values on the CPU, as
    did structures holding a pointer and an int.
  • Pointer casts convert per-lane addresses to integers and per-lane integers to addresses lane by lane.
  • The views and indices of element addresses stay in use until the last load or store through them, also when the
    addresses are passed to helpers or used in pointer differences.

6. Tests (Src/ILGPUC.Tests/)

  • NonKernelTests/SelfReferentialPointerTests.cs: structures pointing to their own types, directly, through a struct
    value or through another struct, generate source on all backends; their self-pointing fields are opaque pointers in
    the IR type; structures holding arrays of themselves report ILGPU1200 at the local's location. On Metal and OpenCL
    the kernels that store a local's address in a local struct (ListKernel, SelfKernel, MutualKernel) report the
    located "does not support generic pointers" diagnostic.
  • ConvertExecutionTests.NativeInt_ProducesCorrectOutput (TestPrograms/ConvertInt/NativeInt.cs): nint/nuint
    locals and helper parameters with arithmetic, shifts, comparisons with 0, signed and unsigned comparisons, negation,
    complement and zero extension, at O0, O1 and O2.
  • ConvertExecutionTests.PointerInt_ProducesCorrectOutput (TestPrograms/ConvertInt/PointerInt.cs): (long)&x,
    (nint)&x, (int*)address, (int*)native, an address stored to memory and converted back to a pointer, and
    pointer differences, on CPU, Metal, CUDA, ROCm and OpenCL.
  • ArrayViewExecutionTests.AsPointer_ProducesCorrectOutput (TestPrograms/ArrayView/AsPointer.cs): Unsafe.AsPointer
    of a view element and of a local.
  • AddressSpaceExecutionTests.PointerArithmetic_ProducesCorrectOutput (TestPrograms/AddressSpace/PointerArithmetic.cs):
    constant and variable offsets added to and subtracted from int, long and 12-byte structure pointers, at O0, O1, O2
    and from Debug IL.
  • AddressSpaceExecutionTests.PointerSelect_ProducesCorrectOutput (TestPrograms/AddressSpace/PointerSelect.cs): a
    pointer joining an element address with null on CPU, Metal, CUDA, ROCm and OpenCL.
  • StructureExecutionTests.PointerFieldPadding_ProducesCorrectOutput (TestPrograms/Structure/PointerFieldPadding.cs):
    the second of two padded structures in a local struct — holding a pointer and an int, or ints and longs — read
    through a pointer to the first one, in a helper and in the kernel, at O0, O1 and O2.
  • ArrayViewExecutionTests.ElementRefStore_ProducesCorrectOutput (TestPrograms/ArrayView/ElementRefStore.cs): a
    reference to a view element stored through and passed to a helper after other elements were stored, at O0, O1 and
    O2.
  • StructureExecutionTests.SelfReferentialPointer_ProducesCorrectOutput (TestPrograms/Structure/SelfReferentialPointer.cs):
    a linked list of locals, a node read by value and indexed, a struct whose only field points to itself, and two
    structs pointing to each other, on the backends with a generic address space (CPU, CUDA, ROCm), at O0, O1, O2 and
    from Debug IL.

Depends on

#1687 (based on its head; the base is retargeted once it merges).

Testing

Suite This PR
Full test suite, Apple Silicon (CPU, Metal, compile-only for all backends) 10943 passed, 2000 skipped, 0 failed
CUDA backend tests with nvcc, NVIDIA GB10 1131 passed, 0 failed
CUDA execution tests, NVIDIA GB10 477 passed, 3 skipped, 0 failed
Release build with TreatWarningsAsErrors 0 errors
Every commit of the PR builds

🤖 Generated with Claude Code

m4rs-mt and others added 6 commits October 18, 2026 16:40
…by typing the fields that close the cycle as opaque pointers, and reported structures holding arrays of themselves as unsupported.

Co-Authored-By: Claude <noreply@anthropic.com>
…ts and comparisons and of conversions between pointers and integers by computing them on the integer values of native integers.

Co-Authored-By: Claude <noreply@anthropic.com>
… to a pointer instead of failing with an internal compiler error.

Co-Authored-By: Claude <noreply@anthropic.com>
…offsets of pointers to structures of any size and joins of pointers with null, which were miscompiled or raised internal compiler errors.

Co-Authored-By: Claude <noreply@anthropic.com>
…nter and long fields are padded to their alignment and for structures that point to their own types.

Co-Authored-By: Claude <noreply@anthropic.com>
… casts of vectorized CPU kernels, and kept the views and indices of element addresses alive until the loads and stores through them.

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