blob: cb3608747ae6fdf84dfe030317b591cb08341460 [file] [log] [blame]
Chris Forbescc5697f2019-01-30 11:54:08 -08001// Copyright (c) 2018 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "source/opt/const_folding_rules.h"
16
17#include "source/opt/ir_context.h"
18
19namespace spvtools {
20namespace opt {
21namespace {
22
23const uint32_t kExtractCompositeIdInIdx = 0;
24
Nicolas Capens6cacf182021-11-30 11:15:46 -050025// Returns a constants with the value NaN of the given type. Only works for
26// 32-bit and 64-bit float point types. Returns |nullptr| if an error occurs.
27const analysis::Constant* GetNan(const analysis::Type* type,
28 analysis::ConstantManager* const_mgr) {
29 const analysis::Float* float_type = type->AsFloat();
30 if (float_type == nullptr) {
31 return nullptr;
32 }
33
34 switch (float_type->width()) {
35 case 32:
36 return const_mgr->GetFloatConst(std::numeric_limits<float>::quiet_NaN());
37 case 64:
38 return const_mgr->GetDoubleConst(
39 std::numeric_limits<double>::quiet_NaN());
40 default:
41 return nullptr;
42 }
43}
44
45// Returns a constants with the value INF of the given type. Only works for
46// 32-bit and 64-bit float point types. Returns |nullptr| if an error occurs.
47const analysis::Constant* GetInf(const analysis::Type* type,
48 analysis::ConstantManager* const_mgr) {
49 const analysis::Float* float_type = type->AsFloat();
50 if (float_type == nullptr) {
51 return nullptr;
52 }
53
54 switch (float_type->width()) {
55 case 32:
56 return const_mgr->GetFloatConst(std::numeric_limits<float>::infinity());
57 case 64:
58 return const_mgr->GetDoubleConst(std::numeric_limits<double>::infinity());
59 default:
60 return nullptr;
61 }
62}
63
Chris Forbescc5697f2019-01-30 11:54:08 -080064// Returns true if |type| is Float or a vector of Float.
65bool HasFloatingPoint(const analysis::Type* type) {
66 if (type->AsFloat()) {
67 return true;
68 } else if (const analysis::Vector* vec_type = type->AsVector()) {
69 return vec_type->element_type()->AsFloat() != nullptr;
70 }
71
72 return false;
73}
74
Nicolas Capens6cacf182021-11-30 11:15:46 -050075// Returns a constants with the value |-val| of the given type. Only works for
76// 32-bit and 64-bit float point types. Returns |nullptr| if an error occurs.
77const analysis::Constant* negateFPConst(const analysis::Type* result_type,
78 const analysis::Constant* val,
79 analysis::ConstantManager* const_mgr) {
80 const analysis::Float* float_type = result_type->AsFloat();
81 assert(float_type != nullptr);
82 if (float_type->width() == 32) {
83 float fa = val->GetFloat();
84 return const_mgr->GetFloatConst(-fa);
85 } else if (float_type->width() == 64) {
86 double da = val->GetDouble();
87 return const_mgr->GetDoubleConst(-da);
88 }
89 return nullptr;
90}
91
Chris Forbescc5697f2019-01-30 11:54:08 -080092// Folds an OpcompositeExtract where input is a composite constant.
93ConstantFoldingRule FoldExtractWithConstants() {
94 return [](IRContext* context, Instruction* inst,
95 const std::vector<const analysis::Constant*>& constants)
96 -> const analysis::Constant* {
97 const analysis::Constant* c = constants[kExtractCompositeIdInIdx];
98 if (c == nullptr) {
99 return nullptr;
100 }
101
102 for (uint32_t i = 1; i < inst->NumInOperands(); ++i) {
103 uint32_t element_index = inst->GetSingleWordInOperand(i);
104 if (c->AsNullConstant()) {
105 // Return Null for the return type.
106 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
107 analysis::TypeManager* type_mgr = context->get_type_mgr();
108 return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), {});
109 }
110
111 auto cc = c->AsCompositeConstant();
112 assert(cc != nullptr);
113 auto components = cc->GetComponents();
Ben Claytond0f684e2019-08-30 22:36:08 +0100114 // Protect against invalid IR. Refuse to fold if the index is out
115 // of bounds.
116 if (element_index >= components.size()) return nullptr;
Chris Forbescc5697f2019-01-30 11:54:08 -0800117 c = components[element_index];
118 }
119 return c;
120 };
121}
122
123ConstantFoldingRule FoldVectorShuffleWithConstants() {
124 return [](IRContext* context, Instruction* inst,
125 const std::vector<const analysis::Constant*>& constants)
126 -> const analysis::Constant* {
127 assert(inst->opcode() == SpvOpVectorShuffle);
128 const analysis::Constant* c1 = constants[0];
129 const analysis::Constant* c2 = constants[1];
130 if (c1 == nullptr || c2 == nullptr) {
131 return nullptr;
132 }
133
134 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
135 const analysis::Type* element_type = c1->type()->AsVector()->element_type();
136
137 std::vector<const analysis::Constant*> c1_components;
138 if (const analysis::VectorConstant* vec_const = c1->AsVectorConstant()) {
139 c1_components = vec_const->GetComponents();
140 } else {
141 assert(c1->AsNullConstant());
142 const analysis::Constant* element =
143 const_mgr->GetConstant(element_type, {});
144 c1_components.resize(c1->type()->AsVector()->element_count(), element);
145 }
146 std::vector<const analysis::Constant*> c2_components;
147 if (const analysis::VectorConstant* vec_const = c2->AsVectorConstant()) {
148 c2_components = vec_const->GetComponents();
149 } else {
150 assert(c2->AsNullConstant());
151 const analysis::Constant* element =
152 const_mgr->GetConstant(element_type, {});
153 c2_components.resize(c2->type()->AsVector()->element_count(), element);
154 }
155
156 std::vector<uint32_t> ids;
157 const uint32_t undef_literal_value = 0xffffffff;
158 for (uint32_t i = 2; i < inst->NumInOperands(); ++i) {
159 uint32_t index = inst->GetSingleWordInOperand(i);
160 if (index == undef_literal_value) {
161 // Don't fold shuffle with undef literal value.
162 return nullptr;
163 } else if (index < c1_components.size()) {
164 Instruction* member_inst =
165 const_mgr->GetDefiningInstruction(c1_components[index]);
166 ids.push_back(member_inst->result_id());
167 } else {
168 Instruction* member_inst = const_mgr->GetDefiningInstruction(
169 c2_components[index - c1_components.size()]);
170 ids.push_back(member_inst->result_id());
171 }
172 }
173
174 analysis::TypeManager* type_mgr = context->get_type_mgr();
175 return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), ids);
176 };
177}
178
179ConstantFoldingRule FoldVectorTimesScalar() {
180 return [](IRContext* context, Instruction* inst,
181 const std::vector<const analysis::Constant*>& constants)
182 -> const analysis::Constant* {
183 assert(inst->opcode() == SpvOpVectorTimesScalar);
184 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
185 analysis::TypeManager* type_mgr = context->get_type_mgr();
186
187 if (!inst->IsFloatingPointFoldingAllowed()) {
188 if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
189 return nullptr;
190 }
191 }
192
193 const analysis::Constant* c1 = constants[0];
194 const analysis::Constant* c2 = constants[1];
195
196 if (c1 && c1->IsZero()) {
197 return c1;
198 }
199
200 if (c2 && c2->IsZero()) {
201 // Get or create the NullConstant for this type.
202 std::vector<uint32_t> ids;
203 return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), ids);
204 }
205
206 if (c1 == nullptr || c2 == nullptr) {
207 return nullptr;
208 }
209
210 // Check result type.
211 const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
212 const analysis::Vector* vector_type = result_type->AsVector();
213 assert(vector_type != nullptr);
214 const analysis::Type* element_type = vector_type->element_type();
215 assert(element_type != nullptr);
216 const analysis::Float* float_type = element_type->AsFloat();
217 assert(float_type != nullptr);
218
219 // Check types of c1 and c2.
220 assert(c1->type()->AsVector() == vector_type);
221 assert(c1->type()->AsVector()->element_type() == element_type &&
222 c2->type() == element_type);
223
224 // Get a float vector that is the result of vector-times-scalar.
225 std::vector<const analysis::Constant*> c1_components =
226 c1->GetVectorComponents(const_mgr);
227 std::vector<uint32_t> ids;
228 if (float_type->width() == 32) {
229 float scalar = c2->GetFloat();
230 for (uint32_t i = 0; i < c1_components.size(); ++i) {
231 utils::FloatProxy<float> result(c1_components[i]->GetFloat() * scalar);
232 std::vector<uint32_t> words = result.GetWords();
233 const analysis::Constant* new_elem =
234 const_mgr->GetConstant(float_type, words);
235 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
236 }
237 return const_mgr->GetConstant(vector_type, ids);
238 } else if (float_type->width() == 64) {
239 double scalar = c2->GetDouble();
240 for (uint32_t i = 0; i < c1_components.size(); ++i) {
241 utils::FloatProxy<double> result(c1_components[i]->GetDouble() *
242 scalar);
243 std::vector<uint32_t> words = result.GetWords();
244 const analysis::Constant* new_elem =
245 const_mgr->GetConstant(float_type, words);
246 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
247 }
248 return const_mgr->GetConstant(vector_type, ids);
249 }
250 return nullptr;
251 };
252}
253
Nicolas Capens00a1bcc2022-07-29 16:49:40 -0400254ConstantFoldingRule FoldVectorTimesMatrix() {
255 return [](IRContext* context, Instruction* inst,
256 const std::vector<const analysis::Constant*>& constants)
257 -> const analysis::Constant* {
258 assert(inst->opcode() == SpvOpVectorTimesMatrix);
259 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
260 analysis::TypeManager* type_mgr = context->get_type_mgr();
261
262 if (!inst->IsFloatingPointFoldingAllowed()) {
263 if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
264 return nullptr;
265 }
266 }
267
268 const analysis::Constant* c1 = constants[0];
269 const analysis::Constant* c2 = constants[1];
270
271 if (c1 == nullptr || c2 == nullptr) {
272 return nullptr;
273 }
274
275 // Check result type.
276 const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
277 const analysis::Vector* vector_type = result_type->AsVector();
278 assert(vector_type != nullptr);
279 const analysis::Type* element_type = vector_type->element_type();
280 assert(element_type != nullptr);
281 const analysis::Float* float_type = element_type->AsFloat();
282 assert(float_type != nullptr);
283
284 // Check types of c1 and c2.
285 assert(c1->type()->AsVector() == vector_type);
286 assert(c1->type()->AsVector()->element_type() == element_type &&
287 c2->type()->AsMatrix()->element_type() == vector_type);
288
289 // Get a float vector that is the result of vector-times-matrix.
290 std::vector<const analysis::Constant*> c1_components =
291 c1->GetVectorComponents(const_mgr);
292 std::vector<const analysis::Constant*> c2_components =
293 c2->AsMatrixConstant()->GetComponents();
294 uint32_t resultVectorSize = result_type->AsVector()->element_count();
295
296 std::vector<uint32_t> ids;
297
298 if ((c1 && c1->IsZero()) || (c2 && c2->IsZero())) {
299 std::vector<uint32_t> words(float_type->width() / 32, 0);
300 for (uint32_t i = 0; i < resultVectorSize; ++i) {
301 const analysis::Constant* new_elem =
302 const_mgr->GetConstant(float_type, words);
303 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
304 }
305 return const_mgr->GetConstant(vector_type, ids);
306 }
307
308 if (float_type->width() == 32) {
309 for (uint32_t i = 0; i < resultVectorSize; ++i) {
310 float result_scalar = 0.0f;
311 const analysis::VectorConstant* c2_vec =
312 c2_components[i]->AsVectorConstant();
313 for (uint32_t j = 0; j < c2_vec->GetComponents().size(); ++j) {
314 float c1_scalar = c1_components[j]->GetFloat();
315 float c2_scalar = c2_vec->GetComponents()[j]->GetFloat();
316 result_scalar += c1_scalar * c2_scalar;
317 }
318 utils::FloatProxy<float> result(result_scalar);
319 std::vector<uint32_t> words = result.GetWords();
320 const analysis::Constant* new_elem =
321 const_mgr->GetConstant(float_type, words);
322 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
323 }
324 return const_mgr->GetConstant(vector_type, ids);
325 } else if (float_type->width() == 64) {
326 for (uint32_t i = 0; i < c2_components.size(); ++i) {
327 double result_scalar = 0.0;
328 const analysis::VectorConstant* c2_vec =
329 c2_components[i]->AsVectorConstant();
330 for (uint32_t j = 0; j < c2_vec->GetComponents().size(); ++j) {
331 double c1_scalar = c1_components[j]->GetDouble();
332 double c2_scalar = c2_vec->GetComponents()[j]->GetDouble();
333 result_scalar += c1_scalar * c2_scalar;
334 }
335 utils::FloatProxy<double> result(result_scalar);
336 std::vector<uint32_t> words = result.GetWords();
337 const analysis::Constant* new_elem =
338 const_mgr->GetConstant(float_type, words);
339 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
340 }
341 return const_mgr->GetConstant(vector_type, ids);
342 }
343 return nullptr;
344 };
345}
346
347ConstantFoldingRule FoldMatrixTimesVector() {
348 return [](IRContext* context, Instruction* inst,
349 const std::vector<const analysis::Constant*>& constants)
350 -> const analysis::Constant* {
351 assert(inst->opcode() == SpvOpMatrixTimesVector);
352 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
353 analysis::TypeManager* type_mgr = context->get_type_mgr();
354
355 if (!inst->IsFloatingPointFoldingAllowed()) {
356 if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
357 return nullptr;
358 }
359 }
360
361 const analysis::Constant* c1 = constants[0];
362 const analysis::Constant* c2 = constants[1];
363
364 if (c1 == nullptr || c2 == nullptr) {
365 return nullptr;
366 }
367
368 // Check result type.
369 const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
370 const analysis::Vector* vector_type = result_type->AsVector();
371 assert(vector_type != nullptr);
372 const analysis::Type* element_type = vector_type->element_type();
373 assert(element_type != nullptr);
374 const analysis::Float* float_type = element_type->AsFloat();
375 assert(float_type != nullptr);
376
377 // Check types of c1 and c2.
378 assert(c1->type()->AsMatrix()->element_type() == vector_type);
379 assert(c2->type()->AsVector()->element_type() == element_type);
380
381 // Get a float vector that is the result of matrix-times-vector.
382 std::vector<const analysis::Constant*> c1_components =
383 c1->AsMatrixConstant()->GetComponents();
384 std::vector<const analysis::Constant*> c2_components =
385 c2->GetVectorComponents(const_mgr);
386 uint32_t resultVectorSize = result_type->AsVector()->element_count();
387
388 std::vector<uint32_t> ids;
389
390 if ((c1 && c1->IsZero()) || (c2 && c2->IsZero())) {
391 std::vector<uint32_t> words(float_type->width() / 32, 0);
392 for (uint32_t i = 0; i < resultVectorSize; ++i) {
393 const analysis::Constant* new_elem =
394 const_mgr->GetConstant(float_type, words);
395 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
396 }
397 return const_mgr->GetConstant(vector_type, ids);
398 }
399
400 if (float_type->width() == 32) {
401 for (uint32_t i = 0; i < resultVectorSize; ++i) {
402 float result_scalar = 0.0f;
403 for (uint32_t j = 0; j < c1_components.size(); ++j) {
404 float c1_scalar = c1_components[j]
405 ->AsVectorConstant()
406 ->GetComponents()[i]
407 ->GetFloat();
408 float c2_scalar = c2_components[j]->GetFloat();
409 result_scalar += c1_scalar * c2_scalar;
410 }
411 utils::FloatProxy<float> result(result_scalar);
412 std::vector<uint32_t> words = result.GetWords();
413 const analysis::Constant* new_elem =
414 const_mgr->GetConstant(float_type, words);
415 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
416 }
417 return const_mgr->GetConstant(vector_type, ids);
418 } else if (float_type->width() == 64) {
419 for (uint32_t i = 0; i < resultVectorSize; ++i) {
420 double result_scalar = 0.0;
421 for (uint32_t j = 0; j < c1_components.size(); ++j) {
422 double c1_scalar = c1_components[j]
423 ->AsVectorConstant()
424 ->GetComponents()[i]
425 ->GetDouble();
426 double c2_scalar = c2_components[j]->GetDouble();
427 result_scalar += c1_scalar * c2_scalar;
428 }
429 utils::FloatProxy<double> result(result_scalar);
430 std::vector<uint32_t> words = result.GetWords();
431 const analysis::Constant* new_elem =
432 const_mgr->GetConstant(float_type, words);
433 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
434 }
435 return const_mgr->GetConstant(vector_type, ids);
436 }
437 return nullptr;
438 };
439}
440
Chris Forbescc5697f2019-01-30 11:54:08 -0800441ConstantFoldingRule FoldCompositeWithConstants() {
442 // Folds an OpCompositeConstruct where all of the inputs are constants to a
443 // constant. A new constant is created if necessary.
444 return [](IRContext* context, Instruction* inst,
445 const std::vector<const analysis::Constant*>& constants)
446 -> const analysis::Constant* {
447 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
448 analysis::TypeManager* type_mgr = context->get_type_mgr();
449 const analysis::Type* new_type = type_mgr->GetType(inst->type_id());
450 Instruction* type_inst =
451 context->get_def_use_mgr()->GetDef(inst->type_id());
452
453 std::vector<uint32_t> ids;
454 for (uint32_t i = 0; i < constants.size(); ++i) {
455 const analysis::Constant* element_const = constants[i];
456 if (element_const == nullptr) {
457 return nullptr;
458 }
459
460 uint32_t component_type_id = 0;
461 if (type_inst->opcode() == SpvOpTypeStruct) {
462 component_type_id = type_inst->GetSingleWordInOperand(i);
463 } else if (type_inst->opcode() == SpvOpTypeArray) {
464 component_type_id = type_inst->GetSingleWordInOperand(0);
465 }
466
467 uint32_t element_id =
468 const_mgr->FindDeclaredConstant(element_const, component_type_id);
469 if (element_id == 0) {
470 return nullptr;
471 }
472 ids.push_back(element_id);
473 }
474 return const_mgr->GetConstant(new_type, ids);
475 };
476}
477
478// The interface for a function that returns the result of applying a scalar
479// floating-point binary operation on |a| and |b|. The type of the return value
480// will be |type|. The input constants must also be of type |type|.
481using UnaryScalarFoldingRule = std::function<const analysis::Constant*(
482 const analysis::Type* result_type, const analysis::Constant* a,
483 analysis::ConstantManager*)>;
484
485// The interface for a function that returns the result of applying a scalar
486// floating-point binary operation on |a| and |b|. The type of the return value
487// will be |type|. The input constants must also be of type |type|.
488using BinaryScalarFoldingRule = std::function<const analysis::Constant*(
489 const analysis::Type* result_type, const analysis::Constant* a,
490 const analysis::Constant* b, analysis::ConstantManager*)>;
491
492// Returns a |ConstantFoldingRule| that folds unary floating point scalar ops
493// using |scalar_rule| and unary float point vectors ops by applying
494// |scalar_rule| to the elements of the vector. The |ConstantFoldingRule|
495// that is returned assumes that |constants| contains 1 entry. If they are
496// not |nullptr|, then their type is either |Float| or |Integer| or a |Vector|
497// whose element type is |Float| or |Integer|.
498ConstantFoldingRule FoldFPUnaryOp(UnaryScalarFoldingRule scalar_rule) {
499 return [scalar_rule](IRContext* context, Instruction* inst,
500 const std::vector<const analysis::Constant*>& constants)
501 -> const analysis::Constant* {
502 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
503 analysis::TypeManager* type_mgr = context->get_type_mgr();
504 const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
505 const analysis::Vector* vector_type = result_type->AsVector();
506
507 if (!inst->IsFloatingPointFoldingAllowed()) {
508 return nullptr;
509 }
510
Ben Claytondc6b76a2020-02-24 14:53:40 +0000511 const analysis::Constant* arg =
512 (inst->opcode() == SpvOpExtInst) ? constants[1] : constants[0];
513
514 if (arg == nullptr) {
Chris Forbescc5697f2019-01-30 11:54:08 -0800515 return nullptr;
516 }
517
518 if (vector_type != nullptr) {
519 std::vector<const analysis::Constant*> a_components;
520 std::vector<const analysis::Constant*> results_components;
521
Ben Claytondc6b76a2020-02-24 14:53:40 +0000522 a_components = arg->GetVectorComponents(const_mgr);
Chris Forbescc5697f2019-01-30 11:54:08 -0800523
524 // Fold each component of the vector.
525 for (uint32_t i = 0; i < a_components.size(); ++i) {
526 results_components.push_back(scalar_rule(vector_type->element_type(),
527 a_components[i], const_mgr));
528 if (results_components[i] == nullptr) {
529 return nullptr;
530 }
531 }
532
533 // Build the constant object and return it.
534 std::vector<uint32_t> ids;
535 for (const analysis::Constant* member : results_components) {
536 ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
537 }
538 return const_mgr->GetConstant(vector_type, ids);
539 } else {
Ben Claytondc6b76a2020-02-24 14:53:40 +0000540 return scalar_rule(result_type, arg, const_mgr);
Chris Forbescc5697f2019-01-30 11:54:08 -0800541 }
542 };
543}
544
Ben Claytond552f632019-11-18 11:18:41 +0000545// Returns the result of folding the constants in |constants| according the
546// |scalar_rule|. If |result_type| is a vector, then |scalar_rule| is applied
547// per component.
548const analysis::Constant* FoldFPBinaryOp(
549 BinaryScalarFoldingRule scalar_rule, uint32_t result_type_id,
550 const std::vector<const analysis::Constant*>& constants,
551 IRContext* context) {
552 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
553 analysis::TypeManager* type_mgr = context->get_type_mgr();
554 const analysis::Type* result_type = type_mgr->GetType(result_type_id);
555 const analysis::Vector* vector_type = result_type->AsVector();
556
557 if (constants[0] == nullptr || constants[1] == nullptr) {
558 return nullptr;
559 }
560
561 if (vector_type != nullptr) {
562 std::vector<const analysis::Constant*> a_components;
563 std::vector<const analysis::Constant*> b_components;
564 std::vector<const analysis::Constant*> results_components;
565
566 a_components = constants[0]->GetVectorComponents(const_mgr);
567 b_components = constants[1]->GetVectorComponents(const_mgr);
568
569 // Fold each component of the vector.
570 for (uint32_t i = 0; i < a_components.size(); ++i) {
571 results_components.push_back(scalar_rule(vector_type->element_type(),
572 a_components[i], b_components[i],
573 const_mgr));
574 if (results_components[i] == nullptr) {
575 return nullptr;
576 }
577 }
578
579 // Build the constant object and return it.
580 std::vector<uint32_t> ids;
581 for (const analysis::Constant* member : results_components) {
582 ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
583 }
584 return const_mgr->GetConstant(vector_type, ids);
585 } else {
586 return scalar_rule(result_type, constants[0], constants[1], const_mgr);
587 }
588}
589
Chris Forbescc5697f2019-01-30 11:54:08 -0800590// Returns a |ConstantFoldingRule| that folds floating point scalars using
591// |scalar_rule| and vectors of floating point by applying |scalar_rule| to the
592// elements of the vector. The |ConstantFoldingRule| that is returned assumes
593// that |constants| contains 2 entries. If they are not |nullptr|, then their
594// type is either |Float| or a |Vector| whose element type is |Float|.
595ConstantFoldingRule FoldFPBinaryOp(BinaryScalarFoldingRule scalar_rule) {
596 return [scalar_rule](IRContext* context, Instruction* inst,
597 const std::vector<const analysis::Constant*>& constants)
598 -> const analysis::Constant* {
Chris Forbescc5697f2019-01-30 11:54:08 -0800599 if (!inst->IsFloatingPointFoldingAllowed()) {
600 return nullptr;
601 }
Ben Claytond552f632019-11-18 11:18:41 +0000602 if (inst->opcode() == SpvOpExtInst) {
603 return FoldFPBinaryOp(scalar_rule, inst->type_id(),
604 {constants[1], constants[2]}, context);
Chris Forbescc5697f2019-01-30 11:54:08 -0800605 }
Ben Claytond552f632019-11-18 11:18:41 +0000606 return FoldFPBinaryOp(scalar_rule, inst->type_id(), constants, context);
Chris Forbescc5697f2019-01-30 11:54:08 -0800607 };
608}
609
610// This macro defines a |UnaryScalarFoldingRule| that performs float to
611// integer conversion.
612// TODO(greg-lunarg): Support for 64-bit integer types.
613UnaryScalarFoldingRule FoldFToIOp() {
614 return [](const analysis::Type* result_type, const analysis::Constant* a,
615 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
616 assert(result_type != nullptr && a != nullptr);
617 const analysis::Integer* integer_type = result_type->AsInteger();
618 const analysis::Float* float_type = a->type()->AsFloat();
619 assert(float_type != nullptr);
620 assert(integer_type != nullptr);
621 if (integer_type->width() != 32) return nullptr;
622 if (float_type->width() == 32) {
623 float fa = a->GetFloat();
624 uint32_t result = integer_type->IsSigned()
625 ? static_cast<uint32_t>(static_cast<int32_t>(fa))
626 : static_cast<uint32_t>(fa);
627 std::vector<uint32_t> words = {result};
628 return const_mgr->GetConstant(result_type, words);
629 } else if (float_type->width() == 64) {
630 double fa = a->GetDouble();
631 uint32_t result = integer_type->IsSigned()
632 ? static_cast<uint32_t>(static_cast<int32_t>(fa))
633 : static_cast<uint32_t>(fa);
634 std::vector<uint32_t> words = {result};
635 return const_mgr->GetConstant(result_type, words);
636 }
637 return nullptr;
638 };
639}
640
641// This function defines a |UnaryScalarFoldingRule| that performs integer to
642// float conversion.
643// TODO(greg-lunarg): Support for 64-bit integer types.
644UnaryScalarFoldingRule FoldIToFOp() {
645 return [](const analysis::Type* result_type, const analysis::Constant* a,
646 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
647 assert(result_type != nullptr && a != nullptr);
648 const analysis::Integer* integer_type = a->type()->AsInteger();
649 const analysis::Float* float_type = result_type->AsFloat();
650 assert(float_type != nullptr);
651 assert(integer_type != nullptr);
652 if (integer_type->width() != 32) return nullptr;
653 uint32_t ua = a->GetU32();
654 if (float_type->width() == 32) {
655 float result_val = integer_type->IsSigned()
656 ? static_cast<float>(static_cast<int32_t>(ua))
657 : static_cast<float>(ua);
658 utils::FloatProxy<float> result(result_val);
659 std::vector<uint32_t> words = {result.data()};
660 return const_mgr->GetConstant(result_type, words);
661 } else if (float_type->width() == 64) {
662 double result_val = integer_type->IsSigned()
663 ? static_cast<double>(static_cast<int32_t>(ua))
664 : static_cast<double>(ua);
665 utils::FloatProxy<double> result(result_val);
666 std::vector<uint32_t> words = result.GetWords();
667 return const_mgr->GetConstant(result_type, words);
668 }
669 return nullptr;
670 };
671}
672
Ben Claytonb73b7602019-07-29 13:56:13 +0100673// This defines a |UnaryScalarFoldingRule| that performs |OpQuantizeToF16|.
674UnaryScalarFoldingRule FoldQuantizeToF16Scalar() {
675 return [](const analysis::Type* result_type, const analysis::Constant* a,
676 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
677 assert(result_type != nullptr && a != nullptr);
678 const analysis::Float* float_type = a->type()->AsFloat();
679 assert(float_type != nullptr);
680 if (float_type->width() != 32) {
681 return nullptr;
682 }
683
684 float fa = a->GetFloat();
685 utils::HexFloat<utils::FloatProxy<float>> orignal(fa);
686 utils::HexFloat<utils::FloatProxy<utils::Float16>> quantized(0);
687 utils::HexFloat<utils::FloatProxy<float>> result(0.0f);
688 orignal.castTo(quantized, utils::round_direction::kToZero);
689 quantized.castTo(result, utils::round_direction::kToZero);
690 std::vector<uint32_t> words = {result.getBits()};
691 return const_mgr->GetConstant(result_type, words);
692 };
693}
694
Chris Forbescc5697f2019-01-30 11:54:08 -0800695// This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
696// operator |op| must work for both float and double, and use syntax "f1 op f2".
Ben Claytond552f632019-11-18 11:18:41 +0000697#define FOLD_FPARITH_OP(op) \
698 [](const analysis::Type* result_type_in_macro, const analysis::Constant* a, \
699 const analysis::Constant* b, \
700 analysis::ConstantManager* const_mgr_in_macro) \
701 -> const analysis::Constant* { \
702 assert(result_type_in_macro != nullptr && a != nullptr && b != nullptr); \
703 assert(result_type_in_macro == a->type() && \
704 result_type_in_macro == b->type()); \
705 const analysis::Float* float_type_in_macro = \
706 result_type_in_macro->AsFloat(); \
707 assert(float_type_in_macro != nullptr); \
708 if (float_type_in_macro->width() == 32) { \
709 float fa = a->GetFloat(); \
710 float fb = b->GetFloat(); \
711 utils::FloatProxy<float> result_in_macro(fa op fb); \
712 std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
713 return const_mgr_in_macro->GetConstant(result_type_in_macro, \
714 words_in_macro); \
715 } else if (float_type_in_macro->width() == 64) { \
716 double fa = a->GetDouble(); \
717 double fb = b->GetDouble(); \
718 utils::FloatProxy<double> result_in_macro(fa op fb); \
719 std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
720 return const_mgr_in_macro->GetConstant(result_type_in_macro, \
721 words_in_macro); \
722 } \
723 return nullptr; \
Chris Forbescc5697f2019-01-30 11:54:08 -0800724 }
725
726// Define the folding rule for conversion between floating point and integer
727ConstantFoldingRule FoldFToI() { return FoldFPUnaryOp(FoldFToIOp()); }
728ConstantFoldingRule FoldIToF() { return FoldFPUnaryOp(FoldIToFOp()); }
Ben Claytonb73b7602019-07-29 13:56:13 +0100729ConstantFoldingRule FoldQuantizeToF16() {
730 return FoldFPUnaryOp(FoldQuantizeToF16Scalar());
731}
Chris Forbescc5697f2019-01-30 11:54:08 -0800732
733// Define the folding rules for subtraction, addition, multiplication, and
734// division for floating point values.
735ConstantFoldingRule FoldFSub() { return FoldFPBinaryOp(FOLD_FPARITH_OP(-)); }
736ConstantFoldingRule FoldFAdd() { return FoldFPBinaryOp(FOLD_FPARITH_OP(+)); }
737ConstantFoldingRule FoldFMul() { return FoldFPBinaryOp(FOLD_FPARITH_OP(*)); }
Nicolas Capens6cacf182021-11-30 11:15:46 -0500738
739// Returns the constant that results from evaluating |numerator| / 0.0. Returns
sugoi1b398bf32022-02-18 10:27:28 -0500740// |nullptr| if the result could not be evaluated.
Nicolas Capens6cacf182021-11-30 11:15:46 -0500741const analysis::Constant* FoldFPScalarDivideByZero(
742 const analysis::Type* result_type, const analysis::Constant* numerator,
743 analysis::ConstantManager* const_mgr) {
744 if (numerator == nullptr) {
745 return nullptr;
746 }
747
748 if (numerator->IsZero()) {
749 return GetNan(result_type, const_mgr);
750 }
751
752 const analysis::Constant* result = GetInf(result_type, const_mgr);
753 if (result == nullptr) {
754 return nullptr;
755 }
756
757 if (numerator->AsFloatConstant()->GetValueAsDouble() < 0.0) {
758 result = negateFPConst(result_type, result, const_mgr);
759 }
760 return result;
761}
762
763// Returns the result of folding |numerator| / |denominator|. Returns |nullptr|
764// if it cannot be folded.
765const analysis::Constant* FoldScalarFPDivide(
766 const analysis::Type* result_type, const analysis::Constant* numerator,
767 const analysis::Constant* denominator,
768 analysis::ConstantManager* const_mgr) {
769 if (denominator == nullptr) {
770 return nullptr;
771 }
772
773 if (denominator->IsZero()) {
774 return FoldFPScalarDivideByZero(result_type, numerator, const_mgr);
775 }
776
777 const analysis::FloatConstant* denominator_float =
778 denominator->AsFloatConstant();
779 if (denominator_float && denominator->GetValueAsDouble() == -0.0) {
780 const analysis::Constant* result =
781 FoldFPScalarDivideByZero(result_type, numerator, const_mgr);
782 if (result != nullptr)
783 result = negateFPConst(result_type, result, const_mgr);
784 return result;
785 } else {
786 return FOLD_FPARITH_OP(/)(result_type, numerator, denominator, const_mgr);
787 }
788}
789
790// Returns the constant folding rule to fold |OpFDiv| with two constants.
791ConstantFoldingRule FoldFDiv() { return FoldFPBinaryOp(FoldScalarFPDivide); }
Chris Forbescc5697f2019-01-30 11:54:08 -0800792
793bool CompareFloatingPoint(bool op_result, bool op_unordered,
794 bool need_ordered) {
795 if (need_ordered) {
796 // operands are ordered and Operand 1 is |op| Operand 2
797 return !op_unordered && op_result;
798 } else {
799 // operands are unordered or Operand 1 is |op| Operand 2
800 return op_unordered || op_result;
801 }
802}
803
804// This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
805// operator |op| must work for both float and double, and use syntax "f1 op f2".
806#define FOLD_FPCMP_OP(op, ord) \
807 [](const analysis::Type* result_type, const analysis::Constant* a, \
808 const analysis::Constant* b, \
809 analysis::ConstantManager* const_mgr) -> const analysis::Constant* { \
810 assert(result_type != nullptr && a != nullptr && b != nullptr); \
811 assert(result_type->AsBool()); \
812 assert(a->type() == b->type()); \
813 const analysis::Float* float_type = a->type()->AsFloat(); \
814 assert(float_type != nullptr); \
815 if (float_type->width() == 32) { \
816 float fa = a->GetFloat(); \
817 float fb = b->GetFloat(); \
818 bool result = CompareFloatingPoint( \
819 fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
820 std::vector<uint32_t> words = {uint32_t(result)}; \
821 return const_mgr->GetConstant(result_type, words); \
822 } else if (float_type->width() == 64) { \
823 double fa = a->GetDouble(); \
824 double fb = b->GetDouble(); \
825 bool result = CompareFloatingPoint( \
826 fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
827 std::vector<uint32_t> words = {uint32_t(result)}; \
828 return const_mgr->GetConstant(result_type, words); \
829 } \
830 return nullptr; \
831 }
832
833// Define the folding rules for ordered and unordered comparison for floating
834// point values.
835ConstantFoldingRule FoldFOrdEqual() {
836 return FoldFPBinaryOp(FOLD_FPCMP_OP(==, true));
837}
838ConstantFoldingRule FoldFUnordEqual() {
839 return FoldFPBinaryOp(FOLD_FPCMP_OP(==, false));
840}
841ConstantFoldingRule FoldFOrdNotEqual() {
842 return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, true));
843}
844ConstantFoldingRule FoldFUnordNotEqual() {
845 return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, false));
846}
847ConstantFoldingRule FoldFOrdLessThan() {
848 return FoldFPBinaryOp(FOLD_FPCMP_OP(<, true));
849}
850ConstantFoldingRule FoldFUnordLessThan() {
851 return FoldFPBinaryOp(FOLD_FPCMP_OP(<, false));
852}
853ConstantFoldingRule FoldFOrdGreaterThan() {
854 return FoldFPBinaryOp(FOLD_FPCMP_OP(>, true));
855}
856ConstantFoldingRule FoldFUnordGreaterThan() {
857 return FoldFPBinaryOp(FOLD_FPCMP_OP(>, false));
858}
859ConstantFoldingRule FoldFOrdLessThanEqual() {
860 return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, true));
861}
862ConstantFoldingRule FoldFUnordLessThanEqual() {
863 return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, false));
864}
865ConstantFoldingRule FoldFOrdGreaterThanEqual() {
866 return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, true));
867}
868ConstantFoldingRule FoldFUnordGreaterThanEqual() {
869 return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, false));
870}
871
872// Folds an OpDot where all of the inputs are constants to a
873// constant. A new constant is created if necessary.
874ConstantFoldingRule FoldOpDotWithConstants() {
875 return [](IRContext* context, Instruction* inst,
876 const std::vector<const analysis::Constant*>& constants)
877 -> const analysis::Constant* {
878 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
879 analysis::TypeManager* type_mgr = context->get_type_mgr();
880 const analysis::Type* new_type = type_mgr->GetType(inst->type_id());
881 assert(new_type->AsFloat() && "OpDot should have a float return type.");
882 const analysis::Float* float_type = new_type->AsFloat();
883
884 if (!inst->IsFloatingPointFoldingAllowed()) {
885 return nullptr;
886 }
887
888 // If one of the operands is 0, then the result is 0.
889 bool has_zero_operand = false;
890
891 for (int i = 0; i < 2; ++i) {
892 if (constants[i]) {
893 if (constants[i]->AsNullConstant() ||
894 constants[i]->AsVectorConstant()->IsZero()) {
895 has_zero_operand = true;
896 break;
897 }
898 }
899 }
900
901 if (has_zero_operand) {
902 if (float_type->width() == 32) {
903 utils::FloatProxy<float> result(0.0f);
904 std::vector<uint32_t> words = result.GetWords();
905 return const_mgr->GetConstant(float_type, words);
906 }
907 if (float_type->width() == 64) {
908 utils::FloatProxy<double> result(0.0);
909 std::vector<uint32_t> words = result.GetWords();
910 return const_mgr->GetConstant(float_type, words);
911 }
912 return nullptr;
913 }
914
915 if (constants[0] == nullptr || constants[1] == nullptr) {
916 return nullptr;
917 }
918
919 std::vector<const analysis::Constant*> a_components;
920 std::vector<const analysis::Constant*> b_components;
921
922 a_components = constants[0]->GetVectorComponents(const_mgr);
923 b_components = constants[1]->GetVectorComponents(const_mgr);
924
925 utils::FloatProxy<double> result(0.0);
926 std::vector<uint32_t> words = result.GetWords();
927 const analysis::Constant* result_const =
928 const_mgr->GetConstant(float_type, words);
Ben Claytonb73b7602019-07-29 13:56:13 +0100929 for (uint32_t i = 0; i < a_components.size() && result_const != nullptr;
930 ++i) {
Chris Forbescc5697f2019-01-30 11:54:08 -0800931 if (a_components[i] == nullptr || b_components[i] == nullptr) {
932 return nullptr;
933 }
934
935 const analysis::Constant* component = FOLD_FPARITH_OP(*)(
936 new_type, a_components[i], b_components[i], const_mgr);
Ben Claytonb73b7602019-07-29 13:56:13 +0100937 if (component == nullptr) {
938 return nullptr;
939 }
Chris Forbescc5697f2019-01-30 11:54:08 -0800940 result_const =
941 FOLD_FPARITH_OP(+)(new_type, result_const, component, const_mgr);
942 }
943 return result_const;
944 };
945}
946
947// This function defines a |UnaryScalarFoldingRule| that subtracts the constant
948// from zero.
949UnaryScalarFoldingRule FoldFNegateOp() {
950 return [](const analysis::Type* result_type, const analysis::Constant* a,
951 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
952 assert(result_type != nullptr && a != nullptr);
953 assert(result_type == a->type());
Nicolas Capens6cacf182021-11-30 11:15:46 -0500954 return negateFPConst(result_type, a, const_mgr);
Chris Forbescc5697f2019-01-30 11:54:08 -0800955 };
956}
957
958ConstantFoldingRule FoldFNegate() { return FoldFPUnaryOp(FoldFNegateOp()); }
959
960ConstantFoldingRule FoldFClampFeedingCompare(uint32_t cmp_opcode) {
961 return [cmp_opcode](IRContext* context, Instruction* inst,
962 const std::vector<const analysis::Constant*>& constants)
963 -> const analysis::Constant* {
964 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
965 analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
966
967 if (!inst->IsFloatingPointFoldingAllowed()) {
968 return nullptr;
969 }
970
971 uint32_t non_const_idx = (constants[0] ? 1 : 0);
972 uint32_t operand_id = inst->GetSingleWordInOperand(non_const_idx);
973 Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
974
975 analysis::TypeManager* type_mgr = context->get_type_mgr();
976 const analysis::Type* operand_type =
977 type_mgr->GetType(operand_inst->type_id());
978
979 if (!operand_type->AsFloat()) {
980 return nullptr;
981 }
982
983 if (operand_type->AsFloat()->width() != 32 &&
984 operand_type->AsFloat()->width() != 64) {
985 return nullptr;
986 }
987
988 if (operand_inst->opcode() != SpvOpExtInst) {
989 return nullptr;
990 }
991
992 if (operand_inst->GetSingleWordInOperand(1) != GLSLstd450FClamp) {
993 return nullptr;
994 }
995
996 if (constants[1] == nullptr && constants[0] == nullptr) {
997 return nullptr;
998 }
999
1000 uint32_t max_id = operand_inst->GetSingleWordInOperand(4);
1001 const analysis::Constant* max_const =
1002 const_mgr->FindDeclaredConstant(max_id);
1003
1004 uint32_t min_id = operand_inst->GetSingleWordInOperand(3);
1005 const analysis::Constant* min_const =
1006 const_mgr->FindDeclaredConstant(min_id);
1007
1008 bool found_result = false;
1009 bool result = false;
1010
1011 switch (cmp_opcode) {
1012 case SpvOpFOrdLessThan:
1013 case SpvOpFUnordLessThan:
1014 case SpvOpFOrdGreaterThanEqual:
1015 case SpvOpFUnordGreaterThanEqual:
1016 if (constants[0]) {
1017 if (min_const) {
1018 if (constants[0]->GetValueAsDouble() <
1019 min_const->GetValueAsDouble()) {
1020 found_result = true;
1021 result = (cmp_opcode == SpvOpFOrdLessThan ||
1022 cmp_opcode == SpvOpFUnordLessThan);
1023 }
1024 }
1025 if (max_const) {
1026 if (constants[0]->GetValueAsDouble() >=
1027 max_const->GetValueAsDouble()) {
1028 found_result = true;
1029 result = !(cmp_opcode == SpvOpFOrdLessThan ||
1030 cmp_opcode == SpvOpFUnordLessThan);
1031 }
1032 }
1033 }
1034
1035 if (constants[1]) {
1036 if (max_const) {
1037 if (max_const->GetValueAsDouble() <
1038 constants[1]->GetValueAsDouble()) {
1039 found_result = true;
1040 result = (cmp_opcode == SpvOpFOrdLessThan ||
1041 cmp_opcode == SpvOpFUnordLessThan);
1042 }
1043 }
1044
1045 if (min_const) {
1046 if (min_const->GetValueAsDouble() >=
1047 constants[1]->GetValueAsDouble()) {
1048 found_result = true;
1049 result = !(cmp_opcode == SpvOpFOrdLessThan ||
1050 cmp_opcode == SpvOpFUnordLessThan);
1051 }
1052 }
1053 }
1054 break;
1055 case SpvOpFOrdGreaterThan:
1056 case SpvOpFUnordGreaterThan:
1057 case SpvOpFOrdLessThanEqual:
1058 case SpvOpFUnordLessThanEqual:
1059 if (constants[0]) {
1060 if (min_const) {
1061 if (constants[0]->GetValueAsDouble() <=
1062 min_const->GetValueAsDouble()) {
1063 found_result = true;
1064 result = (cmp_opcode == SpvOpFOrdLessThanEqual ||
1065 cmp_opcode == SpvOpFUnordLessThanEqual);
1066 }
1067 }
1068 if (max_const) {
1069 if (constants[0]->GetValueAsDouble() >
1070 max_const->GetValueAsDouble()) {
1071 found_result = true;
1072 result = !(cmp_opcode == SpvOpFOrdLessThanEqual ||
1073 cmp_opcode == SpvOpFUnordLessThanEqual);
1074 }
1075 }
1076 }
1077
1078 if (constants[1]) {
1079 if (max_const) {
1080 if (max_const->GetValueAsDouble() <=
1081 constants[1]->GetValueAsDouble()) {
1082 found_result = true;
1083 result = (cmp_opcode == SpvOpFOrdLessThanEqual ||
1084 cmp_opcode == SpvOpFUnordLessThanEqual);
1085 }
1086 }
1087
1088 if (min_const) {
1089 if (min_const->GetValueAsDouble() >
1090 constants[1]->GetValueAsDouble()) {
1091 found_result = true;
1092 result = !(cmp_opcode == SpvOpFOrdLessThanEqual ||
1093 cmp_opcode == SpvOpFUnordLessThanEqual);
1094 }
1095 }
1096 }
1097 break;
1098 default:
1099 return nullptr;
1100 }
1101
1102 if (!found_result) {
1103 return nullptr;
1104 }
1105
1106 const analysis::Type* bool_type =
1107 context->get_type_mgr()->GetType(inst->type_id());
1108 const analysis::Constant* result_const =
1109 const_mgr->GetConstant(bool_type, {static_cast<uint32_t>(result)});
1110 assert(result_const);
1111 return result_const;
1112 };
1113}
1114
Ben Claytond0f684e2019-08-30 22:36:08 +01001115ConstantFoldingRule FoldFMix() {
1116 return [](IRContext* context, Instruction* inst,
1117 const std::vector<const analysis::Constant*>& constants)
1118 -> const analysis::Constant* {
1119 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
1120 assert(inst->opcode() == SpvOpExtInst &&
1121 "Expecting an extended instruction.");
1122 assert(inst->GetSingleWordInOperand(0) ==
1123 context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
1124 "Expecting a GLSLstd450 extended instruction.");
1125 assert(inst->GetSingleWordInOperand(1) == GLSLstd450FMix &&
1126 "Expecting and FMix instruction.");
1127
1128 if (!inst->IsFloatingPointFoldingAllowed()) {
1129 return nullptr;
1130 }
1131
1132 // Make sure all FMix operands are constants.
1133 for (uint32_t i = 1; i < 4; i++) {
1134 if (constants[i] == nullptr) {
1135 return nullptr;
1136 }
1137 }
1138
1139 const analysis::Constant* one;
Ben Claytond552f632019-11-18 11:18:41 +00001140 bool is_vector = false;
1141 const analysis::Type* result_type = constants[1]->type();
1142 const analysis::Type* base_type = result_type;
1143 if (base_type->AsVector()) {
1144 is_vector = true;
1145 base_type = base_type->AsVector()->element_type();
1146 }
1147 assert(base_type->AsFloat() != nullptr &&
1148 "FMix is suppose to act on floats or vectors of floats.");
1149
1150 if (base_type->AsFloat()->width() == 32) {
1151 one = const_mgr->GetConstant(base_type,
Ben Claytond0f684e2019-08-30 22:36:08 +01001152 utils::FloatProxy<float>(1.0f).GetWords());
1153 } else {
Ben Claytond552f632019-11-18 11:18:41 +00001154 one = const_mgr->GetConstant(base_type,
Ben Claytond0f684e2019-08-30 22:36:08 +01001155 utils::FloatProxy<double>(1.0).GetWords());
1156 }
1157
Ben Claytond552f632019-11-18 11:18:41 +00001158 if (is_vector) {
1159 uint32_t one_id = const_mgr->GetDefiningInstruction(one)->result_id();
1160 one =
1161 const_mgr->GetConstant(result_type, std::vector<uint32_t>(4, one_id));
1162 }
1163
1164 const analysis::Constant* temp1 = FoldFPBinaryOp(
1165 FOLD_FPARITH_OP(-), inst->type_id(), {one, constants[3]}, context);
Ben Claytond0f684e2019-08-30 22:36:08 +01001166 if (temp1 == nullptr) {
1167 return nullptr;
1168 }
1169
Ben Claytond552f632019-11-18 11:18:41 +00001170 const analysis::Constant* temp2 = FoldFPBinaryOp(
1171 FOLD_FPARITH_OP(*), inst->type_id(), {constants[1], temp1}, context);
Ben Claytond0f684e2019-08-30 22:36:08 +01001172 if (temp2 == nullptr) {
1173 return nullptr;
1174 }
Ben Claytond552f632019-11-18 11:18:41 +00001175 const analysis::Constant* temp3 =
1176 FoldFPBinaryOp(FOLD_FPARITH_OP(*), inst->type_id(),
1177 {constants[2], constants[3]}, context);
Ben Claytond0f684e2019-08-30 22:36:08 +01001178 if (temp3 == nullptr) {
1179 return nullptr;
1180 }
Ben Claytond552f632019-11-18 11:18:41 +00001181 return FoldFPBinaryOp(FOLD_FPARITH_OP(+), inst->type_id(), {temp2, temp3},
1182 context);
Ben Claytond0f684e2019-08-30 22:36:08 +01001183 };
1184}
1185
Ben Claytond552f632019-11-18 11:18:41 +00001186template <class IntType>
1187IntType FoldIClamp(IntType x, IntType min_val, IntType max_val) {
1188 if (x < min_val) {
1189 x = min_val;
1190 }
1191 if (x > max_val) {
1192 x = max_val;
1193 }
1194 return x;
1195}
1196
1197const analysis::Constant* FoldMin(const analysis::Type* result_type,
1198 const analysis::Constant* a,
1199 const analysis::Constant* b,
1200 analysis::ConstantManager*) {
1201 if (const analysis::Integer* int_type = result_type->AsInteger()) {
1202 if (int_type->width() == 32) {
1203 if (int_type->IsSigned()) {
1204 int32_t va = a->GetS32();
1205 int32_t vb = b->GetS32();
1206 return (va < vb ? a : b);
1207 } else {
1208 uint32_t va = a->GetU32();
1209 uint32_t vb = b->GetU32();
1210 return (va < vb ? a : b);
1211 }
1212 } else if (int_type->width() == 64) {
1213 if (int_type->IsSigned()) {
1214 int64_t va = a->GetS64();
1215 int64_t vb = b->GetS64();
1216 return (va < vb ? a : b);
1217 } else {
1218 uint64_t va = a->GetU64();
1219 uint64_t vb = b->GetU64();
1220 return (va < vb ? a : b);
1221 }
1222 }
1223 } else if (const analysis::Float* float_type = result_type->AsFloat()) {
1224 if (float_type->width() == 32) {
1225 float va = a->GetFloat();
1226 float vb = b->GetFloat();
1227 return (va < vb ? a : b);
1228 } else if (float_type->width() == 64) {
1229 double va = a->GetDouble();
1230 double vb = b->GetDouble();
1231 return (va < vb ? a : b);
1232 }
1233 }
1234 return nullptr;
1235}
1236
1237const analysis::Constant* FoldMax(const analysis::Type* result_type,
1238 const analysis::Constant* a,
1239 const analysis::Constant* b,
1240 analysis::ConstantManager*) {
1241 if (const analysis::Integer* int_type = result_type->AsInteger()) {
1242 if (int_type->width() == 32) {
1243 if (int_type->IsSigned()) {
1244 int32_t va = a->GetS32();
1245 int32_t vb = b->GetS32();
1246 return (va > vb ? a : b);
1247 } else {
1248 uint32_t va = a->GetU32();
1249 uint32_t vb = b->GetU32();
1250 return (va > vb ? a : b);
1251 }
1252 } else if (int_type->width() == 64) {
1253 if (int_type->IsSigned()) {
1254 int64_t va = a->GetS64();
1255 int64_t vb = b->GetS64();
1256 return (va > vb ? a : b);
1257 } else {
1258 uint64_t va = a->GetU64();
1259 uint64_t vb = b->GetU64();
1260 return (va > vb ? a : b);
1261 }
1262 }
1263 } else if (const analysis::Float* float_type = result_type->AsFloat()) {
1264 if (float_type->width() == 32) {
1265 float va = a->GetFloat();
1266 float vb = b->GetFloat();
1267 return (va > vb ? a : b);
1268 } else if (float_type->width() == 64) {
1269 double va = a->GetDouble();
1270 double vb = b->GetDouble();
1271 return (va > vb ? a : b);
1272 }
1273 }
1274 return nullptr;
1275}
1276
1277// Fold an clamp instruction when all three operands are constant.
1278const analysis::Constant* FoldClamp1(
1279 IRContext* context, Instruction* inst,
1280 const std::vector<const analysis::Constant*>& constants) {
1281 assert(inst->opcode() == SpvOpExtInst &&
1282 "Expecting an extended instruction.");
1283 assert(inst->GetSingleWordInOperand(0) ==
1284 context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
1285 "Expecting a GLSLstd450 extended instruction.");
1286
1287 // Make sure all Clamp operands are constants.
Alexis Hetu00e0af12021-11-08 08:57:46 -05001288 for (uint32_t i = 1; i < 4; i++) {
Ben Claytond552f632019-11-18 11:18:41 +00001289 if (constants[i] == nullptr) {
1290 return nullptr;
1291 }
1292 }
1293
1294 const analysis::Constant* temp = FoldFPBinaryOp(
1295 FoldMax, inst->type_id(), {constants[1], constants[2]}, context);
1296 if (temp == nullptr) {
1297 return nullptr;
1298 }
1299 return FoldFPBinaryOp(FoldMin, inst->type_id(), {temp, constants[3]},
1300 context);
1301}
1302
Alexis Hetu00e0af12021-11-08 08:57:46 -05001303// Fold a clamp instruction when |x <= min_val|.
Ben Claytond552f632019-11-18 11:18:41 +00001304const analysis::Constant* FoldClamp2(
1305 IRContext* context, Instruction* inst,
1306 const std::vector<const analysis::Constant*>& constants) {
1307 assert(inst->opcode() == SpvOpExtInst &&
1308 "Expecting an extended instruction.");
1309 assert(inst->GetSingleWordInOperand(0) ==
1310 context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
1311 "Expecting a GLSLstd450 extended instruction.");
1312
1313 const analysis::Constant* x = constants[1];
1314 const analysis::Constant* min_val = constants[2];
1315
1316 if (x == nullptr || min_val == nullptr) {
1317 return nullptr;
1318 }
1319
1320 const analysis::Constant* temp =
1321 FoldFPBinaryOp(FoldMax, inst->type_id(), {x, min_val}, context);
1322 if (temp == min_val) {
1323 // We can assume that |min_val| is less than |max_val|. Therefore, if the
1324 // result of the max operation is |min_val|, we know the result of the min
1325 // operation, even if |max_val| is not a constant.
1326 return min_val;
1327 }
1328 return nullptr;
1329}
1330
1331// Fold a clamp instruction when |x >= max_val|.
1332const analysis::Constant* FoldClamp3(
1333 IRContext* context, Instruction* inst,
1334 const std::vector<const analysis::Constant*>& constants) {
1335 assert(inst->opcode() == SpvOpExtInst &&
1336 "Expecting an extended instruction.");
1337 assert(inst->GetSingleWordInOperand(0) ==
1338 context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
1339 "Expecting a GLSLstd450 extended instruction.");
1340
1341 const analysis::Constant* x = constants[1];
1342 const analysis::Constant* max_val = constants[3];
1343
1344 if (x == nullptr || max_val == nullptr) {
1345 return nullptr;
1346 }
1347
1348 const analysis::Constant* temp =
1349 FoldFPBinaryOp(FoldMin, inst->type_id(), {x, max_val}, context);
1350 if (temp == max_val) {
1351 // We can assume that |min_val| is less than |max_val|. Therefore, if the
1352 // result of the max operation is |min_val|, we know the result of the min
1353 // operation, even if |max_val| is not a constant.
1354 return max_val;
1355 }
1356 return nullptr;
1357}
1358
Ben Claytondc6b76a2020-02-24 14:53:40 +00001359UnaryScalarFoldingRule FoldFTranscendentalUnary(double (*fp)(double)) {
1360 return
1361 [fp](const analysis::Type* result_type, const analysis::Constant* a,
1362 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
1363 assert(result_type != nullptr && a != nullptr);
1364 const analysis::Float* float_type = a->type()->AsFloat();
1365 assert(float_type != nullptr);
1366 assert(float_type == result_type->AsFloat());
1367 if (float_type->width() == 32) {
1368 float fa = a->GetFloat();
1369 float res = static_cast<float>(fp(fa));
1370 utils::FloatProxy<float> result(res);
1371 std::vector<uint32_t> words = result.GetWords();
1372 return const_mgr->GetConstant(result_type, words);
1373 } else if (float_type->width() == 64) {
1374 double fa = a->GetDouble();
1375 double res = fp(fa);
1376 utils::FloatProxy<double> result(res);
1377 std::vector<uint32_t> words = result.GetWords();
1378 return const_mgr->GetConstant(result_type, words);
1379 }
1380 return nullptr;
1381 };
1382}
1383
1384BinaryScalarFoldingRule FoldFTranscendentalBinary(double (*fp)(double,
1385 double)) {
1386 return
1387 [fp](const analysis::Type* result_type, const analysis::Constant* a,
1388 const analysis::Constant* b,
1389 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
1390 assert(result_type != nullptr && a != nullptr);
1391 const analysis::Float* float_type = a->type()->AsFloat();
1392 assert(float_type != nullptr);
1393 assert(float_type == result_type->AsFloat());
1394 assert(float_type == b->type()->AsFloat());
1395 if (float_type->width() == 32) {
1396 float fa = a->GetFloat();
1397 float fb = b->GetFloat();
1398 float res = static_cast<float>(fp(fa, fb));
1399 utils::FloatProxy<float> result(res);
1400 std::vector<uint32_t> words = result.GetWords();
1401 return const_mgr->GetConstant(result_type, words);
1402 } else if (float_type->width() == 64) {
1403 double fa = a->GetDouble();
1404 double fb = b->GetDouble();
1405 double res = fp(fa, fb);
1406 utils::FloatProxy<double> result(res);
1407 std::vector<uint32_t> words = result.GetWords();
1408 return const_mgr->GetConstant(result_type, words);
1409 }
1410 return nullptr;
1411 };
1412}
Chris Forbescc5697f2019-01-30 11:54:08 -08001413} // namespace
1414
Ben Claytond0f684e2019-08-30 22:36:08 +01001415void ConstantFoldingRules::AddFoldingRules() {
Chris Forbescc5697f2019-01-30 11:54:08 -08001416 // Add all folding rules to the list for the opcodes to which they apply.
1417 // Note that the order in which rules are added to the list matters. If a rule
1418 // applies to the instruction, the rest of the rules will not be attempted.
1419 // Take that into consideration.
1420
1421 rules_[SpvOpCompositeConstruct].push_back(FoldCompositeWithConstants());
1422
1423 rules_[SpvOpCompositeExtract].push_back(FoldExtractWithConstants());
1424
1425 rules_[SpvOpConvertFToS].push_back(FoldFToI());
1426 rules_[SpvOpConvertFToU].push_back(FoldFToI());
1427 rules_[SpvOpConvertSToF].push_back(FoldIToF());
1428 rules_[SpvOpConvertUToF].push_back(FoldIToF());
1429
1430 rules_[SpvOpDot].push_back(FoldOpDotWithConstants());
1431 rules_[SpvOpFAdd].push_back(FoldFAdd());
1432 rules_[SpvOpFDiv].push_back(FoldFDiv());
1433 rules_[SpvOpFMul].push_back(FoldFMul());
1434 rules_[SpvOpFSub].push_back(FoldFSub());
1435
1436 rules_[SpvOpFOrdEqual].push_back(FoldFOrdEqual());
1437
1438 rules_[SpvOpFUnordEqual].push_back(FoldFUnordEqual());
1439
1440 rules_[SpvOpFOrdNotEqual].push_back(FoldFOrdNotEqual());
1441
1442 rules_[SpvOpFUnordNotEqual].push_back(FoldFUnordNotEqual());
1443
1444 rules_[SpvOpFOrdLessThan].push_back(FoldFOrdLessThan());
1445 rules_[SpvOpFOrdLessThan].push_back(
1446 FoldFClampFeedingCompare(SpvOpFOrdLessThan));
1447
1448 rules_[SpvOpFUnordLessThan].push_back(FoldFUnordLessThan());
1449 rules_[SpvOpFUnordLessThan].push_back(
1450 FoldFClampFeedingCompare(SpvOpFUnordLessThan));
1451
1452 rules_[SpvOpFOrdGreaterThan].push_back(FoldFOrdGreaterThan());
1453 rules_[SpvOpFOrdGreaterThan].push_back(
1454 FoldFClampFeedingCompare(SpvOpFOrdGreaterThan));
1455
1456 rules_[SpvOpFUnordGreaterThan].push_back(FoldFUnordGreaterThan());
1457 rules_[SpvOpFUnordGreaterThan].push_back(
1458 FoldFClampFeedingCompare(SpvOpFUnordGreaterThan));
1459
1460 rules_[SpvOpFOrdLessThanEqual].push_back(FoldFOrdLessThanEqual());
1461 rules_[SpvOpFOrdLessThanEqual].push_back(
1462 FoldFClampFeedingCompare(SpvOpFOrdLessThanEqual));
1463
1464 rules_[SpvOpFUnordLessThanEqual].push_back(FoldFUnordLessThanEqual());
1465 rules_[SpvOpFUnordLessThanEqual].push_back(
1466 FoldFClampFeedingCompare(SpvOpFUnordLessThanEqual));
1467
1468 rules_[SpvOpFOrdGreaterThanEqual].push_back(FoldFOrdGreaterThanEqual());
1469 rules_[SpvOpFOrdGreaterThanEqual].push_back(
1470 FoldFClampFeedingCompare(SpvOpFOrdGreaterThanEqual));
1471
1472 rules_[SpvOpFUnordGreaterThanEqual].push_back(FoldFUnordGreaterThanEqual());
1473 rules_[SpvOpFUnordGreaterThanEqual].push_back(
1474 FoldFClampFeedingCompare(SpvOpFUnordGreaterThanEqual));
1475
1476 rules_[SpvOpVectorShuffle].push_back(FoldVectorShuffleWithConstants());
1477 rules_[SpvOpVectorTimesScalar].push_back(FoldVectorTimesScalar());
Nicolas Capens00a1bcc2022-07-29 16:49:40 -04001478 rules_[SpvOpVectorTimesMatrix].push_back(FoldVectorTimesMatrix());
1479 rules_[SpvOpMatrixTimesVector].push_back(FoldMatrixTimesVector());
Chris Forbescc5697f2019-01-30 11:54:08 -08001480
1481 rules_[SpvOpFNegate].push_back(FoldFNegate());
Ben Claytonb73b7602019-07-29 13:56:13 +01001482 rules_[SpvOpQuantizeToF16].push_back(FoldQuantizeToF16());
Ben Claytond0f684e2019-08-30 22:36:08 +01001483
1484 // Add rules for GLSLstd450
1485 FeatureManager* feature_manager = context_->get_feature_mgr();
1486 uint32_t ext_inst_glslstd450_id =
1487 feature_manager->GetExtInstImportId_GLSLstd450();
1488 if (ext_inst_glslstd450_id != 0) {
1489 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMix}].push_back(FoldFMix());
Ben Claytond552f632019-11-18 11:18:41 +00001490 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMin}].push_back(
1491 FoldFPBinaryOp(FoldMin));
1492 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMin}].push_back(
1493 FoldFPBinaryOp(FoldMin));
1494 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMin}].push_back(
1495 FoldFPBinaryOp(FoldMin));
1496 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMax}].push_back(
1497 FoldFPBinaryOp(FoldMax));
1498 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMax}].push_back(
1499 FoldFPBinaryOp(FoldMax));
1500 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMax}].push_back(
1501 FoldFPBinaryOp(FoldMax));
1502 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
1503 FoldClamp1);
1504 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
1505 FoldClamp2);
1506 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
1507 FoldClamp3);
1508 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
1509 FoldClamp1);
1510 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
1511 FoldClamp2);
1512 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
1513 FoldClamp3);
1514 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
1515 FoldClamp1);
1516 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
1517 FoldClamp2);
1518 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
1519 FoldClamp3);
Ben Claytondc6b76a2020-02-24 14:53:40 +00001520 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Sin}].push_back(
1521 FoldFPUnaryOp(FoldFTranscendentalUnary(std::sin)));
1522 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Cos}].push_back(
1523 FoldFPUnaryOp(FoldFTranscendentalUnary(std::cos)));
1524 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Tan}].push_back(
1525 FoldFPUnaryOp(FoldFTranscendentalUnary(std::tan)));
1526 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Asin}].push_back(
1527 FoldFPUnaryOp(FoldFTranscendentalUnary(std::asin)));
1528 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Acos}].push_back(
1529 FoldFPUnaryOp(FoldFTranscendentalUnary(std::acos)));
1530 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Atan}].push_back(
1531 FoldFPUnaryOp(FoldFTranscendentalUnary(std::atan)));
1532 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp}].push_back(
1533 FoldFPUnaryOp(FoldFTranscendentalUnary(std::exp)));
1534 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log}].push_back(
1535 FoldFPUnaryOp(FoldFTranscendentalUnary(std::log)));
1536
1537#ifdef __ANDROID__
sugoi1b398bf32022-02-18 10:27:28 -05001538 // Android NDK r15c targeting ABI 15 doesn't have full support for C++11
Ben Claytondc6b76a2020-02-24 14:53:40 +00001539 // (no std::exp2/log2). ::exp2 is available from C99 but ::log2 isn't
1540 // available up until ABI 18 so we use a shim
1541 auto log2_shim = [](double v) -> double { return log(v) / log(2.0); };
1542 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp2}].push_back(
1543 FoldFPUnaryOp(FoldFTranscendentalUnary(::exp2)));
1544 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log2}].push_back(
1545 FoldFPUnaryOp(FoldFTranscendentalUnary(log2_shim)));
1546#else
1547 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp2}].push_back(
1548 FoldFPUnaryOp(FoldFTranscendentalUnary(std::exp2)));
1549 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log2}].push_back(
1550 FoldFPUnaryOp(FoldFTranscendentalUnary(std::log2)));
1551#endif
1552
1553 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Sqrt}].push_back(
1554 FoldFPUnaryOp(FoldFTranscendentalUnary(std::sqrt)));
1555 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Atan2}].push_back(
1556 FoldFPBinaryOp(FoldFTranscendentalBinary(std::atan2)));
1557 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Pow}].push_back(
1558 FoldFPBinaryOp(FoldFTranscendentalBinary(std::pow)));
Ben Claytond0f684e2019-08-30 22:36:08 +01001559 }
Chris Forbescc5697f2019-01-30 11:54:08 -08001560}
1561} // namespace opt
1562} // namespace spvtools