wrap_opkill.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. auto func_to_process =
  21. context()->GetStructuredCFGAnalysis()->FindFuncsCalledFromContinue();
  22. for (uint32_t func_id : func_to_process) {
  23. Function* func = context()->GetFunction(func_id);
  24. bool successful = func->WhileEachInst([this, &modified](Instruction* inst) {
  25. if (inst->opcode() == SpvOpKill) {
  26. modified = true;
  27. if (!ReplaceWithFunctionCall(inst)) {
  28. return false;
  29. }
  30. }
  31. return true;
  32. });
  33. if (!successful) {
  34. return Status::Failure;
  35. }
  36. }
  37. if (opkill_function_ != nullptr) {
  38. assert(modified &&
  39. "The function should only be generated if something was modified.");
  40. context()->AddFunction(std::move(opkill_function_));
  41. }
  42. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  43. }
  44. bool WrapOpKill::ReplaceWithFunctionCall(Instruction* inst) {
  45. assert(inst->opcode() == SpvOpKill &&
  46. "|inst| must be an OpKill instruction.");
  47. InstructionBuilder ir_builder(
  48. context(), inst,
  49. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  50. uint32_t func_id = GetOpKillFuncId();
  51. if (func_id == 0) {
  52. return false;
  53. }
  54. if (ir_builder.AddFunctionCall(GetVoidTypeId(), func_id, {}) == nullptr) {
  55. return false;
  56. }
  57. Instruction* return_inst = nullptr;
  58. uint32_t return_type_id = GetOwningFunctionsReturnType(inst);
  59. if (return_type_id != GetVoidTypeId()) {
  60. Instruction* undef = ir_builder.AddNullaryOp(return_type_id, SpvOpUndef);
  61. if (undef == nullptr) {
  62. return false;
  63. }
  64. return_inst =
  65. ir_builder.AddUnaryOp(0, SpvOpReturnValue, undef->result_id());
  66. } else {
  67. return_inst = ir_builder.AddNullaryOp(0, SpvOpReturn);
  68. }
  69. if (return_inst == nullptr) {
  70. return false;
  71. }
  72. context()->KillInst(inst);
  73. return true;
  74. }
  75. uint32_t WrapOpKill::GetVoidTypeId() {
  76. if (void_type_id_ != 0) {
  77. return void_type_id_;
  78. }
  79. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  80. analysis::Void void_type;
  81. void_type_id_ = type_mgr->GetTypeInstruction(&void_type);
  82. return void_type_id_;
  83. }
  84. uint32_t WrapOpKill::GetVoidFunctionTypeId() {
  85. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  86. analysis::Void void_type;
  87. const analysis::Type* registered_void_type =
  88. type_mgr->GetRegisteredType(&void_type);
  89. analysis::Function func_type(registered_void_type, {});
  90. return type_mgr->GetTypeInstruction(&func_type);
  91. }
  92. uint32_t WrapOpKill::GetOpKillFuncId() {
  93. if (opkill_function_ != nullptr) {
  94. return opkill_function_->result_id();
  95. }
  96. uint32_t opkill_func_id = TakeNextId();
  97. if (opkill_func_id == 0) {
  98. return 0;
  99. }
  100. uint32_t void_type_id = GetVoidTypeId();
  101. if (void_type_id == 0) {
  102. return 0;
  103. }
  104. // Generate the function start instruction
  105. std::unique_ptr<Instruction> func_start(new Instruction(
  106. context(), SpvOpFunction, void_type_id, opkill_func_id, {}));
  107. func_start->AddOperand({SPV_OPERAND_TYPE_FUNCTION_CONTROL, {0}});
  108. func_start->AddOperand({SPV_OPERAND_TYPE_ID, {GetVoidFunctionTypeId()}});
  109. opkill_function_.reset(new Function(std::move(func_start)));
  110. // Generate the function end instruction
  111. std::unique_ptr<Instruction> func_end(
  112. new Instruction(context(), SpvOpFunctionEnd, 0, 0, {}));
  113. opkill_function_->SetFunctionEnd(std::move(func_end));
  114. // Create the one basic block for the function.
  115. uint32_t lab_id = TakeNextId();
  116. if (lab_id == 0) {
  117. return 0;
  118. }
  119. std::unique_ptr<Instruction> label_inst(
  120. new Instruction(context(), SpvOpLabel, 0, lab_id, {}));
  121. std::unique_ptr<BasicBlock> bb(new BasicBlock(std::move(label_inst)));
  122. // Add the OpKill to the basic block
  123. std::unique_ptr<Instruction> kill_inst(
  124. new Instruction(context(), SpvOpKill, 0, 0, {}));
  125. bb->AddInstruction(std::move(kill_inst));
  126. // Add the bb to the function
  127. opkill_function_->AddBasicBlock(std::move(bb));
  128. // Add the function to the module.
  129. if (context()->AreAnalysesValid(IRContext::kAnalysisDefUse)) {
  130. opkill_function_->ForEachInst(
  131. [this](Instruction* inst) { context()->AnalyzeDefUse(inst); });
  132. }
  133. if (context()->AreAnalysesValid(IRContext::kAnalysisInstrToBlockMapping)) {
  134. for (BasicBlock& basic_block : *opkill_function_) {
  135. context()->set_instr_block(basic_block.GetLabelInst(), &basic_block);
  136. for (Instruction& inst : basic_block) {
  137. context()->set_instr_block(&inst, &basic_block);
  138. }
  139. }
  140. }
  141. return opkill_function_->result_id();
  142. }
  143. uint32_t WrapOpKill::GetOwningFunctionsReturnType(Instruction* inst) {
  144. BasicBlock* bb = context()->get_instr_block(inst);
  145. if (bb == nullptr) {
  146. return 0;
  147. }
  148. Function* func = bb->GetParent();
  149. return func->type_id();
  150. }
  151. } // namespace opt
  152. } // namespace spvtools