Skip to content

Commit 12497a1

Browse files
MrSidimsjsji
authored andcommitted
Fix OpVariable creation during memset translation (#3436)
It should be a function-level OpVariable, not Global var. Fixes KhronosGroup/SPIRV-LLVM-Translator#3402 Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com> Original commit: KhronosGroup/SPIRV-LLVM-Translator@8ef804541897a80
1 parent 80e2792 commit 12497a1

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4858,16 +4858,17 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
48584858
std::vector<SPIRVValue *> Elts(TNumElts, transValue(Val, BB));
48594859
Init = BM->addCompositeConstant(CompositeTy, Elts);
48604860
}
4861-
SPIRVType *VarTy = transPointerType(AT, SPIRV::SPIRAS_Constant);
4862-
SPIRVValue *Var = BM->addVariable(VarTy, nullptr, /*isConstant*/ true,
4861+
SPIRVType *VarTy = transPointerType(AT, SPIRV::SPIRAS_Private);
4862+
SPIRVBasicBlock *EntryBB = BB->getParent()->getBasicBlock(0);
4863+
SPIRVValue *Var = BM->addVariable(VarTy, nullptr, /*isConstant*/ false,
48634864
spv::internal::LinkageTypeInternal, Init,
4864-
"", StorageClassUniformConstant, nullptr);
4865+
"", StorageClassFunction, EntryBB);
48654866
std::vector<SPIRVWord> MemAccess = GetMemoryAccess(
48664867
MSI, BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_4));
48674868
if (!MemAccess.empty() && MemAccess[0] == MemoryAccessAlignedMask)
48684869
Var->setAlignment(MemAccess[1]);
48694870
SPIRVType *SourceTy =
4870-
transPointerType(Val->getType(), SPIRV::SPIRAS_Constant);
4871+
transPointerType(Val->getType(), SPIRV::SPIRAS_Private);
48714872
SPIRVValue *Source = BM->addUnaryInst(OpBitcast, SourceTy, Var, BB);
48724873
SPIRVValue *Target = transValue(MSI->getRawDest(), BB);
48734874
return BM->addCopyMemorySizedInst(Target, Source, CompositeTy->getLength(),

llvm-spirv/test/llvm-intrinsics/memset-align.ll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
; RUN: llvm-spirv %t.bc -spirv-text -o %t.txt
33
; RUN: FileCheck < %t.txt %s --check-prefix=CHECK-SPIRV
44
; RUN: llvm-spirv %t.bc -o %t.spv
5+
; RUN: spirv-val %t.spv
56
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
67
; RUN: llvm-dis %t.rev.bc
78
; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM
@@ -16,12 +17,14 @@ entry:
1617

1718
; CHECK-SPIRV: Decorate [[#]] Alignment 16
1819
; CHECK-SPIRV: Decorate [[#SrcVar:]] Alignment [[#SrcAlignment:]]
20+
; CHECK-SPIRV: Variable [[#]] [[#SrcVar]] 7
1921
; CHECK-SPIRV: Bitcast [[#]] [[#]] [[#]]
2022
; CHECK-SPIRV: Bitcast [[#]] [[#Src:]] [[#SrcVar]]
2123
; CHECK-SPIRV: CopyMemorySized [[#]] [[#Src]] [[#]] 2 [[#SrcAlignment]]
22-
; CHECK-LLVM: [[SrcVar:@[0-9]+]] = internal unnamed_addr addrspace(2) constant [16 x i8] zeroinitializer, align [[#SrcAlignment:]]
23-
; CHECK-LLVM: [[SrcOp:%[0-9]+]] = bitcast ptr addrspace(2) [[SrcVar]] to ptr addrspace(2)
24-
; CHECK-LLVM: call void @llvm.memcpy.p4.p2.i64(ptr addrspace(4) align 16 %0, ptr addrspace(2) align [[#SrcAlignment]] [[SrcOp]], i64 16, i1 false)
24+
; CHECK-LLVM: %{{[0-9]+}} = alloca [16 x i8], align [[#SrcAlignment:]]
25+
; CHECK-LLVM: store [16 x i8] zeroinitializer, ptr %{{[0-9]+}}, align 1
26+
; CHECK-LLVM: [[SrcOp:%[0-9]+]] = bitcast ptr %{{[0-9]+}} to ptr
27+
; CHECK-LLVM: call void @llvm.memcpy.p4.p0.i64(ptr addrspace(4) align 16 %{{[0-9]+}}, ptr align [[#SrcAlignment]] [[SrcOp]], i64 16, i1 false)
2528
call void @llvm.memset.p4.i64(ptr addrspace(4) align 16 %r.sroa.0.0.r.ascast.sroa_cast1, i8 0, i64 16, i1 false)
2629
ret void
2730
}

llvm-spirv/test/llvm-intrinsics/memset-opaque.ll

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
; CHECK-SPIRV: TypeArray [[Int8x4:[0-9]+]] [[Int8]] [[Lenmemset21]]
2121
; CHECK-SPIRV-TYPED-PTR: TypePointer [[Int8Ptr:[0-9]+]] 8 [[Int8]]
2222
; CHECK-SPIRV: TypeArray [[Int8x12:[0-9]+]] [[Int8]] [[Lenmemset0]]
23-
; CHECK-SPIRV-TYPED-PTR: TypePointer [[Int8PtrConst:[0-9]+]] 0 [[Int8]]
24-
; CHECK-SPIRV-UNTYPED-PTR: TypeUntypedPointerKHR [[Int8PtrConst:[0-9]+]] 0
23+
; CHECK-SPIRV-TYPED-PTR: TypePointer [[Int8PtrConst:[0-9]+]] 7 [[Int8]]
24+
; CHECK-SPIRV-UNTYPED-PTR: TypeUntypedPointerKHR [[Int8PtrConst:[0-9]+]] 7
2525

2626
; CHECK-SPIRV: ConstantNull [[Int8x12]] [[Init:[0-9]+]]
27-
; CHECK-SPIRV: Variable {{[0-9]+}} [[Val:[0-9]+]] 0 [[Init]]
2827
; CHECK-SPIRV: 7 ConstantComposite [[Int8x4]] [[InitComp:[0-9]+]] [[Const21]] [[Const21]] [[Const21]] [[Const21]]
29-
; CHECK-SPIRV: Variable {{[0-9]+}} [[ValComp:[0-9]+]] 0 [[InitComp]]
3028
; CHECK-SPIRV: ConstantFalse [[#]] [[#False:]]
29+
; CHECK-SPIRV: Variable {{[0-9]+}} [[Val:[0-9]+]] 7 [[Init]]
30+
; CHECK-SPIRV: Variable {{[0-9]+}} [[ValComp:[0-9]+]] 7 [[InitComp]]
3131

3232
; CHECK-SPIRV: Bitcast [[Int8Ptr]] [[Target:[0-9]+]] {{[0-9]+}}
3333
; CHECK-SPIRV: Bitcast [[Int8PtrConst]] [[Source:[0-9]+]] [[Val]]
@@ -68,16 +68,18 @@ target triple = "spir"
6868

6969
%struct.S1 = type { i32, i32, i32 }
7070

71-
; CHECK-LLVM-OPAQUE: internal unnamed_addr addrspace(2) constant [12 x i8] zeroinitializer
72-
; CHECK-LLVM-OPAQUE: internal unnamed_addr addrspace(2) constant [4 x i8] c"\15\15\15\15"
71+
; CHECK-LLVM-OPAQUE: %{{[0-9]+}} = alloca [12 x i8], align 4
72+
; CHECK-LLVM-OPAQUE: store [12 x i8] zeroinitializer, ptr %{{[0-9]+}}, align 1
73+
; CHECK-LLVM-OPAQUE: %{{[0-9]+}} = alloca [4 x i8], align 4
74+
; CHECK-LLVM-OPAQUE: store [4 x i8] c"\15\15\15\15", ptr %{{[0-9]+}}, align 1
7375

7476
; Function Attrs: nounwind
7577
define spir_func void @_Z5foo11v(ptr addrspace(4) noalias captures(none) sret(%struct.S1) %agg.result, i32 %s1, i64 %s2, i8 %v) #0 {
7678
%x = alloca [4 x i8]
7779
tail call void @llvm.memset.p4.i32(ptr addrspace(4) align 4 %agg.result, i8 0, i32 12, i1 false)
78-
; CHECK-LLVM-OPAQUE: call void @llvm.memcpy.p4.p2.i32(ptr addrspace(4) align 4 %1, ptr addrspace(2) align 4 %2, i32 12, i1 false)
80+
; CHECK-LLVM-OPAQUE: call void @llvm.memcpy.p4.p0.i32(ptr addrspace(4) align 4 %{{[0-9]+}}, ptr align 4 %{{[0-9]+}}, i32 12, i1 false)
7981
tail call void @llvm.memset.p0.i32(ptr align 4 %x, i8 21, i32 4, i1 false)
80-
; CHECK-LLVM-OPAQUE: call void @llvm.memcpy.p0.p2.i32(ptr align 4 %3, ptr addrspace(2) align 4 %4, i32 4, i1 false)
82+
; CHECK-LLVM-OPAQUE: call void @llvm.memcpy.p0.p0.i32(ptr align 4 %{{[0-9]+}}, ptr align 4 %{{[0-9]+}}, i32 4, i1 false)
8183

8284
; non-const value
8385
tail call void @llvm.memset.p0.i32(ptr align 4 %x, i8 %v, i32 3, i1 false)

llvm-spirv/test/transcoding/intrinsic_result_store.ll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
; with subsequent store instructions.
33

44
; RUN: llvm-spirv %s -o %t.spv
5+
; RUN: spirv-val %t.spv
56
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
67
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM
78

89
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
910
target triple = "spir64-unknown-unknown"
1011

11-
; CHECK-LLVM: [[ZERO_INIT:@[0-9]+]] = {{.*}} addrspace(2) constant [8 x i8] zeroinitializer
12+
; CHECK-LLVM: %[[ZERO_INIT:[0-9]+]] = alloca [8 x i8], align 8
13+
; CHECK-LLVM: store [8 x i8] zeroinitializer, ptr %[[ZERO_INIT]], align 1
1214

1315
; Function Attrs: convergent noinline nounwind optnone
1416
define spir_kernel void @test_memset(ptr addrspace(1) %data, i32 %input) #0 !kernel_arg_addr_space !1 !kernel_arg_access_qual !5 !kernel_arg_type !6 !kernel_arg_base_type !6 !kernel_arg_type_qual !7 {
1517
entry:
1618
; CHECK-LLVM: %[[BITCAST_RES:[[:alnum:].]+]] = bitcast ptr addrspace(1) %{{[[:alnum:].]+}} to ptr addrspace(1)
1719
%ptr = bitcast ptr addrspace(1) %data to ptr addrspace(1)
18-
; CHECK-LLVM: %[[#ZERO_INIT_BITCAST:]] = bitcast ptr addrspace(2) [[ZERO_INIT]] to ptr addrspace(2)
19-
; CHECK-LLVM: call void @llvm.memcpy.p1.p2.i64(ptr addrspace(1) align 8 %[[BITCAST_RES]], ptr addrspace(2) align 8 %[[#ZERO_INIT_BITCAST]], i64 8, i1 false)
20+
; CHECK-LLVM: %[[#ZERO_INIT_BITCAST:]] = bitcast ptr %[[ZERO_INIT]] to ptr
21+
; CHECK-LLVM: call void @llvm.memcpy.p1.p0.i64(ptr addrspace(1) align 8 %[[BITCAST_RES]], ptr align 8 %[[#ZERO_INIT_BITCAST]], i64 8, i1 false)
2022
call void @llvm.memset.p1.i64(ptr addrspace(1) align 8 %ptr, i8 0, i64 8, i1 false)
2123
; CHECK-LLVM: store i8 0, ptr addrspace(1) %[[BITCAST_RES]]
2224
store i8 0, ptr addrspace(1) %ptr

0 commit comments

Comments
 (0)