Skip to content

Commit

Permalink
[Backport to 17] [SPIR-V 1.1] SPIRVReader: Add OpSizeOf support (#2853)…
Browse files Browse the repository at this point in the history
… (#2863)

The `OpSizeOf` instruction was added in SPIR-V 1.1, but not supported
yet.

(cherry picked from commit 9aeb7eb)
  • Loading branch information
svenvh authored Nov 19, 2024
1 parent 728261f commit 76e2161
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,15 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
case OpUndef:
return mapValue(BV, UndefValue::get(transType(BV->getType())));

case OpSizeOf: {
Type *ResTy = transType(BV->getType());
auto *BI = static_cast<SPIRVSizeOf *>(BV);
SPIRVType *TypeArg = reinterpret_cast<SPIRVType *>(BI->getOpValue(0));
Type *EltTy = transType(TypeArg->getPointerElementType());
uint64_t Size = M->getDataLayout().getTypeStoreSize(EltTy).getFixedValue();
return mapValue(BV, ConstantInt::get(ResTy, Size));
}

case OpVariable: {
auto BVar = static_cast<SPIRVVariable *>(BV);
auto *PreTransTy = BVar->getType()->getPointerElementType();
Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/libSPIRV/SPIRVEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,6 @@ _SPIRV_OP(NamedBarrierInitialize)
_SPIRV_OP(MemoryNamedBarrier)
_SPIRV_OP(GetKernelMaxNumSubgroups)
_SPIRV_OP(GetKernelLocalSizeForSubgroupCount)
_SPIRV_OP(SizeOf)
#undef _SPIRV_OP

} // namespace SPIRV
Expand Down
10 changes: 10 additions & 0 deletions lib/SPIRV/libSPIRV/SPIRVInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,16 @@ class SPIRVTranspose : public SPIRVInstruction {
SPIRVId Matrix;
};

class SPIRVSizeOfInstBase : public SPIRVInstTemplateBase {
protected:
SPIRVWord getRequiredSPIRVVersion() const override {
return static_cast<SPIRVWord>(VersionNumber::SPIRV_1_1);
}
};

typedef SPIRVInstTemplate<SPIRVSizeOfInstBase, OpSizeOf, true, 4, false>
SPIRVSizeOf;

class SPIRVUnary : public SPIRVInstTemplateBase {
protected:
void validate() const override {
Expand Down
1 change: 1 addition & 0 deletions lib/SPIRV/libSPIRV/SPIRVOpCodeEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ _SPIRV_OP(ImageSparseTexelsResident, 316)
_SPIRV_OP(NoLine, 317)
_SPIRV_OP(AtomicFlagTestAndSet, 318)
_SPIRV_OP(AtomicFlagClear, 319)
_SPIRV_OP(SizeOf, 321)
_SPIRV_OP(TypePipeStorage, 322)
_SPIRV_OP(ConstantPipeStorage, 323)
_SPIRV_OP(CreatePipeFromPipeStorage, 324)
Expand Down
33 changes: 33 additions & 0 deletions test/OpSizeOf.spvasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
; REQUIRES: spirv-as
; RUN: spirv-as --target-env spv1.1 -o %t.spv %s
; TODO: reenable spirv-val when OpSizeOf is supported.
; R/U/N: spirv-val %t.spv
; RUN: llvm-spirv -r -o - %t.spv | llvm-dis | FileCheck %s
OpCapability Addresses
OpCapability Kernel
OpMemoryModel Physical64 OpenCL
OpEntryPoint Kernel %f "testSizeOf"
OpName %p "p"
%void = OpTypeVoid
%bool = OpTypeBool
%i32 = OpTypeInt 32 0
%struct = OpTypeStruct %i32 %i32
%_ptr_CrossWorkgroup_bool = OpTypePointer CrossWorkgroup %bool
%_ptr_CrossWorkgroup_i32 = OpTypePointer CrossWorkgroup %i32
%_ptr_CrossWorkgroup_struct = OpTypePointer CrossWorkgroup %struct
%fnTy = OpTypeFunction %void %_ptr_CrossWorkgroup_i32

%f = OpFunction %void None %fnTy
%p = OpFunctionParameter %_ptr_CrossWorkgroup_i32
%10 = OpLabel

; CHECK: store i32 1, ptr addrspace(1) %p
%11 = OpSizeOf %i32 %_ptr_CrossWorkgroup_bool
OpStore %p %11

; CHECK: store i32 8, ptr addrspace(1) %p
%12 = OpSizeOf %i32 %_ptr_CrossWorkgroup_struct
OpStore %p %12

OpReturn
OpFunctionEnd

0 comments on commit 76e2161

Please sign in to comment.