Просмотр исходного кода

Fix a crash and bad codegen with operators on vectors (#2192)

Tristan Labelle 6 лет назад
Родитель
Сommit
d7f794c3c5

+ 16 - 0
tools/clang/lib/AST/Type.cpp

@@ -1577,6 +1577,10 @@ AutoType *Type::getContainedAutoType() const {
 bool Type::hasIntegerRepresentation() const {
   if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
     return VT->getElementType()->isIntegerType();
+  // HLSL Change Begins
+  else if (hlsl::IsHLSLVecType(CanonicalType))
+    return hlsl::GetHLSLVecElementType(CanonicalType)->isIntegerType();
+  // HLSL Change Ends
   else
     return isIntegerType();
 }
@@ -1714,6 +1718,10 @@ bool Type::isSignedIntegerOrEnumerationType() const {
 bool Type::hasSignedIntegerRepresentation() const {
   if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
     return VT->getElementType()->isSignedIntegerOrEnumerationType();
+  // HLSL Change Begins
+  else if (hlsl::IsHLSLVecType(CanonicalType))
+    return hlsl::GetHLSLVecElementType(CanonicalType)->isSignedIntegerOrEnumerationType();
+  // HLSL Change Ends
   else
     return isSignedIntegerOrEnumerationType();
 }
@@ -1754,6 +1762,10 @@ bool Type::isUnsignedIntegerOrEnumerationType() const {
 bool Type::hasUnsignedIntegerRepresentation() const {
   if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
     return VT->getElementType()->isUnsignedIntegerOrEnumerationType();
+  // HLSL Change Begins
+  else if (hlsl::IsHLSLVecType(CanonicalType))
+    return hlsl::GetHLSLVecElementType(CanonicalType)->isUnsignedIntegerOrEnumerationType();
+  // HLSL Change Ends
   else
     return isUnsignedIntegerOrEnumerationType();
 }
@@ -1770,6 +1782,10 @@ bool Type::isFloatingType() const {
 bool Type::hasFloatingRepresentation() const {
   if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
     return VT->getElementType()->isFloatingType();
+  // HLSL Change Begins
+  else if (hlsl::IsHLSLVecType(CanonicalType))
+    return hlsl::GetHLSLVecElementType(CanonicalType)->isFloatingType();
+  // HLSL Change Ends
   else
     return isFloatingType();
 }

+ 2 - 2
tools/clang/test/CodeGenHLSL/batch/expressions/operators/matrices/arithmetic.hlsl

@@ -88,8 +88,8 @@ void main()
     // CHECK: i32 1, i32 undef
     Append(output_b, f0_5 || f1_5); // LOr
 
-    // CHECK: i32 6, i32 undef
-    Append(output_u, u3 * u2); // UDiv
+    // CHECK: i32 2147483647, i32 undef
+    Append(output_u, um1 / u2); // UDiv
     // CHECK: i32 1, i32 undef
     Append(output_u, u3 % u2); // URem
     // CHECK: i32 2147483647, i32 undef

+ 0 - 0
tools/clang/test/CodeGenHLSL/batch/expressions/operators/vector-matrix-binops.hlsl → tools/clang/test/CodeGenHLSL/batch/expressions/operators/vectors/vector-matrix-binops.hlsl


+ 128 - 0
tools/clang/test/CodeGenHLSL/batch/expressions/operators/vectors/vector_arithmetic.hlsl

@@ -0,0 +1,128 @@
+// RUN: %dxc -E main -T vs_6_0 %s | FileCheck %s
+
+// Tests the implementation of unary and binary matrix operators
+
+AppendStructuredBuffer<int1> output_i;
+AppendStructuredBuffer<uint1> output_u;
+AppendStructuredBuffer<float1> output_f;
+AppendStructuredBuffer<bool1> output_b;
+
+void main()
+{
+    int1 i1 = int1(1);
+    int1 i2 = int1(2);
+    int1 i3 = int1(3);
+    int1 im1 = int1(-1);
+    int1 im3 = int1(-3);
+    uint1 u1 = uint1(1);
+    uint1 u2 = uint1(2);
+    uint1 u3 = uint1(3);
+    uint1 um1 = uint1((uint)(-1));
+    float1 fm0_5 = float1(-0.5);
+    float1 f0_5 = float1(0.5);
+    float1 f1 = float1(1);
+    float1 f1_5 = float1(1.5);
+    float1 f2 = float1(2);
+
+    // Unary operators, except pre/post inc/dec
+    // CHECK: i32 3, i32 undef
+    output_i.Append(+i3); // Plus
+    // CHECK: i32 -3, i32 undef
+    output_i.Append(-i3); // Minus
+    // CHECK: i32 -4, i32 undef
+    output_i.Append(~i3); // Not
+    // CHECK: i32 0, i32 undef
+    output_b.Append(!i3); // LNot
+    
+    // CHECK: float 5.000000e-01, float undef
+    output_f.Append(+f0_5); // Plus
+    // CHECK: float -5.000000e-01, float undef
+    output_f.Append(-f0_5); // Minus
+    // CHECK: i32 0, i32 undef
+    output_b.Append(!f0_5); // LNot
+
+    // Binary operators
+    // CHECK: i32 6, i32 undef
+    output_i.Append(i3 * i2); // Mul
+    // CHECK: i32 -1, i32 undef
+    output_i.Append(im3 / i2); // Div
+    // CHECK: i32 -1, i32 undef
+    output_i.Append(im3 % i2); // Rem
+    // CHECK: i32 3, i32 undef
+    output_i.Append(i1 + i2); // Add
+    // CHECK: i32 2, i32 undef
+    output_i.Append(i3 - i1); // Sub
+
+    // CHECK: float 1.000000e+00, float undef
+    output_f.Append(f0_5 * f2); // Mul
+    // CHECK: float 2.000000e+00, float undef
+    output_f.Append(f1 / f0_5); // Div
+    // CHECK: float 5.000000e-01, float undef
+    output_f.Append(f2 % f1_5); // Rem
+    // CHECK: float 2.000000e+00, float undef
+    output_f.Append(f0_5 + f1_5); // Add
+    // CHECK: float -1.000000e+00, float undef
+    output_f.Append(f0_5 - f1_5); // Sub
+
+    // CHECK: i32 6, i32 undef
+    output_i.Append(i3 << i1); // Shl
+    // CHECK: i32 -1, i32 undef
+    output_i.Append(im1 >> i1); // Shr
+    // CHECK: i32 2, i32 undef
+    output_i.Append(i3 & i2); // And
+    // CHECK: i32 2, i32 undef
+    output_i.Append(i3 ^ i1); // Xor
+    // CHECK: i32 3, i32 undef
+    output_i.Append(i2 | i1); // Or
+
+    // CHECK: i32 1, i32 undef
+    output_b.Append(i3 && i2); // LAnd
+    // CHECK: i32 1, i32 undef
+    output_b.Append(i3 || i2); // LOr
+    
+    // CHECK: i32 1, i32 undef
+    output_b.Append(f0_5 && f1_5); // LAnd
+    // CHECK: i32 1, i32 undef
+    output_b.Append(f0_5 || f1_5); // LOr
+
+    // CHECK: i32 2147483647, i32 undef
+    output_u.Append(um1 / u2); // UDiv
+    // CHECK: i32 1, i32 undef
+    output_u.Append(u3 % u2); // URem
+    // CHECK: i32 2147483647, i32 undef
+    output_u.Append(um1 >> u1); // UShr
+
+    // CHECK: i32 1, i32 undef
+    output_b.Append(im1 < i1); // LT
+    // CHECK: i32 0, i32 undef
+    output_b.Append(im1 > i1); // GT
+    // CHECK: i32 1, i32 undef
+    output_b.Append(im1 <= i1); // LE
+    // CHECK: i32 0, i32 undef
+    output_b.Append(im1 >= i1); // GE
+    // CHECK: i32 0, i32 undef
+    output_b.Append(im1 == i1); // EQ
+    // CHECK: i32 1, i32 undef
+    output_b.Append(im1 != i1); // NE
+    // CHECK: i32 0, i32 undef
+    output_b.Append(um1 < u1); // ULT
+    // CHECK: i32 1, i32 undef
+    output_b.Append(um1 > u1); // UGT
+    // CHECK: i32 0, i32 undef
+    output_b.Append(um1 <= u1); // ULE
+    // CHECK: i32 1, i32 undef
+    output_b.Append(um1 >= u1); // UGE
+    
+    // CHECK: i32 1, i32 undef
+    output_b.Append(fm0_5 < f1_5); // LT
+    // CHECK: i32 0, i32 undef
+    output_b.Append(fm0_5 > f1_5); // GT
+    // CHECK: i32 1, i32 undef
+    output_b.Append(fm0_5 <= f1_5); // LE
+    // CHECK: i32 0, i32 undef
+    output_b.Append(fm0_5 >= f1_5); // GE
+    // CHECK: i32 0, i32 undef
+    output_b.Append(fm0_5 == f1_5); // EQ
+    // CHECK: i32 1, i32 undef
+    output_b.Append(fm0_5 != f1_5); // NE
+}

+ 0 - 0
tools/clang/test/CodeGenHLSL/batch/expressions/operators/vector_comparison.hlsl → tools/clang/test/CodeGenHLSL/batch/expressions/operators/vectors/vector_comparison.hlsl


+ 36 - 0
tools/clang/test/CodeGenHLSL/batch/expressions/operators/vectors/vector_increment_decrement.hlsl

@@ -0,0 +1,36 @@
+// RUN: %dxc /T vs_6_0 /E main %s | FileCheck %s
+
+// Check that pre/post increment/decrement operators on
+// matrices have the intended semantics for both the original
+// variable and the returned value.
+
+AppendStructuredBuffer<int2> results;
+
+void main()
+{
+  int1 variable, result;
+  
+  // Post-increment
+  // CHECK: i32 11, i32 10
+  variable = int1(10);
+  result = variable++;
+  results.Append(int2(variable.x, result.x));
+  
+  // Post-decrement
+  // CHECK: i32 9, i32 10
+  variable = int1(10);
+  result = variable--;
+  results.Append(int2(variable.x, result.x));
+  
+  // Pre-increment
+  // CHECK: i32 11, i32 11
+  variable = int1(10);
+  result = ++variable;
+  results.Append(int2(variable.x, result.x));
+  
+  // Pre-decrement
+  // CHECK: i32 9, i32 9
+  variable = int1(10);
+  result = --variable;
+  results.Append(int2(variable.x, result.x));
+}

+ 1 - 1
tools/clang/test/CodeGenHLSL/batch/misc/uint64_1.hlsl

@@ -2,7 +2,7 @@
 
 // CHECK: 64-Bit integer
 // CHECK: add i64
-// CHECK: sdiv i64
+// CHECK: udiv i64
 // CHECK: shl i64
 // CHECK: mul i64
 // CHECK: UMax

+ 0 - 6
tools/clang/test/CodeGenHLSL/crashes/inout_vector_numerical_conversion.hlsl

@@ -1,6 +0,0 @@
-// RUN: %dxc -E main -T vs_6_2 %s | FileCheck %s
-
-// Repro of GitHub #1916
-
-void inc_i32x2(inout int2 val) { val++; }
-void main(inout float2 f32x2 : F32X2) { inc_i32x2(f32x2); }