From 94ddeec28305ed471b67e088f35f467275ba2220 Mon Sep 17 00:00:00 2001 From: Abir Deol Date: Fri, 20 Nov 2026 09:16:39 -0700 Subject: [PATCH] gpuav: Skip data race pass on Subgroup shared barriers A Subgroup scope OpControlBarrier with WorkgroupMemory semantics orders shared memory within a subgroup, but only a Workgroup scope barrier resets the shadow memory, so the accesses after it were reported as races. Skip the pass for those shaders instead. --- layers/gpuav/spirv/module.cpp | 7 ++++ layers/gpuav/spirv/module.h | 2 + .../spirv/shared_memory_data_race_pass.cpp | 7 ++++ ...pu_av_shared_memory_data_race_positive.cpp | 40 +++++++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/layers/gpuav/spirv/module.cpp b/layers/gpuav/spirv/module.cpp index 524f75057bf..94d2689270d 100644 --- a/layers/gpuav/spirv/module.cpp +++ b/layers/gpuav/spirv/module.cpp @@ -290,6 +290,13 @@ Module::Module(vvl::span words, DebugReport* debug_report, const function_end_found = true; } else if (opcode == spv::OpAssumeTrueKHR) { has_assume_true_ = true; + } else if (opcode == spv::OpControlBarrier && !has_subgroup_shared_memory_barrier_) { + const Constant* scope = type_manager_.FindConstantById(new_inst->Word(1)); + const Constant* semantics = type_manager_.FindConstantById(new_inst->Word(3)); + if (scope && semantics && scope->GetValueUint32() == spv::ScopeSubgroup && + (semantics->GetValueUint32() & spv::MemorySemanticsWorkgroupMemoryMask) != 0) { + has_subgroup_shared_memory_barrier_ = true; + } } if (opcode == spv::OpLoopMerge) { diff --git a/layers/gpuav/spirv/module.h b/layers/gpuav/spirv/module.h index 319954e2533..0a972a036e5 100644 --- a/layers/gpuav/spirv/module.h +++ b/layers/gpuav/spirv/module.h @@ -126,6 +126,8 @@ class Module { // Lets ExpectAssumePass be skipped for the (majority of) shaders without an OpAssumeTrueKHR bool has_assume_true_ = false; + // Lets SharedMemoryDataRacePass skip shaders with a Subgroup scope barrier on Workgroup memory + bool has_subgroup_shared_memory_barrier_ = false; // Used when UseErrorPayloadVariable is set. Needs to be same for all passes. // Will be set in the LogErrorPass diff --git a/layers/gpuav/spirv/shared_memory_data_race_pass.cpp b/layers/gpuav/spirv/shared_memory_data_race_pass.cpp index 6c11d7c657a..b65db4e6a7b 100644 --- a/layers/gpuav/spirv/shared_memory_data_race_pass.cpp +++ b/layers/gpuav/spirv/shared_memory_data_race_pass.cpp @@ -350,6 +350,13 @@ bool SharedMemoryDataRacePass::Instrument() { return false; } + // Only a Workgroup scope barrier resets the shadow memory, + // so accesses ordered by a Subgroup scope barrier would be reported as races + // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/12813 + if (module_.has_subgroup_shared_memory_barrier_) { + return false; + } + const std::vector& shmem_vars = type_manager_.GetSharedMemoryVariables(); // Since we don't support WorkgroupMemoryExplicitLayoutKHR, we don't need to worry about Aliased blocks diff --git a/tests/unit/gpu_av_shared_memory_data_race_positive.cpp b/tests/unit/gpu_av_shared_memory_data_race_positive.cpp index 7a0288ec873..2f95085a289 100644 --- a/tests/unit/gpu_av_shared_memory_data_race_positive.cpp +++ b/tests/unit/gpu_av_shared_memory_data_race_positive.cpp @@ -401,6 +401,46 @@ TEST_F(PositiveGpuAVSharedMemoryDataRace, SlangBasic) { TestHelper(shader_source, SPV_SOURCE_SLANG); } +TEST_F(PositiveGpuAVSharedMemoryDataRace, SubgroupBarrier) { + TEST_DESCRIPTION("https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/12813"); + const char* shader_source = R"glsl( + #version 450 + #extension GL_KHR_memory_scope_semantics : enable + #extension GL_KHR_shader_subgroup_basic : enable + + layout(local_size_x = 64) in; + shared uint bins[64]; + void main() { + bins[gl_LocalInvocationIndex] = 0; + controlBarrier(gl_ScopeSubgroup, gl_ScopeWorkgroup, gl_StorageSemanticsShared, gl_SemanticsAcquireRelease); + // Another invocation's slot, but always in the same subgroup + uint other = gl_SubgroupID * gl_SubgroupSize + (gl_SubgroupInvocationID ^ 1); + atomicAdd(bins[other], 1); + } + )glsl"; + + TestHelper(shader_source, SPV_SOURCE_GLSL); +} + +TEST_F(PositiveGpuAVSharedMemoryDataRace, SubgroupBarrierSlang) { + TEST_DESCRIPTION("https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/12813"); + const char* shader_source = R"slang( + groupshared uint sharedBins[64]; + + [numthreads(64, 1, 1)] + void main() + { + uint waveBinsIndex = WaveGetWaveIndex() * WaveGetLaneCount(); + sharedBins[waveBinsIndex + WaveGetLaneIndex()] = 0; + GroupMemoryBarrierWithWaveSync(); + + InterlockedAdd(sharedBins[waveBinsIndex + (WaveGetLaneIndex() ^ 1)], 1); + } + )slang"; + + TestHelper(shader_source, SPV_SOURCE_SLANG); +} + TEST_F(PositiveGpuAVSharedMemoryDataRace, SlangIssue12415) { TEST_DESCRIPTION( "Regression test for https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/12415. A first load from a "