From 531262e6f2ec27637af8046d91a91e40744b981c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 20 Oct 2023 14:29:57 +0200 Subject: [PATCH] Always generate unsafe accessors for all fields --- ...scriptorGenerator.ConstantBuffer.Syntax.cs | 19 ++++++------------- ...haderDescriptorGenerator.ConstantBuffer.cs | 5 ++--- .../Models/FieldInfo.cs | 9 +++------ ...DescriptorGenerator.DispatchDataLoading.cs | 6 +++--- 4 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs index bdb9ea37..51914b70 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs @@ -265,19 +265,16 @@ partial class D2DPixelShaderDescriptorGenerator using ImmutableHashSetBuilder<(string ContainerType, string FieldName)> generatedAccessors = new(); - // Define all field accessors + // Define all field accessors. Note that these are generated regardless of the accessibility + // of the field they're getting the value for. This simplifies the rest of the code, and allows + // stripping the readonly-ness of the field references, and has no binary size impact anyway. + // This is because the fields are compiled to be exactly equivalent to direct field references. foreach (FieldInfo fieldInfo in info.Fields) { for (int i = 0; i < fieldInfo.FieldPath.Length; i++) { FieldPathPart pathPart = fieldInfo.FieldPath[i]; - // Only generate field accessors for fields that are not accessible - if (pathPart.IsAccessible) - { - continue; - } - // Get the friendly type name for the field: // - If the part path is a nested struct, the type name is embedded // - Otherwise, get the correct HLSL mapped type name for the field @@ -330,14 +327,10 @@ partial class D2DPixelShaderDescriptorGenerator { string fieldExpression = "shader"; - // Format the field access expression, there's two options: - // - If the field is accessible, just access it directly - // - If the field is not accessible, use the unsafe accessor + // Invoke all generated field accessors to get to the target field foreach (FieldPathPart part in fieldInfo.FieldPath) { - fieldExpression = part.IsAccessible - ? $"{fieldExpression}.{part.Name}" - : $"{part.Name}(in {fieldExpression})"; + fieldExpression = $"{part.Name}(in {fieldExpression})"; } // Return a mutable ref if needed (the expression is always readonly here) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs index 6133ee9d..f528dd71 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs @@ -58,7 +58,6 @@ partial class D2DPixelShaderDescriptorGenerator string fieldName = fieldSymbol.Name; string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName(); - bool isAccessible = fieldSymbol.IsAccessibleFromCompilationAssembly(compilation); // The first item in each nested struct needs to be aligned to 16 bytes if (isFirstField && fieldPath.Length > 0) @@ -70,14 +69,14 @@ partial class D2DPixelShaderDescriptorGenerator if (HlslKnownTypes.IsKnownHlslType(typeName)) { - ImmutableArray nestedFieldPath = fieldPath.Add(new FieldPathPart.Leaf(fieldName, isAccessible)); + ImmutableArray nestedFieldPath = fieldPath.Add(new FieldPathPart.Leaf(fieldName)); yield return GetHlslKnownTypeFieldInfo(nestedFieldPath, typeName, ref rawDataOffset.Value); } else if (fieldSymbol.Type.IsUnmanagedType) { string nestedTypeName = fieldSymbol.Type.GetFullyQualifiedName(includeGlobal: true); - ImmutableArray nestedFieldPath = fieldPath.Add(new FieldPathPart.Nested(fieldName, isAccessible, nestedTypeName)); + ImmutableArray nestedFieldPath = fieldPath.Add(new FieldPathPart.Nested(fieldName, nestedTypeName)); // Custom struct type defined by the user foreach (FieldInfo fieldInfo in GetCapturedFieldInfos(compilation, fieldSymbol.Type, nestedFieldPath, rawDataOffset)) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs index db30dd33..dfecf3af 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs @@ -39,21 +39,18 @@ internal abstract record FieldInfo(EquatableArray FieldPath, stri /// A model for a part of a field path, ie. the name of the immediate parent field, and whether it's accessible. /// /// The name of the immediate parent field to access (or self). -/// Whether the current path part is accessible from the compilation. -internal abstract record FieldPathPart(string Name, bool IsAccessible) +internal abstract record FieldPathPart(string Name) { /// /// A model for a leaf part of a field path. /// /// - /// - public sealed record Leaf(string Name, bool IsAccessible) : FieldPathPart(Name, IsAccessible); + public sealed record Leaf(string Name) : FieldPathPart(Name); /// /// A model for a nested field part path (ie. going through a nested struct type). /// /// - /// /// The fully qualified type name of the containing type. - public sealed record Nested(string Name, bool IsAccessible, string TypeName) : FieldPathPart(Name, IsAccessible); + public sealed record Nested(string Name, string TypeName) : FieldPathPart(Name); } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs index acb1e7b2..718d790a 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs @@ -82,13 +82,13 @@ partial class ComputeShaderDescriptorGenerator } else if (HlslKnownTypes.IsKnownHlslType(typeName)) { - // TODO: share the logic and fix the field path parts (here they just always return accessible leaves for now) - fields.Add(GetHlslKnownTypeFieldInfo(fieldPath.Add(new FieldPathPart.Leaf(fieldName, true)), typeName, ref rawDataOffset)); + // TODO: share the logic and fix the field path parts + fields.Add(GetHlslKnownTypeFieldInfo(fieldPath.Add(new FieldPathPart.Leaf(fieldName)), typeName, ref rawDataOffset)); } else if (fieldSymbol.Type.IsUnmanagedType) { // Custom struct type defined by the user - GetInfo(compilation, fieldSymbol.Type, fieldPath.Add(new FieldPathPart.Leaf(fieldName, true)), ref resourceOffset, ref rawDataOffset, fields, resources); + GetInfo(compilation, fieldSymbol.Type, fieldPath.Add(new FieldPathPart.Leaf(fieldName)), ref resourceOffset, ref rawDataOffset, fields, resources); } } }