wrap_opkill.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/wrap_opkill.h"
  15. #include "ir_builder.h"
  16. namespace spvtools {
  17. namespace opt {
  18. Pass::Status WrapOpKill::Process() {
  19. bool modified = false;
  20. for (auto& func : *get_module()) {
  21. bool successful = func.WhileEachInst([this, &modified](Instruction* inst) {
  22. if (inst->opcode() == SpvOpKill) {
  23. modified = true;
  24. if (!ReplaceWithFunctionCall(inst)) {
  25. return false;
  26. }
  27. }
  28. return true;
  29. });
  30. if (!successful) {
  31. return Status::Failure;
  32. }
  33. }
  34. if (opkill_function_ != nullptr) {
  35. assert(modified &&
  36. "The function should only be generated if something was modified.");
  37. context()->AddFunction(std::move(opkill_function_));
  38. }
  39. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  40. }
  41. bool WrapOpKill::ReplaceWithFunctionCall(Instruction* inst) {
  42. assert(inst->opcode() == SpvOpKill &&
  43. "|inst| must be an OpKill instruction.");
  44. InstructionBuilder ir_builder(
  45. context(), inst,
  46. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  47. uint32_t func_id = GetOpKillFuncId();
  48. if (func_id == 0) {
  49. return false;
  50. }
  51. if (ir_builder.AddFunctionCall(GetVoidTypeId(), func_id, {}) == nullptr) {
  52. return false;
  53. }
  54. ir_builder.AddUnreachable();
  55. context()->KillInst(inst);
  56. return true;
  57. }
  58. uint32_t WrapOpKill::GetVoidTypeId() {
  59. if (void_type_id_ != 0) {
  60. return void_type_id_;
  61. }
  62. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  63. analysis::Void void_type;
  64. void_type_id_ = type_mgr->GetTypeInstruction(&void_type);
  65. return void_type_id_;
  66. }
  67. uint32_t WrapOpKill::GetVoidFunctionTypeId() {
  68. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  69. analysis::Void void_type;
  70. const analysis::Type* registered_void_type =
  71. type_mgr->GetRegisteredType(&void_type);
  72. analysis::Function func_type(registered_void_type, {});
  73. return type_mgr->GetTypeInstruction(&func_type);
  74. }
  75. uint32_t WrapOpKill::GetOpKillFuncId() {
  76. if (opkill_function_ != nullptr) {
  77. return opkill_function_->result_id();
  78. }
  79. uint32_t opkill_func_id = TakeNextId();
  80. if (opkill_func_id == 0) {
  81. return 0;
  82. }
  83. uint32_t void_type_id = GetVoidTypeId();
  84. if (void_type_id == 0) {
  85. return 0;
  86. }
  87. // Generate the function start instruction
  88. std::unique_ptr<Instruction> func_start(new Instruction(
  89. context(), SpvOpFunction, void_type_id, opkill_func_id, {}));
  90. func_start->AddOperand({SPV_OPERAND_TYPE_FUNCTION_CONTROL, {0}});
  91. func_start->AddOperand({SPV_OPERAND_TYPE_ID, {GetVoidFunctionTypeId()}});
  92. opkill_function_.reset(new Function(std::move(func_start)));
  93. // Generate the function end instruction
  94. std::unique_ptr<Instruction> func_end(
  95. new Instruction(context(), SpvOpFunctionEnd, 0, 0, {}));
  96. opkill_function_->SetFunctionEnd(std::move(func_end));
  97. // Create the one basic block for the function.
  98. uint32_t lab_id = TakeNextId();
  99. if (lab_id == 0) {
  100. return 0;
  101. }
  102. std::unique_ptr<Instruction> label_inst(
  103. new Instruction(context(), SpvOpLabel, 0, lab_id, {}));
  104. std::unique_ptr<BasicBlock> bb(new BasicBlock(std::move(label_inst)));
  105. // Add the OpKill to the basic block
  106. std::unique_ptr<Instruction> kill_inst(
  107. new Instruction(context(), SpvOpKill, 0, 0, {}));
  108. bb->AddInstruction(std::move(kill_inst));
  109. // Add the bb to the function
  110. opkill_function_->AddBasicBlock(std::move(bb));
  111. // Add the function to the module.
  112. if (context()->AreAnalysesValid(IRContext::kAnalysisDefUse)) {
  113. opkill_function_->ForEachInst(
  114. [this](Instruction* inst) { context()->AnalyzeDefUse(inst); });
  115. }
  116. if (context()->AreAnalysesValid(IRContext::kAnalysisInstrToBlockMapping)) {
  117. for (BasicBlock& basic_block : *opkill_function_) {
  118. context()->set_instr_block(basic_block.GetLabelInst(), &basic_block);
  119. for (Instruction& inst : basic_block) {
  120. context()->set_instr_block(&inst, &basic_block);
  121. }
  122. }
  123. }
  124. return opkill_function_->result_id();
  125. }
  126. } // namespace opt
  127. } // namespace spvtools