Skip to content

Commit e838b8b

Browse files
authored
[Matrix] Fix a miscompile due to an incorrect double-transpose fold (llvm#135397)
Transposes are only inverses of each other when they have matching shapes. rdar://145592582
1 parent d39d24c commit e838b8b

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,9 +804,10 @@ class LowerMatrixIntrinsics {
804804
m_Value(TA), m_ConstantInt(R), m_ConstantInt(C))))
805805
return nullptr;
806806

807-
// Transpose of a transpose is a nop
807+
// Transpose of a transpose is a nop when the shapes match.
808808
Value *TATA;
809-
if (match(TA, m_Intrinsic<Intrinsic::matrix_transpose>(m_Value(TATA)))) {
809+
if (match(TA, m_Intrinsic<Intrinsic::matrix_transpose>(
810+
m_Value(TATA), m_Specific(C), m_Specific(R)))) {
810811
updateShapeAndReplaceAllUsesWith(I, TATA);
811812
eraseFromParentAndMove(&I, II, BB);
812813
eraseFromParentAndMove(TA, II, BB);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -passes='lower-matrix-intrinsics' -S < %s | FileCheck %s
3+
4+
; We can only fold matching transposes.
5+
6+
define void @reshape(ptr %in, ptr %out) {
7+
; CHECK-LABEL: define void @reshape(
8+
; CHECK-SAME: ptr [[IN:%.*]], ptr [[OUT:%.*]]) {
9+
; CHECK-NEXT: [[ENTRY:.*:]]
10+
; CHECK-NEXT: [[COL_LOAD:%.*]] = load <4 x double>, ptr [[IN]], align 8
11+
; CHECK-NEXT: [[SPLIT:%.*]] = shufflevector <4 x double> [[COL_LOAD]], <4 x double> poison, <2 x i32> <i32 0, i32 1>
12+
; CHECK-NEXT: [[SPLIT1:%.*]] = shufflevector <4 x double> [[COL_LOAD]], <4 x double> poison, <2 x i32> <i32 2, i32 3>
13+
; CHECK-NEXT: [[TMP0:%.*]] = extractelement <2 x double> [[SPLIT]], i64 0
14+
; CHECK-NEXT: [[TMP1:%.*]] = insertelement <2 x double> poison, double [[TMP0]], i64 0
15+
; CHECK-NEXT: [[TMP2:%.*]] = extractelement <2 x double> [[SPLIT1]], i64 0
16+
; CHECK-NEXT: [[TMP3:%.*]] = insertelement <2 x double> [[TMP1]], double [[TMP2]], i64 1
17+
; CHECK-NEXT: [[TMP4:%.*]] = extractelement <2 x double> [[SPLIT]], i64 1
18+
; CHECK-NEXT: [[TMP5:%.*]] = insertelement <2 x double> poison, double [[TMP4]], i64 0
19+
; CHECK-NEXT: [[TMP6:%.*]] = extractelement <2 x double> [[SPLIT1]], i64 1
20+
; CHECK-NEXT: [[TMP7:%.*]] = insertelement <2 x double> [[TMP5]], double [[TMP6]], i64 1
21+
; CHECK-NEXT: store <2 x double> [[TMP3]], ptr [[OUT]], align 8
22+
; CHECK-NEXT: [[VEC_GEP:%.*]] = getelementptr double, ptr [[OUT]], i64 2
23+
; CHECK-NEXT: store <2 x double> [[TMP7]], ptr [[VEC_GEP]], align 8
24+
; CHECK-NEXT: ret void
25+
;
26+
entry:
27+
%0 = load <4 x double>, ptr %in, align 8
28+
%1 = tail call <4 x double> @llvm.matrix.transpose.v4f64(<4 x double> %0, i32 4, i32 1)
29+
%2 = tail call <4 x double> @llvm.matrix.transpose.v4f64(<4 x double> %1, i32 1, i32 4)
30+
%3 = tail call <4 x double> @llvm.matrix.transpose.v4f64(<4 x double> %2, i32 2, i32 2)
31+
%4 = tail call <4 x double> @llvm.matrix.transpose.v4f64(<4 x double> %3, i32 2, i32 2)
32+
%5 = tail call <4 x double> @llvm.matrix.transpose.v4f64(<4 x double> %4, i32 2, i32 2)
33+
store <4 x double> %5, ptr %out, align 8
34+
ret void
35+
}

0 commit comments

Comments
 (0)