Debian Codegraph @

clang/test/SemaHLSL/MatrixElementOverloadResolution.hlsl

raw
1 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type -finclude-default-header -verify -o - %s -DERROR=1
2 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type -finclude-default-header -ast-dump %s | FileCheck %s
3
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 matrix types.
7
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.
13
14 void HalfFloatDouble(double2x2 D);
15 void HalfFloatDouble(float2x2 F);
16 void HalfFloatDouble(half2x2 H);
17
18 // CHECK: FunctionDecl {{.*}} used HalfFloatDouble 'void (double2x2)'
19 // CHECK: FunctionDecl {{.*}} used HalfFloatDouble 'void (float2x2)'
20 // CHECK: FunctionDecl {{.*}} used HalfFloatDouble 'void (half2x2)'
21
22 void FloatDouble(double2x2 D); // expected-note {{candidate function}}
23 void FloatDouble(float2x2 F); // expected-note {{candidate function}}
24
25 // CHECK: FunctionDecl {{.*}} used FloatDouble 'void (double2x2)'
26 // CHECK: FunctionDecl {{.*}} used FloatDouble 'void (float2x2)'
27
28 void HalfDouble(double2x2 D);
29 void HalfDouble(half2x2 H);
30
31 // CHECK: FunctionDecl {{.*}} used HalfDouble 'void (double2x2)'
32 // CHECK: FunctionDecl {{.*}} used HalfDouble 'void (half2x2)'
33
34 void HalfFloat(float2x2 F); // expected-note {{candidate function}}
35 void HalfFloat(half2x2 H); // expected-note {{candidate function}}
36
37 // CHECK: FunctionDecl {{.*}} used HalfFloat 'void (float2x2)'
38 // CHECK: FunctionDecl {{.*}} used HalfFloat 'void (half2x2)'
39
40 void Double(double2x2 D);
41 void Float(float2x2 F);
42 void Half(half2x2 H);
43
44 // CHECK: FunctionDecl {{.*}} used Double 'void (double2x2)'
45 // CHECK: FunctionDecl {{.*}} used Float 'void (float2x2)'
46 // CHECK: FunctionDecl {{.*}} used Half 'void (half2x2)'
47
48 // Case 1: A function declared with overloads for half float and double types.
49 // (a) When called with half, it will resolve to half because half is an exact
50 // match.
51 // (b) When called with float it will resolve to float because float is an
52 // exact match.
53 // (c) When called with double it will resolve to double because it is an
54 // exact match.
55
56 // CHECK-LABEL: FunctionDecl {{.*}} Case1 'void (half2x2, float2x2, double2x2)'
57 void Case1(half2x2 H, float2x2 F, double2x2 D) {
58 // CHECK: CallExpr {{.*}} 'void'
59 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half2x2)' <FunctionToPointerDecay>
60 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half2x2)' lvalue Function {{.*}} 'HalfFloatDouble' 'void (half2x2)'
61 HalfFloatDouble(H);
62
63 // CHECK: CallExpr {{.*}} 'void'
64 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float2x2)' <FunctionToPointerDecay>
65 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float2x2)' lvalue Function {{.*}} 'HalfFloatDouble' 'void (float2x2)'
66 HalfFloatDouble(F);
67
68 // CHECK: CallExpr {{.*}} 'void'
69 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double2x2)' <FunctionToPointerDecay>
70 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double2x2)' lvalue Function {{.*}} 'HalfFloatDouble' 'void (double2x2)'
71 HalfFloatDouble(D);
72 }
73
74 // Case 2: A function declared with double and float overlaods.
75 // (a) When called with half, it fails to resulve the ambiguous promotion.
76 // (b) When called with float it will resolve to float because float is an
77 // exact match.
78 // (c) When called with double it will resolve to double because it is an
79 // exact match.
80
81 // CHECK-LABEL: FunctionDecl {{.*}} Case2 'void (half2x2, float2x2, double2x2)'
82 void Case2(half2x2 H, float2x2 F, double2x2 D) {
83 #if ERROR
84 FloatDouble(H); // expected-error {{call to 'FloatDouble' is ambiguous}}
85 #endif
86
87 // CHECK: CallExpr {{.*}} 'void'
88 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float2x2)' <FunctionToPointerDecay>
89 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float2x2)' lvalue Function {{.*}} 'FloatDouble' 'void (float2x2)'
90 FloatDouble(F);
91
92 // CHECK: CallExpr {{.*}} 'void'
93 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double2x2)' <FunctionToPointerDecay>
94 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double2x2)' lvalue Function {{.*}} 'FloatDouble' 'void (double2x2)'
95 FloatDouble(D);
96 }
97
98 // Case 3: A function declared with half and double overloads
99 // (a) When called with half, it will resolve to half because it is an exact
100 // match.
101 // (b) When called with flaot, it will resolve to double because double is a
102 // valid promotion.
103 // (c) When called with double, it will resolve to double because it is an
104 // exact match.
105
106 // CHECK-LABEL: FunctionDecl {{.*}} Case3 'void (half2x2, float2x2, double2x2)'
107 void Case3(half2x2 H, float2x2 F, double2x2 D) {
108 // CHECK: CallExpr {{.*}} 'void'
109 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half2x2)' <FunctionToPointerDecay>
110 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half2x2)' lvalue Function {{.*}} 'HalfDouble' 'void (half2x2)'
111 HalfDouble(H);
112
113 // CHECK: CallExpr {{.*}} 'void'
114 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double2x2)' <FunctionToPointerDecay>
115 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double2x2)' lvalue Function {{.*}} 'HalfDouble' 'void (double2x2)'
116 HalfDouble(F);
117
118 // CHECK: CallExpr {{.*}} 'void'
119 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double2x2)' <FunctionToPointerDecay>
120 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double2x2)' lvalue Function {{.*}} 'HalfDouble' 'void (double2x2)'
121 HalfDouble(D);
122 }
123
124 // Case 4: A function declared with half and float overloads.
125 // (a) When called with half, it will resolve to half because half is an exact
126 // match.
127 // (b) When called with float it will resolve to float because float is an
128 // exact match.
129 // (c) When called with double it fails to resolve the ambigjuous conversion.
130
131 // CHECK-LABEL: FunctionDecl {{.*}} Case4 'void (half2x2, float2x2, double2x2)'
132 void Case4(half2x2 H, float2x2 F, double2x2 D) {
133 // CHECK: CallExpr {{.*}} 'void'
134 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half2x2)' <FunctionToPointerDecay>
135 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half2x2)' lvalue Function {{.*}} 'HalfFloat' 'void (half2x2)'
136 HalfFloat(H);
137
138 // CHECK: CallExpr {{.*}} 'void'
139 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float2x2)' <FunctionToPointerDecay>
140 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float2x2)' lvalue Function {{.*}} 'HalfFloat' 'void (float2x2)'
141 HalfFloat(F);
142
143 #if ERROR
144 HalfFloat(D); // expected-error{{call to 'HalfFloat' is ambiguous}}
145 #endif
146 }
147
148 // Case 5: A function declared with only a double overload.
149 // (a) When called with half, it will resolve to double because double is a
150 // valid promotion.
151 // (b) When called with float it will resolve to double because double is a
152 // valid promotion.
153 // (c) When called with double it will resolve to double because it is an
154 // exact match.
155
156 // CHECK-LABEL: FunctionDecl {{.*}} Case5 'void (half2x2, float2x2, double2x2)'
157 void Case5(half2x2 H, float2x2 F, double2x2 D) {
158 // CHECK: CallExpr {{.*}} 'void'
159 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double2x2)' <FunctionToPointerDecay>
160 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double2x2)' lvalue Function {{.*}} 'Double' 'void (double2x2)'
161 Double(H);
162
163 // CHECK: CallExpr {{.*}} 'void'
164 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double2x2)' <FunctionToPointerDecay>
165 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double2x2)' lvalue Function {{.*}} 'Double' 'void (double2x2)'
166 Double(F);
167
168 // CHECK: CallExpr {{.*}} 'void'
169 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double2x2)' <FunctionToPointerDecay>
170 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (double2x2)' lvalue Function {{.*}} 'Double' 'void (double2x2)'
171 Double(D);
172 }
173
174 // Case 6: A function declared with only a float overload.
175 // (a) When called with half, it will resolve to float because float is a
176 // valid promotion.
177 // (b) When called with float it will resolve to float because float is an
178 // exact match.
179 // (c) When called with double it will resolve to float because it is a
180 // valid conversion.
181
182 // CHECK-LABEL: FunctionDecl {{.*}} Case6 'void (half2x2, float2x2, double2x2)'
183 void Case6(half2x2 H, float2x2 F, double2x2 D) {
184 // CHECK: CallExpr {{.*}} 'void'
185 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float2x2)' <FunctionToPointerDecay>
186 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float2x2)' lvalue Function {{.*}} 'Float' 'void (float2x2)'
187 Float(H);
188
189 // CHECK: CallExpr {{.*}} 'void'
190 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float2x2)' <FunctionToPointerDecay>
191 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float2x2)' lvalue Function {{.*}} 'Float' 'void (float2x2)'
192 Float(F);
193
194 // CHECK: CallExpr {{.*}} 'void'
195 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(float2x2)' <FunctionToPointerDecay>
196 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (float2x2)' lvalue Function {{.*}} 'Float' 'void (float2x2)'
197 Float(D); // TODO: See #168944. Make this an expect warning. {{implicit conversion loses floating-point precision: 'double2x2' (aka 'matrix<double, 2, 2>') to 'matrix<float, 2, 2>' (matrix of 2 'float' values)}}
198 }
199
200 // Case 7: A function declared with only a half overload.
201 // (a) When called with half, it will resolve to half because half is an
202 // exact match
203 // (b) When called with float it will resolve to half because half is a
204 // valid conversion.
205 // (c) When called with double it will resolve to float because it is a
206 // valid conversion.
207
208 // CHECK-LABEL: FunctionDecl {{.*}} Case7 'void (half2x2, float2x2, double2x2)'
209 void Case7(half2x2 H, float2x2 F, double2x2 D) {
210 // CHECK: CallExpr {{.*}} 'void'
211 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half2x2)' <FunctionToPointerDecay>
212 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half2x2)' lvalue Function {{.*}} 'Half' 'void (half2x2)'
213 Half(H);
214
215 // CHECK: CallExpr {{.*}} 'void'
216 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half2x2)' <FunctionToPointerDecay>
217 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half2x2)' lvalue Function {{.*}} 'Half' 'void (half2x2)'
218 Half(F); // TODO: See #168944. Make this an expect warning. {{implicit conversion loses floating-point precision: 'float2x2' (aka 'matrix<float, 2, 2>') to 'matrix<half, 2, 2>' (matrix of 4 'half' values)}}
219
220 // CHECK: CallExpr {{.*}} 'void'
221 // CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(half2x2)' <FunctionToPointerDecay>
222 // CHECK-NEXT: DeclRefExpr {{.*}} 'void (half2x2)' lvalue Function {{.*}} 'Half' 'void (half2x2)'
223 Half(D); // TODO: See #168944. Make this an expect warning. {{implicit conversion loses floating-point precision: 'double2x2' (aka 'matrix<double, 2, 2>') to 'matrix<half, 2, 2>' (matrix of 4 'half' values)}}
224 }
225
226 void fn3x2(float3x2) {} // expected-note{{candidate function}}
227 void fn2x2(float2x2) {}
228 void fn2x2IO(inout float2x2) {}
229 void fnI2x2IO(inout int2x2) {}
230
231 void matOrVec(float4 F) {} // expected-note {{candidate function}}
232 void matOrVec(float2x2 F) {} // expected-note {{candidate function}}
233
234 void matOrVec2(float3 F) {} // expected-note{{candidate function}}
235 void matOrVec2(float2x3 F) {} // expected-note{{candidate function}}
236
237 void matOrVec3(float4x4 F) {}
238
239 export void Case8(float2x3 f23, float4x4 f44, float3x3 f33, float3x2 f32) {
240 int2x2 i22 = f23;
241 // expected-warning@-1{{implicit conversion truncates matrix: 'float2x3' (aka 'matrix<float, 2, 3>') to 'int2x2' (aka 'matrix<int, 2, 2>')}}
242 //CHECK: VarDecl {{.*}} i22 'int2x2':'matrix<int, 2, 2>' cinit
243 //CHECK-NEXT: ImplicitCastExpr {{.*}} 'int2x2':'matrix<int, 2, 2>' <FloatingToIntegral>
244 //CHECK-NEXT: ImplicitCastExpr {{.*}} 'float2x3':'matrix<float, 2, 3>' <LValueToRValue>
245 #ifdef ERROR
246 int3x2 i32 = f23; // expected-error{{cannot initialize a variable of type 'matrix<int, 3, 2>' with an lvalue of type 'matrix<float, 2, 3>'}}
247 fn3x2(f23); // expected-error{{no matching function for call to 'fn3x2'}}
248 #endif
249
250 fn2x2(f23);
251 // expected-warning@-1{{implicit conversion truncates matrix: 'float2x3' (aka 'matrix<float, 2, 3>') to 'matrix<float, 2, 2>'}}
252 //CHECK: DeclRefExpr {{.*}} 'void (float2x2)' lvalue Function {{.*}} 'fn2x2' 'void (float2x2)'
253 //CHECK-NEXT: ImplicitCastExpr {{.*}} 'matrix<float, 2, 2>' <HLSLMatrixTruncation>
254 //CHECK-NEXT: ImplicitCastExpr {{.*}} 'float2x3':'matrix<float, 2, 3>' <LValueToRValue>
255
256 #ifdef ERROR
257 fn2x2IO(f23); // expected-error{{assigning to 'matrix<[2 * ...], 3>' from incompatible type 'matrix<[2 * ...], 2>'}}
258 fnI2x2IO(f23); // expected-error{{assigning to 'matrix<float, [...], 3>' from incompatible type 'matrix<int, [...], 2>'}}
259 #endif
260
261 matOrVec(f23);
262 // expected-warning@-1{{implicit conversion truncates matrix: 'float2x3' (aka 'matrix<float, 2, 3>') to 'matrix<float, 2, 2>'}}
263 //CHECK: DeclRefExpr {{.*}} 'void (float2x2)' lvalue Function {{.*}} 'matOrVec' 'void (float2x2)'
264 //CHECK-NEXT: ImplicitCastExpr {{.*}} 'matrix<float, 2, 2>' <HLSLMatrixTruncation>
265 //CHECK-NEXT: ImplicitCastExpr {{.*}} 'float2x3':'matrix<float, 2, 3>' <LValueToRValue>
266
267 matOrVec(f44);
268 // expected-warning@-1{{implicit conversion truncates matrix: 'float4x4' (aka 'matrix<float, 4, 4>') to 'matrix<float, 2, 2>'}}
269 //CHECK: DeclRefExpr {{.*}} 'void (float2x2)' lvalue Function {{.*}} 'matOrVec' 'void (float2x2)'
270 //CHECK-NEXT: ImplicitCastExpr {{.*}} 'matrix<float, 2, 2>' <HLSLMatrixTruncation>
271 //CHECK-NEXT: ImplicitCastExpr {{.*}} 'float4x4':'matrix<float, 4, 4>' <LValueToRValue>
272
273 #ifdef ERROR
274 matOrVec(2.0); // expected-error {{call to 'matOrVec' is ambiguous}}
275 #endif
276 matOrVec3(3.14);
277 //CHECK: ImplicitCastExpr {{.*}} 'float4x4':'matrix<float, 4, 4>' <HLSLAggregateSplatCast>
278 //CHECK-NEXT: FloatingLiteral {{.*}} <col:13> 'float' 3.140000e+00
279
280 matOrVec2(f23);
281 //CHECK: DeclRefExpr {{.*}} 'void (float2x3)' lvalue Function {{.*}} 'matOrVec2' 'void (float2x3)'
282 //CHECK-NEXT: ImplicitCastExpr {{.*}} 'float2x3':'matrix<float, 2, 3>' <LValueToRValue>
283
284 matOrVec2(f44);
285 // expected-warning@-1{{implicit conversion truncates matrix: 'float4x4' (aka 'matrix<float, 4, 4>') to 'matrix<float, 2, 3>'}}
286 //CHECK: DeclRefExpr {{.*}} 'void (float2x3)' lvalue Function {{.*}} 'matOrVec2' 'void (float2x3)'
287 //CHECK-NEXT: ImplicitCastExpr {{.*}} 'matrix<float, 2, 3>' <HLSLMatrixTruncation>
288 //CHECK-NEXT: ImplicitCastExpr {{.*}} 'float4x4':'matrix<float, 4, 4>' <LValueToRValue>
289
290 matOrVec2(f33);
291 // expected-warning@-1{{implicit conversion truncates matrix: 'float3x3' (aka 'matrix<float, 3, 3>') to 'matrix<float, 2, 3>'}}
292 //CHECK: DeclRefExpr {{.*}} 'void (float2x3)' lvalue Function {{.*}} 'matOrVec2' 'void (float2x3)'
293 //CHECK-NEXT: ImplicitCastExpr {{.*}} 'matrix<float, 2, 3>' <HLSLMatrixTruncation>
294 //CHECK-NEXT: ImplicitCastExpr {{.*}} 'float3x3':'matrix<float, 3, 3>' <LValueToRValue>
295
296 #ifdef ERROR
297 matOrVec2(f32); // expected-error{{no matching function for call to 'matOrVec2'}}
298 #endif
299 }