From c8be3c44526ec7df5a08d82710396cb12023c95f Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Mon, 11 Nov 2024 12:51:28 +0100 Subject: [PATCH] [Backport to 15] SPIRVReader: Support OpConstantComposite for cooperative matrix (#2826) (#2851) 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 | 31 +++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 test/extensions/KHR/SPV_KHR_cooperative_matrix/constant_composite.spvasm diff --git a/lib/SPIRV/SPIRVReader.cpp b/lib/SPIRV/SPIRVReader.cpp index 9e0c2a2437..1823f35334 100644 --- a/lib/SPIRV/SPIRVReader.cpp +++ b/lib/SPIRV/SPIRVReader.cpp @@ -1611,6 +1611,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 be5e7db44e..45dcb7c8ca 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/extensions/KHR/SPV_KHR_cooperative_matrix/constant_composite.spvasm b/test/extensions/KHR/SPV_KHR_cooperative_matrix/constant_composite.spvasm new file mode 100644 index 0000000000..93754d4400 --- /dev/null +++ b/test/extensions/KHR/SPV_KHR_cooperative_matrix/constant_composite.spvasm @@ -0,0 +1,31 @@ +; 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 -emit-opaque-pointers -r -o - %t.spv | llvm-dis -opaque-pointers | 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-LABEL: @testCoopMat +; CHECK: call spir_func ptr addrspace(1) @__spirv_ConstantComposite_RPU3AS1c(i32 42)