Skip to content

Commit 56b3936

Browse files
authored
Translate runtime-known barrier's MemScope from SPV-IR to OCL2.0 IR (#1804)
This partially addresses #1805 Signed-off-by: Sidorov, Dmitry <[email protected]>
1 parent 365fcc5 commit 56b3936

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

lib/SPIRV/SPIRVToOCL20.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void SPIRVToOCL20Base::visitCallSPIRVControlBarrier(CallInst *CI) {
9090
};
9191
auto ExecScope = static_cast<Scope>(GetArg(0));
9292
Value *MemScope =
93-
getInt32(M, rmap<OCLScopeKind>(static_cast<Scope>(GetArg(1))));
93+
SPIRV::transSPIRVMemoryScopeIntoOCLMemoryScope(CI->getArgOperand(1), CI);
9494
Value *MemFenceFlags = SPIRV::transSPIRVMemorySemanticsIntoOCLMemFenceFlags(
9595
CI->getArgOperand(2), CI);
9696
mutateCallInst(CI, ExecScope == ScopeWorkgroup
@@ -100,11 +100,8 @@ void SPIRVToOCL20Base::visitCallSPIRVControlBarrier(CallInst *CI) {
100100
}
101101

102102
void SPIRVToOCL20Base::visitCallSPIRVSplitBarrierINTEL(CallInst *CI, Op OC) {
103-
auto GetArg = [=](unsigned I) {
104-
return cast<ConstantInt>(CI->getArgOperand(I))->getZExtValue();
105-
};
106103
Value *MemScope =
107-
getInt32(M, rmap<OCLScopeKind>(static_cast<Scope>(GetArg(1))));
104+
SPIRV::transSPIRVMemoryScopeIntoOCLMemoryScope(CI->getArgOperand(1), CI);
108105
Value *MemFenceFlags = SPIRV::transSPIRVMemorySemanticsIntoOCLMemFenceFlags(
109106
CI->getArgOperand(2), CI);
110107
mutateCallInst(CI, OCLSPIRVBuiltinMap::rmap(OC))
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
; This test checks that the translator is capable to correctly translate
2+
; __spirv_ControlBarrier with runtime-known MemScope parameter
3+
; to SPIR-V and back to OpenCL 2.0 IR.
4+
; TODO: to remove this test once
5+
; https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/1805
6+
; is fixed as barrier.cl and sub_group_barrier.cl will be enough to test this
7+
; case
8+
9+
; RUN: llvm-as %s -o %t.bc
10+
; RUN: llvm-spirv %t.bc -spirv-text -o %t.spv.txt
11+
; RUN: FileCheck < %t.spv.txt %s --check-prefix=CHECK-SPIRV
12+
; RUN: llvm-spirv -to-binary %t.spv.txt -o %t.spv
13+
; RUN: spirv-val %t.spv
14+
; RUN: llvm-spirv --spirv-target-env=CL2.0 -r %t.spv -o %t.bc
15+
; RUN: llvm-dis %t.bc -o - | FileCheck %s --check-prefix=CHECK-LLVM
16+
17+
; CHECK-SPIRV: FunctionCall [[#]] [[#SCOPE:]] [[#]]
18+
; CHECK-SPIRV: ControlBarrier [[#]] [[#SCOPE]] [[#]]
19+
20+
; CHECK-LLVM: %[[#SCOPE:]] = call spir_func i32 @_Z8getScopev()
21+
; CHECK-LLVM: [[CALL:%[a-z0-9]+]] = call spir_func i32 @__translate_spirv_memory_scope(i32 %[[#SCOPE]])
22+
; CHECK-LLVM: call spir_func void @_Z17sub_group_barrierj12memory_scope(i32 3, i32 [[CALL]])
23+
24+
; CHECK-LLVM: define private spir_func i32 @__translate_spirv_memory_scope(i32 %key) {
25+
; CHECK-LLVM: entry:
26+
; CHECK-LLVM: switch i32 %key, label %default [
27+
; CHECK-LLVM: i32 4, label %case.4
28+
; CHECK-LLVM: i32 2, label %case.2
29+
; CHECK-LLVM: i32 1, label %case.1
30+
; CHECK-LLVM: i32 0, label %case.0
31+
; CHECK-LLVM: i32 3, label %case.3
32+
; CHECK-LLVM: ]
33+
; CHECK-LLVM: default: ; preds = %entry
34+
; CHECK-LLVM: unreachable
35+
; CHECK-LLVM: case.4: ; preds = %entry
36+
; CHECK-LLVM: ret i32 0
37+
; CHECK-LLVM: case.2: ; preds = %entry
38+
; CHECK-LLVM: ret i32 1
39+
; CHECK-LLVM: case.1: ; preds = %entry
40+
; CHECK-LLVM: ret i32 2
41+
; CHECK-LLVM: case.0: ; preds = %entry
42+
; CHECK-LLVM: ret i32 3
43+
; CHECK-LLVM: case.3: ; preds = %entry
44+
; CHECK-LLVM: ret i32 4
45+
; CHECK-LLVM: }
46+
47+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
48+
target triple = "spirv64-unknown-unknown"
49+
50+
define spir_func void @_Z3foov() {
51+
%1 = call noundef i32 @_Z8getScopev()
52+
call void @_Z22__spirv_ControlBarrieriii(i32 noundef 3, i32 noundef %1, i32 noundef 912)
53+
ret void
54+
}
55+
56+
declare spir_func void @_Z22__spirv_ControlBarrieriii(i32 noundef, i32 noundef, i32 noundef)
57+
58+
declare spir_func i32 @_Z8getScopev()

0 commit comments

Comments
 (0)