Skip to content

Commit dcfd949

Browse files
authored
Support casting of OpConstantNull to SPIRVConstant
Current design does not imply inheritance between SPIRVConstantNull and SPIRVConstant classes, however it is possible that we do the static_cast of SPIRVValue to SPIRVConstant for null constant. Return zero-value in that case.
1 parent 37a91c7 commit dcfd949

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/SPIRV/libSPIRV/SPIRVValue.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ template <spv::Op OC> class SPIRVConstantBase : public SPIRVValue {
180180

181181
// Common method for getting values of size less or equal to 64 bits.
182182
template <typename T> T getValue() const {
183+
if (OpCode == OpConstantNull) {
184+
assert(std::is_arithmetic_v<T> &&
185+
"OpConstantNull has no defined value for this type");
186+
return 0;
187+
}
183188
constexpr auto ValueSize = static_cast<unsigned>(sizeof(T));
184189
assert((ValueSize <= 8) && "Incorrect result type of requested value");
185190
T TheValue{};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; REQUIRES: spirv-as
2+
; RUN: spirv-as --target-env spv1.0 -o %t.spv %s
3+
; RUN: spirv-val %t.spv
4+
; RUN: llvm-spirv -r -o - %t.spv | llvm-dis | FileCheck %s
5+
6+
OpCapability Addresses
7+
OpCapability Kernel
8+
OpCapability CooperativeMatrixKHR
9+
OpExtension "SPV_KHR_cooperative_matrix"
10+
OpMemoryModel Physical64 OpenCL
11+
OpEntryPoint Kernel %1 "test"
12+
%uint = OpTypeInt 32 0
13+
%uint_0 = OpConstantNull %uint
14+
%uint_12 = OpConstant %uint 12
15+
%uint_2 = OpConstant %uint 2
16+
%void = OpTypeVoid
17+
%fnTy = OpTypeFunction %void
18+
%matTy = OpTypeCooperativeMatrixKHR %uint %uint_0 %uint_12 %uint_12 %uint_0
19+
%1 = OpFunction %void None %fnTy
20+
%2 = OpLabel
21+
%3 = OpCompositeConstruct %matTy %uint_0
22+
OpReturn
23+
OpFunctionEnd
24+
25+
; CHECK: call spir_func target("spirv.CooperativeMatrixKHR", i32, 0, 12, 12, 0) @_Z26__spirv_CompositeConstructi(i32 0)

0 commit comments

Comments
 (0)