wrap_opkill.cpp 6.2 KB

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