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

Function TransposeMatrix

source/opt/const_folding_rules.cpp:365–402  ·  view source on GitHub ↗

Returns to the constant that results from tranposing |matrix|. The result will have type |result_type|, and |matrix| must exist in |context|. The result constant will also exist in |context|.

Source from the content-addressed store, hash-verified

363// will have type |result_type|, and |matrix| must exist in |context|. The
364// result constant will also exist in |context|.
365const analysis::Constant* TransposeMatrix(const analysis::Constant* matrix,
366 analysis::Matrix* result_type,
367 IRContext* context) {
368 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
369 if (matrix->AsNullConstant() != nullptr) {
370 return const_mgr->GetNullCompositeConstant(result_type);
371 }
372
373 const auto& columns = matrix->AsMatrixConstant()->GetComponents();
374 uint32_t number_of_rows = columns[0]->type()->AsVector()->element_count();
375
376 // Collect the ids of the elements in their new positions.
377 std::vector<std::vector<uint32_t>> result_elements(number_of_rows);
378 for (const analysis::Constant* column : columns) {
379 if (column->AsNullConstant()) {
380 column = const_mgr->GetNullCompositeConstant(column->type());
381 }
382 const auto& column_components = column->AsVectorConstant()->GetComponents();
383
384 for (uint32_t row = 0; row < number_of_rows; ++row) {
385 result_elements[row].push_back(
386 const_mgr->GetDefiningInstruction(column_components[row])
387 ->result_id());
388 }
389 }
390
391 // Create the constant for each row in the result, and collect the ids.
392 std::vector<uint32_t> result_columns(number_of_rows);
393 for (uint32_t col = 0; col < number_of_rows; ++col) {
394 auto* element = const_mgr->GetConstant(result_type->element_type(),
395 result_elements[col]);
396 result_columns[col] =
397 const_mgr->GetDefiningInstruction(element)->result_id();
398 }
399
400 // Create the matrix constant from the row ids, and return it.
401 return const_mgr->GetConstant(result_type, result_columns);
402}
403
404const analysis::Constant* FoldTranspose(
405 IRContext* context, Instruction* inst,

Callers 1

FoldTransposeFunction · 0.85

Calls 13

get_constant_mgrMethod · 0.80
AsVectorMethod · 0.80
AsNullConstantMethod · 0.45
AsMatrixConstantMethod · 0.45
element_countMethod · 0.45
typeMethod · 0.45
AsVectorConstantMethod · 0.45
push_backMethod · 0.45
result_idMethod · 0.45
GetConstantMethod · 0.45

Tested by

no test coverage detected