From 69de0642f893ddb39421d6ec3cecf5711eaba8ca Mon Sep 17 00:00:00 2001 From: Steven Perron Date: Mon, 12 May 2025 11:01:33 -0400 Subject: [PATCH 1/2] [SPIRV] Add warning for initialized globals To be consistent with DXIL, we will start emitting a warning for extenally visible variables that have an initializer. Until now, there were silently ignored. Fixes #3950 --- tools/clang/lib/SPIRV/SpirvEmitter.cpp | 5 +++++ .../groupshared.init.warning.hlsl | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl diff --git a/tools/clang/lib/SPIRV/SpirvEmitter.cpp b/tools/clang/lib/SPIRV/SpirvEmitter.cpp index 7337a33b01..c521d67e47 100644 --- a/tools/clang/lib/SPIRV/SpirvEmitter.cpp +++ b/tools/clang/lib/SPIRV/SpirvEmitter.cpp @@ -2021,6 +2021,11 @@ void SpirvEmitter::doVarDecl(const VarDecl *decl) { // variables) belongs to the Function storage class. if (isExternalVar(decl)) { var = declIdMapper.createExternVar(decl); + + if (decl->hasInit()) { + emitWarning("Initializer of external global will be ignored", + decl->getLocation()); + } } else { // We already know the variable is not externally visible here. If it does // not have local storage, it should be file scope variable. diff --git a/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl b/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl new file mode 100644 index 0000000000..c49534948b --- /dev/null +++ b/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl @@ -0,0 +1,19 @@ +// RUN: %dxc -T cs_6_0 -E main -spirv %s 2>&1 | FileCheck %s + +groupshared uint testing = 0; + +[numthreads(64, 1, 1)] +void main(uint local_thread_id_flat : SV_GroupIndex) { + + InterlockedAdd(testing, 1); + GroupMemoryBarrierWithGroupSync(); + + if (local_thread_id_flat == 0) { + if (testing > 64) { + printf("testing is %u wtf", testing); + } + } +} + +// CHECK: warning: Initializer of external global will be ignored +// CHECK-NEXT: groupshared uint testing = 0; \ No newline at end of file From 4358d0f0c981aea291fc941844d7bf8bc1011020 Mon Sep 17 00:00:00 2001 From: Steven Perron Date: Thu, 15 May 2025 14:40:35 -0400 Subject: [PATCH 2/2] Remove unnecessary new line. --- tools/clang/lib/SPIRV/SpirvEmitter.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/clang/lib/SPIRV/SpirvEmitter.cpp b/tools/clang/lib/SPIRV/SpirvEmitter.cpp index c521d67e47..5411b3555e 100644 --- a/tools/clang/lib/SPIRV/SpirvEmitter.cpp +++ b/tools/clang/lib/SPIRV/SpirvEmitter.cpp @@ -2021,7 +2021,6 @@ void SpirvEmitter::doVarDecl(const VarDecl *decl) { // variables) belongs to the Function storage class. if (isExternalVar(decl)) { var = declIdMapper.createExternVar(decl); - if (decl->hasInit()) { emitWarning("Initializer of external global will be ignored", decl->getLocation());