Skip to content

Optimized DXIL doesn't work correctly when memory barriers are placed in between identical conditions #2743

Closed

Description

Title

Optimized DXIL doesn't work correctly when memory barriers are placed in between identical conditions

Functional impact

InterlockedOr (and potentially other atomic operation) doesn't work correctly and its groupshared result ends up with a value belonging to the first thread in the group which sets its initial value. In practice, this affects our (Unity HDRP) compute shader which produces a lower resolution stencil buffer in a conservative fashion (meaning that the resulting "larger" pixels should contain the result of oring all the associated smaller pixels but don't when optimization is turned on).

Minimal repro steps

  1. build dxc.exe from source (I used 2020 March 3 version 227c8e6)
  2. Compile the attached example shader repro_shader.txt with optimization turned on (any of O1/O2/O3/O4 will do). The command dxc.exe -T cs_6_3 -E main repro_shader.txt -O1 produces the following DXIL:
target datalayout = "e-m:e-p:32:32-i1:32-i8:32-i16:32-i32:32-i64:64-f16:32-f32:32-f64:64-n8:16:32:64"
target triple = "dxil-ms-dx"

%dx.types.Handle = type { i8* }
%dx.types.CBufRet.i32 = type { i32, i32, i32, i32 }
%"class.RWStructuredBuffer<unsigned int>" = type { i32 }
%"$Globals" = type { i32 }

@"\01?g_GroupsharedValue@@3IA" = external addrspace(3) global i32, align 4

define void @main() {
  %1 = call %dx.types.Handle @dx.op.createHandle(i32 57, i8 1, i32 0, i32 0, i1 false)  ; CreateHandle(resourceClass,rangeId,index,nonUniformIndex)
  %2 = call %dx.types.Handle @dx.op.createHandle(i32 57, i8 2, i32 0, i32 0, i1 false)  ; CreateHandle(resourceClass,rangeId,index,nonUniformIndex)
  %3 = call i32 @dx.op.flattenedThreadIdInGroup.i32(i32 96)  ; FlattenedThreadIdInGroup()
  %4 = lshr i32 %3, 1
  %5 = and i32 %4, 31
  %6 = shl i32 1, %5
  %7 = icmp eq i32 %3, 0
  br i1 %7, label %8, label %17

; <label>:8                                       ; preds = %0
  %9 = call i32 @dx.op.groupId.i32(i32 94, i32 1)  ; GroupId(component)
  %10 = call i32 @dx.op.groupId.i32(i32 94, i32 0)  ; GroupId(component)
  store i32 0, i32 addrspace(3)* @"\01?g_GroupsharedValue@@3IA", align 4, !tbaa !13
  call void @dx.op.barrier(i32 80, i32 9)  ; Barrier(barrierMode)
  %11 = atomicrmw or i32 addrspace(3)* @"\01?g_GroupsharedValue@@3IA", i32 %6 seq_cst
  call void @dx.op.barrier(i32 80, i32 9)  ; Barrier(barrierMode)
  %12 = call %dx.types.CBufRet.i32 @dx.op.cbufferLoadLegacy.i32(i32 59, %dx.types.Handle %2, i32 0)  ; CBufferLoadLegacy(handle,regIndex)
  %13 = extractvalue %dx.types.CBufRet.i32 %12, 0
  %14 = mul i32 %13, %9
  %15 = add i32 %14, %10
  %16 = load i32, i32 addrspace(3)* @"\01?g_GroupsharedValue@@3IA", align 4, !tbaa !13
  call void @dx.op.rawBufferStore.i32(i32 140, %dx.types.Handle %1, i32 %15, i32 0, i32 %16, i32 undef, i32 undef, i32 undef, i8 1, i32 4)  ; RawBufferStore(uav,index,elementOffset,value0,value1,value2,value3,mask,alignment)
  br label %19

; <label>:17                                      ; preds = %0
  call void @dx.op.barrier(i32 80, i32 9)  ; Barrier(barrierMode)
  %18 = atomicrmw or i32 addrspace(3)* @"\01?g_GroupsharedValue@@3IA", i32 %6 seq_cst
  call void @dx.op.barrier(i32 80, i32 9)  ; Barrier(barrierMode)
  br label %19

; <label>:19                                      ; preds = %17, %8
  ret void
}

; Function Attrs: nounwind readnone
declare i32 @dx.op.groupId.i32(i32, i32) #0

; Function Attrs: nounwind readnone
declare i32 @dx.op.flattenedThreadIdInGroup.i32(i32) #0

; Function Attrs: noduplicate nounwind
declare void @dx.op.barrier(i32, i32) #1

; Function Attrs: nounwind
declare void @dx.op.rawBufferStore.i32(i32, %dx.types.Handle, i32, i32, i32, i32, i32, i32, i8, i32) #2

; Function Attrs: nounwind readonly
declare %dx.types.CBufRet.i32 @dx.op.cbufferLoadLegacy.i32(i32, %dx.types.Handle, i32) #3

; Function Attrs: nounwind readonly
declare %dx.types.Handle @dx.op.createHandle(i32, i8, i32, i32, i1) #3

attributes #0 = { nounwind readnone }
attributes #1 = { noduplicate nounwind }
attributes #2 = { nounwind }
attributes #3 = { nounwind readonly }

!llvm.ident = !{!0}
!dx.version = !{!1}
!dx.valver = !{!2}
!dx.shaderModel = !{!3}
!dx.resources = !{!4}
!dx.entryPoints = !{!10}

!0 = !{!"clang version 3.7 (tags/RELEASE_370/final)"}
!1 = !{i32 1, i32 3}
!2 = !{i32 1, i32 6}
!3 = !{!"cs", i32 6, i32 3}
!4 = !{null, !5, !8, null}
!5 = !{!6}
!6 = !{i32 0, %"class.RWStructuredBuffer<unsigned int>"* undef, !"", i32 0, i32 0, i32 1, i32 12, i1 false, i1 false, i1 false, !7}
!7 = !{i32 1, i32 4}
!8 = !{!9}
!9 = !{i32 0, %"$Globals"* undef, !"", i32 0, i32 0, i32 1, i32 4, null}
!10 = !{void ()* @main, !"main", null, !4, !11}
!11 = !{i32 0, i64 16, i32 4, !12}
!12 = !{i32 8, i32 8, i32 1}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !15, i64 0}
!15 = !{!"omnipotent char", !16, i64 0}
!16 = !{!"Simple C/C++ TBAA"}
  1. Compile the same shader with optimization turned off with dxc.exe repro_shader.txt -E main -T cs_6_3 -Od to get the following DXIL:
datalayout = "e-m:e-p:32:32-i1:32-i8:32-i16:32-i32:32-i64:64-f16:32-f32:32-f64:64-n8:16:32:64"
target triple = "dxil-ms-dx"

%dx.types.Handle = type { i8* }
%dx.types.CBufRet.i32 = type { i32, i32, i32, i32 }
%"class.RWStructuredBuffer<unsigned int>" = type { i32 }
%"$Globals" = type { i32 }

@"\01?g_GroupsharedValue@@3IA" = external addrspace(3) global i32, align 4
@dx.nothing = internal constant i32 0

define void @main() {
  %1 = call %dx.types.Handle @dx.op.createHandle(i32 57, i8 1, i32 0, i32 0, i1 false)  ; CreateHandle(resourceClass,rangeId,index,nonUniformIndex)
  %2 = call %dx.types.Handle @dx.op.createHandle(i32 57, i8 2, i32 0, i32 0, i1 false)  ; CreateHandle(resourceClass,rangeId,index,nonUniformIndex)
  %3 = call i32 @dx.op.groupId.i32(i32 94, i32 0)  ; GroupId(component)
  %4 = call i32 @dx.op.groupId.i32(i32 94, i32 1)  ; GroupId(component)
  %5 = call i32 @dx.op.flattenedThreadIdInGroup.i32(i32 96)  ; FlattenedThreadIdInGroup()
  %6 = udiv i32 %5, 2
  %7 = and i32 %6, 31
  %8 = shl i32 1, %7
  %9 = icmp eq i32 %5, 0
  %10 = icmp ne i1 %9, false
  %11 = zext i1 %10 to i32
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %13, label %14

; <label>:13                                      ; preds = %0
  store i32 0, i32 addrspace(3)* @"\01?g_GroupsharedValue@@3IA", align 4
  br label %14

; <label>:14                                      ; preds = %13, %0
  call void @dx.op.barrier(i32 80, i32 9)  ; Barrier(barrierMode)
  %15 = atomicrmw or i32 addrspace(3)* @"\01?g_GroupsharedValue@@3IA", i32 %8 seq_cst
  call void @dx.op.barrier(i32 80, i32 9)  ; Barrier(barrierMode)
  %16 = icmp ne i32 %11, 0
  br i1 %16, label %17, label %23

; <label>:17                                      ; preds = %14
  %18 = call %dx.types.CBufRet.i32 @dx.op.cbufferLoadLegacy.i32(i32 59, %dx.types.Handle %2, i32 0)  ; CBufferLoadLegacy(handle,regIndex)
  %19 = extractvalue %dx.types.CBufRet.i32 %18, 0
  %20 = mul i32 %4, %19
  %21 = add i32 %20, %3
  %22 = load i32, i32 addrspace(3)* @"\01?g_GroupsharedValue@@3IA", align 4
  call void @dx.op.rawBufferStore.i32(i32 140, %dx.types.Handle %1, i32 %21, i32 0, i32 %22, i32 undef, i32 undef, i32 undef, i8 1, i32 4)  ; RawBufferStore(uav,index,elementOffset,value0,value1,value2,value3,mask,alignment)
  br label %23

; <label>:23                                      ; preds = %17, %14
  %24 = load i32, i32* @dx.nothing
  ret void
}

; Function Attrs: nounwind readnone
declare i32 @dx.op.groupId.i32(i32, i32) #0

; Function Attrs: nounwind readnone
declare i32 @dx.op.flattenedThreadIdInGroup.i32(i32) #0

; Function Attrs: noduplicate nounwind
declare void @dx.op.barrier(i32, i32) #1

; Function Attrs: nounwind
declare void @dx.op.rawBufferStore.i32(i32, %dx.types.Handle, i32, i32, i32, i32, i32, i32, i8, i32) #2

; Function Attrs: nounwind readonly
declare %dx.types.CBufRet.i32 @dx.op.cbufferLoadLegacy.i32(i32, %dx.types.Handle, i32) #3

; Function Attrs: nounwind readonly
declare %dx.types.Handle @dx.op.createHandle(i32, i8, i32, i32, i1) #3

attributes #0 = { nounwind readnone }
attributes #1 = { noduplicate nounwind }
attributes #2 = { nounwind }
attributes #3 = { nounwind readonly }

!llvm.ident = !{!0}
!dx.version = !{!1}
!dx.valver = !{!2}
!dx.shaderModel = !{!3}
!dx.resources = !{!4}
!dx.entryPoints = !{!10}

!0 = !{!"clang version 3.7 (tags/RELEASE_370/final)"}
!1 = !{i32 1, i32 3}
!2 = !{i32 1, i32 6}
!3 = !{!"cs", i32 6, i32 3}
!4 = !{null, !5, !8, null}
!5 = !{!6}
!6 = !{i32 0, %"class.RWStructuredBuffer<unsigned int>"* undef, !"", i32 0, i32 0, i32 1, i32 12, i1 false, i1 false, i1 false, !7}
!7 = !{i32 1, i32 4}
!8 = !{!9}
!9 = !{i32 0, %"$Globals"* undef, !"", i32 0, i32 0, i32 1, i32 4, null}
!10 = !{void ()* @main, !"main", null, !4, !11}
!11 = !{i32 0, i64 17, i32 4, !12}
!12 = !{i32 8, i32 8, i32 1}
  1. Observe that the optimized version combines both branches if(isFirstThreadInGroup) surrounding the barriers into one and in doing so moves the InterlockedOr operation with its surrounding memory barrier instructions into two sepparate branches.

Expected result

The @dx.op.barrier(i32 80, i32 9) instructions and the atomicrmw instruction they surround or do not get moved into two separate branches when optimization is turned on and the value written into the structured buffer is the result of oring values of all the groups in the thread.

Actual result

The @dx.op.barrier(i32 80, i32 9) instructions and the atomicrmw instruction they surround get put into two branches when optimization is turned on and the resulting code does not work as expected (It works correctly with optimization turned off or when compiling with FXC)

Further technical details

Although I'm not completely sure that the barrier instructions being in both branches is the root cause of the synchronization issue, this trait seems to be the only common factor in the cases when the shader does not work as expected. Also, observe that adding the workaround seen in workaround_shader.txt
the barriers get moved out of the branches and the shader works as expected again, I'm attaching a link to this in shader playground as well http://shader-playground.timjones.io/4cce6c87cb809f659c7f285d14554fbe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions