Skip to content

ILGPU V2.0: Named Errors for Views, Pointers and Objects in Static Fields - #1702

Open
m4rs-mt wants to merge 5 commits into
recursive_type_errorsfrom
static_field_diagnostics
Open

m4rs-mt wants to merge 5 commits into
recursive_type_errorsfrom
static_field_diagnostics

Conversation

@m4rs-mt

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

Copy link
Copy Markdown
Owner

Builds on #1701. The compiler reads the values of static fields when it compiles a kernel, so a view
or a pointer in a static field never refers to device memory, and a kernel reading a static readonly ArrayView<int>
compiled to reads through a null pointer. Such loads are now reported as the new ILGPU1214 at the load of the field,
naming the field and its type. Static strings, class instances and structures holding arrays, strings or class
instances, which failed with unnamed or internal compiler errors, are reported the same way, even if they are null.
A helper method that loads such a field is only an error if a call to it remains after optimization, like helpers
failing with other errors. Static nint and nuint fields, which kernels read as zero, now give their values.

struct Holder { public int V; public int[] A; }

static readonly ArrayView<int> Table = default;
static readonly string Name = "abc";
static readonly Holder Values = new() { V = 1, A = [1, 2] };
static readonly nint Offset = 4;

d[i] = Table[i];                         // error ILGPU1214 instead of a read through a null pointer
d[i] = Name.Length;                      // error ILGPU1214 instead of an unnamed error
d[i] = Values.V;                         // error ILGPU1214 instead of an internal compiler error
d[i] = ArrayView<int>.Empty.IntLength;   // 0, as before
d[i] = (int)Offset;                      // 4 instead of 0

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. Views and pointers in static fields (Src/ILGPUC/Frontend/CodeGenerator/Fields.cs, Src/ILGPUC/Roslyn/Analysis/KernelDiagnostic.cs, Src/ILGPUC/IR/PureValues/Construction/Structures.cs)

  • A kernel that loads a static field gets the value the field has at compile time, inlined as a constant. A view in it
    does not refer to device memory, so loading a view from a static readonly field compiled to reads through a null
    pointer.

  • The load is now rejected with ILGPU1214 when the field's type refers to memory: pointers, every type that the
    compiler maps to a view or a pointer (detected from its IR type, so every view type is covered), and structures,
    arrays and ImmutableArray<T> containing them, nested to any depth.

  • The check looks at the type before the field is read, so the static constructor of the declaring class no longer
    runs in the compiler for these fields. It applies in the MutableStaticFields mode, too. The fields of ILGPU itself,
    such as ArrayView<T>.Empty, are exempt and stay usable:

    Static field 'K.Table' of type 'ArrayView1D<int, Dense>' holds a view or a pointer, which kernels cannot load:
    its value is read when the kernel is compiled and does not refer to memory of the device. Pass the view to the
    kernel as a parameter or capture it in an inline kernel instead.

  • The messages name types in their C# spelling with TypeDisplay.Format of ILGPU V2.0: Int128, UInt128 and decimal Values in Kernels, and C# Type Names in Kernel Diagnostics #1690, such as ArrayView<int>[].

  • Native integers are void pointers in the IR, so their IR types depend on the address space, but they hold no
    memory: static nint and nuint fields and structures holding them are not rejected. Their values were inlined as
    zero, because the type information of nint has no field to read the value from. CreateObjectValue now builds
    them from their values.

2. Strings, class instances and structures holding them (Src/ILGPUC/Frontend/CodeGenerator/Fields.cs, Src/ILGPU/Resources/ErrorMessages.resx)

  • Static readonly structures are inlined by building their values as constants. This failed with unnamed internal
    errors when they held arrays, ImmutableArray<T>, strings or class instances, null or not, also in nested
    structures, tuples and the elements of arrays.

  • Top-level static fields of strings, classes and interfaces failed with an unnamed error that the class type is not
    supported, which named RuntimeType for null fields, and with internal errors for boxed values.

  • Both cases are now ILGPU1214. The error depends only on the type of the field, which is checked before the field is
    read, so null fields and Type objects get the same messages:

    Static field 'K.Name' has the reference type 'string', which kernels cannot load, even if the field is null:
    strings, class instances and other objects, such as Type objects, do not exist on the device. ...

    Static field 'K.Holder' of type 'H' contains the field 'H.A' of type 'int[]', which holds a reference that
    kernels cannot load, even if it is null: arrays, strings and class instances inside a static value do not exist
    on the device. ...

  • Auto-properties are named by their property names.

  • Top-level static arrays keep their own handling. Delegates keep their own diagnostics, also inside structures, and
    the fields of closure classes are left to the closure handling.

  • Fields of generic classes are checked by the type of their instantiation: GenericHolder<string>.Value is rejected,
    and GenericHolder<int>.Value is still inlined.

  • A static structure holding an array of itself, such as the TreeNode of ILGPU V2.0: Named Errors for Recursive Types + Nested Closures in Debug Builds #1701, is reported for
    that array before its recursive type is created.

  • Not covered: strings or class instances held in top-level static arrays, such as a static readonly string[], are
    not part of this check. Fields of generic classes appear under their metadata names, such as
    GenericHolder`1.Value.

3. Helpers that load such fields (Src/ILGPUC/Frontend/ILFrontend.cs, Src/ILGPUC/Frontend/FailedMethodVerifier.cs, Src/ILGPUC/KernelCompilationException.cs)

  • A helper that fails to compile becomes an external declaration. FailedMethodVerifier reports only the calls to it
    that remain after optimization, so a helper called only on a path that constant arguments rule out does not fail the
    kernel.

  • Kernel compilation exceptions were exempt from this and failed the kernel right away. That is right for the
    constructs they usually report, but not for ILGPU1214, because helpers often load such fields only on paths that
    are removed.

  • ILGPU1214 in a helper now fails like any other helper error. If a call survives, the verifier reports the original
    ILGPU1214 at the load in the helper, instead of an error naming the caller and the helper. Entry points and other
    kernel compilation exceptions fail at once as before.

  • Calls to recursive and mutually recursive helpers, which are never inlined, and calls from the loops of
    tail-recursive helpers survive optimization and are reported, on Metal before its error for recursion.

  • In two cases the error points at the call instead:

    • the failing helper has no source location, such as a library method without debug information (the getter of
      BigInteger.One, which loads a static BigInteger holding an array; before, its error had no location);
    • the helper is the accessor the C# compiler generates for an auto-implemented property, whose location is the
      property declaration.

    Local functions keep their own locations.

4. Documentation (Docs/03_Advanced/02_Kernels.md)

  • The diagnostics table lists ILGPU1214 with the rejected fields and the rule for helper methods.

5. Tests (Src/ILGPUC.Tests/)

  • StaticViewFieldTests (new, NonKernelTests/):
    • StaticViewFields_AreNamedErrors checks the field and type in the message for:
      • 1D, strided, 2D and 3D views;
      • structures and nested structures holding a view, also when only a scalar field is read and when passed by in;
      • pointers and structures holding pointers;
      • arrays and ImmutableArray<T> of views.
    • StaticReadonlyView_IsRejectedOnAllBackends runs on CPU, CUDA and Metal.
    • MutableStaticView_IsRejectedInMutableStaticFieldMode and MutableStaticView_IsNotReadOnlyByDefault.
    • StaticViewField_DoesNotRunStaticConstructor.
    • StaticFieldsWithoutViews_AreInlined (CPU, CUDA and Metal, and CPU in the mutable mode): scalars, structures, and
      nested structures with an Index2D and an enum still fold to one constant.
    • StaticNativeIntegers_AreInlined (CPU, CUDA and Metal): nint and nuint fields and a structure holding them
      fold to the sum of their values.
    • LibraryEmptyViews_AreInlined (CUDA, Metal): the Empty views of ILGPU.
    • StaticManagedReferences_AreNamedErrors, with the error at the load in the kernel, for:
      • structures holding an array, an object, a string or an ImmutableArray<T>, each null and non-null;
      • nested structures, arrays of structures and tuples.
    • StaticDelegateHolder_IsStoredDelegateError: a structure holding a delegate still gets ILGPU1205.
    • StaticObjects_AreNamedErrors covers strings, class instances, null fields, object fields holding a class
      instance or a boxed value, interface fields, a property returning a static field, and an auto-property, which is
      reported at the call of its accessor.
    • RemovedHelperCalls_Compile: helpers loading a view, a string, a class instance or a structure holding an array,
      and a helper throwing with a static string, all on paths that optimization removes. A recursive helper that the
      frontend folds is never generated.
    • SurvivingHelperCalls_AreNamedErrors (CPU, and Metal for the recursive helpers) and
      SurvivingReferenceHelperCalls_AreNamedErrors: the original ILGPU1214 at the load in the helper, for direct,
      recursive, mutually recursive and tail-recursive helpers, a throwing helper and a local function. For
      BigInteger.One, the error is reported at the call in the kernel.
    • StaticObjectReference_IsNamedError (ref readonly to a static string) and
      GenericStaticFields_AreCheckedByInstantiation.
  • KernelDiagnosticTests:
    • StaticViewField_Reports1214.
    • Through the ilgpuc build path: StaticViewField_FailsBuild, StaticArrayHolder_FailsBuild,
      StaticString_FailsBuild, StaticClassProperty_FailsBuild, StaticStringInRemovedHelper_Compiles and
      StaticStringInSurvivingHelper_FailsBuild; the last four with Debug and Release programs.
    • AssertBuildErrorAsync takes the optimization level of the program, and the new AssertBuildSucceedsAsync checks
      a build that succeeds.
  • RecursiveTypeTests.StaticTree_ReportsHeldArray: the static TreeNode is reported for its Children array at the
    line of the load.
  • All of these are compiler tests on CPU, plus CUDA and Metal where noted. None of them launches a kernel.

Depends on

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

Testing

Suite This PR
Full test suite, Apple Silicon (CPU, Metal; CUDA, ROCm and OpenCL compile-only) 11606 passed, 1715 skipped, 0 failed
Release build with TreatWarningsAsErrors 0 errors
Every commit of the PR build (5/5, 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 5 commits September 11, 2026 00:34
…hich compiled to reads through null pointers, with ILGPU1214.

The value of a static field is read when the kernel is compiled, so a view
in it never refers to device memory. The check looks at the type of the
field, including structures, arrays and ImmutableArray<T> holding views or
pointers. It runs before the field is read, so static constructors no longer
run in the compiler for these fields. The fields of the ILGPU runtime, such
as ArrayView<T>.Empty, are exempt. The message names the field and its type
in C# spelling, such as 'ArrayView<int>[]', and the kernel documentation
lists the new diagnostic.

Native integers are void pointers in the IR, but hold no memory, so static
nint and nuint fields and structures holding them stay loadable. Their
values are now inlined as such; before, they were read as zero.

Co-Authored-By: Claude <noreply@anthropic.com>
…y if a call to the helper survives optimization, like helpers failing with other errors, and then with the original ILGPU1214 at its location.

Helpers that fail to compile are turned into external declarations, and
FailedMethodVerifier reports the calls to them that remain after
optimization, so that helpers whose calls are removed, for example on a path
that constant arguments rule out, do not fail the kernel. Kernel compilation
exceptions were exempt from this and failed the kernel right away, which is
right for the constructs they usually report but not for ILGPU1214: helpers
often load such fields only on paths that are removed. ILGPU1214 in a helper
now fails like any other helper error, and the verifier reports the original
diagnostic with its identifier and its location in the helper instead of an
error naming the caller and the helper. Entry points and other kernel
compilation exceptions fail at once as before. The kernel documentation of
ILGPU1214 says so.

The tests pass the constant to the helper through a tuple element, which the
constant evaluator of the frontend does not fold, so the helper is generated
and only the IR optimizer removes its call. Calls to recursive and mutually
recursive helpers, which are never inlined, and from the loops of
tail-recursive helpers remain and are reported, on Metal before the error
for recursion, unless the constant evaluator folds the recursion.

Co-Authored-By: Claude <noreply@anthropic.com>
…of interface types, which failed with unnamed or internal compiler errors, with ILGPU1214.

Top-level static fields of strings, classes and interfaces failed with an
unnamed error that the class type is not supported, naming RuntimeType for
null fields, and with internal errors for boxed values. The check looks at
the type of the field before it is read, so the message holds for null
fields and Type objects alike. It names the static field, by its property
name for auto-properties, and leaves top-level static arrays, delegates and
the fields of closure classes to their own handling. The kernel
documentation of ILGPU1214 lists these cases.

Helpers loading these fields are reported only if a call to them survives
optimization, like helpers loading views. The accessor the compiler
generates for an auto-implemented property has the property declaration as
its location, so its error now points at the call of the accessor instead.
So does an error in a library method without debug information, instead of
having no location. Local functions, which the compiler generates as well,
keep their own locations.

Co-Authored-By: Claude <noreply@anthropic.com>
…or class instances, which failed with internal compiler errors while their values were inlined, with ILGPU1214.

Static readonly structures are inlined by building their values as constants
when the kernel is compiled, which failed with unnamed internal errors for
arrays, ImmutableArray<T>, strings and class instances in their fields, null
or not, in nested structures, tuples and the elements of arrays. They are
now checked before the field is read as well. The message also names the
field holding the reference and its type and says that it holds a
reference, which also fits ImmutableArray<T>, a structure wrapping an
array. A static structure holding an array of itself is now reported this
way as well, before its recursive type is created. Helpers loading these
structures are reported like helpers loading strings; the getter of
BigInteger.One, which loads a static BigInteger holding an array in a
library without debug information, is reported at its call. The kernel
documentation of ILGPU1214 lists these cases.

Co-Authored-By: Claude <noreply@anthropic.com>
…holding arrays in kernels, including auto-properties and helpers that optimization removes or keeps, in debug and release builds.

The tests build small programs through the Roslyn build path and check
that ILGPU1214 points at the marked line of the source: the load of a
static structure holding an array, the load of a static string, and the
call of the generated getter of an auto-implemented property holding a
class instance. Helpers loading strings and class instances compile if
optimization removes their calls and fail at the load in the helper if a
call survives. All but the structure case run in debug and release builds,
for which the helper that creates and analyzes the program now takes the
optimization level and is shared with a check that the build succeeds.

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