Skip to content

Incorrect SPIR-V generated when InterlockedAdd present #2828

@thisistherk

Description

@thisistherk

Bit of an odd one this... I'm compiling this shader library with:

dxc -O3 -T lib_6_4 -spirv -fspv-target-env=vulkan1.1 -enable-16bit-types -Vn ShaderData -Fh Shader.hlsl

The InterlockedAdd in the entryHistogram shader appears to be causing the if statement and corresponding assignment in the entryAverage shader to be ignored. Shader playground link here:

http://shader-playground.timjones.io/931323597f6ce322859fe59fbe9bef5d

[[vk::binding(0, 0)]]
RWStructuredBuffer<uint> uHistogram;

// Local workgroup histogram
groupshared uint sharedHistogram[256];



[shader("compute")]
[numthreads(16, 16, 1)]
void entryHistogram(uint3 id: SV_DispatchThreadID, uint idx: SV_GroupIndex)
{
    // !!! Comment out this line to 'fix' problem !!!
    InterlockedAdd(sharedHistogram[0], 1);
}



[shader("compute")]
[numthreads(256, 1, 1)]
void entryAverage(uint3 id: SV_DispatchThreadID, uint idx: SV_GroupIndex)
{
  sharedHistogram[idx] = 0;
  GroupMemoryBarrierWithGroupSync();
    
  // !!! This if statement and following assignment is ignored !!!
  if (idx == 0)
    sharedHistogram[idx] = 1;

  GroupMemoryBarrierWithGroupSync();
  uHistogram[idx] = sharedHistogram[idx];
}

SPIRV generated for entryAverage with the InterlockedAdd:

%entryAverage = OpFunction %void None %23
         %30 = OpLabel
         %31 = OpLoad %uint %gl_LocalInvocationIndex
         %32 = OpAccessChain %_ptr_Workgroup_uint %sharedHistogram %31
               OpStore %32 %uint_0
               OpControlBarrier %uint_2 %uint_2 %uint_264
               OpControlBarrier %uint_2 %uint_2 %uint_264
         %33 = OpLoad %uint %32
         %34 = OpAccessChain %_ptr_Uniform_uint %uHistogram %int_0 %31
               OpStore %34 %33
               OpReturn
               OpFunctionEnd

SPIRV generated for entryAverage without the InterlockedAdd:

%entryAverage = OpFunction %void None %23
         %29 = OpLabel
         %30 = OpLoad %uint %gl_LocalInvocationIndex
         %31 = OpAccessChain %_ptr_Workgroup_uint %sharedHistogram %30
               OpStore %31 %uint_0
               OpControlBarrier %uint_2 %uint_2 %uint_264
         %32 = OpIEqual %bool %30 %uint_0
               OpSelectionMerge %33 None
               OpBranchConditional %32 %34 %33
         %34 = OpLabel
               OpStore %31 %uint_1
               OpBranch %33
         %33 = OpLabel
               OpControlBarrier %uint_2 %uint_2 %uint_264
         %35 = OpLoad %uint %31
         %36 = OpAccessChain %_ptr_Uniform_uint %uHistogram %int_0 %30
               OpStore %36 %35
               OpReturn
               OpFunctionEnd

Metadata

Metadata

Assignees

Labels

spirvWork related to SPIR-V

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions