pass.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (c) 2017 The Khronos Group Inc.
  2. // Copyright (c) 2017 Valve Corporation
  3. // Copyright (c) 2017 LunarG Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #include "source/opt/pass.h"
  17. #include "source/opt/ir_builder.h"
  18. #include "source/opt/iterator.h"
  19. namespace spvtools {
  20. namespace opt {
  21. namespace {
  22. const uint32_t kTypePointerTypeIdInIdx = 1;
  23. } // namespace
  24. Pass::Pass() : consumer_(nullptr), context_(nullptr), already_run_(false) {}
  25. Pass::Status Pass::Run(IRContext* ctx) {
  26. if (already_run_) {
  27. return Status::Failure;
  28. }
  29. already_run_ = true;
  30. context_ = ctx;
  31. Pass::Status status = Process();
  32. context_ = nullptr;
  33. if (status == Status::SuccessWithChange) {
  34. ctx->InvalidateAnalysesExceptFor(GetPreservedAnalyses());
  35. }
  36. assert((status == Status::Failure || ctx->IsConsistent()) &&
  37. "An analysis in the context is out of date.");
  38. return status;
  39. }
  40. uint32_t Pass::GetPointeeTypeId(const Instruction* ptrInst) const {
  41. const uint32_t ptrTypeId = ptrInst->type_id();
  42. const Instruction* ptrTypeInst = get_def_use_mgr()->GetDef(ptrTypeId);
  43. return ptrTypeInst->GetSingleWordInOperand(kTypePointerTypeIdInIdx);
  44. }
  45. Instruction* Pass::GetBaseType(uint32_t ty_id) {
  46. Instruction* ty_inst = get_def_use_mgr()->GetDef(ty_id);
  47. if (ty_inst->opcode() == SpvOpTypeMatrix) {
  48. uint32_t vty_id = ty_inst->GetSingleWordInOperand(0);
  49. ty_inst = get_def_use_mgr()->GetDef(vty_id);
  50. }
  51. if (ty_inst->opcode() == SpvOpTypeVector) {
  52. uint32_t cty_id = ty_inst->GetSingleWordInOperand(0);
  53. ty_inst = get_def_use_mgr()->GetDef(cty_id);
  54. }
  55. return ty_inst;
  56. }
  57. bool Pass::IsFloat(uint32_t ty_id, uint32_t width) {
  58. Instruction* ty_inst = GetBaseType(ty_id);
  59. if (ty_inst->opcode() != SpvOpTypeFloat) return false;
  60. return ty_inst->GetSingleWordInOperand(0) == width;
  61. }
  62. uint32_t Pass::GetNullId(uint32_t type_id) {
  63. if (IsFloat(type_id, 16)) context()->AddCapability(SpvCapabilityFloat16);
  64. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  65. analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
  66. const analysis::Type* type = type_mgr->GetType(type_id);
  67. const analysis::Constant* null_const = const_mgr->GetConstant(type, {});
  68. Instruction* null_inst =
  69. const_mgr->GetDefiningInstruction(null_const, type_id);
  70. return null_inst->result_id();
  71. }
  72. uint32_t Pass::GenerateCopy(Instruction* object_to_copy, uint32_t new_type_id,
  73. Instruction* insertion_position) {
  74. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  75. analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
  76. uint32_t original_type_id = object_to_copy->type_id();
  77. if (original_type_id == new_type_id) {
  78. return object_to_copy->result_id();
  79. }
  80. InstructionBuilder ir_builder(
  81. context(), insertion_position,
  82. IRContext::kAnalysisInstrToBlockMapping | IRContext::kAnalysisDefUse);
  83. analysis::Type* original_type = type_mgr->GetType(original_type_id);
  84. analysis::Type* new_type = type_mgr->GetType(new_type_id);
  85. if (const analysis::Array* original_array_type = original_type->AsArray()) {
  86. uint32_t original_element_type_id =
  87. type_mgr->GetId(original_array_type->element_type());
  88. analysis::Array* new_array_type = new_type->AsArray();
  89. assert(new_array_type != nullptr && "Can't copy an array to a non-array.");
  90. uint32_t new_element_type_id =
  91. type_mgr->GetId(new_array_type->element_type());
  92. std::vector<uint32_t> element_ids;
  93. const analysis::Constant* length_const =
  94. const_mgr->FindDeclaredConstant(original_array_type->LengthId());
  95. assert(length_const->AsIntConstant());
  96. uint32_t array_length = length_const->AsIntConstant()->GetU32();
  97. for (uint32_t i = 0; i < array_length; i++) {
  98. Instruction* extract = ir_builder.AddCompositeExtract(
  99. original_element_type_id, object_to_copy->result_id(), {i});
  100. element_ids.push_back(
  101. GenerateCopy(extract, new_element_type_id, insertion_position));
  102. }
  103. return ir_builder.AddCompositeConstruct(new_type_id, element_ids)
  104. ->result_id();
  105. } else if (const analysis::Struct* original_struct_type =
  106. original_type->AsStruct()) {
  107. analysis::Struct* new_struct_type = new_type->AsStruct();
  108. const std::vector<const analysis::Type*>& original_types =
  109. original_struct_type->element_types();
  110. const std::vector<const analysis::Type*>& new_types =
  111. new_struct_type->element_types();
  112. std::vector<uint32_t> element_ids;
  113. for (uint32_t i = 0; i < original_types.size(); i++) {
  114. Instruction* extract = ir_builder.AddCompositeExtract(
  115. type_mgr->GetId(original_types[i]), object_to_copy->result_id(), {i});
  116. element_ids.push_back(GenerateCopy(extract, type_mgr->GetId(new_types[i]),
  117. insertion_position));
  118. }
  119. return ir_builder.AddCompositeConstruct(new_type_id, element_ids)
  120. ->result_id();
  121. } else {
  122. // If we do not have an aggregate type, then we have a problem. Either we
  123. // found multiple instances of the same type, or we are copying to an
  124. // incompatible type. Either way the code is illegal.
  125. assert(false &&
  126. "Don't know how to copy this type. Code is likely illegal.");
  127. }
  128. return 0;
  129. }
  130. } // namespace opt
  131. } // namespace spvtools