[llvm] c9e84a7 - [SPIR-V] Lower 1xN/Nx1 matrix transpose to a copy (#201332)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 8 02:13:47 PDT 2026


Author: Arseniy Obolenskiy
Date: 2026-06-08T11:13:43+02:00
New Revision: c9e84a7284b601df7fc3c5b90e5b02007c491b00

URL: https://github.com/llvm/llvm-project/commit/c9e84a7284b601df7fc3c5b90e5b02007c491b00
DIFF: https://github.com/llvm/llvm-project/commit/c9e84a7284b601df7fc3c5b90e5b02007c491b00.diff

LOG: [SPIR-V] Lower 1xN/Nx1 matrix transpose to a copy (#201332)

Remove extra `OpVectorShuffle` while lowering trivial matrix transpose that is in fact basically a reshape

Added: 
    

Modified: 
    llvm/lib/Target/SPIRV/SPIRVCombinerHelper.cpp
    llvm/test/CodeGen/SPIRV/llvm-intrinsics/matrix-transpose.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/SPIRV/SPIRVCombinerHelper.cpp b/llvm/lib/Target/SPIRV/SPIRVCombinerHelper.cpp
index 279c9e438f6a8..c774b873b95a5 100644
--- a/llvm/lib/Target/SPIRV/SPIRVCombinerHelper.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVCombinerHelper.cpp
@@ -227,7 +227,8 @@ void SPIRVCombinerHelper::applyMatrixTranspose(MachineInstr &MI) const {
 
   Builder.setInstrAndDebugLoc(MI);
 
-  if (Rows == 1 && Cols == 1) {
+  // A 1xN or Nx1 transpose is a pure reshape.
+  if (Rows == 1 || Cols == 1) {
     Builder.buildCopy(ResReg, InReg);
     MI.eraseFromParent();
     return;

diff  --git a/llvm/test/CodeGen/SPIRV/llvm-intrinsics/matrix-transpose.ll b/llvm/test/CodeGen/SPIRV/llvm-intrinsics/matrix-transpose.ll
index 3b746c68b97ff..23479a7f4f9c4 100644
--- a/llvm/test/CodeGen/SPIRV/llvm-intrinsics/matrix-transpose.ll
+++ b/llvm/test/CodeGen/SPIRV/llvm-intrinsics/matrix-transpose.ll
@@ -74,7 +74,9 @@ define internal void @test_transpose_f32_2x3() {
 
 ; Test Transpose 1x4 float (Result is 4x1 float), should be a copy (vector of 4 floats)
 ; CHECK-LABEL: ; -- Begin function test_transpose_f32_1x4_to_4x1
-; CHECK: %[[Shuffle:[0-9]+]] = OpVectorShuffle %[[V4F32_ID]] {{.*}} 0 1 2 3
+; CHECK-COUNT-4: OpCompositeInsert %[[V4F32_ID]]
+; CHECK-NOT: OpVectorShuffle
+; CHECK-COUNT-4: OpCompositeExtract %[[Float_ID]]
 define internal void @test_transpose_f32_1x4_to_4x1() {
  %1 = load <4 x float>, ptr addrspace(10) @private_v4f32
  %2 = call <4 x float> @llvm.matrix.transpose.v4f32.i32(<4 x float> %1, i32 1, i32 4)
@@ -84,7 +86,9 @@ define internal void @test_transpose_f32_1x4_to_4x1() {
 
 ; Test Transpose 4x1 float (Result is 1x4 float), should be a copy (vector of 4 floats)
 ; CHECK-LABEL: ; -- Begin function test_transpose_f32_4x1_to_1x4
-; CHECK: %[[Shuffle:[0-9]+]] = OpVectorShuffle %[[V4F32_ID]] {{.*}} 0 1 2 3
+; CHECK-COUNT-4: OpCompositeInsert %[[V4F32_ID]]
+; CHECK-NOT: OpVectorShuffle
+; CHECK-COUNT-4: OpCompositeExtract %[[Float_ID]]
 define internal void @test_transpose_f32_4x1_to_1x4() {
  %1 = load <4 x float>, ptr addrspace(10) @private_v4f32
  %2 = call <4 x float> @llvm.matrix.transpose.v4f32.i32(<4 x float> %1, i32 4, i32 1)


        


More information about the llvm-commits mailing list