SaurabhJha created this revision.
Herald added a subscriber: tschuett.
SaurabhJha requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101754

Files:
  clang/lib/CodeGen/CGExprScalar.cpp
  clang/test/CodeGen/matrix-cast.c


Index: clang/test/CodeGen/matrix-cast.c
===================================================================
--- clang/test/CodeGen/matrix-cast.c
+++ clang/test/CodeGen/matrix-cast.c
@@ -52,13 +52,23 @@
 void cast_int_matrix_to_float(ix5x5 i, fx5x5 f) {
   // CHECK-LABEL: define{{.*}} void @cast_int_matrix_to_float(<25 x i32> %i, 
<25 x float> %f)
   // CHECK:       [[I:%.*]] = load <25 x i32>, <25 x i32>* {{.*}}, align 4
-  // CHECK-NEXT:  [[CONV:%.*]] = bitcast <25 x i32> [[I]] to <25 x float>
+  // CHECK-NEXT:  [[CONV:%.*]] = sitofp <25 x i32> [[I]] to <25 x float>
   // CHECK-NEXT:  store <25 x float> [[CONV]], <25 x float>* {{.*}}, align 4
   // CHECK-NEXT:  ret void
 
   f = (fx5x5)i;
 }
 
+void cast_unsigned_int_matrix_to_float(unsigned_short_int_5x5 u, fx5x5 f) {
+  // CHECK-LABEL: define{{.*}} void @cast_unsigned_int_matrix_to_float(<25 x 
i16> %u, <25 x float> %f)
+  // CHECK:       [[U:%.*]] = load <25 x i16>, <25 x i16>* {{.*}}, align 2
+  // CHECK-NEXT:  [[CONV:%.*]] = uitofp <25 x i16> [[U]] to <25 x float>
+  // CHECK-NEXT:  store <25 x float> [[CONV]], <25 x float>* {{.*}}, align 4
+  // CHECK-NEXT:  ret void
+
+  f = (fx5x5)u;
+}
+
 void cast_double_matrix_to_int(dx5x5 d, ix5x5 i) {
   // CHECK-LABEL: define{{.*}} void @cast_double_matrix_to_int(<25 x double> 
%d, <25 x i32> %i)
   // CHECK:       [[D:%.*]] = load <25 x double>, <25 x double>* {{.*}}, align 
8
@@ -108,3 +118,23 @@
 
   s = (unsigned_short_int_5x5)l;
 }
+
+void cast_unsigned_short_int_to_int(unsigned_short_int_5x5 u, ix5x5 i) {
+  // CHECK-LABEL: define{{.*}} void @cast_unsigned_short_int_to_int(<25 x i16> 
%u, <25 x i32> %i)
+  // CHECK:       [[U:%.*]] = load <25 x i16>, <25 x i16>* %0, align 2
+  // CHECK-NEXT:  [[CONV:%.*]] = zext <25 x i16> [[U]] to <25 x i32>
+  // CHECK-NEXT:  store <25 x i32> [[CONV]], <25 x i32>* {{.*}}, align 4
+  // CHECK-NEXT:  ret void
+
+  i = (ix5x5)u;
+}
+
+void cast_int_to_unsigned_long_int(ix5x5 i, unsigned_long_int_5x5 u) {
+  // CHECK-LABEL: define{{.*}} void @cast_int_to_unsigned_long_int(<25 x i32> 
%i, <25 x i64> %u)
+  // CHECK:       [[I:%.*]] = load <25 x i32>, <25 x i32>* %0, align 4
+  // CHECK-NEXT:  [[CONV:%.*]] = sext <25 x i32> [[I]] to <25 x i64>
+  // CHECK-NEXT:  store <25 x i64> [[CONV]], <25 x i64>* {{.*}}, align 8
+  // CHECK-NEXT:  ret void
+
+  u = (unsigned_long_int_5x5)i;
+}
Index: clang/lib/CodeGen/CGExprScalar.cpp
===================================================================
--- clang/lib/CodeGen/CGExprScalar.cpp
+++ clang/lib/CodeGen/CGExprScalar.cpp
@@ -1205,10 +1205,6 @@
   QualType SrcElementType;
   QualType DstElementType;
   if (SrcType->isMatrixType() && DstType->isMatrixType()) {
-    // Allow bitcast between matrixes of the same size.
-    if (SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits())
-      return Builder.CreateBitCast(Src, DstTy, "conv");
-
     SrcElementTy = cast<llvm::VectorType>(SrcTy)->getElementType();
     DstElementTy = cast<llvm::VectorType>(DstTy)->getElementType();
     SrcElementType = SrcType->castAs<MatrixType>()->getElementType();


Index: clang/test/CodeGen/matrix-cast.c
===================================================================
--- clang/test/CodeGen/matrix-cast.c
+++ clang/test/CodeGen/matrix-cast.c
@@ -52,13 +52,23 @@
 void cast_int_matrix_to_float(ix5x5 i, fx5x5 f) {
   // CHECK-LABEL: define{{.*}} void @cast_int_matrix_to_float(<25 x i32> %i, <25 x float> %f)
   // CHECK:       [[I:%.*]] = load <25 x i32>, <25 x i32>* {{.*}}, align 4
-  // CHECK-NEXT:  [[CONV:%.*]] = bitcast <25 x i32> [[I]] to <25 x float>
+  // CHECK-NEXT:  [[CONV:%.*]] = sitofp <25 x i32> [[I]] to <25 x float>
   // CHECK-NEXT:  store <25 x float> [[CONV]], <25 x float>* {{.*}}, align 4
   // CHECK-NEXT:  ret void
 
   f = (fx5x5)i;
 }
 
+void cast_unsigned_int_matrix_to_float(unsigned_short_int_5x5 u, fx5x5 f) {
+  // CHECK-LABEL: define{{.*}} void @cast_unsigned_int_matrix_to_float(<25 x i16> %u, <25 x float> %f)
+  // CHECK:       [[U:%.*]] = load <25 x i16>, <25 x i16>* {{.*}}, align 2
+  // CHECK-NEXT:  [[CONV:%.*]] = uitofp <25 x i16> [[U]] to <25 x float>
+  // CHECK-NEXT:  store <25 x float> [[CONV]], <25 x float>* {{.*}}, align 4
+  // CHECK-NEXT:  ret void
+
+  f = (fx5x5)u;
+}
+
 void cast_double_matrix_to_int(dx5x5 d, ix5x5 i) {
   // CHECK-LABEL: define{{.*}} void @cast_double_matrix_to_int(<25 x double> %d, <25 x i32> %i)
   // CHECK:       [[D:%.*]] = load <25 x double>, <25 x double>* {{.*}}, align 8
@@ -108,3 +118,23 @@
 
   s = (unsigned_short_int_5x5)l;
 }
+
+void cast_unsigned_short_int_to_int(unsigned_short_int_5x5 u, ix5x5 i) {
+  // CHECK-LABEL: define{{.*}} void @cast_unsigned_short_int_to_int(<25 x i16> %u, <25 x i32> %i)
+  // CHECK:       [[U:%.*]] = load <25 x i16>, <25 x i16>* %0, align 2
+  // CHECK-NEXT:  [[CONV:%.*]] = zext <25 x i16> [[U]] to <25 x i32>
+  // CHECK-NEXT:  store <25 x i32> [[CONV]], <25 x i32>* {{.*}}, align 4
+  // CHECK-NEXT:  ret void
+
+  i = (ix5x5)u;
+}
+
+void cast_int_to_unsigned_long_int(ix5x5 i, unsigned_long_int_5x5 u) {
+  // CHECK-LABEL: define{{.*}} void @cast_int_to_unsigned_long_int(<25 x i32> %i, <25 x i64> %u)
+  // CHECK:       [[I:%.*]] = load <25 x i32>, <25 x i32>* %0, align 4
+  // CHECK-NEXT:  [[CONV:%.*]] = sext <25 x i32> [[I]] to <25 x i64>
+  // CHECK-NEXT:  store <25 x i64> [[CONV]], <25 x i64>* {{.*}}, align 8
+  // CHECK-NEXT:  ret void
+
+  u = (unsigned_long_int_5x5)i;
+}
Index: clang/lib/CodeGen/CGExprScalar.cpp
===================================================================
--- clang/lib/CodeGen/CGExprScalar.cpp
+++ clang/lib/CodeGen/CGExprScalar.cpp
@@ -1205,10 +1205,6 @@
   QualType SrcElementType;
   QualType DstElementType;
   if (SrcType->isMatrixType() && DstType->isMatrixType()) {
-    // Allow bitcast between matrixes of the same size.
-    if (SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits())
-      return Builder.CreateBitCast(Src, DstTy, "conv");
-
     SrcElementTy = cast<llvm::VectorType>(SrcTy)->getElementType();
     DstElementTy = cast<llvm::VectorType>(DstTy)->getElementType();
     SrcElementType = SrcType->castAs<MatrixType>()->getElementType();
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to