-
Notifications
You must be signed in to change notification settings - Fork 248
Align SPV_INTEL_joint_matrix implementation to the spec #3468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3691,27 +3691,6 @@ _SPIRV_OP(ConvertFToBF16INTEL) | |||||||||||||||||||||||||||||||
| _SPIRV_OP(ConvertBF16ToFINTEL) | ||||||||||||||||||||||||||||||||
| #undef _SPIRV_OP | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| class SPIRVJointMatrixINTELInstBase : public SPIRVInstTemplateBase { | ||||||||||||||||||||||||||||||||
| protected: | ||||||||||||||||||||||||||||||||
| std::optional<ExtensionID> getRequiredExtension() const override { | ||||||||||||||||||||||||||||||||
| return ExtensionID::SPV_INTEL_joint_matrix; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| class SPIRVJointMatrixINTELWorkItemInst : public SPIRVJointMatrixINTELInstBase { | ||||||||||||||||||||||||||||||||
| protected: | ||||||||||||||||||||||||||||||||
| SPIRVCapVec getRequiredCapability() const override { | ||||||||||||||||||||||||||||||||
| return getVec(internal::CapabilityJointMatrixWIInstructionsINTEL); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| #define _SPIRV_OP(x, ...) \ | ||||||||||||||||||||||||||||||||
| typedef SPIRVInstTemplate<SPIRVJointMatrixINTELWorkItemInst, \ | ||||||||||||||||||||||||||||||||
| internal::Op##x##INTEL, __VA_ARGS__> \ | ||||||||||||||||||||||||||||||||
| SPIRV##x##INTEL; | ||||||||||||||||||||||||||||||||
| _SPIRV_OP(JointMatrixGetElementCoord, true, 5) | ||||||||||||||||||||||||||||||||
| #undef _SPIRV_OP | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| class SPIRVCooperativeMatrixPrefetchINTELInstBase | ||||||||||||||||||||||||||||||||
| : public SPIRVInstTemplateBase { | ||||||||||||||||||||||||||||||||
| protected: | ||||||||||||||||||||||||||||||||
|
|
@@ -3737,8 +3716,28 @@ class SPIRVCooperativeMatrixCheckedInstructionsINTELInstBase | |||||||||||||||||||||||||||||||
| return ExtensionID::SPV_INTEL_joint_matrix; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| SPIRVCapVec getRequiredCapability() const override { | ||||||||||||||||||||||||||||||||
| return getVec( | ||||||||||||||||||||||||||||||||
| internal::CapabilityCooperativeMatrixCheckedInstructionsINTEL); | ||||||||||||||||||||||||||||||||
| auto CV = | ||||||||||||||||||||||||||||||||
| getVec(internal::CapabilityCooperativeMatrixCheckedInstructionsINTEL); | ||||||||||||||||||||||||||||||||
| if (SPIRVValue *LayoutVal = getMemoryLayout()) { | ||||||||||||||||||||||||||||||||
| if (isConstantOpCode(LayoutVal->getOpCode())) { | ||||||||||||||||||||||||||||||||
| uint64_t Layout = | ||||||||||||||||||||||||||||||||
| static_cast<SPIRVConstant *>(LayoutVal)->getZExtIntValue(); | ||||||||||||||||||||||||||||||||
| if (Layout == internal::CooperativeMatrixLayoutPackedINTEL) | ||||||||||||||||||||||||||||||||
| CV.push_back(internal::CapabilityPackedCooperativeMatrixINTEL); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return CV; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| SPIRVValue *getMemoryLayout() const { | ||||||||||||||||||||||||||||||||
| if (OpCode == internal::OpCooperativeMatrixLoadCheckedINTEL) | ||||||||||||||||||||||||||||||||
| return const_cast< | ||||||||||||||||||||||||||||||||
| SPIRVCooperativeMatrixCheckedInstructionsINTELInstBase *>(this) | ||||||||||||||||||||||||||||||||
| ->getOperand(3); | ||||||||||||||||||||||||||||||||
| if (OpCode == internal::OpCooperativeMatrixStoreCheckedINTEL) | ||||||||||||||||||||||||||||||||
| return const_cast< | ||||||||||||||||||||||||||||||||
| SPIRVCooperativeMatrixCheckedInstructionsINTELInstBase *>(this) | ||||||||||||||||||||||||||||||||
| ->getOperand(4); | ||||||||||||||||||||||||||||||||
| return nullptr; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -3790,6 +3789,7 @@ class SPIRVCooperativeMatrixInvocationInstructionsINTELInstBase | |||||||||||||||||||||||||||||||
| internal::Op##x##INTEL, __VA_ARGS__> \ | ||||||||||||||||||||||||||||||||
| SPIRV##x##INTEL; | ||||||||||||||||||||||||||||||||
| _SPIRV_OP(CooperativeMatrixApplyFunction, true, 5) | ||||||||||||||||||||||||||||||||
| _SPIRV_OP(CooperativeMatrixGetElementCoord, true, 5) | ||||||||||||||||||||||||||||||||
| #undef _SPIRV_OP | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| class SPIRVCooperativeMatrixKHRInstBase : public SPIRVInstTemplateBase { | ||||||||||||||||||||||||||||||||
|
|
@@ -3798,7 +3798,51 @@ class SPIRVCooperativeMatrixKHRInstBase : public SPIRVInstTemplateBase { | |||||||||||||||||||||||||||||||
| return ExtensionID::SPV_KHR_cooperative_matrix; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| SPIRVCapVec getRequiredCapability() const override { | ||||||||||||||||||||||||||||||||
| return getVec(CapabilityCooperativeMatrixKHR); | ||||||||||||||||||||||||||||||||
| auto CV = getVec(CapabilityCooperativeMatrixKHR); | ||||||||||||||||||||||||||||||||
| if (SPIRVValue *LayoutVal = getMemoryLayout()) { | ||||||||||||||||||||||||||||||||
| if (isConstantOpCode(LayoutVal->getOpCode())) { | ||||||||||||||||||||||||||||||||
| uint64_t Layout = | ||||||||||||||||||||||||||||||||
| static_cast<SPIRVConstant *>(LayoutVal)->getZExtIntValue(); | ||||||||||||||||||||||||||||||||
| if (Layout == internal::CooperativeMatrixLayoutPackedINTEL) | ||||||||||||||||||||||||||||||||
| CV.push_back(internal::CapabilityPackedCooperativeMatrixINTEL); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| if (OpCode == OpCooperativeMatrixMulAddKHR && Ops.size() == 4) { | ||||||||||||||||||||||||||||||||
| // If Cooperative Matrix Operand literal is present, check for the | ||||||||||||||||||||||||||||||||
| // additional capabilities it may require. | ||||||||||||||||||||||||||||||||
| uint64_t CoopOperands = Ops[3]; | ||||||||||||||||||||||||||||||||
| if (CoopOperands & | ||||||||||||||||||||||||||||||||
| internal:: | ||||||||||||||||||||||||||||||||
| CooperativeMatrixOperandsMatrixAAndBTF32ComponentsINTELMask) { | ||||||||||||||||||||||||||||||||
| CV.push_back( | ||||||||||||||||||||||||||||||||
| internal::CapabilityCooperativeMatrixTF32ComponentTypeINTEL); | ||||||||||||||||||||||||||||||||
| Module->addExtension(ExtensionID::SPV_INTEL_joint_matrix); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| if (CoopOperands & | ||||||||||||||||||||||||||||||||
| internal:: | ||||||||||||||||||||||||||||||||
| CooperativeMatrixOperandsMatrixAAndBBFloat16ComponentsINTELMask || | ||||||||||||||||||||||||||||||||
| CoopOperands & | ||||||||||||||||||||||||||||||||
| internal:: | ||||||||||||||||||||||||||||||||
| CooperativeMatrixOperandsMatrixCBFloat16ComponentsINTELMask || | ||||||||||||||||||||||||||||||||
| CoopOperands & | ||||||||||||||||||||||||||||||||
| internal:: | ||||||||||||||||||||||||||||||||
| CooperativeMatrixOperandsMatrixResultBFloat16ComponentsINTELMask) { | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| if (CoopOperands & | |
| internal:: | |
| CooperativeMatrixOperandsMatrixAAndBBFloat16ComponentsINTELMask || | |
| CoopOperands & | |
| internal:: | |
| CooperativeMatrixOperandsMatrixCBFloat16ComponentsINTELMask || | |
| CoopOperands & | |
| internal:: | |
| CooperativeMatrixOperandsMatrixResultBFloat16ComponentsINTELMask) { | |
| constexpr auto BF16Masks = | |
| internal::CooperativeMatrixOperandsMatrixAAndBBFloat16ComponentsINTELMask | | |
| internal::CooperativeMatrixOperandsMatrixCBFloat16ComponentsINTELMask | | |
| internal::CooperativeMatrixOperandsMatrixResultBFloat16ComponentsINTELMask; | |
| if (CoopOperands & BF16Masks) { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| ; RUN: llvm-as < %s -o %t.bc | ||
| ; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_KHR_cooperative_matrix,+SPV_INTEL_joint_matrix -o %t.spv | ||
| ; RUN: llvm-spirv %t.spv -to-text -o %t.spt | ||
| ; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have a spirv-val check? Or is not prepared yet? |
||
|
|
||
| ; RUN: llvm-spirv -r %t.spv -o %t.rev.bc | ||
| ; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM | ||
|
|
||
| ; RUN: not llvm-spirv %t.bc --spirv-ext=+SPV_KHR_cooperative_matrix 2>&1 \ | ||
| ; RUN: | FileCheck %s --check-prefix=CHECK-ERROR | ||
|
|
||
| ; CHECK-ERROR: RequiresExtension: Feature requires the following SPIR-V extension | ||
| ; CHECK-ERROR-NEXT: SPV_INTEL_joint_matrix | ||
|
|
||
| ; CHECK-SPIRV-DAG: Capability CooperativeMatrixKHR | ||
| ; CHECK-SPIRV-DAG: Extension "SPV_KHR_cooperative_matrix" | ||
| ; CHECK-SPIRV-DAG: Extension "SPV_INTEL_joint_matrix" | ||
| ; CHECK-SPIRV-DAG: Capability CooperativeMatrixBFloat16ComponentTypeINTEL | ||
| ; 64 stays for MatrixAAndBBFloat16ComponentsINTEL (0x40) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also test 0x80 and 0x100? I don't see them tested now. |
||
| ; CHECK-SPIRV: CooperativeMatrixMulAddKHR [[#]] [[#]] [[#]] [[#]] [[#]] 64 | ||
|
|
||
| ; CHECK-LLVM: call spir_func target("spirv.CooperativeMatrixKHR", float, 3, 12, 12, 2) @_Z34__spirv_CooperativeMatrixMulAddKHRPU3AS145__spirv_CooperativeMatrixKHR__short_3_12_48_0PU3AS145__spirv_CooperativeMatrixKHR__short_2_48_12_1PU3AS145__spirv_CooperativeMatrixKHR__float_3_12_12_2i({{.*}}, i32 64) | ||
|
|
||
| target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" | ||
| target triple = "spir64-unknown-unknown" | ||
|
|
||
| define spir_kernel void @matrix_multiply(ptr addrspace(1) noundef align 1 %_arg_accA, ptr addrspace(1) noundef align 1 %_arg_accB, ptr addrspace(1) noundef align 1 %_arg_accC, i64 noundef %_arg_N, i64 noundef %_arg_K, i32 noundef %_arg_Initvalue) { | ||
| entry: | ||
| %matrixC = tail call spir_func target("spirv.CooperativeMatrixKHR", float, 3, 12, 12, 2) @_Z26__spirv_CompositeConstruct(float 0.0) | ||
| %matrixA = tail call spir_func noundef target("spirv.CooperativeMatrixKHR", i16, 3, 12, 48, 0) @_Z32__spirv_CooperativeMatrixLoadKHR_1(ptr addrspace(1) noundef %_arg_accA, i32 noundef 0, i64 noundef %_arg_K, i32 noundef 1) | ||
| %matrixB = tail call spir_func noundef target("spirv.CooperativeMatrixKHR", i16, 2, 48, 12, 1) @_Z32__spirv_CooperativeMatrixLoadKHR_2(ptr addrspace(1) noundef %_arg_accB, i32 noundef 1, i64 noundef %_arg_K) | ||
| %res = tail call spir_func noundef target("spirv.CooperativeMatrixKHR", float, 3, 12, 12, 2) @_Z34__spirv_CooperativeMatrixMulAddKHR(target("spirv.CooperativeMatrixKHR", i16, 3, 12, 48, 0) noundef %matrixA, target("spirv.CooperativeMatrixKHR", i16, 2, 48, 12, 1) noundef %matrixB, target("spirv.CooperativeMatrixKHR", float, 3, 12, 12, 2) noundef %matrixC, i32 noundef 64) | ||
| tail call spir_func void @_Z33__spirv_CooperativeMatrixStoreKHR(ptr addrspace(1) noundef %_arg_accC, target("spirv.CooperativeMatrixKHR", float, 3, 12, 12, 2) noundef %res, i32 noundef 0, i64 noundef %_arg_N, i32 noundef 1) | ||
| ret void | ||
| } | ||
|
|
||
| declare dso_local spir_func noundef target("spirv.CooperativeMatrixKHR", float, 3, 12, 12, 2) @_Z26__spirv_CompositeConstruct(float noundef) | ||
|
|
||
| declare dso_local spir_func noundef target("spirv.CooperativeMatrixKHR", float, 3, 12, 12, 2) @_Z34__spirv_CooperativeMatrixMulAddKHR(target("spirv.CooperativeMatrixKHR", i16, 3, 12, 48, 0) noundef, target("spirv.CooperativeMatrixKHR", i16, 2, 48, 12, 1) noundef, target("spirv.CooperativeMatrixKHR", float, 3, 12, 12, 2) noundef, i32 noundef) | ||
|
|
||
| declare dso_local spir_func target("spirv.CooperativeMatrixKHR", i16, 3, 12, 48, 0) @_Z32__spirv_CooperativeMatrixLoadKHR_1(ptr addrspace(1), i32, i64, i32) | ||
|
|
||
| declare dso_local spir_func target("spirv.CooperativeMatrixKHR", i16, 2, 48, 12, 1) @_Z32__spirv_CooperativeMatrixLoadKHR_2(ptr addrspace(1), i32, i64) | ||
|
|
||
| declare dso_local spir_func void @_Z33__spirv_CooperativeMatrixStoreKHR(ptr addrspace(1), target("spirv.CooperativeMatrixKHR", float, 3, 12, 12, 2), i32, i64, i32) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To double check, what instruction is used in SYCL headers right now?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oopsie: https://github.com/intel/llvm/blob/bece203068c9e6469d22da410df88b9f8e56e727/sycl/include/sycl/__spirv/spirv_ops.hpp#L110-L116
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, then I'd suggest to first replace the joint matrix version to cooperative matrix version in the headers and see if anything fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created a draft PR: intel/llvm#20855