From e037e10488405ec26f52bcfab668e13383585a62 Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Mon, 11 Nov 2024 10:11:19 +0100 Subject: [PATCH] [Backport to 14] SPIRVReader: Support OpConstantComposite for cooperative matrix (#2826) (#2850) This was hitting a "not implemented UNREACHABLE". Like the other cooperative matrix operations, map this construct to a SPIR-V friendly IR function call. Let `transDbgInfo` skip over `OpConstantComposite` because we're mapping `OpConstantComposite` to an LLVM `Instruction` without having a corresponding `SPIRVInstruction`. (cherry picked from commit 04b546550077c4fb51535022af6bf76dcb59362d) --- lib/SPIRV/SPIRVReader.cpp | 15 ++++++++++ lib/SPIRV/SPIRVToLLVMDbgTran.cpp | 6 ++-- .../constant_composite.spvasm | 30 +++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 test/transcoding/SPV_KHR_cooperative_matrix/constant_composite.spvasm diff --git a/lib/SPIRV/SPIRVReader.cpp b/lib/SPIRV/SPIRVReader.cpp index 12dd6bad1b..ecb8557071 100644 --- a/lib/SPIRV/SPIRVReader.cpp +++ b/lib/SPIRV/SPIRVReader.cpp @@ -1510,6 +1510,21 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F, ConstantStruct::get( dyn_cast(transType(BCC->getType())), CV)); } + case OpTypeCooperativeMatrixKHR: { + assert(CV.size() == 1 && + "expecting exactly one operand for cooperative matrix types"); + llvm::Type *RetTy = transType(BCC->getType()); + llvm::Type *EltTy = transType( + static_cast(BV->getType()) + ->getCompType()); + auto *FTy = FunctionType::get(RetTy, {EltTy}, false); + FunctionCallee Func = + M->getOrInsertFunction(getSPIRVFuncName(OC, RetTy), FTy); + IRBuilder<> Builder(BB); + CallInst *Call = Builder.CreateCall(Func, CV.front()); + Call->setCallingConv(CallingConv::SPIR_FUNC); + return Call; + } default: llvm_unreachable("not implemented"); return nullptr; diff --git a/lib/SPIRV/SPIRVToLLVMDbgTran.cpp b/lib/SPIRV/SPIRVToLLVMDbgTran.cpp index 033ecc7a5b..7cf4721420 100644 --- a/lib/SPIRV/SPIRVToLLVMDbgTran.cpp +++ b/lib/SPIRV/SPIRVToLLVMDbgTran.cpp @@ -158,8 +158,10 @@ SPIRVToLLVMDbgTran::getStringSourceContinued(const SPIRVId Id, } void SPIRVToLLVMDbgTran::transDbgInfo(const SPIRVValue *SV, Value *V) { - // A constant sampler does not have a corresponding SPIRVInstruction. - if (SV->getOpCode() == OpConstantSampler) + // Constant samplers and composites do not have a corresponding + // SPIRVInstruction, but they may be mapped to an LLVM Instruction. + if (SV->getOpCode() == OpConstantSampler || + SV->getOpCode() == OpConstantComposite) return; if (Instruction *I = dyn_cast(V)) { diff --git a/test/transcoding/SPV_KHR_cooperative_matrix/constant_composite.spvasm b/test/transcoding/SPV_KHR_cooperative_matrix/constant_composite.spvasm new file mode 100644 index 0000000000..a05c2f7da3 --- /dev/null +++ b/test/transcoding/SPV_KHR_cooperative_matrix/constant_composite.spvasm @@ -0,0 +1,30 @@ +; REQUIRES: spirv-as +; RUN: spirv-as --target-env spv1.0 -o %t.spv %s +; TODO: re-enable spirv-val +; R/U/N: spirv-val %t.spv +; RUN: llvm-spirv -r -o - %t.spv | llvm-dis | FileCheck %s + +OpCapability Addresses +OpCapability Kernel +OpCapability CooperativeMatrixKHR +OpExtension "SPV_KHR_cooperative_matrix" +OpMemoryModel Physical64 OpenCL +OpEntryPoint Kernel %1 "testCoopMat" +%void = OpTypeVoid +%uint = OpTypeInt 32 0 +%_ptr_CrossWorkgroup_uint = OpTypePointer CrossWorkgroup %uint +%fnTy = OpTypeFunction %void %_ptr_CrossWorkgroup_uint +%uint_0 = OpConstant %uint 0 +%uint_3 = OpConstant %uint 3 +%uint_8 = OpConstant %uint 8 +%uint_42 = OpConstant %uint 42 +%matTy = OpTypeCooperativeMatrixKHR %uint %uint_3 %uint_8 %uint_8 %uint_0 +%matConst = OpConstantComposite %matTy %uint_42 +%1 = OpFunction %void None %fnTy +%outPtr = OpFunctionParameter %_ptr_CrossWorkgroup_uint +%2 = OpLabel +OpCooperativeMatrixStoreKHR %outPtr %matConst %uint_0 %uint_8 +OpReturn +OpFunctionEnd + +; CHECK: call spir_func %spirv.CooperativeMatrixKHR._int_3_8_8_0 addrspace(1)* @__spirv_ConstantComposite_RPU3AS141__spirv_CooperativeMatrixKHR__int_3_8_8_0(i32 42)