wrap_opkill.cpp 6.1 KB

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