MCPcopy Create free account
hub / github.com/KhronosGroup/SPIRV-Tools / FoldVectorTimesMatrix

Function FoldVectorTimesMatrix

source/opt/const_folding_rules.cpp:425–520  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

423}
424
425ConstantFoldingRule FoldVectorTimesMatrix() {
426 return [](IRContext* context, Instruction* inst,
427 const std::vector<const analysis::Constant*>& constants)
428 -> const analysis::Constant* {
429 assert(inst->opcode() == spv::Op::OpVectorTimesMatrix);
430 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
431 analysis::TypeManager* type_mgr = context->get_type_mgr();
432
433 if (!inst->IsFloatingPointFoldingAllowed()) {
434 if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
435 return nullptr;
436 }
437 }
438
439 const analysis::Constant* c1 = constants[0];
440 const analysis::Constant* c2 = constants[1];
441
442 if (c1 == nullptr || c2 == nullptr) {
443 return nullptr;
444 }
445
446 // Check result type.
447 const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
448 const analysis::Vector* vector_type = result_type->AsVector();
449 assert(vector_type != nullptr);
450 const analysis::Type* element_type = vector_type->element_type();
451 assert(element_type != nullptr);
452 const analysis::Float* float_type = element_type->AsFloat();
453 assert(float_type != nullptr);
454
455 // Check types of c1 and c2.
456 assert(c1->type()->AsVector() == vector_type);
457 assert(c1->type()->AsVector()->element_type() == element_type &&
458 c2->type()->AsMatrix()->element_type() == vector_type);
459
460 uint32_t resultVectorSize = result_type->AsVector()->element_count();
461 std::vector<uint32_t> ids;
462
463 if ((c1 && c1->IsZero()) || (c2 && c2->IsZero())) {
464 std::vector<uint32_t> words(float_type->width() / 32, 0);
465 for (uint32_t i = 0; i < resultVectorSize; ++i) {
466 const analysis::Constant* new_elem =
467 const_mgr->GetConstant(float_type, words);
468 ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
469 }
470 return const_mgr->GetConstant(vector_type, ids);
471 }
472
473 // Get a float vector that is the result of vector-times-matrix.
474 std::vector<const analysis::Constant*> c1_components =
475 c1->GetVectorComponents(const_mgr);
476 std::vector<const analysis::Constant*> c2_components =
477 c2->AsMatrixConstant()->GetComponents();
478
479 if (float_type->width() == 32) {
480 for (uint32_t i = 0; i < resultVectorSize; ++i) {
481 float result_scalar = 0.0f;
482 if (!c2_components[i]->AsNullConstant()) {

Callers 1

AddFoldingRulesMethod · 0.85

Calls 15

get_constant_mgrMethod · 0.80
get_type_mgrMethod · 0.80
AsVectorMethod · 0.80
AsFloatMethod · 0.80
AsMatrixMethod · 0.80
GetVectorComponentsMethod · 0.80
GetFloatMethod · 0.80
GetWordsMethod · 0.80
GetDoubleMethod · 0.80
HasFloatingPointFunction · 0.70

Tested by

no test coverage detected