diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md
index 2be10a4e..4157950a 100644
--- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md
+++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md
@@ -63,3 +63,4 @@ CMPS0054 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser
CMPS0055 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0056 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0057 | ComputeSharp.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+CMPS0058 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGloballyCoherentFieldDeclarationAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGloballyCoherentFieldDeclarationAnalyzer.cs
new file mode 100644
index 00000000..f3b00ae8
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGloballyCoherentFieldDeclarationAnalyzer.cs
@@ -0,0 +1,62 @@
+using System.Collections.Immutable;
+using ComputeSharp.SourceGeneration.Extensions;
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.Diagnostics;
+using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
+
+namespace ComputeSharp.SourceGenerators;
+
+///
+/// A diagnostic analyzer that generates an error whenever the [GloballyCoherent] attribute is used on an invalid target field.
+///
+[DiagnosticAnalyzer(LanguageNames.CSharp)]
+public sealed class InvalidGloballyCoherentFieldDeclarationAnalyzer : DiagnosticAnalyzer
+{
+ ///
+ public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidGloballyCoherentFieldDeclaration);
+
+ ///
+ public override void Initialize(AnalysisContext context)
+ {
+ context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
+ context.EnableConcurrentExecution();
+
+ context.RegisterCompilationStartAction(static context =>
+ {
+ // Get the IComputeShader, IComputeShader, ReadWriteBuffer and [GloballyCoherent] symbols
+ if (context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader") is not { } computeShaderSymbol ||
+ context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader`1") is not { } pixelShaderSymbol ||
+ context.Compilation.GetTypeByMetadataName("ComputeSharp.ReadWriteBuffer`1") is not { } readWriteBufferSymbol ||
+ context.Compilation.GetTypeByMetadataName("ComputeSharp.GloballyCoherentAttribute") is not { } globallyCoherentAttributeSymbol)
+ {
+ return;
+ }
+
+ context.RegisterSymbolAction(context =>
+ {
+ if (context.Symbol is not IFieldSymbol { ContainingType: { } typeSymbol } fieldSymbol)
+ {
+ return;
+ }
+
+ // If the current type does not have [GloballyCoherent], there is nothing to do
+ if (!fieldSymbol.TryGetAttributeWithType(globallyCoherentAttributeSymbol, out AttributeData? attribute))
+ {
+ return;
+ }
+
+ // Emit a diagnostic if the field is not valid (either static, not of ReadWriteBuffer type, or not within a compute shader type)
+ if (fieldSymbol.IsStatic ||
+ fieldSymbol.Type is not INamedTypeSymbol { IsGenericType: true } fieldTypeSymbol ||
+ !SymbolEqualityComparer.Default.Equals(fieldTypeSymbol.ConstructedFrom, readWriteBufferSymbol) ||
+ !MissingComputeShaderDescriptorOnComputeShaderAnalyzer.IsComputeShaderType(typeSymbol, computeShaderSymbol, pixelShaderSymbol))
+ {
+ context.ReportDiagnostic(Diagnostic.Create(
+ InvalidGloballyCoherentFieldDeclaration,
+ attribute.GetLocation(),
+ fieldSymbol));
+ }
+ }, SymbolKind.Field);
+ });
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index 1219c4d1..a879aabb 100644
--- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -814,4 +814,20 @@ partial class DiagnosticDescriptors
isEnabledByDefault: true,
description: "Shader types should be readonly (shaders cannot mutate their instance state while running, so shader types not being readonly makes them error prone).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
+
+ ///
+ /// Gets a for when a field annotated with [GloballyCoherent] is not valid.
+ ///
+ /// Format: "The field "{0}" is annotated with [GloballyCoherent], but is not a valid target for it (only ReadWriteBuffer<T> instance fields in compute shader types can be annotated with [GloballyCoherent])".
+ ///
+ ///
+ public static readonly DiagnosticDescriptor InvalidGloballyCoherentFieldDeclaration = new(
+ id: "CMPS0058",
+ title: "Invalid [GloballyCoherent] field declaration",
+ messageFormat: """The field "{0}" is annotated with [GloballyCoherent], but is not a valid target for it (only ReadWriteBuffer instance fields in compute shader types can be annotated with [GloballyCoherent])""",
+ category: "ComputeSharp.Shaders",
+ defaultSeverity: DiagnosticSeverity.Error,
+ isEnabledByDefault: true,
+ description: "The [GloballyCoherent] attribute is only valid on ReadWriteBuffer instance fields in compute shader types.",
+ helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
\ No newline at end of file
diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs b/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs
index 113cd657..a489605f 100644
--- a/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs
+++ b/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs
@@ -85,4 +85,88 @@ public class Test_Analyzers
await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
}
+
+ [TestMethod]
+ public async Task GloballyCoherentAttribute_NotWithinShaderType()
+ {
+ const string source = """
+ using ComputeSharp;
+
+ namespace MyFancyApp.Sample;
+
+ internal partial struct MyType
+ {
+ [{|CMPS0058:GloballyCoherent|}]
+ public ReadWriteBuffer buffer;
+ }
+ """;
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+
+ [TestMethod]
+ public async Task GloballyCoherentAttribute_IncorrectType()
+ {
+ const string source = """
+ using ComputeSharp;
+
+ namespace MyFancyApp.Sample;
+
+ internal partial struct MyType : IComputeShader
+ {
+ [{|CMPS0058:GloballyCoherent|}]
+ public ReadOnlyBuffer buffer;
+
+ public void Execute()
+ {
+ }
+ }
+ """;
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+
+ [TestMethod]
+ public async Task GloballyCoherentAttribute_StaticField()
+ {
+ const string source = """
+ using ComputeSharp;
+
+ namespace MyFancyApp.Sample;
+
+ internal partial struct MyType : IComputeShader
+ {
+ [{|CMPS0058:GloballyCoherent|}]
+ public static ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ }
+ }
+ """;
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+
+ [TestMethod]
+ public async Task GloballyCoherentAttribute_ValidField_DoesNotWarn()
+ {
+ const string source = """
+ using ComputeSharp;
+
+ namespace MyFancyApp.Sample;
+
+ internal partial struct MyType : IComputeShader
+ {
+ [GloballyCoherent]
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ }
+ }
+ """;
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
}
\ No newline at end of file
diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs b/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs
index 86ed4d84..d62829e7 100644
--- a/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs
+++ b/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs
@@ -895,6 +895,7 @@ public class Test_D2DPixelShaderSourceGenerator
// Also validate all analyzers
await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);