Skip to content

Fix implicit conversion rank ordering #106811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 9, 2024

Conversation

llvm-beanz
Copy link
Collaborator

DXC prefers dimension-preserving conversions over precision-losing conversions. This means a double4 -> float4 conversion is preferred over a double4 -> double3 or double4 -> double conversion.

DXC prefers dimension-preserving conversions over precision-losing
conversions. This means a double4 -> float4 conversion is preferred over
a double4 -> double3 or double4 -> double conversion.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" HLSL HLSL Language Support labels Aug 30, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 30, 2024

@llvm/pr-subscribers-hlsl

Author: Chris B (llvm-beanz)

Changes

DXC prefers dimension-preserving conversions over precision-losing conversions. This means a double4 -> float4 conversion is preferred over a double4 -> double3 or double4 -> double conversion.


Full diff: https://github.com/llvm/llvm-project/pull/106811.diff

2 Files Affected:

  • (modified) clang/include/clang/Sema/Overload.h (+3-3)
  • (modified) clang/test/SemaHLSL/TruncationOverloadResolution.hlsl (+13-15)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index d6a6cee62a7528..f63f48e0fa3048 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -225,12 +225,12 @@ class Sema;
     /// HLSL Scalar Widening with promotion
     ICR_HLSL_Scalar_Widening_Promotion,
 
-    /// HLSL Matching Dimension Reduction
-    ICR_HLSL_Dimension_Reduction,
-
     /// Conversion
     ICR_Conversion,
 
+    /// HLSL Matching Dimension Reduction
+    ICR_HLSL_Dimension_Reduction,
+
     /// OpenCL Scalar Widening
     ICR_OCL_Scalar_Widening,
 
diff --git a/clang/test/SemaHLSL/TruncationOverloadResolution.hlsl b/clang/test/SemaHLSL/TruncationOverloadResolution.hlsl
index f8cfe22372e885..2bcb367c5669a3 100644
--- a/clang/test/SemaHLSL/TruncationOverloadResolution.hlsl
+++ b/clang/test/SemaHLSL/TruncationOverloadResolution.hlsl
@@ -1,24 +1,16 @@
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type -finclude-default-header -fsyntax-only %s -DERROR=1 -verify
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type -finclude-default-header -fsyntax-only -Wconversion %s -DERROR=1 -verify
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type -finclude-default-header -ast-dump %s | FileCheck %s
 
-// Case 1: Prefer exact-match truncation over conversion.
-void Half4Float4Double2(double2 D);
-void Half4Float4Double2(float4 D);
-void Half4Float4Double2(half4 D);
+// Case 1: Prefer conversion over exact match truncation.
 
 void Half4Float2(float2 D);
 void Half4Float2(half4 D);
 
 void Case1(float4 F, double4 D) {
   // CHECK: CallExpr {{.*}} 'void'
-  // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double2)' <FunctionToPointerDecay>
-  // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double2)' lvalue Function {{.*}} 'Half4Float4Double2' 'void (double2)'
-  Half4Float4Double2(D); // expected-warning{{implicit conversion truncates vector: 'double4' (aka 'vector<double, 4>') to 'vector<double, 2>' (vector of 2 'double' values)}}
-
-  // CHECK: CallExpr {{.*}} 'void'
-  // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float2)' <FunctionToPointerDecay>
-  // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float2)' lvalue Function {{.*}} 'Half4Float2' 'void (float2)'
-  Half4Float2(F); // expected-warning{{implicit conversion truncates vector: 'float4' (aka 'vector<float, 4>') to 'vector<float, 2>' (vector of 2 'float' values)}}
+  // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half4)' <FunctionToPointerDecay>
+  // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half4)' lvalue Function {{.*}} 'Half4Float2' 'void (half4)'
+  Half4Float2(F); // expected-warning{{implicit conversion loses floating-point precision: 'float4' (aka 'vector<float, 4>') to 'vector<half, 4>' (vector of 4 'half' values)}}
 }
 
 // Case 2: Prefer promotions over conversions when truncating.
@@ -46,7 +38,13 @@ void Half2Half3(half2 D); // expected-note{{candidate function}} expected-note{{
 void Double2Double3(double3 D); // expected-note{{candidate function}} expected-note{{candidate function}} expected-note{{candidate function}}
 void Double2Double3(double2 D); // expected-note{{candidate function}} expected-note{{candidate function}} expected-note{{candidate function}}
 
+void Half4Float4Double2(double2 D);
+void Half4Float4Double2(float4 D); // expected-note{{candidate function}}
+void Half4Float4Double2(half4 D); // expected-note{{candidate function}}
+
 void Case1(half4 H, float4 F, double4 D) {
+  Half4Float4Double2(D); // expected-error {{call to 'Half4Float4Double2' is ambiguous}}
+
   Float2Double2(H); // expected-error {{call to 'Float2Double2' is ambiguous}}
 
   Half2Float2(D); // expected-error {{call to 'Half2Float2' is ambiguous}}
@@ -55,8 +53,8 @@ void Case1(half4 H, float4 F, double4 D) {
   Half2Half3(F); // expected-error {{call to 'Half2Half3' is ambiguous}}
   Half2Half3(D); // expected-error {{call to 'Half2Half3' is ambiguous}}
   Half2Half3(H.xyz);
-  Half2Half3(F.xyz);
-  Half2Half3(D.xyz);
+  Half2Half3(F.xyz); // expected-warning {{implicit conversion loses floating-point precision: 'vector<float, 3>' (vector of 3 'float' values) to 'vector<half, 3>' (vector of 3 'half' values)}}
+  Half2Half3(D.xyz); // expected-warning {{implicit conversion loses floating-point precision: 'vector<double, 3>' (vector of 3 'double' values) to 'vector<half, 3>' (vector of 3 'half' values)}}
 
   Double2Double3(H); // expected-error {{call to 'Double2Double3' is ambiguous}}
   Double2Double3(F); // expected-error {{call to 'Double2Double3' is ambiguous}}

@llvmbot
Copy link
Member

llvmbot commented Aug 30, 2024

@llvm/pr-subscribers-clang

Author: Chris B (llvm-beanz)

Changes

DXC prefers dimension-preserving conversions over precision-losing conversions. This means a double4 -> float4 conversion is preferred over a double4 -> double3 or double4 -> double conversion.


Full diff: https://github.com/llvm/llvm-project/pull/106811.diff

2 Files Affected:

  • (modified) clang/include/clang/Sema/Overload.h (+3-3)
  • (modified) clang/test/SemaHLSL/TruncationOverloadResolution.hlsl (+13-15)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index d6a6cee62a7528..f63f48e0fa3048 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -225,12 +225,12 @@ class Sema;
     /// HLSL Scalar Widening with promotion
     ICR_HLSL_Scalar_Widening_Promotion,
 
-    /// HLSL Matching Dimension Reduction
-    ICR_HLSL_Dimension_Reduction,
-
     /// Conversion
     ICR_Conversion,
 
+    /// HLSL Matching Dimension Reduction
+    ICR_HLSL_Dimension_Reduction,
+
     /// OpenCL Scalar Widening
     ICR_OCL_Scalar_Widening,
 
diff --git a/clang/test/SemaHLSL/TruncationOverloadResolution.hlsl b/clang/test/SemaHLSL/TruncationOverloadResolution.hlsl
index f8cfe22372e885..2bcb367c5669a3 100644
--- a/clang/test/SemaHLSL/TruncationOverloadResolution.hlsl
+++ b/clang/test/SemaHLSL/TruncationOverloadResolution.hlsl
@@ -1,24 +1,16 @@
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type -finclude-default-header -fsyntax-only %s -DERROR=1 -verify
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type -finclude-default-header -fsyntax-only -Wconversion %s -DERROR=1 -verify
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type -finclude-default-header -ast-dump %s | FileCheck %s
 
-// Case 1: Prefer exact-match truncation over conversion.
-void Half4Float4Double2(double2 D);
-void Half4Float4Double2(float4 D);
-void Half4Float4Double2(half4 D);
+// Case 1: Prefer conversion over exact match truncation.
 
 void Half4Float2(float2 D);
 void Half4Float2(half4 D);
 
 void Case1(float4 F, double4 D) {
   // CHECK: CallExpr {{.*}} 'void'
-  // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double2)' <FunctionToPointerDecay>
-  // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double2)' lvalue Function {{.*}} 'Half4Float4Double2' 'void (double2)'
-  Half4Float4Double2(D); // expected-warning{{implicit conversion truncates vector: 'double4' (aka 'vector<double, 4>') to 'vector<double, 2>' (vector of 2 'double' values)}}
-
-  // CHECK: CallExpr {{.*}} 'void'
-  // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float2)' <FunctionToPointerDecay>
-  // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float2)' lvalue Function {{.*}} 'Half4Float2' 'void (float2)'
-  Half4Float2(F); // expected-warning{{implicit conversion truncates vector: 'float4' (aka 'vector<float, 4>') to 'vector<float, 2>' (vector of 2 'float' values)}}
+  // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half4)' <FunctionToPointerDecay>
+  // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half4)' lvalue Function {{.*}} 'Half4Float2' 'void (half4)'
+  Half4Float2(F); // expected-warning{{implicit conversion loses floating-point precision: 'float4' (aka 'vector<float, 4>') to 'vector<half, 4>' (vector of 4 'half' values)}}
 }
 
 // Case 2: Prefer promotions over conversions when truncating.
@@ -46,7 +38,13 @@ void Half2Half3(half2 D); // expected-note{{candidate function}} expected-note{{
 void Double2Double3(double3 D); // expected-note{{candidate function}} expected-note{{candidate function}} expected-note{{candidate function}}
 void Double2Double3(double2 D); // expected-note{{candidate function}} expected-note{{candidate function}} expected-note{{candidate function}}
 
+void Half4Float4Double2(double2 D);
+void Half4Float4Double2(float4 D); // expected-note{{candidate function}}
+void Half4Float4Double2(half4 D); // expected-note{{candidate function}}
+
 void Case1(half4 H, float4 F, double4 D) {
+  Half4Float4Double2(D); // expected-error {{call to 'Half4Float4Double2' is ambiguous}}
+
   Float2Double2(H); // expected-error {{call to 'Float2Double2' is ambiguous}}
 
   Half2Float2(D); // expected-error {{call to 'Half2Float2' is ambiguous}}
@@ -55,8 +53,8 @@ void Case1(half4 H, float4 F, double4 D) {
   Half2Half3(F); // expected-error {{call to 'Half2Half3' is ambiguous}}
   Half2Half3(D); // expected-error {{call to 'Half2Half3' is ambiguous}}
   Half2Half3(H.xyz);
-  Half2Half3(F.xyz);
-  Half2Half3(D.xyz);
+  Half2Half3(F.xyz); // expected-warning {{implicit conversion loses floating-point precision: 'vector<float, 3>' (vector of 3 'float' values) to 'vector<half, 3>' (vector of 3 'half' values)}}
+  Half2Half3(D.xyz); // expected-warning {{implicit conversion loses floating-point precision: 'vector<double, 3>' (vector of 3 'double' values) to 'vector<half, 3>' (vector of 3 'half' values)}}
 
   Double2Double3(H); // expected-error {{call to 'Double2Double3' is ambiguous}}
   Double2Double3(F); // expected-error {{call to 'Double2Double3' is ambiguous}}

llvm-beanz and others added 2 commits August 31, 2024 02:56
This is an NFC change. For a given argument in a call there cannot be a
valid implicit conversion seqeunce of any of the scalar widening ranks
and a conversion sequence for a different overload of dimension
reduction because the widening ranks require the argument to be a scalar
and the reduction ranks require the argument to be a vector.

Changing the order in this change does not impact behavior in any way,
but it does align the source with the spec's language and ordering which
makes it cleaner to read.
@llvm-beanz llvm-beanz merged commit 6cc0138 into llvm:main Sep 9, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category HLSL HLSL Language Support
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants