blob: 14f22089b47a523f52f4030ba8cd40ac13d06a71 [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
148 const auto new_composite = const_mgr->GetNullCompositeConstant(type);
149 // Keep track of any indexes along the way to last index
150 if (i != final_index) {
151 chain.push_back(new_composite);
152 }
153 components = new_composite->AsCompositeConstant()->GetComponents();
154 } else {
155 // Keep track of any indexes along the way to last index
156 if (i != final_index) {
157 chain.push_back(composite);
158 }
159 components = composite->AsCompositeConstant()->GetComponents();
Nicolas Capens84c9c452022-11-18 14:11:05 +0000160 }
161 const uint32_t index = inst->GetSingleWordInOperand(i);
Nicolas Capens84c9c452022-11-18 14:11:05 +0000162 composite = components[index];
163 }
164
165 // Final index in hierarchy is inserted with new object.
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500166 const uint32_t final_operand = inst->GetSingleWordInOperand(final_index);
Nicolas Capens84c9c452022-11-18 14:11:05 +0000167 std::vector<uint32_t> ids;
168 for (size_t i = 0; i < components.size(); i++) {
169 const analysis::Constant* constant =
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500170 (i == final_operand) ? object : components[i];
Nicolas Capens84c9c452022-11-18 14:11:05 +0000171 Instruction* member_inst = const_mgr->GetDefiningInstruction(constant);
172 ids.push_back(member_inst->result_id());
173 }
174 const analysis::Constant* new_constant = const_mgr->GetConstant(type, ids);
175
176 // Work backwards up the chain and replace each index with new constant.
177 for (size_t i = chain.size(); i > 0; i--) {
178 // Need to insert any previous instruction into the module first.
179 // Can't just insert in types_values_begin() because it will move above
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500180 // where the types are declared.
181 // Can't compare with location of inst because not all new added
182 // instructions are added to types_values_
183 auto iter = context->types_values_end();
184 Module::inst_iterator* pos = &iter;
185 const_mgr->BuildInstructionAndAddToModule(new_constant, pos);
Nicolas Capens84c9c452022-11-18 14:11:05 +0000186
187 composite = chain[i - 1];
188 components = composite->AsCompositeConstant()->GetComponents();
Alexis Hetu34bbeae2022-12-06 09:37:55 -0500189 type = composite->type();
Nicolas Capens84c9c452022-11-18 14:11:05 +0000190 ids.clear();
191 for (size_t k = 0; k < components.size(); k++) {
192 const uint32_t index =
193 inst->GetSingleWordInOperand(1 + static_cast<uint32_t>(i));
194 const analysis::Constant* constant =
195 (k == index) ? new_constant : components[k];
196 const uint32_t constant_id =
197 const_mgr->FindDeclaredConstant(constant, 0);
198 ids.push_back(constant_id);
199 }
200 new_constant = const_mgr->GetConstant(type, ids);
201 }
202
203 // If multiple constants were created, only need to return the top index.
204 return new_constant;
205 };
206}
207
Chris Forbescc5697f2019-01-30 11:54:08 -0800208ConstantFoldingRule FoldVectorShuffleWithConstants() {
209 return [](IRContext* context, Instruction* inst,
210 const std::vector<const analysis::Constant*>& constants)
211 -> const analysis::Constant* {
Nicolas Capens84c9c452022-11-18 14:11:05 +0000212 assert(inst->opcode() == spv::Op::OpVectorShuffle);
Chris Forbescc5697f2019-01-30 11:54:08 -0800213 const analysis::Constant* c1 = constants[0];
214 const analysis::Constant* c2 = constants[1];
215 if (c1 == nullptr || c2 == nullptr) {
216 return nullptr;
217 }
218
219 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
220 const analysis::Type* element_type = c1->type()->AsVector()->element_type();
221
222 std::vector<const analysis::Constant*> c1_components;
223 if (const analysis::VectorConstant* vec_const = c1->AsVectorConstant()) {
224 c1_components = vec_const->GetComponents();
225 } else {
226 assert(c1->AsNullConstant());
227 const analysis::Constant* element =
228 const_mgr->GetConstant(element_type, {});
229 c1_components.resize(c1->type()->AsVector()->element_count(), element);
230 }
231 std::vector<const analysis::Constant*> c2_components;
232 if (const analysis::VectorConstant* vec_const = c2->AsVectorConstant()) {
233 c2_components = vec_const->GetComponents();
234 } else {
235 assert(c2->AsNullConstant());
236 const analysis::Constant* element =
237 const_mgr->GetConstant(element_type, {});
238 c2_components.resize(c2->type()->AsVector()->element_count(), element);
239 }
240
241 std::vector<uint32_t> ids;
242 const uint32_t undef_literal_value = 0xffffffff;
243 for (uint32_t i = 2; i < inst->NumInOperands(); ++i) {
244 uint32_t index = inst->GetSingleWordInOperand(i);
245 if (index == undef_literal_value) {
246 // Don't fold shuffle with undef literal value.
247 return nullptr;
248 } else if (index < c1_components.size()) {
249 Instruction* member_inst =
250 const_mgr->GetDefiningInstruction(c1_components[index]);
251 ids.push_back(member_inst->result_id());
252 } else {
253 Instruction* member_inst = const_mgr->GetDefiningInstruction(
254 c2_components[index - c1_components.size()]);
255 ids.push_back(member_inst->result_id());
256 }
257 }
258
259 analysis::TypeManager* type_mgr = context->get_type_mgr();
260 return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), ids);
261 };
262}
263
264ConstantFoldingRule FoldVectorTimesScalar() {
265 return [](IRContext* context, Instruction* inst,
266 const std::vector<const analysis::Constant*>& constants)
267 -> const analysis::Constant* {
Nicolas Capens84c9c452022-11-18 14:11:05 +0000268 assert(inst->opcode() == spv::Op::OpVectorTimesScalar);
Chris Forbescc5697f2019-01-30 11:54:08 -0800269 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
270 analysis::TypeManager* type_mgr = context->get_type_mgr();
271
272 if (!inst->IsFloatingPointFoldingAllowed()) {
273 if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
274 return nullptr;
275 }
276 }
277
278 const analysis::Constant* c1 = constants[0];
279 const analysis::Constant* c2 = constants[1];
280
281 if (c1 && c1->IsZero()) {
282 return c1;
283 }
284
285 if (c2 && c2->IsZero()) {
286 // Get or create the NullConstant for this type.
287 std::vector<uint32_t> ids;
288 return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), ids);
289 }
290
291 if (c1 == nullptr || c2 == nullptr) {
292 return nullptr;
293 }
294
295 // Check result type.
296 const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
297 const analysis::Vector* vector_type = result_type->AsVector();
298 assert(vector_type != nullptr);
299 const analysis::Type* element_type = vector_type->element_type();
300 assert(element_type != nullptr);
301 const analysis::Float* float_type = element_type->AsFloat();
302 assert(float_type != nullptr);
303
304 // Check types of c1 and c2.
305 assert(c1->type()->AsVector() == vector_type);
306 assert(c1->type()->AsVector()->element_type() == element_type &&
307 c2->type() == element_type);
308
309 // Get a float vector that is the result of vector-times-scalar.
310 std::vector<const analysis::Constant*> c1_components =
311 c1->GetVectorComponents(const_mgr);
312 std::vector<uint32_t> ids;
313 if (float_type->width() == 32) {
314 float scalar = c2->GetFloat();
315 for (uint32_t i = 0; i < c1_components.size(); ++i) {
316 utils::FloatProxy<float> result(c1_components[i]->GetFloat() * scalar);
317 std::vector<uint32_t> words = result.GetWords();
318 const analysis::Constant* new_elem =
319 const_mgr->GetConstant(float_type, words);
320 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
321 }
322 return const_mgr->GetConstant(vector_type, ids);
323 } else if (float_type->width() == 64) {
324 double scalar = c2->GetDouble();
325 for (uint32_t i = 0; i < c1_components.size(); ++i) {
326 utils::FloatProxy<double> result(c1_components[i]->GetDouble() *
327 scalar);
328 std::vector<uint32_t> words = result.GetWords();
329 const analysis::Constant* new_elem =
330 const_mgr->GetConstant(float_type, words);
331 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
332 }
333 return const_mgr->GetConstant(vector_type, ids);
334 }
335 return nullptr;
336 };
337}
338
Nicolas Capens00a1bcc2022-07-29 16:49:40 -0400339ConstantFoldingRule FoldVectorTimesMatrix() {
340 return [](IRContext* context, Instruction* inst,
341 const std::vector<const analysis::Constant*>& constants)
342 -> const analysis::Constant* {
Nicolas Capens84c9c452022-11-18 14:11:05 +0000343 assert(inst->opcode() == spv::Op::OpVectorTimesMatrix);
Nicolas Capens00a1bcc2022-07-29 16:49:40 -0400344 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
345 analysis::TypeManager* type_mgr = context->get_type_mgr();
346
347 if (!inst->IsFloatingPointFoldingAllowed()) {
348 if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
349 return nullptr;
350 }
351 }
352
353 const analysis::Constant* c1 = constants[0];
354 const analysis::Constant* c2 = constants[1];
355
356 if (c1 == nullptr || c2 == nullptr) {
357 return nullptr;
358 }
359
360 // Check result type.
361 const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
362 const analysis::Vector* vector_type = result_type->AsVector();
363 assert(vector_type != nullptr);
364 const analysis::Type* element_type = vector_type->element_type();
365 assert(element_type != nullptr);
366 const analysis::Float* float_type = element_type->AsFloat();
367 assert(float_type != nullptr);
368
369 // Check types of c1 and c2.
370 assert(c1->type()->AsVector() == vector_type);
371 assert(c1->type()->AsVector()->element_type() == element_type &&
372 c2->type()->AsMatrix()->element_type() == vector_type);
373
374 // Get a float vector that is the result of vector-times-matrix.
375 std::vector<const analysis::Constant*> c1_components =
376 c1->GetVectorComponents(const_mgr);
377 std::vector<const analysis::Constant*> c2_components =
378 c2->AsMatrixConstant()->GetComponents();
379 uint32_t resultVectorSize = result_type->AsVector()->element_count();
380
381 std::vector<uint32_t> ids;
382
383 if ((c1 && c1->IsZero()) || (c2 && c2->IsZero())) {
384 std::vector<uint32_t> words(float_type->width() / 32, 0);
385 for (uint32_t i = 0; i < resultVectorSize; ++i) {
386 const analysis::Constant* new_elem =
387 const_mgr->GetConstant(float_type, words);
388 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
389 }
390 return const_mgr->GetConstant(vector_type, ids);
391 }
392
393 if (float_type->width() == 32) {
394 for (uint32_t i = 0; i < resultVectorSize; ++i) {
395 float result_scalar = 0.0f;
396 const analysis::VectorConstant* c2_vec =
397 c2_components[i]->AsVectorConstant();
398 for (uint32_t j = 0; j < c2_vec->GetComponents().size(); ++j) {
399 float c1_scalar = c1_components[j]->GetFloat();
400 float c2_scalar = c2_vec->GetComponents()[j]->GetFloat();
401 result_scalar += c1_scalar * c2_scalar;
402 }
403 utils::FloatProxy<float> result(result_scalar);
404 std::vector<uint32_t> words = result.GetWords();
405 const analysis::Constant* new_elem =
406 const_mgr->GetConstant(float_type, words);
407 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
408 }
409 return const_mgr->GetConstant(vector_type, ids);
410 } else if (float_type->width() == 64) {
411 for (uint32_t i = 0; i < c2_components.size(); ++i) {
412 double result_scalar = 0.0;
413 const analysis::VectorConstant* c2_vec =
414 c2_components[i]->AsVectorConstant();
415 for (uint32_t j = 0; j < c2_vec->GetComponents().size(); ++j) {
416 double c1_scalar = c1_components[j]->GetDouble();
417 double c2_scalar = c2_vec->GetComponents()[j]->GetDouble();
418 result_scalar += c1_scalar * c2_scalar;
419 }
420 utils::FloatProxy<double> result(result_scalar);
421 std::vector<uint32_t> words = result.GetWords();
422 const analysis::Constant* new_elem =
423 const_mgr->GetConstant(float_type, words);
424 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
425 }
426 return const_mgr->GetConstant(vector_type, ids);
427 }
428 return nullptr;
429 };
430}
431
432ConstantFoldingRule FoldMatrixTimesVector() {
433 return [](IRContext* context, Instruction* inst,
434 const std::vector<const analysis::Constant*>& constants)
435 -> const analysis::Constant* {
Nicolas Capens84c9c452022-11-18 14:11:05 +0000436 assert(inst->opcode() == spv::Op::OpMatrixTimesVector);
Nicolas Capens00a1bcc2022-07-29 16:49:40 -0400437 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
438 analysis::TypeManager* type_mgr = context->get_type_mgr();
439
440 if (!inst->IsFloatingPointFoldingAllowed()) {
441 if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
442 return nullptr;
443 }
444 }
445
446 const analysis::Constant* c1 = constants[0];
447 const analysis::Constant* c2 = constants[1];
448
449 if (c1 == nullptr || c2 == nullptr) {
450 return nullptr;
451 }
452
453 // Check result type.
454 const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
455 const analysis::Vector* vector_type = result_type->AsVector();
456 assert(vector_type != nullptr);
457 const analysis::Type* element_type = vector_type->element_type();
458 assert(element_type != nullptr);
459 const analysis::Float* float_type = element_type->AsFloat();
460 assert(float_type != nullptr);
461
462 // Check types of c1 and c2.
463 assert(c1->type()->AsMatrix()->element_type() == vector_type);
464 assert(c2->type()->AsVector()->element_type() == element_type);
465
466 // Get a float vector that is the result of matrix-times-vector.
467 std::vector<const analysis::Constant*> c1_components =
468 c1->AsMatrixConstant()->GetComponents();
469 std::vector<const analysis::Constant*> c2_components =
470 c2->GetVectorComponents(const_mgr);
471 uint32_t resultVectorSize = result_type->AsVector()->element_count();
472
473 std::vector<uint32_t> ids;
474
475 if ((c1 && c1->IsZero()) || (c2 && c2->IsZero())) {
476 std::vector<uint32_t> words(float_type->width() / 32, 0);
477 for (uint32_t i = 0; i < resultVectorSize; ++i) {
478 const analysis::Constant* new_elem =
479 const_mgr->GetConstant(float_type, words);
480 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
481 }
482 return const_mgr->GetConstant(vector_type, ids);
483 }
484
485 if (float_type->width() == 32) {
486 for (uint32_t i = 0; i < resultVectorSize; ++i) {
487 float result_scalar = 0.0f;
488 for (uint32_t j = 0; j < c1_components.size(); ++j) {
489 float c1_scalar = c1_components[j]
490 ->AsVectorConstant()
491 ->GetComponents()[i]
492 ->GetFloat();
493 float c2_scalar = c2_components[j]->GetFloat();
494 result_scalar += c1_scalar * c2_scalar;
495 }
496 utils::FloatProxy<float> result(result_scalar);
497 std::vector<uint32_t> words = result.GetWords();
498 const analysis::Constant* new_elem =
499 const_mgr->GetConstant(float_type, words);
500 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
501 }
502 return const_mgr->GetConstant(vector_type, ids);
503 } else if (float_type->width() == 64) {
504 for (uint32_t i = 0; i < resultVectorSize; ++i) {
505 double result_scalar = 0.0;
506 for (uint32_t j = 0; j < c1_components.size(); ++j) {
507 double c1_scalar = c1_components[j]
508 ->AsVectorConstant()
509 ->GetComponents()[i]
510 ->GetDouble();
511 double c2_scalar = c2_components[j]->GetDouble();
512 result_scalar += c1_scalar * c2_scalar;
513 }
514 utils::FloatProxy<double> result(result_scalar);
515 std::vector<uint32_t> words = result.GetWords();
516 const analysis::Constant* new_elem =
517 const_mgr->GetConstant(float_type, words);
518 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
519 }
520 return const_mgr->GetConstant(vector_type, ids);
521 }
522 return nullptr;
523 };
524}
525
Chris Forbescc5697f2019-01-30 11:54:08 -0800526ConstantFoldingRule FoldCompositeWithConstants() {
527 // Folds an OpCompositeConstruct where all of the inputs are constants to a
528 // constant. A new constant is created if necessary.
529 return [](IRContext* context, Instruction* inst,
530 const std::vector<const analysis::Constant*>& constants)
531 -> const analysis::Constant* {
532 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
533 analysis::TypeManager* type_mgr = context->get_type_mgr();
534 const analysis::Type* new_type = type_mgr->GetType(inst->type_id());
535 Instruction* type_inst =
536 context->get_def_use_mgr()->GetDef(inst->type_id());
537
538 std::vector<uint32_t> ids;
539 for (uint32_t i = 0; i < constants.size(); ++i) {
540 const analysis::Constant* element_const = constants[i];
541 if (element_const == nullptr) {
542 return nullptr;
543 }
544
545 uint32_t component_type_id = 0;
Nicolas Capens84c9c452022-11-18 14:11:05 +0000546 if (type_inst->opcode() == spv::Op::OpTypeStruct) {
Chris Forbescc5697f2019-01-30 11:54:08 -0800547 component_type_id = type_inst->GetSingleWordInOperand(i);
Nicolas Capens84c9c452022-11-18 14:11:05 +0000548 } else if (type_inst->opcode() == spv::Op::OpTypeArray) {
Chris Forbescc5697f2019-01-30 11:54:08 -0800549 component_type_id = type_inst->GetSingleWordInOperand(0);
550 }
551
552 uint32_t element_id =
553 const_mgr->FindDeclaredConstant(element_const, component_type_id);
554 if (element_id == 0) {
555 return nullptr;
556 }
557 ids.push_back(element_id);
558 }
559 return const_mgr->GetConstant(new_type, ids);
560 };
561}
562
563// The interface for a function that returns the result of applying a scalar
564// floating-point binary operation on |a| and |b|. The type of the return value
565// will be |type|. The input constants must also be of type |type|.
566using UnaryScalarFoldingRule = std::function<const analysis::Constant*(
567 const analysis::Type* result_type, const analysis::Constant* a,
568 analysis::ConstantManager*)>;
569
570// The interface for a function that returns the result of applying a scalar
571// floating-point binary operation on |a| and |b|. The type of the return value
572// will be |type|. The input constants must also be of type |type|.
573using BinaryScalarFoldingRule = std::function<const analysis::Constant*(
574 const analysis::Type* result_type, const analysis::Constant* a,
575 const analysis::Constant* b, analysis::ConstantManager*)>;
576
577// Returns a |ConstantFoldingRule| that folds unary floating point scalar ops
578// using |scalar_rule| and unary float point vectors ops by applying
579// |scalar_rule| to the elements of the vector. The |ConstantFoldingRule|
580// that is returned assumes that |constants| contains 1 entry. If they are
581// not |nullptr|, then their type is either |Float| or |Integer| or a |Vector|
582// whose element type is |Float| or |Integer|.
583ConstantFoldingRule FoldFPUnaryOp(UnaryScalarFoldingRule scalar_rule) {
584 return [scalar_rule](IRContext* context, Instruction* inst,
585 const std::vector<const analysis::Constant*>& constants)
586 -> const analysis::Constant* {
587 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
588 analysis::TypeManager* type_mgr = context->get_type_mgr();
589 const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
590 const analysis::Vector* vector_type = result_type->AsVector();
591
592 if (!inst->IsFloatingPointFoldingAllowed()) {
593 return nullptr;
594 }
595
Ben Claytondc6b76a2020-02-24 14:53:40 +0000596 const analysis::Constant* arg =
Nicolas Capens84c9c452022-11-18 14:11:05 +0000597 (inst->opcode() == spv::Op::OpExtInst) ? constants[1] : constants[0];
Ben Claytondc6b76a2020-02-24 14:53:40 +0000598
599 if (arg == nullptr) {
Chris Forbescc5697f2019-01-30 11:54:08 -0800600 return nullptr;
601 }
602
603 if (vector_type != nullptr) {
604 std::vector<const analysis::Constant*> a_components;
605 std::vector<const analysis::Constant*> results_components;
606
Ben Claytondc6b76a2020-02-24 14:53:40 +0000607 a_components = arg->GetVectorComponents(const_mgr);
Chris Forbescc5697f2019-01-30 11:54:08 -0800608
609 // Fold each component of the vector.
610 for (uint32_t i = 0; i < a_components.size(); ++i) {
611 results_components.push_back(scalar_rule(vector_type->element_type(),
612 a_components[i], const_mgr));
613 if (results_components[i] == nullptr) {
614 return nullptr;
615 }
616 }
617
618 // Build the constant object and return it.
619 std::vector<uint32_t> ids;
620 for (const analysis::Constant* member : results_components) {
621 ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
622 }
623 return const_mgr->GetConstant(vector_type, ids);
624 } else {
Ben Claytondc6b76a2020-02-24 14:53:40 +0000625 return scalar_rule(result_type, arg, const_mgr);
Chris Forbescc5697f2019-01-30 11:54:08 -0800626 }
627 };
628}
629
Ben Claytond552f632019-11-18 11:18:41 +0000630// Returns the result of folding the constants in |constants| according the
631// |scalar_rule|. If |result_type| is a vector, then |scalar_rule| is applied
632// per component.
633const analysis::Constant* FoldFPBinaryOp(
634 BinaryScalarFoldingRule scalar_rule, uint32_t result_type_id,
635 const std::vector<const analysis::Constant*>& constants,
636 IRContext* context) {
637 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
638 analysis::TypeManager* type_mgr = context->get_type_mgr();
639 const analysis::Type* result_type = type_mgr->GetType(result_type_id);
640 const analysis::Vector* vector_type = result_type->AsVector();
641
642 if (constants[0] == nullptr || constants[1] == nullptr) {
643 return nullptr;
644 }
645
646 if (vector_type != nullptr) {
647 std::vector<const analysis::Constant*> a_components;
648 std::vector<const analysis::Constant*> b_components;
649 std::vector<const analysis::Constant*> results_components;
650
651 a_components = constants[0]->GetVectorComponents(const_mgr);
652 b_components = constants[1]->GetVectorComponents(const_mgr);
653
654 // Fold each component of the vector.
655 for (uint32_t i = 0; i < a_components.size(); ++i) {
656 results_components.push_back(scalar_rule(vector_type->element_type(),
657 a_components[i], b_components[i],
658 const_mgr));
659 if (results_components[i] == nullptr) {
660 return nullptr;
661 }
662 }
663
664 // Build the constant object and return it.
665 std::vector<uint32_t> ids;
666 for (const analysis::Constant* member : results_components) {
667 ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
668 }
669 return const_mgr->GetConstant(vector_type, ids);
670 } else {
671 return scalar_rule(result_type, constants[0], constants[1], const_mgr);
672 }
673}
674
Chris Forbescc5697f2019-01-30 11:54:08 -0800675// Returns a |ConstantFoldingRule| that folds floating point scalars using
676// |scalar_rule| and vectors of floating point by applying |scalar_rule| to the
677// elements of the vector. The |ConstantFoldingRule| that is returned assumes
678// that |constants| contains 2 entries. If they are not |nullptr|, then their
679// type is either |Float| or a |Vector| whose element type is |Float|.
680ConstantFoldingRule FoldFPBinaryOp(BinaryScalarFoldingRule scalar_rule) {
681 return [scalar_rule](IRContext* context, Instruction* inst,
682 const std::vector<const analysis::Constant*>& constants)
683 -> const analysis::Constant* {
Chris Forbescc5697f2019-01-30 11:54:08 -0800684 if (!inst->IsFloatingPointFoldingAllowed()) {
685 return nullptr;
686 }
Nicolas Capens84c9c452022-11-18 14:11:05 +0000687 if (inst->opcode() == spv::Op::OpExtInst) {
Ben Claytond552f632019-11-18 11:18:41 +0000688 return FoldFPBinaryOp(scalar_rule, inst->type_id(),
689 {constants[1], constants[2]}, context);
Chris Forbescc5697f2019-01-30 11:54:08 -0800690 }
Ben Claytond552f632019-11-18 11:18:41 +0000691 return FoldFPBinaryOp(scalar_rule, inst->type_id(), constants, context);
Chris Forbescc5697f2019-01-30 11:54:08 -0800692 };
693}
694
695// This macro defines a |UnaryScalarFoldingRule| that performs float to
696// integer conversion.
697// TODO(greg-lunarg): Support for 64-bit integer types.
698UnaryScalarFoldingRule FoldFToIOp() {
699 return [](const analysis::Type* result_type, const analysis::Constant* a,
700 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
701 assert(result_type != nullptr && a != nullptr);
702 const analysis::Integer* integer_type = result_type->AsInteger();
703 const analysis::Float* float_type = a->type()->AsFloat();
704 assert(float_type != nullptr);
705 assert(integer_type != nullptr);
706 if (integer_type->width() != 32) return nullptr;
707 if (float_type->width() == 32) {
708 float fa = a->GetFloat();
709 uint32_t result = integer_type->IsSigned()
710 ? static_cast<uint32_t>(static_cast<int32_t>(fa))
711 : static_cast<uint32_t>(fa);
712 std::vector<uint32_t> words = {result};
713 return const_mgr->GetConstant(result_type, words);
714 } else if (float_type->width() == 64) {
715 double fa = a->GetDouble();
716 uint32_t result = integer_type->IsSigned()
717 ? static_cast<uint32_t>(static_cast<int32_t>(fa))
718 : static_cast<uint32_t>(fa);
719 std::vector<uint32_t> words = {result};
720 return const_mgr->GetConstant(result_type, words);
721 }
722 return nullptr;
723 };
724}
725
726// This function defines a |UnaryScalarFoldingRule| that performs integer to
727// float conversion.
728// TODO(greg-lunarg): Support for 64-bit integer types.
729UnaryScalarFoldingRule FoldIToFOp() {
730 return [](const analysis::Type* result_type, const analysis::Constant* a,
731 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
732 assert(result_type != nullptr && a != nullptr);
733 const analysis::Integer* integer_type = a->type()->AsInteger();
734 const analysis::Float* float_type = result_type->AsFloat();
735 assert(float_type != nullptr);
736 assert(integer_type != nullptr);
737 if (integer_type->width() != 32) return nullptr;
738 uint32_t ua = a->GetU32();
739 if (float_type->width() == 32) {
740 float result_val = integer_type->IsSigned()
741 ? static_cast<float>(static_cast<int32_t>(ua))
742 : static_cast<float>(ua);
743 utils::FloatProxy<float> result(result_val);
744 std::vector<uint32_t> words = {result.data()};
745 return const_mgr->GetConstant(result_type, words);
746 } else if (float_type->width() == 64) {
747 double result_val = integer_type->IsSigned()
748 ? static_cast<double>(static_cast<int32_t>(ua))
749 : static_cast<double>(ua);
750 utils::FloatProxy<double> result(result_val);
751 std::vector<uint32_t> words = result.GetWords();
752 return const_mgr->GetConstant(result_type, words);
753 }
754 return nullptr;
755 };
756}
757
Ben Claytonb73b7602019-07-29 13:56:13 +0100758// This defines a |UnaryScalarFoldingRule| that performs |OpQuantizeToF16|.
759UnaryScalarFoldingRule FoldQuantizeToF16Scalar() {
760 return [](const analysis::Type* result_type, const analysis::Constant* a,
761 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
762 assert(result_type != nullptr && a != nullptr);
763 const analysis::Float* float_type = a->type()->AsFloat();
764 assert(float_type != nullptr);
765 if (float_type->width() != 32) {
766 return nullptr;
767 }
768
769 float fa = a->GetFloat();
770 utils::HexFloat<utils::FloatProxy<float>> orignal(fa);
771 utils::HexFloat<utils::FloatProxy<utils::Float16>> quantized(0);
772 utils::HexFloat<utils::FloatProxy<float>> result(0.0f);
773 orignal.castTo(quantized, utils::round_direction::kToZero);
774 quantized.castTo(result, utils::round_direction::kToZero);
775 std::vector<uint32_t> words = {result.getBits()};
776 return const_mgr->GetConstant(result_type, words);
777 };
778}
779
Chris Forbescc5697f2019-01-30 11:54:08 -0800780// This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
781// operator |op| must work for both float and double, and use syntax "f1 op f2".
Ben Claytond552f632019-11-18 11:18:41 +0000782#define FOLD_FPARITH_OP(op) \
783 [](const analysis::Type* result_type_in_macro, const analysis::Constant* a, \
784 const analysis::Constant* b, \
785 analysis::ConstantManager* const_mgr_in_macro) \
786 -> const analysis::Constant* { \
787 assert(result_type_in_macro != nullptr && a != nullptr && b != nullptr); \
788 assert(result_type_in_macro == a->type() && \
789 result_type_in_macro == b->type()); \
790 const analysis::Float* float_type_in_macro = \
791 result_type_in_macro->AsFloat(); \
792 assert(float_type_in_macro != nullptr); \
793 if (float_type_in_macro->width() == 32) { \
794 float fa = a->GetFloat(); \
795 float fb = b->GetFloat(); \
796 utils::FloatProxy<float> result_in_macro(fa op fb); \
797 std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
798 return const_mgr_in_macro->GetConstant(result_type_in_macro, \
799 words_in_macro); \
800 } else if (float_type_in_macro->width() == 64) { \
801 double fa = a->GetDouble(); \
802 double fb = b->GetDouble(); \
803 utils::FloatProxy<double> result_in_macro(fa op fb); \
804 std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
805 return const_mgr_in_macro->GetConstant(result_type_in_macro, \
806 words_in_macro); \
807 } \
808 return nullptr; \
Chris Forbescc5697f2019-01-30 11:54:08 -0800809 }
810
811// Define the folding rule for conversion between floating point and integer
812ConstantFoldingRule FoldFToI() { return FoldFPUnaryOp(FoldFToIOp()); }
813ConstantFoldingRule FoldIToF() { return FoldFPUnaryOp(FoldIToFOp()); }
Ben Claytonb73b7602019-07-29 13:56:13 +0100814ConstantFoldingRule FoldQuantizeToF16() {
815 return FoldFPUnaryOp(FoldQuantizeToF16Scalar());
816}
Chris Forbescc5697f2019-01-30 11:54:08 -0800817
818// Define the folding rules for subtraction, addition, multiplication, and
819// division for floating point values.
820ConstantFoldingRule FoldFSub() { return FoldFPBinaryOp(FOLD_FPARITH_OP(-)); }
821ConstantFoldingRule FoldFAdd() { return FoldFPBinaryOp(FOLD_FPARITH_OP(+)); }
822ConstantFoldingRule FoldFMul() { return FoldFPBinaryOp(FOLD_FPARITH_OP(*)); }
Nicolas Capens6cacf182021-11-30 11:15:46 -0500823
824// Returns the constant that results from evaluating |numerator| / 0.0. Returns
sugoi1b398bf32022-02-18 10:27:28 -0500825// |nullptr| if the result could not be evaluated.
Nicolas Capens6cacf182021-11-30 11:15:46 -0500826const analysis::Constant* FoldFPScalarDivideByZero(
827 const analysis::Type* result_type, const analysis::Constant* numerator,
828 analysis::ConstantManager* const_mgr) {
829 if (numerator == nullptr) {
830 return nullptr;
831 }
832
833 if (numerator->IsZero()) {
834 return GetNan(result_type, const_mgr);
835 }
836
837 const analysis::Constant* result = GetInf(result_type, const_mgr);
838 if (result == nullptr) {
839 return nullptr;
840 }
841
842 if (numerator->AsFloatConstant()->GetValueAsDouble() < 0.0) {
Nicolas Capens84c9c452022-11-18 14:11:05 +0000843 result = NegateFPConst(result_type, result, const_mgr);
Nicolas Capens6cacf182021-11-30 11:15:46 -0500844 }
845 return result;
846}
847
848// Returns the result of folding |numerator| / |denominator|. Returns |nullptr|
849// if it cannot be folded.
850const analysis::Constant* FoldScalarFPDivide(
851 const analysis::Type* result_type, const analysis::Constant* numerator,
852 const analysis::Constant* denominator,
853 analysis::ConstantManager* const_mgr) {
854 if (denominator == nullptr) {
855 return nullptr;
856 }
857
858 if (denominator->IsZero()) {
859 return FoldFPScalarDivideByZero(result_type, numerator, const_mgr);
860 }
861
862 const analysis::FloatConstant* denominator_float =
863 denominator->AsFloatConstant();
864 if (denominator_float && denominator->GetValueAsDouble() == -0.0) {
865 const analysis::Constant* result =
866 FoldFPScalarDivideByZero(result_type, numerator, const_mgr);
867 if (result != nullptr)
Nicolas Capens84c9c452022-11-18 14:11:05 +0000868 result = NegateFPConst(result_type, result, const_mgr);
Nicolas Capens6cacf182021-11-30 11:15:46 -0500869 return result;
870 } else {
871 return FOLD_FPARITH_OP(/)(result_type, numerator, denominator, const_mgr);
872 }
873}
874
875// Returns the constant folding rule to fold |OpFDiv| with two constants.
876ConstantFoldingRule FoldFDiv() { return FoldFPBinaryOp(FoldScalarFPDivide); }
Chris Forbescc5697f2019-01-30 11:54:08 -0800877
878bool CompareFloatingPoint(bool op_result, bool op_unordered,
879 bool need_ordered) {
880 if (need_ordered) {
881 // operands are ordered and Operand 1 is |op| Operand 2
882 return !op_unordered && op_result;
883 } else {
884 // operands are unordered or Operand 1 is |op| Operand 2
885 return op_unordered || op_result;
886 }
887}
888
889// This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
890// operator |op| must work for both float and double, and use syntax "f1 op f2".
891#define FOLD_FPCMP_OP(op, ord) \
892 [](const analysis::Type* result_type, const analysis::Constant* a, \
893 const analysis::Constant* b, \
894 analysis::ConstantManager* const_mgr) -> const analysis::Constant* { \
895 assert(result_type != nullptr && a != nullptr && b != nullptr); \
896 assert(result_type->AsBool()); \
897 assert(a->type() == b->type()); \
898 const analysis::Float* float_type = a->type()->AsFloat(); \
899 assert(float_type != nullptr); \
900 if (float_type->width() == 32) { \
901 float fa = a->GetFloat(); \
902 float fb = b->GetFloat(); \
903 bool result = CompareFloatingPoint( \
904 fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
905 std::vector<uint32_t> words = {uint32_t(result)}; \
906 return const_mgr->GetConstant(result_type, words); \
907 } else if (float_type->width() == 64) { \
908 double fa = a->GetDouble(); \
909 double fb = b->GetDouble(); \
910 bool result = CompareFloatingPoint( \
911 fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
912 std::vector<uint32_t> words = {uint32_t(result)}; \
913 return const_mgr->GetConstant(result_type, words); \
914 } \
915 return nullptr; \
916 }
917
918// Define the folding rules for ordered and unordered comparison for floating
919// point values.
920ConstantFoldingRule FoldFOrdEqual() {
921 return FoldFPBinaryOp(FOLD_FPCMP_OP(==, true));
922}
923ConstantFoldingRule FoldFUnordEqual() {
924 return FoldFPBinaryOp(FOLD_FPCMP_OP(==, false));
925}
926ConstantFoldingRule FoldFOrdNotEqual() {
927 return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, true));
928}
929ConstantFoldingRule FoldFUnordNotEqual() {
930 return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, false));
931}
932ConstantFoldingRule FoldFOrdLessThan() {
933 return FoldFPBinaryOp(FOLD_FPCMP_OP(<, true));
934}
935ConstantFoldingRule FoldFUnordLessThan() {
936 return FoldFPBinaryOp(FOLD_FPCMP_OP(<, false));
937}
938ConstantFoldingRule FoldFOrdGreaterThan() {
939 return FoldFPBinaryOp(FOLD_FPCMP_OP(>, true));
940}
941ConstantFoldingRule FoldFUnordGreaterThan() {
942 return FoldFPBinaryOp(FOLD_FPCMP_OP(>, false));
943}
944ConstantFoldingRule FoldFOrdLessThanEqual() {
945 return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, true));
946}
947ConstantFoldingRule FoldFUnordLessThanEqual() {
948 return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, false));
949}
950ConstantFoldingRule FoldFOrdGreaterThanEqual() {
951 return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, true));
952}
953ConstantFoldingRule FoldFUnordGreaterThanEqual() {
954 return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, false));
955}
956
957// Folds an OpDot where all of the inputs are constants to a
958// constant. A new constant is created if necessary.
959ConstantFoldingRule FoldOpDotWithConstants() {
960 return [](IRContext* context, Instruction* inst,
961 const std::vector<const analysis::Constant*>& constants)
962 -> const analysis::Constant* {
963 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
964 analysis::TypeManager* type_mgr = context->get_type_mgr();
965 const analysis::Type* new_type = type_mgr->GetType(inst->type_id());
966 assert(new_type->AsFloat() && "OpDot should have a float return type.");
967 const analysis::Float* float_type = new_type->AsFloat();
968
969 if (!inst->IsFloatingPointFoldingAllowed()) {
970 return nullptr;
971 }
972
973 // If one of the operands is 0, then the result is 0.
974 bool has_zero_operand = false;
975
976 for (int i = 0; i < 2; ++i) {
977 if (constants[i]) {
978 if (constants[i]->AsNullConstant() ||
979 constants[i]->AsVectorConstant()->IsZero()) {
980 has_zero_operand = true;
981 break;
982 }
983 }
984 }
985
986 if (has_zero_operand) {
987 if (float_type->width() == 32) {
988 utils::FloatProxy<float> result(0.0f);
989 std::vector<uint32_t> words = result.GetWords();
990 return const_mgr->GetConstant(float_type, words);
991 }
992 if (float_type->width() == 64) {
993 utils::FloatProxy<double> result(0.0);
994 std::vector<uint32_t> words = result.GetWords();
995 return const_mgr->GetConstant(float_type, words);
996 }
997 return nullptr;
998 }
999
1000 if (constants[0] == nullptr || constants[1] == nullptr) {
1001 return nullptr;
1002 }
1003
1004 std::vector<const analysis::Constant*> a_components;
1005 std::vector<const analysis::Constant*> b_components;
1006
1007 a_components = constants[0]->GetVectorComponents(const_mgr);
1008 b_components = constants[1]->GetVectorComponents(const_mgr);
1009
1010 utils::FloatProxy<double> result(0.0);
1011 std::vector<uint32_t> words = result.GetWords();
1012 const analysis::Constant* result_const =
1013 const_mgr->GetConstant(float_type, words);
Ben Claytonb73b7602019-07-29 13:56:13 +01001014 for (uint32_t i = 0; i < a_components.size() && result_const != nullptr;
1015 ++i) {
Chris Forbescc5697f2019-01-30 11:54:08 -08001016 if (a_components[i] == nullptr || b_components[i] == nullptr) {
1017 return nullptr;
1018 }
1019
1020 const analysis::Constant* component = FOLD_FPARITH_OP(*)(
1021 new_type, a_components[i], b_components[i], const_mgr);
Ben Claytonb73b7602019-07-29 13:56:13 +01001022 if (component == nullptr) {
1023 return nullptr;
1024 }
Chris Forbescc5697f2019-01-30 11:54:08 -08001025 result_const =
1026 FOLD_FPARITH_OP(+)(new_type, result_const, component, const_mgr);
1027 }
1028 return result_const;
1029 };
1030}
1031
1032// This function defines a |UnaryScalarFoldingRule| that subtracts the constant
1033// from zero.
1034UnaryScalarFoldingRule FoldFNegateOp() {
1035 return [](const analysis::Type* result_type, const analysis::Constant* a,
1036 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
1037 assert(result_type != nullptr && a != nullptr);
1038 assert(result_type == a->type());
Nicolas Capens84c9c452022-11-18 14:11:05 +00001039 return NegateFPConst(result_type, a, const_mgr);
Chris Forbescc5697f2019-01-30 11:54:08 -08001040 };
1041}
1042
1043ConstantFoldingRule FoldFNegate() { return FoldFPUnaryOp(FoldFNegateOp()); }
1044
Nicolas Capens84c9c452022-11-18 14:11:05 +00001045ConstantFoldingRule FoldFClampFeedingCompare(spv::Op cmp_opcode) {
Chris Forbescc5697f2019-01-30 11:54:08 -08001046 return [cmp_opcode](IRContext* context, Instruction* inst,
1047 const std::vector<const analysis::Constant*>& constants)
1048 -> const analysis::Constant* {
1049 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
1050 analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
1051
1052 if (!inst->IsFloatingPointFoldingAllowed()) {
1053 return nullptr;
1054 }
1055
1056 uint32_t non_const_idx = (constants[0] ? 1 : 0);
1057 uint32_t operand_id = inst->GetSingleWordInOperand(non_const_idx);
1058 Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
1059
1060 analysis::TypeManager* type_mgr = context->get_type_mgr();
1061 const analysis::Type* operand_type =
1062 type_mgr->GetType(operand_inst->type_id());
1063
1064 if (!operand_type->AsFloat()) {
1065 return nullptr;
1066 }
1067
1068 if (operand_type->AsFloat()->width() != 32 &&
1069 operand_type->AsFloat()->width() != 64) {
1070 return nullptr;
1071 }
1072
Nicolas Capens84c9c452022-11-18 14:11:05 +00001073 if (operand_inst->opcode() != spv::Op::OpExtInst) {
Chris Forbescc5697f2019-01-30 11:54:08 -08001074 return nullptr;
1075 }
1076
1077 if (operand_inst->GetSingleWordInOperand(1) != GLSLstd450FClamp) {
1078 return nullptr;
1079 }
1080
1081 if (constants[1] == nullptr && constants[0] == nullptr) {
1082 return nullptr;
1083 }
1084
1085 uint32_t max_id = operand_inst->GetSingleWordInOperand(4);
1086 const analysis::Constant* max_const =
1087 const_mgr->FindDeclaredConstant(max_id);
1088
1089 uint32_t min_id = operand_inst->GetSingleWordInOperand(3);
1090 const analysis::Constant* min_const =
1091 const_mgr->FindDeclaredConstant(min_id);
1092
1093 bool found_result = false;
1094 bool result = false;
1095
1096 switch (cmp_opcode) {
Nicolas Capens84c9c452022-11-18 14:11:05 +00001097 case spv::Op::OpFOrdLessThan:
1098 case spv::Op::OpFUnordLessThan:
1099 case spv::Op::OpFOrdGreaterThanEqual:
1100 case spv::Op::OpFUnordGreaterThanEqual:
Chris Forbescc5697f2019-01-30 11:54:08 -08001101 if (constants[0]) {
1102 if (min_const) {
1103 if (constants[0]->GetValueAsDouble() <
1104 min_const->GetValueAsDouble()) {
1105 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001106 result = (cmp_opcode == spv::Op::OpFOrdLessThan ||
1107 cmp_opcode == spv::Op::OpFUnordLessThan);
Chris Forbescc5697f2019-01-30 11:54:08 -08001108 }
1109 }
1110 if (max_const) {
1111 if (constants[0]->GetValueAsDouble() >=
1112 max_const->GetValueAsDouble()) {
1113 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001114 result = !(cmp_opcode == spv::Op::OpFOrdLessThan ||
1115 cmp_opcode == spv::Op::OpFUnordLessThan);
Chris Forbescc5697f2019-01-30 11:54:08 -08001116 }
1117 }
1118 }
1119
1120 if (constants[1]) {
1121 if (max_const) {
1122 if (max_const->GetValueAsDouble() <
1123 constants[1]->GetValueAsDouble()) {
1124 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001125 result = (cmp_opcode == spv::Op::OpFOrdLessThan ||
1126 cmp_opcode == spv::Op::OpFUnordLessThan);
Chris Forbescc5697f2019-01-30 11:54:08 -08001127 }
1128 }
1129
1130 if (min_const) {
1131 if (min_const->GetValueAsDouble() >=
1132 constants[1]->GetValueAsDouble()) {
1133 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001134 result = !(cmp_opcode == spv::Op::OpFOrdLessThan ||
1135 cmp_opcode == spv::Op::OpFUnordLessThan);
Chris Forbescc5697f2019-01-30 11:54:08 -08001136 }
1137 }
1138 }
1139 break;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001140 case spv::Op::OpFOrdGreaterThan:
1141 case spv::Op::OpFUnordGreaterThan:
1142 case spv::Op::OpFOrdLessThanEqual:
1143 case spv::Op::OpFUnordLessThanEqual:
Chris Forbescc5697f2019-01-30 11:54:08 -08001144 if (constants[0]) {
1145 if (min_const) {
1146 if (constants[0]->GetValueAsDouble() <=
1147 min_const->GetValueAsDouble()) {
1148 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001149 result = (cmp_opcode == spv::Op::OpFOrdLessThanEqual ||
1150 cmp_opcode == spv::Op::OpFUnordLessThanEqual);
Chris Forbescc5697f2019-01-30 11:54:08 -08001151 }
1152 }
1153 if (max_const) {
1154 if (constants[0]->GetValueAsDouble() >
1155 max_const->GetValueAsDouble()) {
1156 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001157 result = !(cmp_opcode == spv::Op::OpFOrdLessThanEqual ||
1158 cmp_opcode == spv::Op::OpFUnordLessThanEqual);
Chris Forbescc5697f2019-01-30 11:54:08 -08001159 }
1160 }
1161 }
1162
1163 if (constants[1]) {
1164 if (max_const) {
1165 if (max_const->GetValueAsDouble() <=
1166 constants[1]->GetValueAsDouble()) {
1167 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001168 result = (cmp_opcode == spv::Op::OpFOrdLessThanEqual ||
1169 cmp_opcode == spv::Op::OpFUnordLessThanEqual);
Chris Forbescc5697f2019-01-30 11:54:08 -08001170 }
1171 }
1172
1173 if (min_const) {
1174 if (min_const->GetValueAsDouble() >
1175 constants[1]->GetValueAsDouble()) {
1176 found_result = true;
Nicolas Capens84c9c452022-11-18 14:11:05 +00001177 result = !(cmp_opcode == spv::Op::OpFOrdLessThanEqual ||
1178 cmp_opcode == spv::Op::OpFUnordLessThanEqual);
Chris Forbescc5697f2019-01-30 11:54:08 -08001179 }
1180 }
1181 }
1182 break;
1183 default:
1184 return nullptr;
1185 }
1186
1187 if (!found_result) {
1188 return nullptr;
1189 }
1190
1191 const analysis::Type* bool_type =
1192 context->get_type_mgr()->GetType(inst->type_id());
1193 const analysis::Constant* result_const =
1194 const_mgr->GetConstant(bool_type, {static_cast<uint32_t>(result)});
1195 assert(result_const);
1196 return result_const;
1197 };
1198}
1199
Ben Claytond0f684e2019-08-30 22:36:08 +01001200ConstantFoldingRule FoldFMix() {
1201 return [](IRContext* context, Instruction* inst,
1202 const std::vector<const analysis::Constant*>& constants)
1203 -> const analysis::Constant* {
1204 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
Nicolas Capens84c9c452022-11-18 14:11:05 +00001205 assert(inst->opcode() == spv::Op::OpExtInst &&
Ben Claytond0f684e2019-08-30 22:36:08 +01001206 "Expecting an extended instruction.");
1207 assert(inst->GetSingleWordInOperand(0) ==
1208 context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
1209 "Expecting a GLSLstd450 extended instruction.");
1210 assert(inst->GetSingleWordInOperand(1) == GLSLstd450FMix &&
1211 "Expecting and FMix instruction.");
1212
1213 if (!inst->IsFloatingPointFoldingAllowed()) {
1214 return nullptr;
1215 }
1216
1217 // Make sure all FMix operands are constants.
1218 for (uint32_t i = 1; i < 4; i++) {
1219 if (constants[i] == nullptr) {
1220 return nullptr;
1221 }
1222 }
1223
1224 const analysis::Constant* one;
Ben Claytond552f632019-11-18 11:18:41 +00001225 bool is_vector = false;
1226 const analysis::Type* result_type = constants[1]->type();
1227 const analysis::Type* base_type = result_type;
1228 if (base_type->AsVector()) {
1229 is_vector = true;
1230 base_type = base_type->AsVector()->element_type();
1231 }
1232 assert(base_type->AsFloat() != nullptr &&
1233 "FMix is suppose to act on floats or vectors of floats.");
1234
1235 if (base_type->AsFloat()->width() == 32) {
1236 one = const_mgr->GetConstant(base_type,
Ben Claytond0f684e2019-08-30 22:36:08 +01001237 utils::FloatProxy<float>(1.0f).GetWords());
1238 } else {
Ben Claytond552f632019-11-18 11:18:41 +00001239 one = const_mgr->GetConstant(base_type,
Ben Claytond0f684e2019-08-30 22:36:08 +01001240 utils::FloatProxy<double>(1.0).GetWords());
1241 }
1242
Ben Claytond552f632019-11-18 11:18:41 +00001243 if (is_vector) {
1244 uint32_t one_id = const_mgr->GetDefiningInstruction(one)->result_id();
1245 one =
1246 const_mgr->GetConstant(result_type, std::vector<uint32_t>(4, one_id));
1247 }
1248
1249 const analysis::Constant* temp1 = FoldFPBinaryOp(
1250 FOLD_FPARITH_OP(-), inst->type_id(), {one, constants[3]}, context);
Ben Claytond0f684e2019-08-30 22:36:08 +01001251 if (temp1 == nullptr) {
1252 return nullptr;
1253 }
1254
Ben Claytond552f632019-11-18 11:18:41 +00001255 const analysis::Constant* temp2 = FoldFPBinaryOp(
1256 FOLD_FPARITH_OP(*), inst->type_id(), {constants[1], temp1}, context);
Ben Claytond0f684e2019-08-30 22:36:08 +01001257 if (temp2 == nullptr) {
1258 return nullptr;
1259 }
Ben Claytond552f632019-11-18 11:18:41 +00001260 const analysis::Constant* temp3 =
1261 FoldFPBinaryOp(FOLD_FPARITH_OP(*), inst->type_id(),
1262 {constants[2], constants[3]}, context);
Ben Claytond0f684e2019-08-30 22:36:08 +01001263 if (temp3 == nullptr) {
1264 return nullptr;
1265 }
Ben Claytond552f632019-11-18 11:18:41 +00001266 return FoldFPBinaryOp(FOLD_FPARITH_OP(+), inst->type_id(), {temp2, temp3},
1267 context);
Ben Claytond0f684e2019-08-30 22:36:08 +01001268 };
1269}
1270
Ben Claytond552f632019-11-18 11:18:41 +00001271const analysis::Constant* FoldMin(const analysis::Type* result_type,
1272 const analysis::Constant* a,
1273 const analysis::Constant* b,
1274 analysis::ConstantManager*) {
1275 if (const analysis::Integer* int_type = result_type->AsInteger()) {
1276 if (int_type->width() == 32) {
1277 if (int_type->IsSigned()) {
1278 int32_t va = a->GetS32();
1279 int32_t vb = b->GetS32();
1280 return (va < vb ? a : b);
1281 } else {
1282 uint32_t va = a->GetU32();
1283 uint32_t vb = b->GetU32();
1284 return (va < vb ? a : b);
1285 }
1286 } else if (int_type->width() == 64) {
1287 if (int_type->IsSigned()) {
1288 int64_t va = a->GetS64();
1289 int64_t vb = b->GetS64();
1290 return (va < vb ? a : b);
1291 } else {
1292 uint64_t va = a->GetU64();
1293 uint64_t vb = b->GetU64();
1294 return (va < vb ? a : b);
1295 }
1296 }
1297 } else if (const analysis::Float* float_type = result_type->AsFloat()) {
1298 if (float_type->width() == 32) {
1299 float va = a->GetFloat();
1300 float vb = b->GetFloat();
1301 return (va < vb ? a : b);
1302 } else if (float_type->width() == 64) {
1303 double va = a->GetDouble();
1304 double vb = b->GetDouble();
1305 return (va < vb ? a : b);
1306 }
1307 }
1308 return nullptr;
1309}
1310
1311const analysis::Constant* FoldMax(const analysis::Type* result_type,
1312 const analysis::Constant* a,
1313 const analysis::Constant* b,
1314 analysis::ConstantManager*) {
1315 if (const analysis::Integer* int_type = result_type->AsInteger()) {
1316 if (int_type->width() == 32) {
1317 if (int_type->IsSigned()) {
1318 int32_t va = a->GetS32();
1319 int32_t vb = b->GetS32();
1320 return (va > vb ? a : b);
1321 } else {
1322 uint32_t va = a->GetU32();
1323 uint32_t vb = b->GetU32();
1324 return (va > vb ? a : b);
1325 }
1326 } else if (int_type->width() == 64) {
1327 if (int_type->IsSigned()) {
1328 int64_t va = a->GetS64();
1329 int64_t vb = b->GetS64();
1330 return (va > vb ? a : b);
1331 } else {
1332 uint64_t va = a->GetU64();
1333 uint64_t vb = b->GetU64();
1334 return (va > vb ? a : b);
1335 }
1336 }
1337 } else if (const analysis::Float* float_type = result_type->AsFloat()) {
1338 if (float_type->width() == 32) {
1339 float va = a->GetFloat();
1340 float vb = b->GetFloat();
1341 return (va > vb ? a : b);
1342 } else if (float_type->width() == 64) {
1343 double va = a->GetDouble();
1344 double vb = b->GetDouble();
1345 return (va > vb ? a : b);
1346 }
1347 }
1348 return nullptr;
1349}
1350
1351// Fold an clamp instruction when all three operands are constant.
1352const analysis::Constant* FoldClamp1(
1353 IRContext* context, Instruction* inst,
1354 const std::vector<const analysis::Constant*>& constants) {
Nicolas Capens84c9c452022-11-18 14:11:05 +00001355 assert(inst->opcode() == spv::Op::OpExtInst &&
Ben Claytond552f632019-11-18 11:18:41 +00001356 "Expecting an extended instruction.");
1357 assert(inst->GetSingleWordInOperand(0) ==
1358 context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
1359 "Expecting a GLSLstd450 extended instruction.");
1360
1361 // Make sure all Clamp operands are constants.
Alexis Hetu00e0af12021-11-08 08:57:46 -05001362 for (uint32_t i = 1; i < 4; i++) {
Ben Claytond552f632019-11-18 11:18:41 +00001363 if (constants[i] == nullptr) {
1364 return nullptr;
1365 }
1366 }
1367
1368 const analysis::Constant* temp = FoldFPBinaryOp(
1369 FoldMax, inst->type_id(), {constants[1], constants[2]}, context);
1370 if (temp == nullptr) {
1371 return nullptr;
1372 }
1373 return FoldFPBinaryOp(FoldMin, inst->type_id(), {temp, constants[3]},
1374 context);
1375}
1376
Alexis Hetu00e0af12021-11-08 08:57:46 -05001377// Fold a clamp instruction when |x <= min_val|.
Ben Claytond552f632019-11-18 11:18:41 +00001378const analysis::Constant* FoldClamp2(
1379 IRContext* context, Instruction* inst,
1380 const std::vector<const analysis::Constant*>& constants) {
Nicolas Capens84c9c452022-11-18 14:11:05 +00001381 assert(inst->opcode() == spv::Op::OpExtInst &&
Ben Claytond552f632019-11-18 11:18:41 +00001382 "Expecting an extended instruction.");
1383 assert(inst->GetSingleWordInOperand(0) ==
1384 context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
1385 "Expecting a GLSLstd450 extended instruction.");
1386
1387 const analysis::Constant* x = constants[1];
1388 const analysis::Constant* min_val = constants[2];
1389
1390 if (x == nullptr || min_val == nullptr) {
1391 return nullptr;
1392 }
1393
1394 const analysis::Constant* temp =
1395 FoldFPBinaryOp(FoldMax, inst->type_id(), {x, min_val}, context);
1396 if (temp == min_val) {
1397 // We can assume that |min_val| is less than |max_val|. Therefore, if the
1398 // result of the max operation is |min_val|, we know the result of the min
1399 // operation, even if |max_val| is not a constant.
1400 return min_val;
1401 }
1402 return nullptr;
1403}
1404
1405// Fold a clamp instruction when |x >= max_val|.
1406const analysis::Constant* FoldClamp3(
1407 IRContext* context, Instruction* inst,
1408 const std::vector<const analysis::Constant*>& constants) {
Nicolas Capens84c9c452022-11-18 14:11:05 +00001409 assert(inst->opcode() == spv::Op::OpExtInst &&
Ben Claytond552f632019-11-18 11:18:41 +00001410 "Expecting an extended instruction.");
1411 assert(inst->GetSingleWordInOperand(0) ==
1412 context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
1413 "Expecting a GLSLstd450 extended instruction.");
1414
1415 const analysis::Constant* x = constants[1];
1416 const analysis::Constant* max_val = constants[3];
1417
1418 if (x == nullptr || max_val == nullptr) {
1419 return nullptr;
1420 }
1421
1422 const analysis::Constant* temp =
1423 FoldFPBinaryOp(FoldMin, inst->type_id(), {x, max_val}, context);
1424 if (temp == max_val) {
1425 // We can assume that |min_val| is less than |max_val|. Therefore, if the
1426 // result of the max operation is |min_val|, we know the result of the min
1427 // operation, even if |max_val| is not a constant.
1428 return max_val;
1429 }
1430 return nullptr;
1431}
1432
Ben Claytondc6b76a2020-02-24 14:53:40 +00001433UnaryScalarFoldingRule FoldFTranscendentalUnary(double (*fp)(double)) {
1434 return
1435 [fp](const analysis::Type* result_type, const analysis::Constant* a,
1436 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
1437 assert(result_type != nullptr && a != nullptr);
1438 const analysis::Float* float_type = a->type()->AsFloat();
1439 assert(float_type != nullptr);
1440 assert(float_type == result_type->AsFloat());
1441 if (float_type->width() == 32) {
1442 float fa = a->GetFloat();
1443 float res = static_cast<float>(fp(fa));
1444 utils::FloatProxy<float> result(res);
1445 std::vector<uint32_t> words = result.GetWords();
1446 return const_mgr->GetConstant(result_type, words);
1447 } else if (float_type->width() == 64) {
1448 double fa = a->GetDouble();
1449 double res = fp(fa);
1450 utils::FloatProxy<double> result(res);
1451 std::vector<uint32_t> words = result.GetWords();
1452 return const_mgr->GetConstant(result_type, words);
1453 }
1454 return nullptr;
1455 };
1456}
1457
1458BinaryScalarFoldingRule FoldFTranscendentalBinary(double (*fp)(double,
1459 double)) {
1460 return
1461 [fp](const analysis::Type* result_type, const analysis::Constant* a,
1462 const analysis::Constant* b,
1463 analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
1464 assert(result_type != nullptr && a != nullptr);
1465 const analysis::Float* float_type = a->type()->AsFloat();
1466 assert(float_type != nullptr);
1467 assert(float_type == result_type->AsFloat());
1468 assert(float_type == b->type()->AsFloat());
1469 if (float_type->width() == 32) {
1470 float fa = a->GetFloat();
1471 float fb = b->GetFloat();
1472 float res = static_cast<float>(fp(fa, fb));
1473 utils::FloatProxy<float> result(res);
1474 std::vector<uint32_t> words = result.GetWords();
1475 return const_mgr->GetConstant(result_type, words);
1476 } else if (float_type->width() == 64) {
1477 double fa = a->GetDouble();
1478 double fb = b->GetDouble();
1479 double res = fp(fa, fb);
1480 utils::FloatProxy<double> result(res);
1481 std::vector<uint32_t> words = result.GetWords();
1482 return const_mgr->GetConstant(result_type, words);
1483 }
1484 return nullptr;
1485 };
1486}
Chris Forbescc5697f2019-01-30 11:54:08 -08001487} // namespace
1488
Ben Claytond0f684e2019-08-30 22:36:08 +01001489void ConstantFoldingRules::AddFoldingRules() {
Chris Forbescc5697f2019-01-30 11:54:08 -08001490 // Add all folding rules to the list for the opcodes to which they apply.
1491 // Note that the order in which rules are added to the list matters. If a rule
1492 // applies to the instruction, the rest of the rules will not be attempted.
1493 // Take that into consideration.
1494
Nicolas Capens84c9c452022-11-18 14:11:05 +00001495 rules_[spv::Op::OpCompositeConstruct].push_back(FoldCompositeWithConstants());
Chris Forbescc5697f2019-01-30 11:54:08 -08001496
Nicolas Capens84c9c452022-11-18 14:11:05 +00001497 rules_[spv::Op::OpCompositeExtract].push_back(FoldExtractWithConstants());
1498 rules_[spv::Op::OpCompositeInsert].push_back(FoldInsertWithConstants());
Chris Forbescc5697f2019-01-30 11:54:08 -08001499
Nicolas Capens84c9c452022-11-18 14:11:05 +00001500 rules_[spv::Op::OpConvertFToS].push_back(FoldFToI());
1501 rules_[spv::Op::OpConvertFToU].push_back(FoldFToI());
1502 rules_[spv::Op::OpConvertSToF].push_back(FoldIToF());
1503 rules_[spv::Op::OpConvertUToF].push_back(FoldIToF());
Chris Forbescc5697f2019-01-30 11:54:08 -08001504
Nicolas Capens84c9c452022-11-18 14:11:05 +00001505 rules_[spv::Op::OpDot].push_back(FoldOpDotWithConstants());
1506 rules_[spv::Op::OpFAdd].push_back(FoldFAdd());
1507 rules_[spv::Op::OpFDiv].push_back(FoldFDiv());
1508 rules_[spv::Op::OpFMul].push_back(FoldFMul());
1509 rules_[spv::Op::OpFSub].push_back(FoldFSub());
Chris Forbescc5697f2019-01-30 11:54:08 -08001510
Nicolas Capens84c9c452022-11-18 14:11:05 +00001511 rules_[spv::Op::OpFOrdEqual].push_back(FoldFOrdEqual());
Chris Forbescc5697f2019-01-30 11:54:08 -08001512
Nicolas Capens84c9c452022-11-18 14:11:05 +00001513 rules_[spv::Op::OpFUnordEqual].push_back(FoldFUnordEqual());
Chris Forbescc5697f2019-01-30 11:54:08 -08001514
Nicolas Capens84c9c452022-11-18 14:11:05 +00001515 rules_[spv::Op::OpFOrdNotEqual].push_back(FoldFOrdNotEqual());
Chris Forbescc5697f2019-01-30 11:54:08 -08001516
Nicolas Capens84c9c452022-11-18 14:11:05 +00001517 rules_[spv::Op::OpFUnordNotEqual].push_back(FoldFUnordNotEqual());
Chris Forbescc5697f2019-01-30 11:54:08 -08001518
Nicolas Capens84c9c452022-11-18 14:11:05 +00001519 rules_[spv::Op::OpFOrdLessThan].push_back(FoldFOrdLessThan());
1520 rules_[spv::Op::OpFOrdLessThan].push_back(
1521 FoldFClampFeedingCompare(spv::Op::OpFOrdLessThan));
Chris Forbescc5697f2019-01-30 11:54:08 -08001522
Nicolas Capens84c9c452022-11-18 14:11:05 +00001523 rules_[spv::Op::OpFUnordLessThan].push_back(FoldFUnordLessThan());
1524 rules_[spv::Op::OpFUnordLessThan].push_back(
1525 FoldFClampFeedingCompare(spv::Op::OpFUnordLessThan));
Chris Forbescc5697f2019-01-30 11:54:08 -08001526
Nicolas Capens84c9c452022-11-18 14:11:05 +00001527 rules_[spv::Op::OpFOrdGreaterThan].push_back(FoldFOrdGreaterThan());
1528 rules_[spv::Op::OpFOrdGreaterThan].push_back(
1529 FoldFClampFeedingCompare(spv::Op::OpFOrdGreaterThan));
Chris Forbescc5697f2019-01-30 11:54:08 -08001530
Nicolas Capens84c9c452022-11-18 14:11:05 +00001531 rules_[spv::Op::OpFUnordGreaterThan].push_back(FoldFUnordGreaterThan());
1532 rules_[spv::Op::OpFUnordGreaterThan].push_back(
1533 FoldFClampFeedingCompare(spv::Op::OpFUnordGreaterThan));
Chris Forbescc5697f2019-01-30 11:54:08 -08001534
Nicolas Capens84c9c452022-11-18 14:11:05 +00001535 rules_[spv::Op::OpFOrdLessThanEqual].push_back(FoldFOrdLessThanEqual());
1536 rules_[spv::Op::OpFOrdLessThanEqual].push_back(
1537 FoldFClampFeedingCompare(spv::Op::OpFOrdLessThanEqual));
Chris Forbescc5697f2019-01-30 11:54:08 -08001538
Nicolas Capens84c9c452022-11-18 14:11:05 +00001539 rules_[spv::Op::OpFUnordLessThanEqual].push_back(FoldFUnordLessThanEqual());
1540 rules_[spv::Op::OpFUnordLessThanEqual].push_back(
1541 FoldFClampFeedingCompare(spv::Op::OpFUnordLessThanEqual));
Chris Forbescc5697f2019-01-30 11:54:08 -08001542
Nicolas Capens84c9c452022-11-18 14:11:05 +00001543 rules_[spv::Op::OpFOrdGreaterThanEqual].push_back(FoldFOrdGreaterThanEqual());
1544 rules_[spv::Op::OpFOrdGreaterThanEqual].push_back(
1545 FoldFClampFeedingCompare(spv::Op::OpFOrdGreaterThanEqual));
Chris Forbescc5697f2019-01-30 11:54:08 -08001546
Nicolas Capens84c9c452022-11-18 14:11:05 +00001547 rules_[spv::Op::OpFUnordGreaterThanEqual].push_back(
1548 FoldFUnordGreaterThanEqual());
1549 rules_[spv::Op::OpFUnordGreaterThanEqual].push_back(
1550 FoldFClampFeedingCompare(spv::Op::OpFUnordGreaterThanEqual));
Chris Forbescc5697f2019-01-30 11:54:08 -08001551
Nicolas Capens84c9c452022-11-18 14:11:05 +00001552 rules_[spv::Op::OpVectorShuffle].push_back(FoldVectorShuffleWithConstants());
1553 rules_[spv::Op::OpVectorTimesScalar].push_back(FoldVectorTimesScalar());
1554 rules_[spv::Op::OpVectorTimesMatrix].push_back(FoldVectorTimesMatrix());
1555 rules_[spv::Op::OpMatrixTimesVector].push_back(FoldMatrixTimesVector());
Chris Forbescc5697f2019-01-30 11:54:08 -08001556
Nicolas Capens84c9c452022-11-18 14:11:05 +00001557 rules_[spv::Op::OpFNegate].push_back(FoldFNegate());
1558 rules_[spv::Op::OpQuantizeToF16].push_back(FoldQuantizeToF16());
Ben Claytond0f684e2019-08-30 22:36:08 +01001559
1560 // Add rules for GLSLstd450
1561 FeatureManager* feature_manager = context_->get_feature_mgr();
1562 uint32_t ext_inst_glslstd450_id =
1563 feature_manager->GetExtInstImportId_GLSLstd450();
1564 if (ext_inst_glslstd450_id != 0) {
1565 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMix}].push_back(FoldFMix());
Ben Claytond552f632019-11-18 11:18:41 +00001566 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMin}].push_back(
1567 FoldFPBinaryOp(FoldMin));
1568 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMin}].push_back(
1569 FoldFPBinaryOp(FoldMin));
1570 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMin}].push_back(
1571 FoldFPBinaryOp(FoldMin));
1572 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMax}].push_back(
1573 FoldFPBinaryOp(FoldMax));
1574 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMax}].push_back(
1575 FoldFPBinaryOp(FoldMax));
1576 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMax}].push_back(
1577 FoldFPBinaryOp(FoldMax));
1578 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
1579 FoldClamp1);
1580 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
1581 FoldClamp2);
1582 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
1583 FoldClamp3);
1584 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
1585 FoldClamp1);
1586 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
1587 FoldClamp2);
1588 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
1589 FoldClamp3);
1590 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
1591 FoldClamp1);
1592 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
1593 FoldClamp2);
1594 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
1595 FoldClamp3);
Ben Claytondc6b76a2020-02-24 14:53:40 +00001596 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Sin}].push_back(
1597 FoldFPUnaryOp(FoldFTranscendentalUnary(std::sin)));
1598 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Cos}].push_back(
1599 FoldFPUnaryOp(FoldFTranscendentalUnary(std::cos)));
1600 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Tan}].push_back(
1601 FoldFPUnaryOp(FoldFTranscendentalUnary(std::tan)));
1602 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Asin}].push_back(
1603 FoldFPUnaryOp(FoldFTranscendentalUnary(std::asin)));
1604 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Acos}].push_back(
1605 FoldFPUnaryOp(FoldFTranscendentalUnary(std::acos)));
1606 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Atan}].push_back(
1607 FoldFPUnaryOp(FoldFTranscendentalUnary(std::atan)));
1608 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp}].push_back(
1609 FoldFPUnaryOp(FoldFTranscendentalUnary(std::exp)));
1610 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log}].push_back(
1611 FoldFPUnaryOp(FoldFTranscendentalUnary(std::log)));
1612
1613#ifdef __ANDROID__
sugoi1b398bf32022-02-18 10:27:28 -05001614 // Android NDK r15c targeting ABI 15 doesn't have full support for C++11
Ben Claytondc6b76a2020-02-24 14:53:40 +00001615 // (no std::exp2/log2). ::exp2 is available from C99 but ::log2 isn't
1616 // available up until ABI 18 so we use a shim
1617 auto log2_shim = [](double v) -> double { return log(v) / log(2.0); };
1618 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp2}].push_back(
1619 FoldFPUnaryOp(FoldFTranscendentalUnary(::exp2)));
1620 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log2}].push_back(
1621 FoldFPUnaryOp(FoldFTranscendentalUnary(log2_shim)));
1622#else
1623 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp2}].push_back(
1624 FoldFPUnaryOp(FoldFTranscendentalUnary(std::exp2)));
1625 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log2}].push_back(
1626 FoldFPUnaryOp(FoldFTranscendentalUnary(std::log2)));
1627#endif
1628
1629 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Sqrt}].push_back(
1630 FoldFPUnaryOp(FoldFTranscendentalUnary(std::sqrt)));
1631 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Atan2}].push_back(
1632 FoldFPBinaryOp(FoldFTranscendentalBinary(std::atan2)));
1633 ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Pow}].push_back(
1634 FoldFPBinaryOp(FoldFTranscendentalBinary(std::pow)));
Ben Claytond0f684e2019-08-30 22:36:08 +01001635 }
Chris Forbescc5697f2019-01-30 11:54:08 -08001636}
1637} // namespace opt
1638} // namespace spvtools