Skip to content

Commit 696becb

Browse files
SaurabhJhafhahn
authored andcommitted
[Matrix] Remove bitcast when casting between matrices of the same size
In matrix type casts, we were doing bitcast when the matrices had the same size. This was incorrect and this patch fixes that. Also added some new CodeGen tests for signed <-> usigned conversions Reviewed By: fhahn Differential Revision: https://reviews.llvm.org/D101754
1 parent 2e4cc9a commit 696becb

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,10 +1205,6 @@ Value *ScalarExprEmitter::EmitScalarCast(Value *Src, QualType SrcType,
12051205
QualType SrcElementType;
12061206
QualType DstElementType;
12071207
if (SrcType->isMatrixType() && DstType->isMatrixType()) {
1208-
// Allow bitcast between matrixes of the same size.
1209-
if (SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits())
1210-
return Builder.CreateBitCast(Src, DstTy, "conv");
1211-
12121208
SrcElementTy = cast<llvm::VectorType>(SrcTy)->getElementType();
12131209
DstElementTy = cast<llvm::VectorType>(DstTy)->getElementType();
12141210
SrcElementType = SrcType->castAs<MatrixType>()->getElementType();

clang/test/CodeGen/matrix-cast.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,23 @@ void cast_int_matrix_to_short(ix5x5 i, sx5x5 s) {
5252
void cast_int_matrix_to_float(ix5x5 i, fx5x5 f) {
5353
// CHECK-LABEL: define{{.*}} void @cast_int_matrix_to_float(<25 x i32> %i, <25 x float> %f)
5454
// CHECK: [[I:%.*]] = load <25 x i32>, <25 x i32>* {{.*}}, align 4
55-
// CHECK-NEXT: [[CONV:%.*]] = bitcast <25 x i32> [[I]] to <25 x float>
55+
// CHECK-NEXT: [[CONV:%.*]] = sitofp <25 x i32> [[I]] to <25 x float>
5656
// CHECK-NEXT: store <25 x float> [[CONV]], <25 x float>* {{.*}}, align 4
5757
// CHECK-NEXT: ret void
5858

5959
f = (fx5x5)i;
6060
}
6161

62+
void cast_unsigned_int_matrix_to_float(unsigned_short_int_5x5 u, fx5x5 f) {
63+
// CHECK-LABEL: define{{.*}} void @cast_unsigned_int_matrix_to_float(<25 x i16> %u, <25 x float> %f)
64+
// CHECK: [[U:%.*]] = load <25 x i16>, <25 x i16>* {{.*}}, align 2
65+
// CHECK-NEXT: [[CONV:%.*]] = uitofp <25 x i16> [[U]] to <25 x float>
66+
// CHECK-NEXT: store <25 x float> [[CONV]], <25 x float>* {{.*}}, align 4
67+
// CHECK-NEXT: ret void
68+
69+
f = (fx5x5)u;
70+
}
71+
6272
void cast_double_matrix_to_int(dx5x5 d, ix5x5 i) {
6373
// CHECK-LABEL: define{{.*}} void @cast_double_matrix_to_int(<25 x double> %d, <25 x i32> %i)
6474
// CHECK: [[D:%.*]] = load <25 x double>, <25 x double>* {{.*}}, align 8
@@ -108,3 +118,23 @@ void cast_unsigned_long_int_to_unsigned_short_int(unsigned_long_int_5x5 l, unsig
108118

109119
s = (unsigned_short_int_5x5)l;
110120
}
121+
122+
void cast_unsigned_short_int_to_int(unsigned_short_int_5x5 u, ix5x5 i) {
123+
// CHECK-LABEL: define{{.*}} void @cast_unsigned_short_int_to_int(<25 x i16> %u, <25 x i32> %i)
124+
// CHECK: [[U:%.*]] = load <25 x i16>, <25 x i16>* %0, align 2
125+
// CHECK-NEXT: [[CONV:%.*]] = zext <25 x i16> [[U]] to <25 x i32>
126+
// CHECK-NEXT: store <25 x i32> [[CONV]], <25 x i32>* {{.*}}, align 4
127+
// CHECK-NEXT: ret void
128+
129+
i = (ix5x5)u;
130+
}
131+
132+
void cast_int_to_unsigned_long_int(ix5x5 i, unsigned_long_int_5x5 u) {
133+
// CHECK-LABEL: define{{.*}} void @cast_int_to_unsigned_long_int(<25 x i32> %i, <25 x i64> %u)
134+
// CHECK: [[I:%.*]] = load <25 x i32>, <25 x i32>* %0, align 4
135+
// CHECK-NEXT: [[CONV:%.*]] = sext <25 x i32> [[I]] to <25 x i64>
136+
// CHECK-NEXT: store <25 x i64> [[CONV]], <25 x i64>* {{.*}}, align 8
137+
// CHECK-NEXT: ret void
138+
139+
u = (unsigned_long_int_5x5)i;
140+
}

0 commit comments

Comments
 (0)