Repository navigation
Conversation
…ld referring to another closure class, as in the debug closures of nested scopes, no longer spans the fields of that class and fails to compile.
Values of class types are pointers in the IR and occupy a single field, but
the type information of a class counted all fields of the class as its
flattened fields. A field holding a class reference was thus accessed with a
field span as wide as the class, and the fields of structures and classes
behind such a field were assigned shifted indices. Debug builds of lambdas
capturing locals of two nested scopes hit this: the inner closure class
refers to the outer one through a field, and storing that field sliced
beyond the end of the inner closure class ("Slice span out of range") on all
backends.
The layout of a class itself is still described by its fields. The
structure builder of a class layout now uses the number of fields of the
class as its capacity hint instead of its flattened field count.
Co-Authored-By: Claude <noreply@anthropic.com>
…h a compile error naming the field path instead of a stack overflow. The type information of a structure resolved the type information of the element types of its arrays and views and of all its fields of class types eagerly. Only arrays of and pointers to the structure being resolved were guarded, so a type referring to itself through a class or a view, such as a structure holding a class that holds the structure again, recursed until the stack overflowed and took the host process down. The guard against recursive classes in CreateClassStructureType never fired: the type information it asked for overflowed the stack before the class was reached again. Element types and class field types are no longer resolved by the type information, which only needs to count them as single fields, and its set of types in progress is gone. This removes the IsBlittable and IsValidKernelParameter flags, which nothing read and which could not be computed without resolving these types. Creating the IR type of a structure or class now registers it as being created. Reaching it again through its own fields rejects it, and the message names the field path, such as 'StructNode.Link.Node'. This replaces the separate check for arrays of structures being created and its message with the reflection name of the array type: a tree node holding an array of child nodes now reports 'TreeNode.Children', too. Pointers to structures being created stay opaque pointers, as before. The error is reported as an unsupported construct (ILGPU1200) at the kernel code using the type, with the C# type names of the other frontend diagnostics, such as 'GenericTree<int>'. The resource NotSupportedRecursiveReferenceType is renamed to NotSupportedRecursiveType, as it covers structures, too. Co-Authored-By: Claude <noreply@anthropic.com>
…Node<T> holding Node<Node<T>>[], with a compile error naming the definition instead of a stack overflow. A generic type definition whose type parameter flows through the type arguments of its field types back into itself, nested in another type argument at least once, has an expanding cycle (ECMA-335 II.9.2), as in Node<T> holding Node<Node<T>>[] or Node<Box<T>>[]: it never reaches the same instantiation twice, so the check for types being created never fires, but it would create new ones without end. Creating the IR type of an instantiation of such a definition now rejects it, and the message names the definition, such as 'Node<T>'. Instantiations nested in their own type arguments, such as deeply nested wrappers or the ValueTuple`8 chains of long tuples, stay finite and are not mistaken for recursive types, however deeply they are nested. A type parameter passed only to other definitions, as in a field of type Box<Box<T>>, does not expand, and a type holding an array of its own instantiation, as in GenericTree<T> with a GenericTree<T>[] field, is still reported through its field path rather than as an expanding definition. The error is reported as an unsupported construct (ILGPU1200) at the kernel code using the type, with the new resource NotSupportedExpandingGenericType. 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 #1700. Types that refer to themselves through a class, such as a class holding a
reference to its own class or a structure holding a class that holds the structure again, are rejected with a compile
error naming the field path through which they refer to themselves. Before, they overflowed the compiler's stack and
took the process down. Generic types whose fields create ever larger instantiations of their own definition, such as
Node<T>holdingNode<Node<T>>[], are rejected the same way, while deeply nested but finite instantiations, such asthe nested tuples of long C# tuples, keep compiling. A structure holding an array of itself, which #1688 already
rejects, now reports the same field-path message. Structures holding pointers to themselves keep compiling as #1688
made them. Separately, lambdas capturing locals of two nested scopes compile in Debug builds.
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. Fields of class types occupy one field (
Src/ILGPUC/IR/TypeInformationManager.cs,Src/ILGPUC/IR/ModuleValues/Construction/ModuleBuilder.cs)all fields of the class as its flattened fields. A field holding a class reference was therefore accessed with a
field span as wide as the class, and the fields of structures and classes behind such a field got shifted indices.
closure classes, and the inner one refers to the outer one through a field. Storing that field sliced beyond the end
of the inner closure class and failed with "Slice span out of range" on all backends.
number of fields of the class as its capacity instead of its flattened field count.
2. Type information no longer recurses into referenced types (
Src/ILGPUC/IR/TypeInformationManager.cs)class fields. Only arrays of and pointers to the structure being resolved were guarded, so a type referring to itself
through a class or a view recursed until the stack overflowed. The existing guard against recursive classes in
CreateClassStructureTypenever fired, because the type information overflowed before the class was reached again.reference types count as single fields without resolving their types. The set of types in progress in the type
information is no longer needed and is removed.
IsBlittableandIsValidKernelParameterflags are removed. Nothing read them, and they could not be computedwithout resolving these types.
3. Recursive types are rejected (
Src/ILGPUC/IR/ModuleValues/Construction/ModuleBuilder.cs,Src/ILGPU/Resources/ErrorMessages.resx)Creating the IR type of a structure or a class registers it as in progress, together with the field being created.
Reaching it again through its own fields raises an unsupported-construct error (
ILGPU1200) at the kernel code thatuses the type. The message names the field path, with auto-properties under their property names:
This replaces the separate check for arrays of structures in progress, whose message named the reflection name of
the array type. A structure holding an array of itself now reports its field path, such as
'ArrayNode.Children'.Pointers to a structure in progress stay opaque pointers, as ILGPU V2.0: Pointer Constructs in Kernels #1688 introduced.
Instantiating a generic type definition with an expanding cycle (ECMA-335 II.9.2) is rejected before its fields are
created. In such a definition, a type parameter flows through the type arguments of its field types back into itself,
nested in another type argument at least once, as in
Node<T>holdingNode<Node<T>>[]orNode<Box<T>>[], orthrough another type, as in
MutualNode<T>holdingMutualLink<T[]>. It never reaches the same instantiation twicebut would create new ones without end. The result is cached per definition:
Finite types are not affected. Instantiations nested in their own type arguments, such as a wrapper nested 40 levels
deep or the
ValueTuplechains of long tuples, still compile. A type parameter passed only to other definitions, asin a field of type
Box<Box<T>>, does not expand. A type holding an array of its own instantiation, such asGenericTree<T>with aGenericTree<T>[]field, is reported through its field path.Type names use the C# spelling of the other frontend diagnostics (
TypeDisplay.Format), such asNode<int>. Theresource
NotSupportedRecursiveReferenceTypeis renamed toNotSupportedRecursiveType, since it now coversstructures, too, and
NotSupportedExpandingGenericTypeis added.4. Tests (
Src/ILGPUC.Tests/)RecursiveTypeTests(new,NonKernelTests/):RecursiveStructure_ReferencesOccupySingleFields: the type information of self-referencing structures (through anarray, a pointer, a class, and generic and expanding definitions) has two fields at offsets 0 and 1.
NestedInstantiations_AreCreatedcreatesWrapper<T>nested 1, 20 and 40 levels deep andValueTuplechains ofthe same depths.
NestedTypeArgument_IsCreatedcreates a structure with aBox<Box<T>>field.DeeplyNestedLocal_Compilescompiles a kernel with a local nested 20 levels deep, andPointerView_Compilesakernel taking a view of a structure that points to itself.
ILGPU1200, the exact message and the line of the kernel that uses the type:StaticTree(a static field of a structure holding an array of itself);CapturedClass(a lambda capturing a self-referencing class) andLocalStructClass(structure → class →structure,
'StructNode.Link.Node');LocalGenericTree(field path),LocalGeneric,LocalExpanding,LocalGrowingandLocalMutual(expandingdefinitions).
SelfReferentialPointerTests.SelfReferentialArray_ReportsUnsupportedConstructexpects the field-path message.KernelDiagnosticTests.RecursiveLocalType_FailsBuild: a structure holding a class that holds the structure fails anilgpucbuild withILGPU1200at the line of the local.CompilerInternalsTests.TypeInfo_ClassField_OccupiesSingleField: a class, a class with a class field and a structurewith a class field have one, one and two flattened fields, with unshifted offsets.
LambdaExecutionTests.NestedScopeCapture_ProducesCorrectOutput(TestPrograms/Lambda/NestedScopeCapture.cs) runs alambda capturing locals of two nested scopes, with Release and Debug programs, on every backend with a device.
LambdaKernels.NestedScopeCaptureKernelis used for source generation on every backend.Depends on
#1700 (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