Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPIRVReader: Add OpCopyMemory support #2779

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1814,21 +1814,40 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
return mapValue(BV, LI);
}

case OpCopyMemory:
case OpCopyMemorySized: {
SPIRVCopyMemorySized *BC = static_cast<SPIRVCopyMemorySized *>(BV);
CallInst *CI = nullptr;
llvm::Value *Dst = transValue(BC->getTarget(), F, BB);
MaybeAlign Align(BC->getAlignment());
llvm::Value *Src = nullptr;
llvm::Value *Dst = nullptr;
llvm::Value *Size = nullptr;
SPIRVMemoryAccess *MA = nullptr;
if (OC == OpCopyMemory) {
auto *BC = static_cast<SPIRVCopyMemory *>(BV);
Src = transValue(BC->getSource(), F, BB);
Dst = transValue(BC->getTarget(), F, BB);
Type *EltTy =
transType(BC->getSource()->getType()->getPointerElementType());
Size = ConstantExpr::getSizeOf(EltTy);
MA = static_cast<SPIRVMemoryAccess *>(BC);
} else {
assert(OC == OpCopyMemorySized);
auto *BC = static_cast<SPIRVCopyMemorySized *>(BV);
Src = transValue(BC->getSource(), F, BB);
Dst = transValue(BC->getTarget(), F, BB);
Size = transValue(BC->getSize(), F, BB);
MA = static_cast<SPIRVMemoryAccess *>(BC);
}
assert(Src);
assert(Dst);
assert(Size);
assert(MA);
vmaksimo marked this conversation as resolved.
Show resolved Hide resolved
MaybeAlign Align(MA->getAlignment());
MaybeAlign SrcAlign =
BC->getSrcAlignment() ? MaybeAlign(BC->getSrcAlignment()) : Align;
llvm::Value *Size = transValue(BC->getSize(), F, BB);
bool IsVolatile = BC->SPIRVMemoryAccess::isVolatile();
IRBuilder<> Builder(BB);
MA->getSrcAlignment() ? MaybeAlign(MA->getSrcAlignment()) : Align;
bool IsVolatile = MA->SPIRVMemoryAccess::isVolatile();

if (!CI) {
llvm::Value *Src = transValue(BC->getSource(), F, BB);
CI = Builder.CreateMemCpy(Dst, Align, Src, SrcAlign, Size, IsVolatile);
}
IRBuilder<> Builder(BB);
CallInst *CI =
Builder.CreateMemCpy(Dst, Align, Src, SrcAlign, Size, IsVolatile);
if (isFuncNoUnwind())
CI->getFunction()->addFnAttr(Attribute::NoUnwind);
return mapValue(BV, CI);
Expand Down
41 changes: 41 additions & 0 deletions test/OpCopyMemory.spvasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
; Check SPIRVReader support for OpCopyMemory.

; REQUIRES: spirv-as
; RUN: spirv-as --target-env spv1.0 -o %t.spv %s
; RUN: spirv-val %t.spv
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis %t.rev.bc -o - | FileCheck %s

OpCapability Addresses
OpCapability Int16
OpCapability Kernel
OpMemoryModel Physical64 OpenCL
OpEntryPoint Kernel %kernel "copymemory"
OpName %pShort "pShort"
OpName %dstShort "dstShort"
OpName %pInt "pInt"
OpName %dstInt "dstInt"
%ushort = OpTypeInt 16 0
%uint = OpTypeInt 32 0
%void = OpTypeVoid
%gptr_short = OpTypePointer CrossWorkgroup %ushort
%pptr_short = OpTypePointer Function %ushort
%gptr_int = OpTypePointer CrossWorkgroup %uint
%pptr_int = OpTypePointer Function %uint
%kernel_sig = OpTypeFunction %void %gptr_short %gptr_int
%ushort_42 = OpConstant %ushort 42
%uint_4242 = OpConstant %uint 4242
%kernel = OpFunction %void None %kernel_sig
%dstShort = OpFunctionParameter %gptr_short
%dstInt = OpFunctionParameter %gptr_int
%entry = OpLabel
%pShort = OpVariable %pptr_short Function %ushort_42
%pInt = OpVariable %pptr_int Function %uint_4242
OpCopyMemory %dstShort %pShort
OpCopyMemory %dstInt %pInt
OpReturn
OpFunctionEnd

; CHECK-LABEL: define spir_kernel void @copymemory(ptr addrspace(1) %dstShort, ptr addrspace(1) %dstInt)
; CHECK: call void @llvm.memcpy.p1.p0.i64(ptr addrspace(1) %dstShort, ptr @pShort, i64 ptrtoint (ptr getelementptr (i16, ptr null, i32 1) to i64), i1 false)
; CHECK: call void @llvm.memcpy.p1.p0.i64(ptr addrspace(1) %dstInt, ptr @pInt, i64 ptrtoint (ptr getelementptr (i32, ptr null, i32 1) to i64), i1 false)
svenvh marked this conversation as resolved.
Show resolved Hide resolved
Loading