Repository navigation
Conversation
…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
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 #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
ILGPU1214at 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
nintandnuintfields, which kernels read as zero, now give their values.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. 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 readonlyfield compiled to reads through a nullpointer.
The load is now rejected with
ILGPU1214when the field's type refers to memory: pointers, every type that thecompiler 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
MutableStaticFieldsmode, too. The fields of ILGPU itself,such as
ArrayView<T>.Empty, are exempt and stay usable:The messages name types in their C# spelling with
TypeDisplay.Formatof ILGPU V2.0: Int128, UInt128 and decimal Values in Kernels, and C# Type Names in Kernel Diagnostics #1690, such asArrayView<int>[].Native integers are void pointers in the IR, so their IR types depend on the address space, but they hold no
memory: static
nintandnuintfields and structures holding them are not rejected. Their values were inlined aszero, because the type information of
ninthas no field to read the value from.CreateObjectValuenow buildsthem 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 nestedstructures, 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
RuntimeTypefor 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 isread, so null fields and
Typeobjects get the same messages: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>.Valueis rejected,and
GenericHolder<int>.Valueis still inlined.A static structure holding an array of itself, such as the
TreeNodeof ILGPU V2.0: Named Errors for Recursive Types + Nested Closures in Debug Builds #1701, is reported forthat 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[], arenot 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.
FailedMethodVerifierreports only the calls to itthat 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 thatare removed.
ILGPU1214in a helper now fails like any other helper error. If a call survives, the verifier reports the originalILGPU1214at the load in the helper, instead of an error naming the caller and the helper. Entry points and otherkernel 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:
BigInteger.One, which loads a staticBigIntegerholding an array; before, its error had no location);property declaration.
Local functions keep their own locations.
4. Documentation (
Docs/03_Advanced/02_Kernels.md)ILGPU1214with the rejected fields and the rule for helper methods.5. Tests (
Src/ILGPUC.Tests/)StaticViewFieldTests(new,NonKernelTests/):StaticViewFields_AreNamedErrorschecks the field and type in the message for:in;ImmutableArray<T>of views.StaticReadonlyView_IsRejectedOnAllBackendsruns on CPU, CUDA and Metal.MutableStaticView_IsRejectedInMutableStaticFieldModeandMutableStaticView_IsNotReadOnlyByDefault.StaticViewField_DoesNotRunStaticConstructor.StaticFieldsWithoutViews_AreInlined(CPU, CUDA and Metal, and CPU in the mutable mode): scalars, structures, andnested structures with an
Index2Dand an enum still fold to one constant.StaticNativeIntegers_AreInlined(CPU, CUDA and Metal):nintandnuintfields and a structure holding themfold to the sum of their values.
LibraryEmptyViews_AreInlined(CUDA, Metal): theEmptyviews of ILGPU.StaticManagedReferences_AreNamedErrors, with the error at the load in the kernel, for:ImmutableArray<T>, each null and non-null;StaticDelegateHolder_IsStoredDelegateError: a structure holding a delegate still getsILGPU1205.StaticObjects_AreNamedErrorscovers strings, class instances, null fields,objectfields holding a classinstance 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) andSurvivingReferenceHelperCalls_AreNamedErrors: the originalILGPU1214at 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 readonlyto a static string) andGenericStaticFields_AreCheckedByInstantiation.KernelDiagnosticTests:StaticViewField_Reports1214.ilgpuc buildpath:StaticViewField_FailsBuild,StaticArrayHolder_FailsBuild,StaticString_FailsBuild,StaticClassProperty_FailsBuild,StaticStringInRemovedHelper_CompilesandStaticStringInSurvivingHelper_FailsBuild; the last four with Debug and Release programs.AssertBuildErrorAsynctakes the optimization level of the program, and the newAssertBuildSucceedsAsyncchecksa build that succeeds.
RecursiveTypeTests.StaticTree_ReportsHeldArray: the staticTreeNodeis reported for itsChildrenarray at theline of the load.
Depends on
#1701 (based on its head; the base is retargeted once it merges).
Testing
TreatWarningsAsErrorsCUDA, ROCm and OpenCL were compiled with nvcc, hipcc and ocloc but not run on a device.
🤖 Generated with Claude Code