blob: 516c34b3bc9aa842baa4af1bacedabb053d3f9d8 [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 {
Nicolas Capens84c9c452022-11-18 14:11:05 +000022constexpr uint32_t kExtractCompositeIdInIdx = 0;
Chris Forbescc5697f2019-01-30 11:54:08 -080023
Nicolas Capens6cacf182021-11-30 11:15:46 -050024// Returns a constants with the value NaN of the given type. Only works for
25// 32-bit and 64-bit float point types. Returns |nullptr| if an error occurs.
26const analysis::Constant* GetNan(const analysis::Type* type,
27 analysis::ConstantManager* const_mgr) {
28 const analysis::Float* float_type = type->AsFloat();
29 if (float_type == nullptr) {
30 return nullptr;
31 }
32
33 switch (float_type->width()) {
34 case 32:
35 return const_mgr->GetFloatConst(std::numeric_limits<float>::quiet_NaN());
36 case 64:
37 return const_mgr->GetDoubleConst(
38 std::numeric_limits<double>::quiet_NaN());
39 default:
40 return nullptr;
41 }
42}
43
44// Returns a constants with the value INF of the given type. Only works for
45// 32-bit and 64-bit float point types. Returns |nullptr| if an error occurs.
46const analysis::Constant* GetInf(const analysis::Type* type,
47 analysis::ConstantManager* const_mgr) {
48 const analysis::Float* float_type = type->AsFloat();
49 if (float_type == nullptr) {
50 return nullptr;
51 }
52
53 switch (float_type->width()) {
54 case 32:
55 return const_mgr->GetFloatConst(std::numeric_limits<float>::infinity());
56 case 64:
57 return const_mgr->GetDoubleConst(std::numeric_limits<double>::infinity());
58 default:
59 return nullptr;
60 }
61}
62
Chris Forbescc5697f2019-01-30 11:54:08 -080063// Returns true if |type| is Float or a vector of Float.
64bool HasFloatingPoint(const analysis::Type* type) {
65 if (type->AsFloat()) {
66 return true;
67 } else if (const analysis::Vector* vec_type = type->AsVector()) {
68 return vec_type->element_type()->AsFloat() != nullptr;
69 }
70
71 return false;
72}
73
Nicolas Capens6cacf182021-11-30 11:15:46 -050074// Returns a constants with the value |-val| of the given type. Only works for
75// 32-bit and 64-bit float point types. Returns |nullptr| if an error occurs.
Nicolas Capens84c9c452022-11-18 14:11:05 +000076const analysis::Constant* NegateFPConst(const analysis::Type* result_type,
Nicolas Capens6cacf182021-11-30 11:15:46 -050077 const analysis::Constant* val,
78 analysis::ConstantManager* const_mgr) {
79 const analysis::Float* float_type = result_type->AsFloat();
80 assert(float_type != nullptr);
81 if (float_type->width() == 32) {
82 float fa = val->GetFloat();
83 return const_mgr->GetFloatConst(-fa);
84 } else if (float_type->width() == 64) {
85 double da = val->GetDouble();
86 return const_mgr->GetDoubleConst(-da);
87 }
88 return nullptr;
89}
90
Chris Forbescc5697f2019-01-30 11:54:08 -080091// Folds an OpcompositeExtract where input is a composite constant.
92ConstantFoldingRule FoldExtractWithConstants() {
93 return [](IRContext* context, Instruction* inst,
94 const std::vector<const analysis::Constant*>& constants)
95 -> const analysis::Constant* {
96 const analysis::Constant* c = constants[kExtractCompositeIdInIdx];
97 if (c == nullptr) {
98 return nullptr;
99 }
100
101 for (uint32_t i = 1; i < inst->NumInOperands(); ++i) {
102 uint32_t element_index = inst->GetSingleWordInOperand(i);
103 if (c->AsNullConstant()) {
104 // Return Null for the return type.
105 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
106 analysis::TypeManager* type_mgr = context->get_type_mgr();
107 return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), {});
108 }
109
110 auto cc = c->AsCompositeConstant();
111 assert(cc != nullptr);
112 auto components = cc->GetComponents();
Ben Claytond0f684e2019-08-30 22:36:08 +0100113 // Protect against invalid IR. Refuse to fold if the index is out
114 // of bounds.
115 if (element_index >= components.size()) return nullptr;
Chris Forbescc5697f2019-01-30 11:54:08 -0800116 c = components[element_index];
117 }
118 return c;
119 };
120}
121
Nicolas Capens84c9c452022-11-18 14:11:05 +0000122// Folds an OpcompositeInsert where input is a composite constant.
123ConstantFoldingRule FoldInsertWithConstants() {
124 return [](IRContext* context, Instruction* inst,
125 const std::vector<const analysis::Constant*>& constants)
126 -> const analysis::Constant* {
127 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
128 const analysis::Constant* object = constants[0];
129 const analysis::Constant* composite = constants[1];
130 if (object == nullptr || composite == nullptr) {
131 return nullptr;
132 }
133
134 // If there is more than 1 index, then each additional constant used by the
135 // index will need to be recreated to use the inserted object.
136 std::vector<const analysis::Constant*> chain;
137 std::vector<const analysis::Constant*> components;
138 const analysis::Type* type = nullptr;
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500139 const uint32_t final_index = (inst->NumInOperands() - 1);
Nicolas Capens84c9c452022-11-18 14:11:05 +0000140
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500141 // Work down hierarchy of all indexes
Nicolas Capens84c9c452022-11-18 14:11:05 +0000142 for (uint32_t i = 2; i < inst->NumInOperands(); ++i) {
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500143 type = composite->type();
Alexis Hetu1ef51fa2022-11-24 09:03:10 -0500144
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500145 if (composite->AsNullConstant()) {
146 // Make new composite so it can be inserted in the index with the
147 // non-null value
Ben Clayton16696652023-02-06 14:45:50 +0000148 if (const auto new_composite =
149 const_mgr->GetNullCompositeConstant(type)) {
150 // Keep track of any indexes along the way to last index
151 if (i != final_index) {
152 chain.push_back(new_composite);
153 }
154 components = new_composite->AsCompositeConstant()->GetComponents();
155 } else {
156 // Unsupported input type (such as structs)
157 return nullptr;
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500158 }
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500159 } else {
160 // Keep track of any indexes along the way to last index
161 if (i != final_index) {
162 chain.push_back(composite);
163 }
164 components = composite->AsCompositeConstant()->GetComponents();
Nicolas Capens84c9c452022-11-18 14:11:05 +0000165 }
166 const uint32_t index = inst->GetSingleWordInOperand(i);
Nicolas Capens84c9c452022-11-18 14:11:05 +0000167 composite = components[index];
168 }
169
170 // Final index in hierarchy is inserted with new object.
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500171 const uint32_t final_operand = inst->GetSingleWordInOperand(final_index);
Nicolas Capens84c9c452022-11-18 14:11:05 +0000172 std::vector<uint32_t> ids;
173 for (size_t i = 0; i < components.size(); i++) {
174 const analysis::Constant* constant =
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500175 (i == final_operand) ? object : components[i];
Nicolas Capens84c9c452022-11-18 14:11:05 +0000176 Instruction* member_inst = const_mgr->GetDefiningInstruction(constant);
177 ids.push_back(member_inst->result_id());
178 }
179 const analysis::Constant* new_constant = const_mgr->GetConstant(type, ids);
180
181 // Work backwards up the chain and replace each index with new constant.
182 for (size_t i = chain.size(); i > 0; i--) {
183 // Need to insert any previous instruction into the module first.
184 // Can't just insert in types_values_begin() because it will move above
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500185 // where the types are declared.
186 // Can't compare with location of inst because not all new added
187 // instructions are added to types_values_
188 auto iter = context->types_values_end();
189 Module::inst_iterator* pos = &iter;
190 const_mgr->BuildInstructionAndAddToModule(new_constant, pos);
Nicolas Capens84c9c452022-11-18 14:11:05 +0000191
192 composite = chain[i - 1];
193 components = composite->AsCompositeConstant()->GetComponents();
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500194 type = composite->type();
Nicolas Capens84c9c452022-11-18 14:11:05 +0000195 ids.clear();
196 for (size_t k = 0; k < components.size(); k++) {
197 const uint32_t index =
198 inst->GetSingleWordInOperand(1 + static_cast<uint32_t>(i));
199 const analysis::Constant* constant =
200 (k == index) ? new_constant : components[k];
201 const uint32_t constant_id =
202 const_mgr->FindDeclaredConstant(constant, 0);
203 ids.push_back(constant_id);
204 }
205 new_constant = const_mgr->GetConstant(type, ids);
206 }
207
208 // If multiple constants were created, only need to return the top index.
209 return new_constant;
210 };
211}
212
Chris Forbescc5697f2019-01-30 11:54:08 -0800213ConstantFoldingRule FoldVectorShuffleWithConstants() {
214 return [](IRContext* context, Instruction* inst,
215 const std::vector<const analysis::Constant*>& constants)
216 -> const analysis::Constant* {
Nicolas Capens84c9c452022-11-18 14:11:05 +0000217 assert(inst->opcode() == spv::Op::OpVectorShuffle);
Chris Forbescc5697f2019-01-30 11:54:08 -0800218 const analysis::Constant* c1 = constants[0];
219 const analysis::Constant* c2 = constants[1];
220 if (c1 == nullptr || c2 == nullptr) {
221 return nullptr;
222 }
223
224 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
225 const analysis::Type* element_type = c1->type()->AsVector()->element_type();
226
227 std::vector<const analysis::Constant*> c1_components;
228 if (const analysis::VectorConstant* vec_const = c1->AsVectorConstant()) {
229 c1_components = vec_const->GetComponents();
230 } else {
231 assert(c1->AsNullConstant());
232 const analysis::Constant* element =
233 const_mgr->GetConstant(element_type, {});
234 c1_components.resize(c1->type()->AsVector()->element_count(), element);
235 }
236 std::vector<const analysis::Constant*> c2_components;
237 if (const analysis::VectorConstant* vec_const = c2->AsVectorConstant()) {
238 c2_components = vec_const->GetComponents();
239 } else {
240 assert(c2->AsNullConstant());
241 const analysis::Constant* element =
242 const_mgr->GetConstant(element_type, {});
243 c2_components.resize(c2->type()->AsVector()->element_count(), element);
244 }
245
246 std::vector<uint32_t> ids;
247 const uint32_t undef_literal_value = 0xffffffff;
248 for (uint32_t i = 2; i < inst->NumInOperands(); ++i) {
249 uint32_t index = inst->GetSingleWordInOperand(i);
250 if (index == undef_literal_value) {
251 // Don't fold shuffle with undef literal value.
252 return nullptr;
253 } else if (index < c1_components.size()) {
254 Instruction* member_inst =
255 const_mgr->GetDefiningInstruction(c1_components[index]);
256 ids.push_back(member_inst->result_id());
257 } else {
258 Instruction* member_inst = const_mgr->GetDefiningInstruction(
259 c2_components[index - c1_components.size()]);
260 ids.push_back(member_inst->result_id());
261 }
262 }
263
264 analysis::TypeManager* type_mgr = context->get_type_mgr();
265 return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), ids);
266 };
267}
268
269ConstantFoldingRule FoldVectorTimesScalar() {
270 return [](IRContext* context, Instruction* inst,
271 const std::vector<const analysis::Constant*>& constants)
272 -> const analysis::Constant* {
Nicolas Capens84c9c452022-11-18 14:11:05 +0000273 assert(inst->opcode() == spv::Op::OpVectorTimesScalar);
Chris Forbescc5697f2019-01-30 11:54:08 -0800274 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
275 analysis::TypeManager* type_mgr = context->get_type_mgr();
276
277 if (!inst->IsFloatingPointFoldingAllowed()) {
278 if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
279 return nullptr;
280 }
281 }
282
283 const analysis::Constant* c1 = constants[0];
284 const analysis::Constant* c2 = constants[1];
285
286 if (c1 && c1->IsZero()) {
287 return c1;
288 }
289
290 if (c2 && c2->IsZero()) {
291 // Get or create the NullConstant for this type.
292 std::vector<uint32_t> ids;
293 return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), ids);
294 }
295
296 if (c1 == nullptr || c2 == nullptr) {
297 return nullptr;
298 }
299
300 // Check result type.
301 const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
302 const analysis::Vector* vector_type = result_type->AsVector();
303 assert(vector_type != nullptr);
304 const analysis::Type* element_type = vector_type->element_type();
305 assert(element_type != nullptr);
306 const analysis::Float* float_type = element_type->AsFloat();
307 assert(float_type != nullptr);
308
309 // Check types of c1 and c2.
310 assert(c1->type()->AsVector() == vector_type);
311 assert(c1->type()->AsVector()->element_type() == element_type &&
312 c2->type() == element_type);
313
314 // Get a float vector that is the result of vector-times-scalar.
315 std::vector<const analysis::Constant*> c1_components =
316 c1->GetVectorComponents(const_mgr);
317 std::vector<uint32_t> ids;
318 if (float_type->width() == 32) {
319 float scalar = c2->GetFloat();
320 for (uint32_t i = 0; i < c1_components.size(); ++i) {
321 utils::FloatProxy<float> result(c1_components[i]->GetFloat() * scalar);
322 std::vector<uint32_t> words = result.GetWords();
323 const analysis::Constant* new_elem =
324 const_mgr->GetConstant(float_type, words);
325 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
326 }
327 return const_mgr->GetConstant(vector_type, ids);
328 } else if (float_type->width() == 64) {
329 double scalar = c2->GetDouble();
330 for (uint32_t i = 0; i < c1_components.size(); ++i) {
331 utils::FloatProxy<double> result(c1_components[i]->GetDouble() *
332 scalar);
333 std::vector<uint32_t> words = result.GetWords();
334 const analysis::Constant* new_elem =
335 const_mgr->GetConstant(float_type, words);
336 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
337 }
338 return const_mgr->GetConstant(vector_type, ids);
339 }
340 return nullptr;
341 };
342}
343
Nicolas Capens00a1bcc2022-07-29 16:49:40 -0400344ConstantFoldingRule FoldVectorTimesMatrix() {
345 return [](IRContext* context, Instruction* inst,
346 const std::vector<const analysis::Constant*>& constants)
347 -> const analysis::Constant* {
Nicolas Capens84c9c452022-11-18 14:11:05 +0000348 assert(inst->opcode() == spv::Op::OpVectorTimesMatrix);
Nicolas Capens00a1bcc2022-07-29 16:49:40 -0400349 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
350 analysis::TypeManager* type_mgr = context->get_type_mgr();
351
352 if (!inst->IsFloatingPointFoldingAllowed()) {
353 if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
354 return nullptr;
355 }
356 }
357
358 const analysis::Constant* c1 = constants[0];
359 const analysis::Constant* c2 = constants[1];
360
361 if (c1 == nullptr || c2 == nullptr) {
362 return nullptr;
363 }
364
365 // Check result type.
366 const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
367 const analysis::Vector* vector_type = result_type->AsVector();
368 assert(vector_type != nullptr);
369 const analysis::Type* element_type = vector_type->element_type();
370 assert(element_type != nullptr);
371 const analysis::Float* float_type = element_type->AsFloat();
372 assert(float_type != nullptr);
373
374 // Check types of c1 and c2.
375 assert(c1->type()->AsVector() == vector_type);
376 assert(c1->type()->AsVector()->element_type() == element_type &&
377 c2->type()->AsMatrix()->element_type() == vector_type);
378
379 // Get a float vector that is the result of vector-times-matrix.
380 std::vector<const analysis::Constant*> c1_components =
381 c1->GetVectorComponents(const_mgr);
382 std::vector<const analysis::Constant*> c2_components =
383 c2->AsMatrixConstant()->GetComponents();
384 uint32_t resultVectorSize = result_type->AsVector()->element_count();
385
386 std::vector<uint32_t> ids;
387
388 if ((c1 && c1->IsZero()) || (c2 && c2->IsZero())) {
389 std::vector<uint32_t> words(float_type->width() / 32, 0);
390 for (uint32_t i = 0; i < resultVectorSize; ++i) {
391 const analysis::Constant* new_elem =
392 const_mgr->GetConstant(float_type, words);
393 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
394 }
395 return const_mgr->GetConstant(vector_type, ids);
396 }
397
398 if (float_type->width() == 32) {
399 for (uint32_t i = 0; i < resultVectorSize; ++i) {
400 float result_scalar = 0.0f;
401 const analysis::VectorConstant* c2_vec =
402 c2_components[i]->AsVectorConstant();
403 for (uint32_t j = 0; j < c2_vec->GetComponents().size(); ++j) {
404 float c1_scalar = c1_components[j]->GetFloat();
405 float c2_scalar = c2_vec->GetComponents()[j]->GetFloat();
406 result_scalar += c1_scalar * c2_scalar;
407 }
408 utils::FloatProxy<float> result(result_scalar);
409 std::vector<uint32_t> words = result.GetWords();
410 const analysis::Constant* new_elem =
411 const_mgr->GetConstant(float_type, words);
412 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
413 }
414 return const_mgr->GetConstant(vector_type, ids);
415 } else if (float_type->width() == 64) {
416 for (uint32_t i = 0; i < c2_components.size(); ++i) {
417 double result_scalar = 0.0;
418 const analysis::VectorConstant* c2_vec =
419 c2_components[i]->AsVectorConstant();
420 for (uint32_t j = 0; j < c2_vec->GetComponents().size(); ++j) {
421 double c1_scalar = c1_components[j]->GetDouble();
422 double c2_scalar = c2_vec->GetComponents()[j]->GetDouble();
423 result_scalar += c1_scalar * c2_scalar;
424 }
425 utils::FloatProxy<double> result(result_scalar);
426 std::vector<uint32_t> words = result.GetWords();
427 const analysis::Constant* new_elem =
428 const_mgr->GetConstant(float_type, words);
429 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
430 }
431 return const_mgr->GetConstant(vector_type, ids);
432 }
433 return nullptr;
434 };
435}
436
437ConstantFoldingRule FoldMatrixTimesVector() {
438 return [](IRContext* context, Instruction* inst,
439 const std::vector<const analysis::Constant*>& constants)
440 -> const analysis::Constant* {
Nicolas Capens84c9c452022-11-18 14:11:05 +0000441 assert(inst->opcode() == spv::Op::OpMatrixTimesVector);
Nicolas Capens00a1bcc2022-07-29 16:49:40 -0400442 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
443 analysis::TypeManager* type_mgr = context->get_type_mgr();
444
445 if (!inst->IsFloatingPointFoldingAllowed()) {
446 if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
447 return nullptr;
448 }
449 }
450
451 const analysis::Constant* c1 = constants[0];
452 const analysis::Constant* c2 = constants[1];
453
454 if (c1 == nullptr || c2 == nullptr) {
455 return nullptr;
456 }
457
458 // Check result type.
459 const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
460 const analysis::Vector* vector_type = result_type->AsVector();
461 assert(vector_type != nullptr);
462 const analysis::Type* element_type = vector_type->element_type();
463 assert(element_type != nullptr);
464 const analysis::Float* float_type = element_type->AsFloat();
465 assert(float_type != nullptr);
466
467 // Check types of c1 and c2.
468 assert(c1->type()->AsMatrix()->element_type() == vector_type);
469 assert(c2->type()->AsVector()->element_type() == element_type);
470
471 // Get a float vector that is the result of matrix-times-vector.
472 std::vector<const analysis::Constant*> c1_components =
473 c1->AsMatrixConstant()->GetComponents();
474 std::vector<const analysis::Constant*> c2_components =
475 c2->GetVectorComponents(const_mgr);
476 uint32_t resultVectorSize = result_type->AsVector()->element_count();
477
478 std::vector<uint32_t> ids;
479
480 if ((c1 && c1->IsZero()) || (c2 && c2->IsZero())) {
481 std::vector<uint32_t> words(float_type->width() / 32, 0);
482 for (uint32_t i = 0; i < resultVectorSize; ++i) {
483 const analysis::Constant* new_elem =
484 const_mgr->GetConstant(float_type, words);
485 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
486 }
487 return const_mgr->GetConstant(vector_type, ids);
488 }
489
490 if (float_type->width() == 32) {
491 for (uint32_t i = 0; i < resultVectorSize; ++i) {
492 float result_scalar = 0.0f;
493 for (uint32_t j = 0; j < c1_components.size(); ++j) {
494 float c1_scalar = c1_components[j]
495 ->AsVectorConstant()
496 ->GetComponents()[i]
497 ->GetFloat();
498 float c2_scalar = c2_components[j]->GetFloat();
499 result_scalar += c1_scalar * c2_scalar;
500 }
501 utils::FloatProxy<float> result(result_scalar);
502 std::vector<uint32_t> words = result.GetWords();
503 const analysis::Constant* new_elem =
504 const_mgr->GetConstant(float_type, words);
505 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
506 }
507 return const_mgr->GetConstant(vector_type, ids);
508 } else if (float_type->width() == 64) {
509 for (uint32_t i = 0; i < resultVectorSize; ++i) {
510 double result_scalar = 0.0;
511 for (uint32_t j = 0; j < c1_components.size(); ++j) {
512 double c1_scalar = c1_components[j]
513 ->AsVectorConstant()
514 ->GetComponents()[i]
515 ->GetDouble();
516 double c2_scalar = c2_components[j]->GetDouble();
517 result_scalar += c1_scalar * c2_scalar;
518 }
519 utils::FloatProxy<double> result(result_scalar);
520 std::vector<uint32_t> words = result.GetWords();
521 const analysis::Constant* new_elem =
522 const_mgr->GetConstant(float_type, words);
523 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
524 }
525 return const_mgr->GetConstant(vector_type, ids);
526 }
527 return nullptr;
528 };
529}
530
Chris Forbescc5697f2019-01-30 11:54:08 -0800531ConstantFoldingRule FoldCompositeWithConstants() {
532 // Folds an OpCompositeConstruct where all of the inputs are constants to a
533 // constant. A new constant is created if necessary.
534 return [](IRContext* context, Instruction* inst,
535 const std::vector<const analysis::Constant*>& constants)
536 -> const analysis::Constant* {
537 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
538 analysis::TypeManager* type_mgr = context->get_type_mgr();
539 const analysis::Type* new_type = type_mgr->GetType(inst->type_id());
540 Instruction* type_inst =
541 context->get_def_use_mgr()->GetDef(inst->type_id());
542
543 std::vector<uint32_t> ids;
544 for (uint32_t i = 0; i < constants.size(); ++i) {
545 const analysis::Constant* element_const = constants[i];
546 if (element_const == nullptr) {
547 return nullptr;
548 }
549
550 uint32_t component_type_id = 0;
Nicolas Capens84c9c452022-11-18 14:11:05 +0000551 if (type_inst->opcode() == spv::Op::OpTypeStruct) {
Chris Forbescc5697f2019-01-30 11:54:08 -0800552 component_type_id = type_inst->GetSingleWordInOperand(i);
Nicolas Capens84c9c452022-11-18 14:11:05 +0000553 } else if (type_inst->opcode() == spv::Op::OpTypeArray) {
Chris Forbescc5697f2019-01-30 11:54:08 -0800554 component_type_id = type_inst->GetSingleWordInOperand(0);
555 }
556
557 uint32_t element_id =
558 const_mgr->FindDeclaredConstant(element_const, component_type_id);
559 if (element_id == 0) {
560 return nullptr;
561 }
562 ids.push_back(element_id);
563 }
564 return const_mgr->GetConstant(new_type, ids);
565 };
566}
567
568// The interface for a function that returns the result of applying a scalar
569// floating-point binary operation on |a| and |b|. The type of the return value
570// will be |type|. The input constants must also be of type |type|.
571using UnaryScalarFoldingRule = std::function<const analysis::Constant*(
572 const analysis::Type* result_type, const analysis::Constant* a,
573 analysis::ConstantManager*)>;
574
575// The interface for a function that returns the result of applying a scalar
576// floating-point binary operation on |a| and |b|. The type of the return value
577// will be |type|. The input constants must also be of type |type|.
578using BinaryScalarFoldingRule = std::function<const analysis::Constant*(
579 const analysis::Type* result_type, const analysis::Constant* a,
580 const analysis::Constant* b, analysis::ConstantManager*)>;
581
582// Returns a |ConstantFoldingRule| that folds unary floating point scalar ops
583// using |scalar_rule| and unary float point vectors ops by applying
584// |scalar_rule| to the elements of the vector. The |ConstantFoldingRule|
585// that is returned assumes that |constants| contains 1 entry. If they are
586// not |nullptr|, then their type is either |Float| or |Integer| or a |Vector|
587// whose element type is |Float| or |Integer|.
588ConstantFoldingRule FoldFPUnaryOp(UnaryScalarFoldingRule scalar_rule) {
589 return [scalar_rule](IRContext* context, Instruction* inst,
590 const std::vector<const analysis::Constant*>& constants)
591 -> const analysis::Constant* {
592 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
593 analysis::TypeManager* type_mgr = context->get_type_mgr();
594 const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
595 const analysis::Vector* vector_type = result_type->AsVector();
596
597 if (!inst->IsFloatingPointFoldingAllowed()) {
598 return nullptr;
599 }
600
Ben Claytondc6b76a2020-02-24 14:53:40 +0000601 const analysis::Constant* arg =
Nicolas Capens84c9c452022-11-18 14:11:05 +0000602 (inst->opcode() == spv::Op::OpExtInst) ? constants[1] : constants[0];
Ben Claytondc6b76a2020-02-24 14:53:40 +0000603
604 if (arg == nullptr) {
Chris Forbescc5697f2019-01-30 11:54:08 -0800605 return nullptr;
606 }
607
608 if (vector_type != nullptr) {
609 std::vector<const analysis::Constant*> a_components;
610 std::vector<const analysis::Constant*> results_components;
611
Ben Claytondc6b76a2020-02-24 14:53:40 +0000612 a_components = arg->GetVectorComponents(const_mgr);
Chris Forbescc5697f2019-01-30 11:54:08 -0800613
614 // Fold each component of the vector.
615 for (uint32_t i = 0; i < a_components.size(); ++i) {
616 results_components.push_back(scalar_rule(vector_type->element_type(),
617 a_components[i], const_mgr));
618 if (results_components[i] == nullptr) {
619 return nullptr;
620 }
621 }
622
623 // Build the constant object and return it.
624 std::vector<uint32_t> ids;
625 for (const analysis::Constant* member : results_components) {
626 ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
627 }
628 return const_mgr->GetConstant(vector_type, ids);
629 } else {
Ben Claytondc6b76a2020-02-24 14:53:40 +0000630 return scalar_rule(result_type, arg, const_mgr);
Chris Forbescc5697f2019-01-30 11:54:08 -0800631 }
632 };
633}
634
Ben Claytond552f632019-11-18 11:18:41 +0000635// Returns the result of folding the constants in |constants| according the
636// |scalar_rule|. If |result_type| is a vector, then |scalar_rule| is applied
637// per component.
638const analysis::Constant* FoldFPBinaryOp(
639 BinaryScalarFoldingRule scalar_rule, uint32_t result_type_id,
640 const std::vector<const analysis::Constant*>& constants,
641 IRContext* context) {
642 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
643 analysis::TypeManager* type_mgr = context->get_type_mgr();
644 const analysis::Type* result_type = type_mgr->GetType(result_type_id);
645 const analysis::Vector* vector_type = result_type->AsVector();
646
647 if (constants[0] == nullptr || constants[1] == nullptr) {
648 return nullptr;
649 }
650
651 if (vector_type != nullptr) {
652 std::vector<const analysis::Constant*> a_components;
653 std::vector<const analysis::Constant*> b_components;
654 std::vector<const analysis::Constant*> results_components;
655
656 a_components = constants[0]->GetVectorComponents(const_mgr);
657 b_components = constants[1]->GetVectorComponents(const_mgr);
658
659 // Fold each component of the vector.
660 for (uint32_t i = 0; i < a_components.size(); ++i) {
661 results_components.push_back(scalar_rule(vector_type->element_type(),
662 a_components[i], b_components[i],
663 const_mgr));
664 if (results_components[i] == nullptr) {
665 return nullptr;
666 }
667 }
668
669 // Build the constant object and return it.
670 std::vector<uint32_t> ids;
671 for (const analysis::Constant* member : results_components) {
672 ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
673 }
674 return const_mgr->GetConstant(vector_type, ids);
675 } else {
676 return scalar_rule(result_type, constants[0], constants[1], const_mgr);
677 }
678}
679
Chris Forbescc5697f2019-01-30 11:54:08 -0800680// Returns a |ConstantFoldingRule| that folds floating point scalars using
681// |scalar_rule| and vectors of floating point by applying |scalar_rule| to the
682// elements of the vector. The |ConstantFoldingRule| that is returned assumes
683// that |constants| contains 2 entries. If they are not |nullptr|, then their
684// type is either |Float| or a |Vector| whose element type is |Float|.
685ConstantFoldingRule FoldFPBinaryOp(BinaryScalarFoldingRule scalar_rule) {
686 return [scalar_rule](IRContext* context, Instruction* inst,
687 const std::vector<const analysis::Constant*>& constants)
688 -> const analysis::Constant* {
Chris Forbescc5697f2019-01-30 11:54:08 -0800689 if (!inst->IsFloatingPointFoldingAllowed()) {
690 return nullptr;
691 }
Nicolas Capens84c9c452022-11-18 14:11:05 +0000692 if (inst->opcode() == spv::Op::OpExtInst) {
Ben Claytond552f632019-11-18 11:18:41 +0000693 return FoldFPBinaryOp(scalar_rule, inst->type_id(),
694 {constants[1], constants[2]}, context);
Chris Forbescc5697f2019-01-30 11:54:08 -0800695 }
Ben Claytond552f632019-11-18 11:18:41 +0000696 return FoldFPBinaryOp(scalar_rule, inst->type_id(), constants, context);
Chris Forbescc5697f2019-01-30 11:54:08 -0800697 };
698}
699
700// This macro defines a |UnaryScalarFoldingRule| that performs float to
701// integer conversion.
702// TODO(greg-lunarg): Support for 64-bit integer types.
703UnaryScalarFoldingRule FoldFToIOp() {
704 return [](const analysis::Type* result_type, const analysis::Constant* a,
705 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
706 assert(result_type != nullptr && a != nullptr);
707 const analysis::Integer* integer_type = result_type->AsInteger();
708 const analysis::Float* float_type = a->type()->AsFloat();
709 assert(float_type != nullptr);
710 assert(integer_type != nullptr);
711 if (integer_type->width() != 32) return nullptr;
712 if (float_type->width() == 32) {
713 float fa = a->GetFloat();
714 uint32_t result = integer_type->IsSigned()
715 ? static_cast<uint32_t>(static_cast<int32_t>(fa))
716 : static_cast<uint32_t>(fa);
717 std::vector<uint32_t> words = {result};
718 return const_mgr->GetConstant(result_type, words);
719 } else if (float_type->width() == 64) {
720 double fa = a->GetDouble();
721 uint32_t result = integer_type->IsSigned()
722 ? static_cast<uint32_t>(static_cast<int32_t>(fa))
723 : static_cast<uint32_t>(fa);
724 std::vector<uint32_t> words = {result};
725 return const_mgr->GetConstant(result_type, words);
726 }
727 return nullptr;
728 };
729}
730
731// This function defines a |UnaryScalarFoldingRule| that performs integer to
732// float conversion.
733// TODO(greg-lunarg): Support for 64-bit integer types.
734UnaryScalarFoldingRule FoldIToFOp() {
735 return [](const analysis::Type* result_type, const analysis::Constant* a,
736 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
737 assert(result_type != nullptr && a != nullptr);
738 const analysis::Integer* integer_type = a->type()->AsInteger();
739 const analysis::Float* float_type = result_type->AsFloat();
740 assert(float_type != nullptr);
741 assert(integer_type != nullptr);
742 if (integer_type->width() != 32) return nullptr;
743 uint32_t ua = a->GetU32();
744 if (float_type->width() == 32) {
745 float result_val = integer_type->IsSigned()
746 ? static_cast<float>(static_cast<int32_t>(ua))
747 : static_cast<float>(ua);
748 utils::FloatProxy<float> result(result_val);
749 std::vector<uint32_t> words = {result.data()};
750 return const_mgr->GetConstant(result_type, words);
751 } else if (float_type->width() == 64) {
752 double result_val = integer_type->IsSigned()
753 ? static_cast<double>(static_cast<int32_t>(ua))
754 : static_cast<double>(ua);
755 utils::FloatProxy<double> result(result_val);
756 std::vector<uint32_t> words = result.GetWords();
757 return const_mgr->GetConstant(result_type, words);
758 }
759 return nullptr;
760 };
761}
762
Ben Claytonb73b7602019-07-29 13:56:13 +0100763// This defines a |UnaryScalarFoldingRule| that performs |OpQuantizeToF16|.
764UnaryScalarFoldingRule FoldQuantizeToF16Scalar() {
765 return [](const analysis::Type* result_type, const analysis::Constant* a,
766 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
767 assert(result_type != nullptr && a != nullptr);
768 const analysis::Float* float_type = a->type()->AsFloat();
769 assert(float_type != nullptr);
770 if (float_type->width() != 32) {
771 return nullptr;
772 }
773
774 float fa = a->GetFloat();
775 utils::HexFloat<utils::FloatProxy<float>> orignal(fa);
776 utils::HexFloat<utils::FloatProxy<utils::Float16>> quantized(0);
777 utils::HexFloat<utils::FloatProxy<float>> result(0.0f);
778 orignal.castTo(quantized, utils::round_direction::kToZero);
779 quantized.castTo(result, utils::round_direction::kToZero);
780 std::vector<uint32_t> words = {result.getBits()};
781 return const_mgr->GetConstant(result_type, words);
782 };
783}
784
Chris Forbescc5697f2019-01-30 11:54:08 -0800785// This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
786// operator |op| must work for both float and double, and use syntax "f1 op f2".
Ben Claytond552f632019-11-18 11:18:41 +0000787#define FOLD_FPARITH_OP(op) \
788 [](const analysis::Type* result_type_in_macro, const analysis::Constant* a, \
789 const analysis::Constant* b, \
790 analysis::ConstantManager* const_mgr_in_macro) \
791 -> const analysis::Constant* { \
792 assert(result_type_in_macro != nullptr && a != nullptr && b != nullptr); \
793 assert(result_type_in_macro == a->type() && \
794 result_type_in_macro == b->type()); \
795 const analysis::Float* float_type_in_macro = \
796 result_type_in_macro->AsFloat(); \
797 assert(float_type_in_macro != nullptr); \
798 if (float_type_in_macro->width() == 32) { \
799 float fa = a->GetFloat(); \
800 float fb = b->GetFloat(); \
801 utils::FloatProxy<float> result_in_macro(fa op fb); \
802 std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
803 return const_mgr_in_macro->GetConstant(result_type_in_macro, \
804 words_in_macro); \
805 } else if (float_type_in_macro->width() == 64) { \
806 double fa = a->GetDouble(); \
807 double fb = b->GetDouble(); \
808 utils::FloatProxy<double> result_in_macro(fa op fb); \
809 std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
810 return const_mgr_in_macro->GetConstant(result_type_in_macro, \
811 words_in_macro); \
812 } \
813 return nullptr; \
Chris Forbescc5697f2019-01-30 11:54:08 -0800814 }
815
816// Define the folding rule for conversion between floating point and integer
817ConstantFoldingRule FoldFToI() { return FoldFPUnaryOp(FoldFToIOp()); }
818ConstantFoldingRule FoldIToF() { return FoldFPUnaryOp(FoldIToFOp()); }
Ben Claytonb73b7602019-07-29 13:56:13 +0100819ConstantFoldingRule FoldQuantizeToF16() {
820 return FoldFPUnaryOp(FoldQuantizeToF16Scalar());
821}
Chris Forbescc5697f2019-01-30 11:54:08 -0800822
823// Define the folding rules for subtraction, addition, multiplication, and
824// division for floating point values.
825ConstantFoldingRule FoldFSub() { return FoldFPBinaryOp(FOLD_FPARITH_OP(-)); }
826ConstantFoldingRule FoldFAdd() { return FoldFPBinaryOp(FOLD_FPARITH_OP(+)); }
827ConstantFoldingRule FoldFMul() { return FoldFPBinaryOp(FOLD_FPARITH_OP(*)); }
Nicolas Capens6cacf182021-11-30 11:15:46 -0500828
829// Returns the constant that results from evaluating |numerator| / 0.0. Returns
sugoi1b398bf32022-02-18 10:27:28 -0500830// |nullptr| if the result could not be evaluated.
Nicolas Capens6cacf182021-11-30 11:15:46 -0500831const analysis::Constant* FoldFPScalarDivideByZero(
832 const analysis::Type* result_type, const analysis::Constant* numerator,
833 analysis::ConstantManager* const_mgr) {
834 if (numerator == nullptr) {
835 return nullptr;
836 }
837
838 if (numerator->IsZero()) {
839 return GetNan(result_type, const_mgr);
840 }
841
842 const analysis::Constant* result = GetInf(result_type, const_mgr);
843 if (result == nullptr) {
844 return nullptr;
845 }
846
847 if (numerator->AsFloatConstant()->GetValueAsDouble() < 0.0) {
Nicolas Capens84c9c452022-11-18 14:11:05 +0000848 result = NegateFPConst(result_type, result, const_mgr);
Nicolas Capens6cacf182021-11-30 11:15:46 -0500849 }
850 return result;
851}
852
853// Returns the result of folding |numerator| / |denominator|. Returns |nullptr|
854// if it cannot be folded.
855const analysis::Constant* FoldScalarFPDivide(
856 const analysis::Type* result_type, const analysis::Constant* numerator,
857 const analysis::Constant* denominator,
858 analysis::ConstantManager* const_mgr) {
859 if (denominator == nullptr) {
860 return nullptr;
861 }
862
863 if (denominator->IsZero()) {
864 return FoldFPScalarDivideByZero(result_type, numerator, const_mgr);
865 }
866
867 const analysis::FloatConstant* denominator_float =
868 denominator->AsFloatConstant();
869 if (denominator_float && denominator->GetValueAsDouble() == -0.0) {
870 const analysis::Constant* result =
871 FoldFPScalarDivideByZero(result_type, numerator, const_mgr);
872 if (result != nullptr)
Nicolas Capens84c9c452022-11-18 14:11:05 +0000873 result = NegateFPConst(result_type, result, const_mgr);
Nicolas Capens6cacf182021-11-30 11:15:46 -0500874 return result;
875 } else {
876 return FOLD_FPARITH_OP(/)(result_type, numerator, denominator, const_mgr);
877 }
878}
879
880// Returns the constant folding rule to fold |OpFDiv| with two constants.
881ConstantFoldingRule FoldFDiv() { return FoldFPBinaryOp(FoldScalarFPDivide); }
Chris Forbescc5697f2019-01-30 11:54:08 -0800882
883bool CompareFloatingPoint(bool op_result, bool op_unordered,
884 bool need_ordered) {
885 if (need_ordered) {
886 // operands are ordered and Operand 1 is |op| Operand 2
887 return !op_unordered && op_result;
888 } else {
889 // operands are unordered or Operand 1 is |op| Operand 2
890 return op_unordered || op_result;
891 }
892}
893
894// This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
895// operator |op| must work for both float and double, and use syntax "f1 op f2".
896#define FOLD_FPCMP_OP(op, ord) \
897 [](const analysis::Type* result_type, const analysis::Constant* a, \
898 const analysis::Constant* b, \
899 analysis::ConstantManager* const_mgr) -> const analysis::Constant* { \
900 assert(result_type != nullptr && a != nullptr && b != nullptr); \
901 assert(result_type->AsBool()); \
902 assert(a->type() == b->type()); \
903 const analysis::Float* float_type = a->type()->AsFloat(); \
904 assert(float_type != nullptr); \
905 if (float_type->width() == 32) { \
906 float fa = a->GetFloat(); \
907 float fb = b->GetFloat(); \
908 bool result = CompareFloatingPoint( \
909 fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
910 std::vector<uint32_t> words = {uint32_t(result)}; \
911 return const_mgr->GetConstant(result_type, words); \
912 } else if (float_type->width() == 64) { \
913 double fa = a->GetDouble(); \
914 double fb = b->GetDouble(); \
915 bool result = CompareFloatingPoint( \
916 fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
917 std::vector<uint32_t> words = {uint32_t(result)}; \
918 return const_mgr->GetConstant(result_type, words); \
919 } \
920 return nullptr; \
921 }
922
923// Define the folding rules for ordered and unordered comparison for floating
924// point values.
925ConstantFoldingRule FoldFOrdEqual() {
926 return FoldFPBinaryOp(FOLD_FPCMP_OP(==, true));
927}
928ConstantFoldingRule FoldFUnordEqual() {
929 return FoldFPBinaryOp(FOLD_FPCMP_OP(==, false));
930}
931ConstantFoldingRule FoldFOrdNotEqual() {
932 return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, true));
933}
934ConstantFoldingRule FoldFUnordNotEqual() {
935 return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, false));
936}
937ConstantFoldingRule FoldFOrdLessThan() {
938 return FoldFPBinaryOp(FOLD_FPCMP_OP(<, true));
939}
940ConstantFoldingRule FoldFUnordLessThan() {
941 return FoldFPBinaryOp(FOLD_FPCMP_OP(<, false));
942}
943ConstantFoldingRule FoldFOrdGreaterThan() {
944 return FoldFPBinaryOp(FOLD_FPCMP_OP(>, true));
945}
946ConstantFoldingRule FoldFUnordGreaterThan() {
947 return FoldFPBinaryOp(FOLD_FPCMP_OP(>, false));
948}
949ConstantFoldingRule FoldFOrdLessThanEqual() {
950 return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, true));
951}
952ConstantFoldingRule FoldFUnordLessThanEqual() {
953 return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, false));
954}
955ConstantFoldingRule FoldFOrdGreaterThanEqual() {
956 return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, true));
957}
958ConstantFoldingRule FoldFUnordGreaterThanEqual() {
959 return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, false));
960}
961
962// Folds an OpDot where all of the inputs are constants to a
963// constant. A new constant is created if necessary.
964ConstantFoldingRule FoldOpDotWithConstants() {
965 return [](IRContext* context, Instruction* inst,
966 const std::vector<const analysis::Constant*>& constants)
967 -> const analysis::Constant* {
968 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
969 analysis::TypeManager* type_mgr = context->get_type_mgr();
970 const analysis::Type* new_type = type_mgr->GetType(inst->type_id());
971 assert(new_type->AsFloat() && "OpDot should have a float return type.");
972 const analysis::Float* float_type = new_type->AsFloat();
973
974 if (!inst->IsFloatingPointFoldingAllowed()) {
975 return nullptr;
976 }
977
978 // If one of the operands is 0, then the result is 0.
979 bool has_zero_operand = false;
980
981 for (int i = 0; i < 2; ++i) {
982 if (constants[i]) {
983 if (constants[i]->AsNullConstant() ||
984 constants[i]->AsVectorConstant()->IsZero()) {
985 has_zero_operand = true;
986 break;
987 }
988 }
989 }
990
991 if (has_zero_operand) {
992 if (float_type->width() == 32) {
993 utils::FloatProxy<float> result(0.0f);
994 std::vector<uint32_t> words = result.GetWords();
995 return const_mgr->GetConstant(float_type, words);
996 }
997 if (float_type->width() == 64) {
998 utils::FloatProxy<double> result(0.0);
999 std::vector<uint32_t> words = result.GetWords();
1000 return const_mgr->GetConstant(float_type, words);
1001 }
1002 return nullptr;
1003 }
1004
1005 if (constants[0] == nullptr || constants[1] == nullptr) {
1006 return nullptr;
1007 }
1008
1009 std::vector<const analysis::Constant*> a_components;
1010 std::vector<const analysis::Constant*> b_components;
1011
1012 a_components = constants[0]->GetVectorComponents(const_mgr);
1013 b_components = constants[1]->GetVectorComponents(const_mgr);
1014
1015 utils::FloatProxy<double> result(0.0);
1016 std::vector<uint32_t> words = result.GetWords();
1017 const analysis::Constant* result_const =
1018 const_mgr->GetConstant(float_type, words);
Ben Claytonb73b7602019-07-29 13:56:13 +01001019 for (uint32_t i = 0; i < a_components.size() && result_const != nullptr;
1020 ++i) {
Chris Forbescc5697f2019-01-30 11:54:08 -08001021 if (a_components[i] == nullptr || b_components[i] == nullptr) {
1022 return nullptr;
1023 }
1024
1025 const analysis::Constant* component = FOLD_FPARITH_OP(*)(
1026 new_type, a_components[i], b_components[i], const_mgr);
Ben Claytonb73b7602019-07-29 13:56:13 +01001027 if (component == nullptr) {
1028 return nullptr;
1029 }
Chris Forbescc5697f2019-01-30 11:54:08 -08001030 result_const =
1031 FOLD_FPARITH_OP(+)(new_type, result_const, component, const_mgr);
1032 }
1033 return result_const;
1034 };
1035}
1036
1037// This function defines a |UnaryScalarFoldingRule| that subtracts the constant
1038// from zero.
1039UnaryScalarFoldingRule FoldFNegateOp() {
1040 return [](const analysis::Type* result_type, const analysis::Constant* a,
1041 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
1042 assert(result_type != nullptr && a != nullptr);
1043 assert(result_type == a->type());
Nicolas Capens84c9c452022-11-18 14:11:05 +00001044 return NegateFPConst(result_type, a, const_mgr);
Chris Forbescc5697f2019-01-30 11:54:08 -08001045 };
1046}
1047
1048ConstantFoldingRule FoldFNegate() { return FoldFPUnaryOp(FoldFNegateOp()); }
1049
Nicolas Capens84c9c452022-11-18 14:11:05 +00001050ConstantFoldingRule FoldFClampFeedingCompare(spv::Op cmp_opcode) {
Chris Forbescc5697f2019-01-30 11:54:08 -08001051 return [cmp_opcode](IRContext* context, Instruction* inst,
1052 const std::vector<const analysis::Constant*>& constants)
1053 -> const analysis::Constant* {
1054 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
1055 analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
1056
1057 if (!inst->IsFloatingPointFoldingAllowed()) {
1058 return nullptr;
1059 }
1060
1061 uint32_t non_const_idx = (constants[0] ? 1 : 0);
1062 uint32_t operand_id = inst->GetSingleWordInOperand(non_const_idx);
1063 Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
1064
1065 analysis::TypeManager* type_mgr = context->get_type_mgr();
1066 const analysis::Type* operand_type =
1067 type_mgr->GetType(operand_inst->type_id());
1068
1069 if (!operand_type->AsFloat()) {
1070 return nullptr;
1071 }
1072
1073 if (operand_type->AsFloat()->width() != 32 &&
1074 operand_type->AsFloat()->width() != 64) {
1075 return nullptr;
1076 }
1077
Nicolas Capens84c9c452022-11-18 14:11:05 +00001078 if (operand_inst->opcode() != spv::Op::OpExtInst) {
Chris Forbescc5697f2019-01-30 11:54:08 -08001079 return nullptr;
1080 }
1081
1082 if (operand_inst->GetSingleWordInOperand(1) != GLSLstd450FClamp) {
1083 return nullptr;
1084 }
1085
1086 if (constants[1] == nullptr && constants[0] == nullptr) {
1087 return nullptr;
1088 }
1089
1090 uint32_t max_id = operand_inst->GetSingleWordInOperand(4);
1091 const analysis::Constant* max_const =
1092 const_mgr->FindDeclaredConstant(max_id);
1093
1094 uint32_t min_id = operand_inst->GetSingleWordInOperand(3);
1095 const analysis::Constant* min_const =
1096 const_mgr->FindDeclaredConstant(min_id);
1097
1098 bool found_result = false;
1099 bool result = false;
1100
1101 switch (cmp_opcode) {
Nicolas Capens84c9c452022-11-18 14:11:05 +00001102 case spv::Op::OpFOrdLessThan:
1103 case spv::Op::OpFUnordLessThan:
1104 case spv::Op::OpFOrdGreaterThanEqual:
1105 case spv::Op::OpFUnordGreaterThanEqual:
Chris Forbescc5697f2019-01-30 11:54:08 -08001106 if (constants[0]) {
1107 if (min_const) {
1108 if (constants[0]->GetValueAsDouble() <
1109 min_const->GetValueAsDouble()) {
1110 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001111 result = (cmp_opcode == spv::Op::OpFOrdLessThan ||
1112 cmp_opcode == spv::Op::OpFUnordLessThan);
Chris Forbescc5697f2019-01-30 11:54:08 -08001113 }
1114 }
1115 if (max_const) {
1116 if (constants[0]->GetValueAsDouble() >=
1117 max_const->GetValueAsDouble()) {
1118 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001119 result = !(cmp_opcode == spv::Op::OpFOrdLessThan ||
1120 cmp_opcode == spv::Op::OpFUnordLessThan);
Chris Forbescc5697f2019-01-30 11:54:08 -08001121 }
1122 }
1123 }
1124
1125 if (constants[1]) {
1126 if (max_const) {
1127 if (max_const->GetValueAsDouble() <
1128 constants[1]->GetValueAsDouble()) {
1129 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001130 result = (cmp_opcode == spv::Op::OpFOrdLessThan ||
1131 cmp_opcode == spv::Op::OpFUnordLessThan);
Chris Forbescc5697f2019-01-30 11:54:08 -08001132 }
1133 }
1134
1135 if (min_const) {
1136 if (min_const->GetValueAsDouble() >=
1137 constants[1]->GetValueAsDouble()) {
1138 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001139 result = !(cmp_opcode == spv::Op::OpFOrdLessThan ||
1140 cmp_opcode == spv::Op::OpFUnordLessThan);
Chris Forbescc5697f2019-01-30 11:54:08 -08001141 }
1142 }
1143 }
1144 break;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001145 case spv::Op::OpFOrdGreaterThan:
1146 case spv::Op::OpFUnordGreaterThan:
1147 case spv::Op::OpFOrdLessThanEqual:
1148 case spv::Op::OpFUnordLessThanEqual:
Chris Forbescc5697f2019-01-30 11:54:08 -08001149 if (constants[0]) {
1150 if (min_const) {
1151 if (constants[0]->GetValueAsDouble() <=
1152 min_const->GetValueAsDouble()) {
1153 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001154 result = (cmp_opcode == spv::Op::OpFOrdLessThanEqual ||
1155 cmp_opcode == spv::Op::OpFUnordLessThanEqual);
Chris Forbescc5697f2019-01-30 11:54:08 -08001156 }
1157 }
1158 if (max_const) {
1159 if (constants[0]->GetValueAsDouble() >
1160 max_const->GetValueAsDouble()) {
1161 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001162 result = !(cmp_opcode == spv::Op::OpFOrdLessThanEqual ||
1163 cmp_opcode == spv::Op::OpFUnordLessThanEqual);
Chris Forbescc5697f2019-01-30 11:54:08 -08001164 }
1165 }
1166 }
1167
1168 if (constants[1]) {
1169 if (max_const) {
1170 if (max_const->GetValueAsDouble() <=
1171 constants[1]->GetValueAsDouble()) {
1172 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001173 result = (cmp_opcode == spv::Op::OpFOrdLessThanEqual ||
1174 cmp_opcode == spv::Op::OpFUnordLessThanEqual);
Chris Forbescc5697f2019-01-30 11:54:08 -08001175 }
1176 }
1177
1178 if (min_const) {
1179 if (min_const->GetValueAsDouble() >
1180 constants[1]->GetValueAsDouble()) {
1181 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001182 result = !(cmp_opcode == spv::Op::OpFOrdLessThanEqual ||
1183 cmp_opcode == spv::Op::OpFUnordLessThanEqual);
Chris Forbescc5697f2019-01-30 11:54:08 -08001184 }
1185 }
1186 }
1187 break;
1188 default:
1189 return nullptr;
1190 }
1191
1192 if (!found_result) {
1193 return nullptr;
1194 }
1195
1196 const analysis::Type* bool_type =
1197 context->get_type_mgr()->GetType(inst->type_id());
1198 const analysis::Constant* result_const =
1199 const_mgr->GetConstant(bool_type, {static_cast<uint32_t>(result)});
1200 assert(result_const);
1201 return result_const;
1202 };
1203}
1204
Ben Claytond0f684e2019-08-30 22:36:08 +01001205ConstantFoldingRule FoldFMix() {
1206 return [](IRContext* context, Instruction* inst,
1207 const std::vector<const analysis::Constant*>& constants)
1208 -> const analysis::Constant* {
1209 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
Nicolas Capens84c9c452022-11-18 14:11:05 +00001210 assert(inst->opcode() == spv::Op::OpExtInst &&
Ben Claytond0f684e2019-08-30 22:36:08 +01001211 "Expecting an extended instruction.");
1212 assert(inst->GetSingleWordInOperand(0) ==
1213 context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
1214 "Expecting a GLSLstd450 extended instruction.");
1215 assert(inst->GetSingleWordInOperand(1) == GLSLstd450FMix &&
1216 "Expecting and FMix instruction.");
1217
1218 if (!inst->IsFloatingPointFoldingAllowed()) {
1219 return nullptr;
1220 }
1221
1222 // Make sure all FMix operands are constants.
1223 for (uint32_t i = 1; i < 4; i++) {
1224 if (constants[i] == nullptr) {
1225 return nullptr;
1226 }
1227 }
1228
1229 const analysis::Constant* one;
Ben Claytond552f632019-11-18 11:18:41 +00001230 bool is_vector = false;
1231 const analysis::Type* result_type = constants[1]->type();
1232 const analysis::Type* base_type = result_type;
1233 if (base_type->AsVector()) {
1234 is_vector = true;
1235 base_type = base_type->AsVector()->element_type();
1236 }
1237 assert(base_type->AsFloat() != nullptr &&
1238 "FMix is suppose to act on floats or vectors of floats.");
1239
1240 if (base_type->AsFloat()->width() == 32) {
1241 one = const_mgr->GetConstant(base_type,
Ben Claytond0f684e2019-08-30 22:36:08 +01001242 utils::FloatProxy<float>(1.0f).GetWords());
1243 } else {
Ben Claytond552f632019-11-18 11:18:41 +00001244 one = const_mgr->GetConstant(base_type,
Ben Claytond0f684e2019-08-30 22:36:08 +01001245 utils::FloatProxy<double>(1.0).GetWords());
1246 }
1247
Ben Claytond552f632019-11-18 11:18:41 +00001248 if (is_vector) {
1249 uint32_t one_id = const_mgr->GetDefiningInstruction(one)->result_id();
1250 one =
1251 const_mgr->GetConstant(result_type, std::vector<uint32_t>(4, one_id));
1252 }
1253
1254 const analysis::Constant* temp1 = FoldFPBinaryOp(
1255 FOLD_FPARITH_OP(-), inst->type_id(), {one, constants[3]}, context);
Ben Claytond0f684e2019-08-30 22:36:08 +01001256 if (temp1 == nullptr) {
1257 return nullptr;
1258 }
1259
Ben Claytond552f632019-11-18 11:18:41 +00001260 const analysis::Constant* temp2 = FoldFPBinaryOp(
1261 FOLD_FPARITH_OP(*), inst->type_id(), {constants[1], temp1}, context);
Ben Claytond0f684e2019-08-30 22:36:08 +01001262 if (temp2 == nullptr) {
1263 return nullptr;
1264 }
Ben Claytond552f632019-11-18 11:18:41 +00001265 const analysis::Constant* temp3 =
1266 FoldFPBinaryOp(FOLD_FPARITH_OP(*), inst->type_id(),
1267 {constants[2], constants[3]}, context);
Ben Claytond0f684e2019-08-30 22:36:08 +01001268 if (temp3 == nullptr) {
1269 return nullptr;
1270 }
Ben Claytond552f632019-11-18 11:18:41 +00001271 return FoldFPBinaryOp(FOLD_FPARITH_OP(+), inst->type_id(), {temp2, temp3},
1272 context);
Ben Claytond0f684e2019-08-30 22:36:08 +01001273 };
1274}
1275
Ben Claytond552f632019-11-18 11:18:41 +00001276const analysis::Constant* FoldMin(const analysis::Type* result_type,
1277 const analysis::Constant* a,
1278 const analysis::Constant* b,
1279 analysis::ConstantManager*) {
1280 if (const analysis::Integer* int_type = result_type->AsInteger()) {
1281 if (int_type->width() == 32) {
1282 if (int_type->IsSigned()) {
1283 int32_t va = a->GetS32();
1284 int32_t vb = b->GetS32();
1285 return (va < vb ? a : b);
1286 } else {
1287 uint32_t va = a->GetU32();
1288 uint32_t vb = b->GetU32();
1289 return (va < vb ? a : b);
1290 }
1291 } else if (int_type->width() == 64) {
1292 if (int_type->IsSigned()) {
1293 int64_t va = a->GetS64();
1294 int64_t vb = b->GetS64();
1295 return (va < vb ? a : b);
1296 } else {
1297 uint64_t va = a->GetU64();
1298 uint64_t vb = b->GetU64();
1299 return (va < vb ? a : b);
1300 }
1301 }
1302 } else if (const analysis::Float* float_type = result_type->AsFloat()) {
1303 if (float_type->width() == 32) {
1304 float va = a->GetFloat();
1305 float vb = b->GetFloat();
1306 return (va < vb ? a : b);
1307 } else if (float_type->width() == 64) {
1308 double va = a->GetDouble();
1309 double vb = b->GetDouble();
1310 return (va < vb ? a : b);
1311 }
1312 }
1313 return nullptr;
1314}
1315
1316const analysis::Constant* FoldMax(const analysis::Type* result_type,
1317 const analysis::Constant* a,
1318 const analysis::Constant* b,
1319 analysis::ConstantManager*) {
1320 if (const analysis::Integer* int_type = result_type->AsInteger()) {
1321 if (int_type->width() == 32) {
1322 if (int_type->IsSigned()) {
1323 int32_t va = a->GetS32();
1324 int32_t vb = b->GetS32();
1325 return (va > vb ? a : b);
1326 } else {
1327 uint32_t va = a->GetU32();
1328 uint32_t vb = b->GetU32();
1329 return (va > vb ? a : b);
1330 }
1331 } else if (int_type->width() == 64) {
1332 if (int_type->IsSigned()) {
1333 int64_t va = a->GetS64();
1334 int64_t vb = b->GetS64();
1335 return (va > vb ? a : b);
1336 } else {
1337 uint64_t va = a->GetU64();
1338 uint64_t vb = b->GetU64();
1339 return (va > vb ? a : b);
1340 }
1341 }
1342 } else if (const analysis::Float* float_type = result_type->AsFloat()) {
1343 if (float_type->width() == 32) {
1344 float va = a->GetFloat();
1345 float vb = b->GetFloat();
1346 return (va > vb ? a : b);
1347 } else if (float_type->width() == 64) {
1348 double va = a->GetDouble();
1349 double vb = b->GetDouble();
1350 return (va > vb ? a : b);
1351 }
1352 }
1353 return nullptr;
1354}
1355
1356// Fold an clamp instruction when all three operands are constant.
1357const analysis::Constant* FoldClamp1(
1358 IRContext* context, Instruction* inst,
1359 const std::vector<const analysis::Constant*>& constants) {
Nicolas Capens84c9c452022-11-18 14:11:05 +00001360 assert(inst->opcode() == spv::Op::OpExtInst &&
Ben Claytond552f632019-11-18 11:18:41 +00001361 "Expecting an extended instruction.");
1362 assert(inst->GetSingleWordInOperand(0) ==
1363 context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
1364 "Expecting a GLSLstd450 extended instruction.");
1365
1366 // Make sure all Clamp operands are constants.
Alexis Hetu00e0af12021-11-08 08:57:46 -05001367 for (uint32_t i = 1; i < 4; i++) {
Ben Claytond552f632019-11-18 11:18:41 +00001368 if (constants[i] == nullptr) {
1369 return nullptr;
1370 }
1371 }
1372
1373 const analysis::Constant* temp = FoldFPBinaryOp(
1374 FoldMax, inst->type_id(), {constants[1], constants[2]}, context);
1375 if (temp == nullptr) {
1376 return nullptr;
1377 }
1378 return FoldFPBinaryOp(FoldMin, inst->type_id(), {temp, constants[3]},
1379 context);
1380}
1381
Alexis Hetu00e0af12021-11-08 08:57:46 -05001382// Fold a clamp instruction when |x <= min_val|.
Ben Claytond552f632019-11-18 11:18:41 +00001383const analysis::Constant* FoldClamp2(
1384 IRContext* context, Instruction* inst,
1385 const std::vector<const analysis::Constant*>& constants) {
Nicolas Capens84c9c452022-11-18 14:11:05 +00001386 assert(inst->opcode() == spv::Op::OpExtInst &&
Ben Claytond552f632019-11-18 11:18:41 +00001387 "Expecting an extended instruction.");
1388 assert(inst->GetSingleWordInOperand(0) ==
1389 context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
1390 "Expecting a GLSLstd450 extended instruction.");
1391
1392 const analysis::Constant* x = constants[1];
1393 const analysis::Constant* min_val = constants[2];
1394
1395 if (x == nullptr || min_val == nullptr) {
1396 return nullptr;
1397 }
1398
1399 const analysis::Constant* temp =
1400 FoldFPBinaryOp(FoldMax, inst->type_id(), {x, min_val}, context);
1401 if (temp == min_val) {
1402 // We can assume that |min_val| is less than |max_val|. Therefore, if the
1403 // result of the max operation is |min_val|, we know the result of the min
1404 // operation, even if |max_val| is not a constant.
1405 return min_val;
1406 }
1407 return nullptr;
1408}
1409
1410// Fold a clamp instruction when |x >= max_val|.
1411const analysis::Constant* FoldClamp3(
1412 IRContext* context, Instruction* inst,
1413 const std::vector<const analysis::Constant*>& constants) {
Nicolas Capens84c9c452022-11-18 14:11:05 +00001414 assert(inst->opcode() == spv::Op::OpExtInst &&
Ben Claytond552f632019-11-18 11:18:41 +00001415 "Expecting an extended instruction.");
1416 assert(inst->GetSingleWordInOperand(0) ==
1417 context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
1418 "Expecting a GLSLstd450 extended instruction.");
1419
1420 const analysis::Constant* x = constants[1];
1421 const analysis::Constant* max_val = constants[3];
1422
1423 if (x == nullptr || max_val == nullptr) {
1424 return nullptr;
1425 }
1426
1427 const analysis::Constant* temp =
1428 FoldFPBinaryOp(FoldMin, inst->type_id(), {x, max_val}, context);
1429 if (temp == max_val) {
1430 // We can assume that |min_val| is less than |max_val|. Therefore, if the
1431 // result of the max operation is |min_val|, we know the result of the min
1432 // operation, even if |max_val| is not a constant.
1433 return max_val;
1434 }
1435 return nullptr;
1436}
1437
Ben Claytondc6b76a2020-02-24 14:53:40 +00001438UnaryScalarFoldingRule FoldFTranscendentalUnary(double (*fp)(double)) {
1439 return
1440 [fp](const analysis::Type* result_type, const analysis::Constant* a,
1441 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
1442 assert(result_type != nullptr && a != nullptr);
1443 const analysis::Float* float_type = a->type()->AsFloat();
1444 assert(float_type != nullptr);
1445 assert(float_type == result_type->AsFloat());
1446 if (float_type->width() == 32) {
1447 float fa = a->GetFloat();
1448 float res = static_cast<float>(fp(fa));
1449 utils::FloatProxy<float> result(res);
1450 std::vector<uint32_t> words = result.GetWords();
1451 return const_mgr->GetConstant(result_type, words);
1452 } else if (float_type->width() == 64) {
1453 double fa = a->GetDouble();
1454 double res = fp(fa);
1455 utils::FloatProxy<double> result(res);
1456 std::vector<uint32_t> words = result.GetWords();
1457 return const_mgr->GetConstant(result_type, words);
1458 }
1459 return nullptr;
1460 };
1461}
1462
1463BinaryScalarFoldingRule FoldFTranscendentalBinary(double (*fp)(double,
1464 double)) {
1465 return
1466 [fp](const analysis::Type* result_type, const analysis::Constant* a,
1467 const analysis::Constant* b,
1468 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
1469 assert(result_type != nullptr && a != nullptr);
1470 const analysis::Float* float_type = a->type()->AsFloat();
1471 assert(float_type != nullptr);
1472 assert(float_type == result_type->AsFloat());
1473 assert(float_type == b->type()->AsFloat());
1474 if (float_type->width() == 32) {
1475 float fa = a->GetFloat();
1476 float fb = b->GetFloat();
1477 float res = static_cast<float>(fp(fa, fb));
1478 utils::FloatProxy<float> result(res);
1479 std::vector<uint32_t> words = result.GetWords();
1480 return const_mgr->GetConstant(result_type, words);
1481 } else if (float_type->width() == 64) {
1482 double fa = a->GetDouble();
1483 double fb = b->GetDouble();
1484 double res = fp(fa, fb);
1485 utils::FloatProxy<double> result(res);
1486 std::vector<uint32_t> words = result.GetWords();
1487 return const_mgr->GetConstant(result_type, words);
1488 }
1489 return nullptr;
1490 };
1491}
Chris Forbescc5697f2019-01-30 11:54:08 -08001492} // namespace
1493
Ben Claytond0f684e2019-08-30 22:36:08 +01001494void ConstantFoldingRules::AddFoldingRules() {
Chris Forbescc5697f2019-01-30 11:54:08 -08001495 // Add all folding rules to the list for the opcodes to which they apply.
1496 // Note that the order in which rules are added to the list matters. If a rule
1497 // applies to the instruction, the rest of the rules will not be attempted.
1498 // Take that into consideration.
1499
Nicolas Capens84c9c452022-11-18 14:11:05 +00001500 rules_[spv::Op::OpCompositeConstruct].push_back(FoldCompositeWithConstants());
Chris Forbescc5697f2019-01-30 11:54:08 -08001501
Nicolas Capens84c9c452022-11-18 14:11:05 +00001502 rules_[spv::Op::OpCompositeExtract].push_back(FoldExtractWithConstants());
1503 rules_[spv::Op::OpCompositeInsert].push_back(FoldInsertWithConstants());
Chris Forbescc5697f2019-01-30 11:54:08 -08001504
Nicolas Capens84c9c452022-11-18 14:11:05 +00001505 rules_[spv::Op::OpConvertFToS].push_back(FoldFToI());
1506 rules_[spv::Op::OpConvertFToU].push_back(FoldFToI());
1507 rules_[spv::Op::OpConvertSToF].push_back(FoldIToF());
1508 rules_[spv::Op::OpConvertUToF].push_back(FoldIToF());
Chris Forbescc5697f2019-01-30 11:54:08 -08001509
Nicolas Capens84c9c452022-11-18 14:11:05 +00001510 rules_[spv::Op::OpDot].push_back(FoldOpDotWithConstants());
1511 rules_[spv::Op::OpFAdd].push_back(FoldFAdd());
1512 rules_[spv::Op::OpFDiv].push_back(FoldFDiv());
1513 rules_[spv::Op::OpFMul].push_back(FoldFMul());
1514 rules_[spv::Op::OpFSub].push_back(FoldFSub());
Chris Forbescc5697f2019-01-30 11:54:08 -08001515
Nicolas Capens84c9c452022-11-18 14:11:05 +00001516 rules_[spv::Op::OpFOrdEqual].push_back(FoldFOrdEqual());
Chris Forbescc5697f2019-01-30 11:54:08 -08001517
Nicolas Capens84c9c452022-11-18 14:11:05 +00001518 rules_[spv::Op::OpFUnordEqual].push_back(FoldFUnordEqual());
Chris Forbescc5697f2019-01-30 11:54:08 -08001519
Nicolas Capens84c9c452022-11-18 14:11:05 +00001520 rules_[spv::Op::OpFOrdNotEqual].push_back(FoldFOrdNotEqual());
Chris Forbescc5697f2019-01-30 11:54:08 -08001521
Nicolas Capens84c9c452022-11-18 14:11:05 +00001522 rules_[spv::Op::OpFUnordNotEqual].push_back(FoldFUnordNotEqual());
Chris Forbescc5697f2019-01-30 11:54:08 -08001523
Nicolas Capens84c9c452022-11-18 14:11:05 +00001524 rules_[spv::Op::OpFOrdLessThan].push_back(FoldFOrdLessThan());
1525 rules_[spv::Op::OpFOrdLessThan].push_back(
1526 FoldFClampFeedingCompare(spv::Op::OpFOrdLessThan));
Chris Forbescc5697f2019-01-30 11:54:08 -08001527
Nicolas Capens84c9c452022-11-18 14:11:05 +00001528 rules_[spv::Op::OpFUnordLessThan].push_back(FoldFUnordLessThan());
1529 rules_[spv::Op::OpFUnordLessThan].push_back(
1530 FoldFClampFeedingCompare(spv::Op::OpFUnordLessThan));
Chris Forbescc5697f2019-01-30 11:54:08 -08001531
Nicolas Capens84c9c452022-11-18 14:11:05 +00001532 rules_[spv::Op::OpFOrdGreaterThan].push_back(FoldFOrdGreaterThan());
1533 rules_[spv::Op::OpFOrdGreaterThan].push_back(
1534 FoldFClampFeedingCompare(spv::Op::OpFOrdGreaterThan));
Chris Forbescc5697f2019-01-30 11:54:08 -08001535
Nicolas Capens84c9c452022-11-18 14:11:05 +00001536 rules_[spv::Op::OpFUnordGreaterThan].push_back(FoldFUnordGreaterThan());
1537 rules_[spv::Op::OpFUnordGreaterThan].push_back(
1538 FoldFClampFeedingCompare(spv::Op::OpFUnordGreaterThan));
Chris Forbescc5697f2019-01-30 11:54:08 -08001539
Nicolas Capens84c9c452022-11-18 14:11:05 +00001540 rules_[spv::Op::OpFOrdLessThanEqual].push_back(FoldFOrdLessThanEqual());
1541 rules_[spv::Op::OpFOrdLessThanEqual].push_back(
1542 FoldFClampFeedingCompare(spv::Op::OpFOrdLessThanEqual));
Chris Forbescc5697f2019-01-30 11:54:08 -08001543
Nicolas Capens84c9c452022-11-18 14:11:05 +00001544 rules_[spv::Op::OpFUnordLessThanEqual].push_back(FoldFUnordLessThanEqual());
1545 rules_[spv::Op::OpFUnordLessThanEqual].push_back(
1546 FoldFClampFeedingCompare(spv::Op::OpFUnordLessThanEqual));
Chris Forbescc5697f2019-01-30 11:54:08 -08001547
Nicolas Capens84c9c452022-11-18 14:11:05 +00001548 rules_[spv::Op::OpFOrdGreaterThanEqual].push_back(FoldFOrdGreaterThanEqual());
1549 rules_[spv::Op::OpFOrdGreaterThanEqual].push_back(
1550 FoldFClampFeedingCompare(spv::Op::OpFOrdGreaterThanEqual));
Chris Forbescc5697f2019-01-30 11:54:08 -08001551
Nicolas Capens84c9c452022-11-18 14:11:05 +00001552 rules_[spv::Op::OpFUnordGreaterThanEqual].push_back(
1553 FoldFUnordGreaterThanEqual());
1554 rules_[spv::Op::OpFUnordGreaterThanEqual].push_back(
1555 FoldFClampFeedingCompare(spv::Op::OpFUnordGreaterThanEqual));
Chris Forbescc5697f2019-01-30 11:54:08 -08001556
Nicolas Capens84c9c452022-11-18 14:11:05 +00001557 rules_[spv::Op::OpVectorShuffle].push_back(FoldVectorShuffleWithConstants());
1558 rules_[spv::Op::OpVectorTimesScalar].push_back(FoldVectorTimesScalar());
1559 rules_[spv::Op::OpVectorTimesMatrix].push_back(FoldVectorTimesMatrix());
1560 rules_[spv::Op::OpMatrixTimesVector].push_back(FoldMatrixTimesVector());
Chris Forbescc5697f2019-01-30 11:54:08 -08001561
Nicolas Capens84c9c452022-11-18 14:11:05 +00001562 rules_[spv::Op::OpFNegate].push_back(FoldFNegate());
1563 rules_[spv::Op::OpQuantizeToF16].push_back(FoldQuantizeToF16());
Ben Claytond0f684e2019-08-30 22:36:08 +01001564
1565 // Add rules for GLSLstd450
1566 FeatureManager* feature_manager = context_->get_feature_mgr();
1567 uint32_t ext_inst_glslstd450_id =
1568 feature_manager->GetExtInstImportId_GLSLstd450();
1569 if (ext_inst_glslstd450_id != 0) {
1570 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMix}].push_back(FoldFMix());
Ben Claytond552f632019-11-18 11:18:41 +00001571 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMin}].push_back(
1572 FoldFPBinaryOp(FoldMin));
1573 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMin}].push_back(
1574 FoldFPBinaryOp(FoldMin));
1575 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMin}].push_back(
1576 FoldFPBinaryOp(FoldMin));
1577 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMax}].push_back(
1578 FoldFPBinaryOp(FoldMax));
1579 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMax}].push_back(
1580 FoldFPBinaryOp(FoldMax));
1581 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMax}].push_back(
1582 FoldFPBinaryOp(FoldMax));
1583 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
1584 FoldClamp1);
1585 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
1586 FoldClamp2);
1587 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
1588 FoldClamp3);
1589 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
1590 FoldClamp1);
1591 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
1592 FoldClamp2);
1593 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
1594 FoldClamp3);
1595 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
1596 FoldClamp1);
1597 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
1598 FoldClamp2);
1599 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
1600 FoldClamp3);
Ben Claytondc6b76a2020-02-24 14:53:40 +00001601 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Sin}].push_back(
1602 FoldFPUnaryOp(FoldFTranscendentalUnary(std::sin)));
1603 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Cos}].push_back(
1604 FoldFPUnaryOp(FoldFTranscendentalUnary(std::cos)));
1605 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Tan}].push_back(
1606 FoldFPUnaryOp(FoldFTranscendentalUnary(std::tan)));
1607 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Asin}].push_back(
1608 FoldFPUnaryOp(FoldFTranscendentalUnary(std::asin)));
1609 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Acos}].push_back(
1610 FoldFPUnaryOp(FoldFTranscendentalUnary(std::acos)));
1611 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Atan}].push_back(
1612 FoldFPUnaryOp(FoldFTranscendentalUnary(std::atan)));
1613 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp}].push_back(
1614 FoldFPUnaryOp(FoldFTranscendentalUnary(std::exp)));
1615 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log}].push_back(
1616 FoldFPUnaryOp(FoldFTranscendentalUnary(std::log)));
1617
1618#ifdef __ANDROID__
sugoi1b398bf32022-02-18 10:27:28 -05001619 // Android NDK r15c targeting ABI 15 doesn't have full support for C++11
Ben Claytondc6b76a2020-02-24 14:53:40 +00001620 // (no std::exp2/log2). ::exp2 is available from C99 but ::log2 isn't
1621 // available up until ABI 18 so we use a shim
1622 auto log2_shim = [](double v) -> double { return log(v) / log(2.0); };
1623 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp2}].push_back(
1624 FoldFPUnaryOp(FoldFTranscendentalUnary(::exp2)));
1625 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log2}].push_back(
1626 FoldFPUnaryOp(FoldFTranscendentalUnary(log2_shim)));
1627#else
1628 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp2}].push_back(
1629 FoldFPUnaryOp(FoldFTranscendentalUnary(std::exp2)));
1630 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log2}].push_back(
1631 FoldFPUnaryOp(FoldFTranscendentalUnary(std::log2)));
1632#endif
1633
1634 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Sqrt}].push_back(
1635 FoldFPUnaryOp(FoldFTranscendentalUnary(std::sqrt)));
1636 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Atan2}].push_back(
1637 FoldFPBinaryOp(FoldFTranscendentalBinary(std::atan2)));
1638 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Pow}].push_back(
1639 FoldFPBinaryOp(FoldFTranscendentalBinary(std::pow)));
Ben Claytond0f684e2019-08-30 22:36:08 +01001640 }
Chris Forbescc5697f2019-01-30 11:54:08 -08001641}
1642} // namespace opt
1643} // namespace spvtools