Skip to content

Commit 27bbf0f

Browse files
authored
[Backport to 17] SPIRVReader: Add OpCopyMemory support (#2779) (#2814)
Add support for translating `OpCopyMemory` into `llvm.memcpy`. Fixes #2769 (cherry picked from commit 8dc0349)
1 parent 4f1fa68 commit 27bbf0f

File tree

2 files changed

+76
-7
lines changed

2 files changed

+76
-7
lines changed

lib/SPIRV/SPIRVReader.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,9 +1855,28 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
18551855
return mapValue(BV, LI);
18561856
}
18571857

1858+
case OpCopyMemory: {
1859+
auto *BC = static_cast<SPIRVCopyMemory *>(BV);
1860+
llvm::Value *Dst = transValue(BC->getTarget(), F, BB);
1861+
MaybeAlign Align(BC->getAlignment());
1862+
MaybeAlign SrcAlign =
1863+
BC->getSrcAlignment() ? MaybeAlign(BC->getSrcAlignment()) : Align;
1864+
Type *EltTy =
1865+
transType(BC->getSource()->getType()->getPointerElementType());
1866+
uint64_t Size = M->getDataLayout().getTypeStoreSize(EltTy).getFixedValue();
1867+
bool IsVolatile = BC->SPIRVMemoryAccess::isVolatile();
1868+
IRBuilder<> Builder(BB);
1869+
1870+
llvm::Value *Src = transValue(BC->getSource(), F, BB);
1871+
CallInst *CI =
1872+
Builder.CreateMemCpy(Dst, Align, Src, SrcAlign, Size, IsVolatile);
1873+
if (isFuncNoUnwind())
1874+
CI->getFunction()->addFnAttr(Attribute::NoUnwind);
1875+
return mapValue(BV, CI);
1876+
}
1877+
18581878
case OpCopyMemorySized: {
18591879
SPIRVCopyMemorySized *BC = static_cast<SPIRVCopyMemorySized *>(BV);
1860-
CallInst *CI = nullptr;
18611880
llvm::Value *Dst = transValue(BC->getTarget(), F, BB);
18621881
MaybeAlign Align(BC->getAlignment());
18631882
MaybeAlign SrcAlign =
@@ -1869,13 +1888,12 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
18691888
// A ptr.annotation may have been generated for the destination variable.
18701889
replaceOperandWithAnnotationIntrinsicCallResult(F, Dst);
18711890

1872-
if (!CI) {
1873-
llvm::Value *Src = transValue(BC->getSource(), F, BB);
1891+
llvm::Value *Src = transValue(BC->getSource(), F, BB);
18741892

1875-
// A ptr.annotation may have been generated for the source variable.
1876-
replaceOperandWithAnnotationIntrinsicCallResult(F, Src);
1877-
CI = Builder.CreateMemCpy(Dst, Align, Src, SrcAlign, Size, IsVolatile);
1878-
}
1893+
// A ptr.annotation may have been generated for the source variable.
1894+
replaceOperandWithAnnotationIntrinsicCallResult(F, Src);
1895+
CallInst *CI =
1896+
Builder.CreateMemCpy(Dst, Align, Src, SrcAlign, Size, IsVolatile);
18791897
if (isFuncNoUnwind())
18801898
CI->getFunction()->addFnAttr(Attribute::NoUnwind);
18811899
return mapValue(BV, CI);

test/OpCopyMemory.spvasm

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; Check SPIRVReader support for OpCopyMemory.
2+
3+
; REQUIRES: spirv-as
4+
; RUN: spirv-as --target-env spv1.0 -o %t.spv %s
5+
; RUN: spirv-val %t.spv
6+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
7+
; RUN: llvm-dis %t.rev.bc -o - | FileCheck %s
8+
9+
OpCapability Addresses
10+
OpCapability Int16
11+
OpCapability Kernel
12+
OpMemoryModel Physical64 OpenCL
13+
OpEntryPoint Kernel %kernel "copymemory"
14+
OpName %pStruct "pStruct"
15+
OpName %dstStruct "dstStruct"
16+
OpName %pShort "pShort"
17+
OpName %dstShort "dstShort"
18+
OpName %pInt "pInt"
19+
OpName %dstInt "dstInt"
20+
%ushort = OpTypeInt 16 0
21+
%uint = OpTypeInt 32 0
22+
%struct = OpTypeStruct %ushort %uint %ushort
23+
%void = OpTypeVoid
24+
%gptr_struct = OpTypePointer CrossWorkgroup %struct
25+
%pptr_struct = OpTypePointer Function %struct
26+
%gptr_short = OpTypePointer CrossWorkgroup %ushort
27+
%pptr_short = OpTypePointer Function %ushort
28+
%gptr_int = OpTypePointer CrossWorkgroup %uint
29+
%pptr_int = OpTypePointer Function %uint
30+
%kernel_sig = OpTypeFunction %void %gptr_short %gptr_int %gptr_struct
31+
%ushort_42 = OpConstant %ushort 42
32+
%uint_4242 = OpConstant %uint 4242
33+
%struct_init = OpConstantComposite %struct %ushort_42 %uint_4242 %ushort_42
34+
%kernel = OpFunction %void None %kernel_sig
35+
%dstShort = OpFunctionParameter %gptr_short
36+
%dstInt = OpFunctionParameter %gptr_int
37+
%dstStruct = OpFunctionParameter %gptr_struct
38+
%entry = OpLabel
39+
%pShort = OpVariable %pptr_short Function %ushort_42
40+
%pInt = OpVariable %pptr_int Function %uint_4242
41+
%pStruct = OpVariable %pptr_struct Function %struct_init
42+
OpCopyMemory %dstShort %pShort
43+
OpCopyMemory %dstInt %pInt
44+
OpCopyMemory %dstStruct %pStruct
45+
OpReturn
46+
OpFunctionEnd
47+
48+
; CHECK-LABEL: define spir_kernel void @copymemory(ptr addrspace(1) %dstShort, ptr addrspace(1) %dstInt, ptr addrspace(1) %dstStruct)
49+
; CHECK: call void @llvm.memcpy.p1.p0.i64(ptr addrspace(1) %dstShort, ptr @pShort, i64 2, i1 false)
50+
; CHECK: call void @llvm.memcpy.p1.p0.i64(ptr addrspace(1) %dstInt, ptr @pInt, i64 4, i1 false)
51+
; CHECK: call void @llvm.memcpy.p1.p0.i64(ptr addrspace(1) %dstStruct, ptr @pStruct, i64 12, i1 false)

0 commit comments

Comments
 (0)