Skip to content

Commit

Permalink
Improve/fix translation of SPIR-V OpControlBarier to OCL1.2
Browse files Browse the repository at this point in the history
This patch features two things:
- fixed translation of OpControlBarrier into OpenCL 2.0 built-ins:

  Scope of the operation is now respected and we can either get
  work_group_barrier or sub_group_barrier depending on it

- improved the translation so it handles non-constant 'Memory Semantic'
  operand of the instruction
  • Loading branch information
AlexeySachkov committed Apr 1, 2020
1 parent e4e8888 commit 33d1092
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
20 changes: 16 additions & 4 deletions lib/SPIRV/SPIRVToOCL20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,23 @@ void SPIRVToOCL20::visitCallSPIRVControlBarrier(CallInst *CI) {
return cast<ConstantInt>(Args[I])->getZExtValue();
};
auto ExecScope = static_cast<Scope>(GetArg(0));
auto MScope = static_cast<Scope>(GetArg(1));
auto Sema = mapSPIRVMemSemanticToOCL(GetArg(2));
auto MemScope = static_cast<Scope>(GetArg(1));

if (auto Arg = dyn_cast<ConstantInt>(Args[2])) {
auto Sema = mapSPIRVMemSemanticToOCL(Arg->getZExtValue());
Args[0] = getInt32(M, Sema.first);
} else {
int ClMemFenceMask = MemorySemanticsWorkgroupMemoryMask |
MemorySemanticsCrossWorkgroupMemoryMask |
MemorySemanticsImageMemoryMask;
Args[0] = getOrCreateSwitchFunc(
kSPIRVName::TranslateSPIRVMemFence, Args[2],
OCLMemFenceExtendedMap::getRMap(), true /*IsReverse*/, None, CI,
M, ClMemFenceMask);
}
Args[1] = getInt32(M, rmap<OCLScopeKind>(MemScope));
Args.resize(2);
Args[0] = getInt32(M, Sema.first);
Args[1] = getInt32(M, rmap<OCLScopeKind>(MScope));

return (ExecScope == ScopeWorkgroup) ? kOCLBuiltinName::WorkGroupBarrier
: kOCLBuiltinName::SubGroupBarrier;
},
Expand Down
17 changes: 12 additions & 5 deletions test/barrier_explicit_arguments.spt
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,41 @@
2 Capability Kernel
5 ExtInstImport 1 "OpenCL.std"
3 MemoryModel 1 2
3 Source 3 102000
4 Name 5 "test"
3 Name 6 "val"
4 Name 7 "entry"
6 Decorate 5 LinkageAttributes "test" Export
4 TypeInt 3 32 0
4 Constant 3 8 2
2 TypeVoid 2
4 Constant 3 9 3
4 TypeFunction 4 2 3

5 Function 2 5 0 4
3 FunctionParameter 3 6

2 Label 7
4 ControlBarrier 8 8 6
4 ControlBarrier 9 9 6
1 Return

1 FunctionEnd

; RUN: llvm-spirv %s -to-binary -o %t.spv
; RUN: spirv-val %t.spv
; RUN: llvm-spirv -r %t.spv -o %t.bc
; RUN: llvm-dis < %t.bc | FileCheck %s
; RUN: llvm-spirv -r -spirv-ocl-builtins-version=CL2.0 %t.spv -o %t.bc
; RUN: llvm-dis < %t.bc | FileCheck %s --check-prefixes=CHECK,CHECK-20
; RUN: llvm-spirv -r -spirv-ocl-builtins-version=CL1.2 %t.spv -o %t.bc
; RUN: llvm-dis < %t.bc | FileCheck %s --check-prefixes=CHECK,CHECK-12

; CHECK: define spir_func void @test(i32 %val)
; CHECK: %call = call spir_func i32 @__translate_spirv_memory_fence(i32 %val)
; CHECK: call spir_func void @_Z7barrierj(i32 %call)
; CHECK-12: call spir_func void @_Z7barrierj(i32 %call)
; There is no support for sub-groups in OpenCL 1.2, so sub-group barrier is
; equal to regular barrier in there
; CHECK-12: call spir_func void @_Z7barrierj(i32 %call1)
; CHECK-20: call spir_func void @_Z18work_group_barrierj12memory_scope(i32 %call
; CHECK-20: call spir_func void @_Z17sub_group_barrierj12memory_scope(i32 %call
; CHECK: define private spir_func i32 @__translate_spirv_memory_fence(i32 %key)
; CHECK: entry:
; CHECK: %key.masked = and i32 2816, %key
Expand Down Expand Up @@ -60,4 +68,3 @@
; CHECK: case.2816:
; CHECK: ret i32 7
; CHECK: }
; CHECK: declare spir_func void @_Z7barrierj(i32)

0 comments on commit 33d1092

Please sign in to comment.