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|.
| 363 | // will have type |result_type|, and |matrix| must exist in |context|. The |
| 364 | // result constant will also exist in |context|. |
| 365 | const 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 | |
| 404 | const analysis::Constant* FoldTranspose( |
| 405 | IRContext* context, Instruction* inst, |
no test coverage detected