Skip to content

Commit 6fb3494

Browse files
bwlodarczsys-ce-bb
authored andcommitted
Struct as result type in OpGroupNonUniformShuffleDown translation fix (#2339)
The contract between some frontends and Translator is that calls to functions which are matching to hardcoded name are converted to specific Ops in SPIR-V. The translation from LLVM to SPIR-V of OpGroupNonUnfiromShuffleDown call was done incorrectly. The struct member which is correct target for this op was left unwrapped which resulted in SPIR-V nonconforming with specification. The result of this were later problems with e.g. SPIR-V to LLVM translation. Original commit: KhronosGroup/SPIRV-LLVM-Translator@6b3ec4166a63039
1 parent e59ed66 commit 6fb3494

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6411,6 +6411,29 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
64116411
return BM->addCompositeConstructInst(transType(CI->getType()), Operands,
64126412
BB);
64136413
}
6414+
case OpGroupNonUniformShuffleDown: {
6415+
Function *F = CI->getCalledFunction();
6416+
if (F->arg_size() && F->getArg(0)->hasStructRetAttr()) {
6417+
StructType *St = cast<StructType>(F->getParamStructRetType(0));
6418+
assert(isSYCLHalfType(St) || isSYCLBfloat16Type(St));
6419+
SPIRVValue *InValue =
6420+
transValue(CI->getArgOperand(0)->stripPointerCasts(), BB);
6421+
SPIRVId ScopeId = transValue(CI->getArgOperand(1), BB)->getId();
6422+
SPIRVValue *Delta = transValue(CI->getArgOperand(3), BB);
6423+
SPIRVValue *Composite0 = BM->addLoadInst(InValue, {}, BB);
6424+
Type *MemberTy = St->getElementType(0);
6425+
SPIRVType *ElementTy = transType(MemberTy);
6426+
SPIRVValue *Element0 =
6427+
BM->addCompositeExtractInst(ElementTy, Composite0, {0}, BB);
6428+
SPIRVValue *Src =
6429+
BM->addGroupInst(OpGroupNonUniformShuffleDown, ElementTy,
6430+
static_cast<Scope>(ScopeId), {Element0, Delta}, BB);
6431+
SPIRVValue *Composite1 =
6432+
BM->addCompositeInsertInst(Src, Composite0, {0}, BB);
6433+
return BM->addStoreInst(InValue, Composite1, {}, BB);
6434+
}
6435+
[[fallthrough]];
6436+
}
64146437
default: {
64156438
if (isCvtOpCode(OC) && OC != OpGenericCastToPtrExplicit) {
64166439
return BM->addUnaryInst(OC, transScavengedType(CI),
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck --check-prefix CHECK-SPIRV %s
3+
; RUN: llvm-spirv %t.bc -o %t.spv
4+
; RUN: spirv-val %t.spv
5+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
6+
; RUN: llvm-dis %t.rev.bc -o - | FileCheck --check-prefix CHECK-LLVM %s
7+
8+
; CHECK-SPIRV-DAG: TypeInt [[#I32:]] 32 0
9+
; CHECK-SPIRV-DAG: Constant [[#I32]] [[#CONST_I32_3:]] 3
10+
; CHECK-SPIRV-DAG: Constant [[#I32]] [[#CONST_I32_8:]] 8
11+
; CHECK-SPIRV-DAG: TypeFloat [[#HALF:]] 16
12+
; CHECK-SPIRV-DAG: TypeStruct [[#S_HALF:]] [[#HALF]]
13+
; CHECK-SPIRV-DAG: TypePointer [[#PTR_S_HALF:]] {{[0-9]+}} [[#S_HALF]]
14+
15+
target triple = "spir64-unknown-unknown"
16+
17+
%"class.sycl::_V1::detail::half_impl::half" = type { half }
18+
19+
define spir_func void @test_group_non_uniform_shuffle_down() {
20+
entry:
21+
%agg.tmp.i.i = alloca %"class.sycl::_V1::detail::half_impl::half", align 2
22+
%ref.tmp.i = alloca %"class.sycl::_V1::detail::half_impl::half", align 2
23+
%ref.tmp.ascast.i = addrspacecast ptr %ref.tmp.i to ptr addrspace(4)
24+
call spir_func void @_Z34__spirv_GroupNonUniformShuffleDownIN4sycl3_V16detail9half_impl4halfEET_N5__spv5Scope4FlagES5_j(ptr addrspace(4) dead_on_unwind writable sret(%"class.sycl::_V1::detail::half_impl::half") align 2 %ref.tmp.ascast.i, i32 noundef 3, ptr noundef nonnull byval(%"class.sycl::_V1::detail::half_impl::half") align 2 %agg.tmp.i.i, i32 noundef 8)
25+
ret void
26+
}
27+
28+
; CHECK-SPIRV: Variable {{[0-9]+}} {{[0-9]+}}
29+
; CHECK-SPIRV: Variable [[#PTR_S_HALF]] [[#VAR_0:]]
30+
; CHECK-SPIRV: Load [[#S_HALF]] [[#COMP_0:]] [[#VAR_0]]
31+
; CHECK-SPIRV: CompositeExtract [[#HALF]] [[#ELEM_0:]] [[#COMP_0]] 0
32+
; CHECK-SPIRV: GroupNonUniformShuffleDown [[#HALF]] [[#ELEM_1:]] [[#CONST_I32_3]] [[#ELEM_0]] [[#CONST_I32_8]]
33+
; CHECK-SPIRV: CompositeInsert [[#S_HALF]] [[#COMP_1:]] [[#ELEM_1]] [[#COMP_0]] 0
34+
; CHECK-SPIRV: Store [[#VAR_0]] [[#COMP_1]]
35+
36+
; CHECK-LLVM: [[ALLOCA_0:%[a-z0-9.]+]] = alloca %"class.sycl::_V1::detail::half_impl::half", align 2
37+
; CHECK-LLVM: [[ALLOCA_1:%[a-z0-9.]+]] = alloca %"class.sycl::_V1::detail::half_impl::half", align 2
38+
; CHECK-LLVM: [[LOAD_0:%[a-z0-9.]+]] = load %"class.sycl::_V1::detail::half_impl::half", ptr [[ALLOCA_1]], align 2
39+
; CHECK-LLVM: [[EXTRACT_0:%[a-z0-9.]+]] = extractvalue %"class.sycl::_V1::detail::half_impl::half" [[LOAD_0]], 0
40+
; CHECK-LLVM: [[CALL_0:%[a-z0-9.]+]] = call spir_func half @_Z22sub_group_shuffle_downDhj(half [[EXTRACT_0]], i32 8) #0
41+
; CHECK-LLVM: [[INSERT_0:%[a-z0-9.]+]] = insertvalue %"class.sycl::_V1::detail::half_impl::half" [[LOAD_0]], half [[CALL_0]], 0
42+
; CHECK-LLVM: store %"class.sycl::_V1::detail::half_impl::half" [[INSERT_0]], ptr [[ALLOCA_1]], align 2
43+
44+
declare dso_local spir_func void @_Z34__spirv_GroupNonUniformShuffleDownIN4sycl3_V16detail9half_impl4halfEET_N5__spv5Scope4FlagES5_j(ptr addrspace(4) dead_on_unwind writable sret(%"class.sycl::_V1::detail::half_impl::half") align 2, i32 noundef, ptr noundef byval(%"class.sycl::_V1::detail::half_impl::half") align 2, i32 noundef) local_unnamed_addr

0 commit comments

Comments
 (0)