diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs
index 91ea3a83c..263651bea 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs
@@ -27,7 +27,7 @@ private static partial class HlslSource
/// Gathers all necessary information on a transpiled HLSL source for a given shader type.
///
/// The collection of produced instances.
- /// The input object currently in use.
+ /// The instance currently in use.
/// The for the shader type.
/// The shader interface type implemented by the shader type.
/// The number of inputs for the shader.
@@ -37,7 +37,7 @@ private static partial class HlslSource
/// The HLSL source for the shader.
public static string GetHlslSource(
ImmutableArrayBuilder diagnostics,
- Compilation compilation,
+ SemanticModelProvider semanticModelProvider,
INamedTypeSymbol structDeclarationSymbol,
INamedTypeSymbol shaderInterfaceType,
int inputCount,
@@ -70,8 +70,6 @@ public static string GetHlslSource(
token.ThrowIfCancellationRequested();
- SemanticModelProvider semanticModelProvider = new(compilation);
-
// Explore the syntax tree and extract the processed info
(string entryPoint, ImmutableArray processedMethods) = GetProcessedMethods(
diagnostics,
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs
index 1d1459093..dbd701d79 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs
@@ -139,10 +139,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
using ImmutableArrayBuilder diagnostics = new();
+ SemanticModelProvider semanticModelProvider = new(context.SemanticModel);
+
// Get HLSL source for HlslSource
string hlslSource = HlslSource.GetHlslSource(
diagnostics,
- context.SemanticModel.Compilation,
+ semanticModelProvider,
typeSymbol,
shaderInterfaceType,
inputCount,
diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SemanticModelProvider.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SemanticModelProvider.cs
index 95a019a38..9a517c874 100644
--- a/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SemanticModelProvider.cs
+++ b/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SemanticModelProvider.cs
@@ -6,13 +6,15 @@ namespace ComputeSharp.SourceGeneration.Helpers;
///
/// A type providing instances for nodes.
///
-/// The source instance.
-internal sealed class SemanticModelProvider(Compilation compilation)
+/// A whose
+/// will be used to get instances for other syntax trees.
+internal sealed class SemanticModelProvider(SemanticModel baseSemanticModel)
{
///
- /// The map of loaded instances.
+ /// The map of loaded instances for syntax trees other than
+ /// the one for .
///
- private readonly Dictionary semanticModelsMap = [];
+ private Dictionary? additionalSemanticModels = null;
///
/// Gets a instance with info on a given .
@@ -21,11 +23,20 @@ internal sealed class SemanticModelProvider(Compilation compilation)
/// A instance containing info on .
public SemanticModel For(SyntaxNode syntaxNode)
{
- if (!this.semanticModelsMap.TryGetValue(syntaxNode.SyntaxTree, out SemanticModel semanticModel))
+ // Reuse the base semantic model if the syntax node belongs to the same tree.
+ // This will avoid creating new semantic models if the entire type's definition is in the same file.
+ if (syntaxNode.SyntaxTree == baseSemanticModel.SyntaxTree)
{
- semanticModel = compilation.GetSemanticModel(syntaxNode.SyntaxTree);
+ return baseSemanticModel;
+ }
+
+ this.additionalSemanticModels ??= [];
+
+ if (!this.additionalSemanticModels.TryGetValue(syntaxNode.SyntaxTree, out SemanticModel semanticModel))
+ {
+ semanticModel = baseSemanticModel.Compilation.GetSemanticModel(syntaxNode.SyntaxTree);
- this.semanticModelsMap.Add(syntaxNode.SyntaxTree, semanticModel);
+ this.additionalSemanticModels.Add(syntaxNode.SyntaxTree, semanticModel);
}
return semanticModel;
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs
index 066e18358..7ff8ff81c 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs
@@ -29,7 +29,7 @@ internal static partial class HlslSource
/// Gathers all necessary information on a transpiled HLSL source for a given shader type.
///
/// The collection of produced instances.
- /// The input object currently in use.
+ /// The instance currently in use.
/// The for the shader type.
/// The shader interface type implemented by the shader type.
/// Whether is a "pixel shader like" type.
@@ -42,7 +42,7 @@ internal static partial class HlslSource
/// The resulting HLSL source for the current shader.
public static void GetInfo(
ImmutableArrayBuilder diagnostics,
- Compilation compilation,
+ SemanticModelProvider semanticModelProvider,
INamedTypeSymbol structDeclarationSymbol,
INamedTypeSymbol shaderInterfaceType,
bool isPixelShaderLike,
@@ -89,8 +89,6 @@ public static void GetInfo(
token.ThrowIfCancellationRequested();
- SemanticModelProvider semanticModelProvider = new(compilation);
-
(string entryPoint, ImmutableArray processedMethods, isSamplerUsed) = GetProcessedMethods(
diagnostics,
structDeclarationSymbol,
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs
index 1ed999402..2d0a600d1 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs
@@ -90,10 +90,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
using ImmutableArrayBuilder diagnostics = new();
+ SemanticModelProvider semanticModelProvider = new(context.SemanticModel);
+
// Transpiled HLSL source info
HlslSource.GetInfo(
diagnostics,
- context.SemanticModel.Compilation,
+ semanticModelProvider,
typeSymbol,
shaderInterfaceType,
isPixelShaderLike,