Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…nslator into amd-staging
  • Loading branch information
AlexVlx committed Nov 1, 2024
2 parents 5be01ba + cc57c3a commit e229d7c
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 24 deletions.
28 changes: 23 additions & 5 deletions lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1856,9 +1856,28 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
return mapValue(BV, LI);
}

case OpCopyMemory: {
auto *BC = static_cast<SPIRVCopyMemory *>(BV);
llvm::Value *Dst = transValue(BC->getTarget(), F, BB);
MaybeAlign Align(BC->getAlignment());
MaybeAlign SrcAlign =
BC->getSrcAlignment() ? MaybeAlign(BC->getSrcAlignment()) : Align;
Type *EltTy =
transType(BC->getSource()->getType()->getPointerElementType());
uint64_t Size = M->getDataLayout().getTypeStoreSize(EltTy).getFixedValue();
bool IsVolatile = BC->SPIRVMemoryAccess::isVolatile();
IRBuilder<> Builder(BB);

llvm::Value *Src = transValue(BC->getSource(), F, BB);
CallInst *CI =
Builder.CreateMemCpy(Dst, Align, Src, SrcAlign, Size, IsVolatile);
if (isFuncNoUnwind())
CI->getFunction()->addFnAttr(Attribute::NoUnwind);
return mapValue(BV, CI);
}

case OpCopyMemorySized: {
SPIRVCopyMemorySized *BC = static_cast<SPIRVCopyMemorySized *>(BV);
CallInst *CI = nullptr;
llvm::Value *Dst = transValue(BC->getTarget(), F, BB);
MaybeAlign Align(BC->getAlignment());
MaybeAlign SrcAlign =
Expand All @@ -1867,10 +1886,9 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
bool IsVolatile = BC->SPIRVMemoryAccess::isVolatile();
IRBuilder<> Builder(BB);

if (!CI) {
llvm::Value *Src = transValue(BC->getSource(), F, BB);
CI = Builder.CreateMemCpy(Dst, Align, Src, SrcAlign, Size, IsVolatile);
}
llvm::Value *Src = transValue(BC->getSource(), F, BB);
CallInst *CI =
Builder.CreateMemCpy(Dst, Align, Src, SrcAlign, Size, IsVolatile);
if (isFuncNoUnwind())
CI->getFunction()->addFnAttr(Attribute::NoUnwind);
return mapValue(BV, CI);
Expand Down
43 changes: 27 additions & 16 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2238,13 +2238,19 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
MemoryAccessNoAliasINTELMaskMask);
if (MemoryAccess.front() == 0)
MemoryAccess.clear();
return mapValue(
V,
BM->addLoadInst(
transValue(LD->getPointerOperand(), BB), MemoryAccess, BB,
BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_untyped_pointers)
? transType(LD->getType())
: nullptr));
if (BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_untyped_pointers)) {
SPIRVValue *Source = transValue(LD->getPointerOperand(), BB);
SPIRVType *LoadTy = transType(LD->getType());
// For images do not use explicit load type, but rather use the source
// type (calculated in SPIRVLoad constructor)
if (LoadTy->isTypeUntypedPointerKHR() &&
(Source->getType()->getPointerElementType()->isTypeImage())) {
LoadTy = nullptr;
}
return mapValue(V, BM->addLoadInst(Source, MemoryAccess, BB, LoadTy));
}
return mapValue(V, BM->addLoadInst(transValue(LD->getPointerOperand(), BB),
MemoryAccess, BB));
}

if (BinaryOperator *B = dyn_cast<BinaryOperator>(V)) {
Expand All @@ -2263,11 +2269,15 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
auto *FrexpResult = transValue(RV, BB);
SPIRVValue *IntFromFrexpResult =
static_cast<SPIRVExtInst *>(FrexpResult)->getArgValues()[1];
SPIRVType *LoadTy = nullptr;

if (IntFromFrexpResult->isUntypedVariable()) {
auto *BV =
static_cast<SPIRVUntypedVariableKHR *>(IntFromFrexpResult);
LoadTy = BV->getDataType();
}
IntFromFrexpResult =
BM->addLoadInst(IntFromFrexpResult, {}, BB,
BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_untyped_pointers)
? transType(cast<StructType>(RV->getType())->getTypeAtIndex(1))
: nullptr);
BM->addLoadInst(IntFromFrexpResult, {}, BB, LoadTy);

std::vector<SPIRVId> Operands = {FrexpResult->getId(),
IntFromFrexpResult->getId()};
Expand Down Expand Up @@ -2489,12 +2499,13 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
// Idx = 1
SPIRVValue *IntFromFrexpResult =
static_cast<SPIRVExtInst *>(Val)->getArgValues()[1];
SPIRVType *LoadTy = nullptr;
if (IntFromFrexpResult->isUntypedVariable()) {
auto *BV = static_cast<SPIRVUntypedVariableKHR *>(IntFromFrexpResult);
LoadTy = BV->getDataType();
}
IntFromFrexpResult =
BM->addLoadInst(
IntFromFrexpResult, {}, BB,
BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_untyped_pointers)
? transType(Ext->getType())
: nullptr);
BM->addLoadInst(IntFromFrexpResult, {}, BB, LoadTy);
return mapValue(V, IntFromFrexpResult);
}
}
Expand Down
51 changes: 51 additions & 0 deletions test/OpCopyMemory.spvasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
; 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: amd-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 %pStruct "pStruct"
OpName %dstStruct "dstStruct"
OpName %pShort "pShort"
OpName %dstShort "dstShort"
OpName %pInt "pInt"
OpName %dstInt "dstInt"
%ushort = OpTypeInt 16 0
%uint = OpTypeInt 32 0
%struct = OpTypeStruct %ushort %uint %ushort
%void = OpTypeVoid
%gptr_struct = OpTypePointer CrossWorkgroup %struct
%pptr_struct = OpTypePointer Function %struct
%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 %gptr_struct
%ushort_42 = OpConstant %ushort 42
%uint_4242 = OpConstant %uint 4242
%struct_init = OpConstantComposite %struct %ushort_42 %uint_4242 %ushort_42
%kernel = OpFunction %void None %kernel_sig
%dstShort = OpFunctionParameter %gptr_short
%dstInt = OpFunctionParameter %gptr_int
%dstStruct = OpFunctionParameter %gptr_struct
%entry = OpLabel
%pShort = OpVariable %pptr_short Function %ushort_42
%pInt = OpVariable %pptr_int Function %uint_4242
%pStruct = OpVariable %pptr_struct Function %struct_init
OpCopyMemory %dstShort %pShort
OpCopyMemory %dstInt %pInt
OpCopyMemory %dstStruct %pStruct
OpReturn
OpFunctionEnd

; CHECK-LABEL: define spir_kernel void @copymemory(ptr addrspace(1) %dstShort, ptr addrspace(1) %dstInt, ptr addrspace(1) %dstStruct)
; CHECK: call void @llvm.memcpy.p1.p0.i64(ptr addrspace(1) %dstShort, ptr @pShort, i64 2, i1 false)
; CHECK: call void @llvm.memcpy.p1.p0.i64(ptr addrspace(1) %dstInt, ptr @pInt, i64 4, i1 false)
; CHECK: call void @llvm.memcpy.p1.p0.i64(ptr addrspace(1) %dstStruct, ptr @pStruct, i64 12, i1 false)
15 changes: 12 additions & 3 deletions test/llvm-intrinsics/frexp.ll
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
; RUN: llvm-as %s -o %t.bc
; RUN: amd-llvm-spirv %t.bc -spirv-text
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
; RUN: FileCheck < %t.spt %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-TYPED-PTR
; RUN: amd-llvm-spirv %t.bc -o %t.spv
; RUN: amd-llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis %t.rev.bc
; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM

; RUN: amd-llvm-spirv %t.bc --spirv-ext=+SPV_KHR_untyped_pointers -spirv-text -o %t.spt
; RUN: FileCheck < %t.spt %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-UNTYPED-PTR
; RUN: amd-llvm-spirv %t.bc --spirv-ext=+SPV_KHR_untyped_pointers -o %t.spv
; RUN: amd-llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis %t.rev.bc
; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM

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

Expand All @@ -14,7 +21,8 @@ target triple = "spir64-unknown-unknown"
; CHECK-SPIRV: TypeInt [[#TypeInt:]] 32
; CHECK-SPIRV: TypeFloat [[#TypeFloat:]] 32
; CHECK-SPIRV: TypeStruct [[#TypeStrFloatInt:]] [[#TypeFloat]] [[#TypeInt]]
; CHECK-SPIRV: TypePointer [[#TypeIntPtr:]] 7 [[#TypeInt]]
; CHECK-SPIRV-TYPED-PTR: TypePointer [[#TypeIntPtr:]] 7 [[#TypeInt]]
; CHECK-SPIRV-UNTYPED-PTR: TypeUntypedPointerKHR [[#TypePtr:]] 7

; CHECK-SPIRV: TypeFloat [[#TypeDouble:]] 64
; CHECK-SPIRV: TypeStruct [[#TypeStrDoubleInt:]] [[#TypeDouble]] [[#TypeInt]]
Expand Down Expand Up @@ -49,7 +57,8 @@ declare { <4 x float>, <4 x i32> } @llvm.frexp.v4f32.v4i32(<4 x float>)
declare { <2 x double>, <2 x i32> } @llvm.frexp.v2f64.v2i32(<2 x double>)

; CHECK-SPIRV: Function [[#TypeStrFloatInt:]]
; CHECK-SPIRV: Variable [[#TypeIntPtr]] [[#IntVar:]] 7
; CHECK-SPIRV-TYPED-PTR: Variable [[#TypeIntPtr]] [[#IntVar:]] 7
; CHECK-SPIRV-UNTYPED-PTR: UntypedVariableKHR [[#TypePtr]] [[#IntVar:]] 7 [[#TypeInt]]
; CHECK-SPIRV: ExtInst [[#TypeFloat]] [[#FrexpId:]] [[#ExtInstSetId]] frexp [[#NegatedZeroConst]] [[#IntVar]]
; CHECK-SPIRV: Load [[#]] [[#LoadId:]] [[#]]
; CHECK-SPIRV: CompositeConstruct [[#TypeStrFloatInt]] [[#ComposConstr:]] [[#FrexpId]] [[#LoadId]]
Expand Down
9 changes: 9 additions & 0 deletions test/transcoding/get_image_num_mip_levels.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-SPV-IR
; RUN: amd-llvm-spirv -spirv-text %t.rev.bc -o - | FileCheck %s --check-prefix=CHECK-SPIRV

; RUN: amd-llvm-spirv --spirv-ext=+SPV_KHR_untyped_pointers -spirv-text %t.bc -o - | FileCheck %s --check-prefix=CHECK-SPIRV
; RUN: amd-llvm-spirv --spirv-ext=+SPV_KHR_untyped_pointers %t.bc -o %t.spv
; RUN: spirv-val %t.spv
; RUN: amd-llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM
; RUN: amd-llvm-spirv -r %t.spv -o %t.rev.bc --spirv-target-env=SPV-IR
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-SPV-IR
; RUN: amd-llvm-spirv -spirv-text %t.rev.bc -o - | FileCheck %s --check-prefix=CHECK-SPIRV

; Generated from the following OpenCL C code:
; #pragma OPENCL EXTENSION cl_khr_mipmap_image : enable
; void test(image1d_t img1,
Expand Down
6 changes: 6 additions & 0 deletions test/transcoding/image_with_access_qualifiers.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
; RUN: amd-llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM

; RUN: amd-llvm-spirv --spirv-ext=+SPV_KHR_untyped_pointers %t.bc -spirv-text -o %t.txt
; RUN: FileCheck < %t.txt %s --check-prefix=CHECK-SPIRV
; RUN: amd-llvm-spirv --spirv-ext=+SPV_KHR_untyped_pointers %t.bc -o %t.spv
; RUN: amd-llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM

; NOTE: access qualifier infomation is not preserved after round-trip conversion to LLVM
; CHECK-LLVM: call spir_func <4 x float> @_Z11read_imagef14ocl_image1d_rw11ocl_sampleri(ptr

Expand Down

0 comments on commit e229d7c

Please sign in to comment.