pass.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. constexpr 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. if (!(status == Status::Failure || ctx->IsConsistent()))
  37. assert(false && "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() == spv::Op::OpTypeMatrix) {
  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() == spv::Op::OpTypeVector) {
  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() != spv::Op::OpTypeFloat) 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(spv::Capability::Float16);
  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::ConstantManager* const_mgr = context()->get_constant_mgr();
  75. uint32_t original_type_id = object_to_copy->type_id();
  76. if (original_type_id == new_type_id) {
  77. return object_to_copy->result_id();
  78. }
  79. InstructionBuilder ir_builder(
  80. context(), insertion_position,
  81. IRContext::kAnalysisInstrToBlockMapping | IRContext::kAnalysisDefUse);
  82. Instruction* original_type = get_def_use_mgr()->GetDef(original_type_id);
  83. Instruction* new_type = get_def_use_mgr()->GetDef(new_type_id);
  84. if (new_type->opcode() != original_type->opcode()) {
  85. return 0;
  86. }
  87. switch (original_type->opcode()) {
  88. case spv::Op::OpTypeArray: {
  89. uint32_t original_element_type_id =
  90. original_type->GetSingleWordInOperand(0);
  91. uint32_t new_element_type_id = new_type->GetSingleWordInOperand(0);
  92. std::vector<uint32_t> element_ids;
  93. uint32_t length_id = original_type->GetSingleWordInOperand(1);
  94. const analysis::Constant* length_const =
  95. const_mgr->FindDeclaredConstant(length_id);
  96. assert(length_const->AsIntConstant());
  97. uint32_t array_length = length_const->AsIntConstant()->GetU32();
  98. for (uint32_t i = 0; i < array_length; i++) {
  99. Instruction* extract = ir_builder.AddCompositeExtract(
  100. original_element_type_id, object_to_copy->result_id(), {i});
  101. uint32_t new_id =
  102. GenerateCopy(extract, new_element_type_id, insertion_position);
  103. if (new_id == 0) {
  104. return 0;
  105. }
  106. element_ids.push_back(new_id);
  107. }
  108. return ir_builder.AddCompositeConstruct(new_type_id, element_ids)
  109. ->result_id();
  110. }
  111. case spv::Op::OpTypeStruct: {
  112. std::vector<uint32_t> element_ids;
  113. for (uint32_t i = 0; i < original_type->NumInOperands(); i++) {
  114. uint32_t orig_member_type_id = original_type->GetSingleWordInOperand(i);
  115. uint32_t new_member_type_id = new_type->GetSingleWordInOperand(i);
  116. Instruction* extract = ir_builder.AddCompositeExtract(
  117. orig_member_type_id, object_to_copy->result_id(), {i});
  118. uint32_t new_id =
  119. GenerateCopy(extract, new_member_type_id, insertion_position);
  120. if (new_id == 0) {
  121. return 0;
  122. }
  123. element_ids.push_back(new_id);
  124. }
  125. return ir_builder.AddCompositeConstruct(new_type_id, element_ids)
  126. ->result_id();
  127. }
  128. default:
  129. // If we do not have an aggregate type, then we have a problem. Either we
  130. // found multiple instances of the same type, or we are copying to an
  131. // incompatible type. Either way the code is illegal. Leave the code as
  132. // is and let the caller deal with it.
  133. return 0;
  134. }
  135. }
  136. } // namespace opt
  137. } // namespace spvtools