1 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type -finclude-default-header -Wconversion -verify -o - -DERROR=1 %s
2 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type -finclude-default-header -ast-dump %s | FileCheck %s
4 // This test verifies floating point type implicit conversion ranks for overload
5 // resolution. In HLSL the built-in type ranks are half < float < double. This
6 // applies to both scalar and vector types.
8 // HLSL allows implicit truncation fo types, so it differentiates between
9 // promotions (converting to larger types) and conversions (converting to
10 // smaller types). Promotions are preferred over conversions. Promotions prefer
11 // promoting to the next lowest type in the ranking order. Conversions prefer
12 // converting to the next highest type in the ranking order.
14 void HalfFloatDouble(double D);
15 void HalfFloatDouble(float F);
16 void HalfFloatDouble(half H);
18 // CHECK: FunctionDecl {{.*}} used HalfFloatDouble 'void (double)'
19 // CHECK: FunctionDecl {{.*}} used HalfFloatDouble 'void (float)'
20 // CHECK: FunctionDecl {{.*}} used HalfFloatDouble 'void (half)'
22 void FloatDouble(double D); // expected-note{{candidate function}}
23 void FloatDouble(float F); // expected-note{{candidate function}}
25 // CHECK: FunctionDecl {{.*}} used FloatDouble 'void (double)'
26 // CHECK: FunctionDecl {{.*}} used FloatDouble 'void (float)'
28 void HalfDouble(double D);
29 void HalfDouble(half H);
31 // CHECK: FunctionDecl {{.*}} used HalfDouble 'void (double)'
32 // CHECK: FunctionDecl {{.*}} used HalfDouble 'void (half)'
34 void HalfFloat(float F); // expected-note{{candidate function}}
35 void HalfFloat(half H); // expected-note{{candidate function}}
37 // CHECK: FunctionDecl {{.*}} used HalfFloat 'void (float)'
38 // CHECK: FunctionDecl {{.*}} used HalfFloat 'void (half)'
40 void Double(double D);
44 // CHECK: FunctionDecl {{.*}} used Double 'void (double)'
45 // CHECK: FunctionDecl {{.*}} used Float 'void (float)'
46 // CHECK: FunctionDecl {{.*}} used Half 'void (half)'
49 // Case 1: A function declared with overloads for half float and double types.
50 // (a) When called with half, it will resolve to half because half is an exact
52 // (b) When called with float it will resolve to float because float is an
54 // (c) When called with double it will resolve to double because it is an
57 // CHECK-LABEL: FunctionDecl {{.*}} Case1 'void (half, float, double)'
58 void Case1(half H, float F, double D) {
59 // CHECK: CallExpr {{.*}} 'void'
60 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half)' <FunctionToPointerDecay>
61 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half)' lvalue Function {{.*}} 'HalfFloatDouble' 'void (half)'
64 // CHECK: CallExpr {{.*}} 'void'
65 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float)' <FunctionToPointerDecay>
66 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float)' lvalue Function {{.*}} 'HalfFloatDouble' 'void (float)'
69 // CHECK: CallExpr {{.*}} 'void'
70 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double)' <FunctionToPointerDecay>
71 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double)' lvalue Function {{.*}} 'HalfFloatDouble' 'void (double)'
75 // Case 2: A function declared with double and float overlaods.
76 // (a) When called with half, it will fail to resolve because it cannot
77 // disambiguate the promotions.
78 // (b) When called with float it will resolve to float because float is an
80 // (c) When called with double it will resolve to double because it is an
83 // CHECK-LABEL: FunctionDecl {{.*}} Case2 'void (half, float, double)'
84 void Case2(half H, float F, double D) {
86 FloatDouble(H); // expected-error{{call to 'FloatDouble' is ambiguous}}
89 // CHECK: CallExpr {{.*}} 'void'
90 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float)' <FunctionToPointerDecay>
91 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float)' lvalue Function {{.*}} 'FloatDouble' 'void (float)'
94 // CHECK: CallExpr {{.*}} 'void'
95 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double)' <FunctionToPointerDecay>
96 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double)' lvalue Function {{.*}} 'FloatDouble' 'void (double)'
100 // Case 3: A function declared with half and double overloads
101 // (a) When called with half, it will resolve to half because it is an exact
103 // (b) When called with flaot, it will resolve to double because double is a
105 // (c) When called with double, it will resolve to double because it is an
108 // CHECK-LABEL: FunctionDecl {{.*}} Case3 'void (half, float, double)'
109 void Case3(half H, float F, double D) {
110 // CHECK: CallExpr {{.*}} 'void'
111 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half)' <FunctionToPointerDecay>
112 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half)' lvalue Function {{.*}} 'HalfDouble' 'void (half)'
115 // CHECK: CallExpr {{.*}} 'void'
116 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double)' <FunctionToPointerDecay>
117 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double)' lvalue Function {{.*}} 'HalfDouble' 'void (double)'
120 // CHECK: CallExpr {{.*}} 'void'
121 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double)' <FunctionToPointerDecay>
122 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double)' lvalue Function {{.*}} 'HalfDouble' 'void (double)'
126 // Case 4: A function declared with half and float overloads.
127 // (a) When called with half, it will resolve to half because half is an exact
129 // (b) When called with float it will resolve to float because float is an
131 // (c) When called with double it will resolve to float because it is the
132 // float is higher rank than half.
134 // CHECK-LABEL: FunctionDecl {{.*}} Case4 'void (half, float, double)'
135 void Case4(half H, float F, double D) {
136 // CHECK: CallExpr {{.*}} 'void'
137 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half)' <FunctionToPointerDecay>
138 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half)' lvalue Function {{.*}} 'HalfFloat' 'void (half)'
141 // CHECK: CallExpr {{.*}} 'void'
142 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float)' <FunctionToPointerDecay>
143 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float)' lvalue Function {{.*}} 'HalfFloat' 'void (float)'
147 HalfFloat(D); // expected-error{{call to 'HalfFloat' is ambiguous}}
151 // Case 5: A function declared with only a double overload.
152 // (a) When called with half, it will resolve to double because double is a
154 // (b) When called with float it will resolve to double because double is a
156 // (c) When called with double it will resolve to double because it is an
159 // CHECK-LABEL: FunctionDecl {{.*}} Case5 'void (half, float, double)'
160 void Case5(half H, float F, double D) {
161 // CHECK: CallExpr {{.*}} 'void'
162 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double)' <FunctionToPointerDecay>
163 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double)' lvalue Function {{.*}} 'Double' 'void (double)'
166 // CHECK: CallExpr {{.*}} 'void'
167 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double)' <FunctionToPointerDecay>
168 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double)' lvalue Function {{.*}} 'Double' 'void (double)'
171 // CHECK: CallExpr {{.*}} 'void'
172 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double)' <FunctionToPointerDecay>
173 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double)' lvalue Function {{.*}} 'Double' 'void (double)'
177 // Case 6: A function declared with only a float overload.
178 // (a) When called with half, it will resolve to float because float is a
180 // (b) When called with float it will resolve to float because float is an
182 // (c) When called with double it will resolve to float because it is a
185 // CHECK-LABEL: FunctionDecl {{.*}} Case6 'void (half, float, double)'
186 void Case6(half H, float F, double D) {
187 // CHECK: CallExpr {{.*}} 'void'
188 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float)' <FunctionToPointerDecay>
189 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float)' lvalue Function {{.*}} 'Float' 'void (float)'
192 // CHECK: CallExpr {{.*}} 'void'
193 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float)' <FunctionToPointerDecay>
194 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float)' lvalue Function {{.*}} 'Float' 'void (float)'
197 // CHECK: CallExpr {{.*}} 'void'
198 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float)' <FunctionToPointerDecay>
199 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float)' lvalue Function {{.*}} 'Float' 'void (float)'
200 Float(D); // expected-warning{{implicit conversion loses floating-point precision: 'double' to 'float'}}
203 // Case 7: A function declared with only a half overload.
204 // (a) When called with half, it will resolve to half because half is an
206 // (b) When called with float it will resolve to half because half is a
208 // (c) When called with double it will resolve to float because it is a
211 // CHECK-LABEL: FunctionDecl {{.*}} Case7 'void (half, float, double)'
212 void Case7(half H, float F, double D) {
213 // CHECK: CallExpr {{.*}} 'void'
214 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half)' <FunctionToPointerDecay>
215 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half)' lvalue Function {{.*}} 'Half' 'void (half)'
218 // CHECK: CallExpr {{.*}} 'void'
219 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half)' <FunctionToPointerDecay>
220 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half)' lvalue Function {{.*}} 'Half' 'void (half)'
221 Half(F); // expected-warning{{implicit conversion loses floating-point precision: 'float' to 'half'}}
223 // CHECK: CallExpr {{.*}} 'void'
224 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half)' <FunctionToPointerDecay>
225 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half)' lvalue Function {{.*}} 'Half' 'void (half)'
226 Half(D); // expected-warning{{implicit conversion loses floating-point precision: 'double' to 'half'}}