replace_invalid_opc.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (c) 2018 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/replace_invalid_opc.h"
  15. #include <bitset>
  16. #include <vector>
  17. namespace spvtools {
  18. namespace opt {
  19. Pass::Status ReplaceInvalidOpcodePass::Process() {
  20. bool modified = false;
  21. if (context()->get_feature_mgr()->HasCapability(SpvCapabilityLinkage)) {
  22. return Status::SuccessWithoutChange;
  23. }
  24. SpvExecutionModel execution_model = GetExecutionModel();
  25. if (execution_model == SpvExecutionModelKernel) {
  26. // We do not handle kernels.
  27. return Status::SuccessWithoutChange;
  28. }
  29. if (execution_model == SpvExecutionModelMax) {
  30. // Mixed execution models for the entry points. This case is not currently
  31. // handled.
  32. return Status::SuccessWithoutChange;
  33. }
  34. for (Function& func : *get_module()) {
  35. modified |= RewriteFunction(&func, execution_model);
  36. }
  37. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  38. }
  39. SpvExecutionModel ReplaceInvalidOpcodePass::GetExecutionModel() {
  40. SpvExecutionModel result = SpvExecutionModelMax;
  41. bool first = true;
  42. for (Instruction& entry_point : get_module()->entry_points()) {
  43. if (first) {
  44. result =
  45. static_cast<SpvExecutionModel>(entry_point.GetSingleWordInOperand(0));
  46. first = false;
  47. } else {
  48. SpvExecutionModel current_model =
  49. static_cast<SpvExecutionModel>(entry_point.GetSingleWordInOperand(0));
  50. if (current_model != result) {
  51. result = SpvExecutionModelMax;
  52. break;
  53. }
  54. }
  55. }
  56. return result;
  57. }
  58. bool ReplaceInvalidOpcodePass::RewriteFunction(Function* function,
  59. SpvExecutionModel model) {
  60. bool modified = false;
  61. Instruction* last_line_dbg_inst = nullptr;
  62. function->ForEachInst(
  63. [model, &modified, &last_line_dbg_inst, this](Instruction* inst) {
  64. // Track the debug information so we can have a meaningful message.
  65. if (inst->opcode() == SpvOpLabel || inst->opcode() == SpvOpNoLine) {
  66. last_line_dbg_inst = nullptr;
  67. return;
  68. } else if (inst->opcode() == SpvOpLine) {
  69. last_line_dbg_inst = inst;
  70. return;
  71. }
  72. bool replace = false;
  73. if (model != SpvExecutionModelFragment &&
  74. IsFragmentShaderOnlyInstruction(inst)) {
  75. replace = true;
  76. }
  77. if (model != SpvExecutionModelTessellationControl &&
  78. model != SpvExecutionModelGLCompute) {
  79. if (inst->opcode() == SpvOpControlBarrier) {
  80. assert(model != SpvExecutionModelKernel &&
  81. "Expecting to be working on a shader module.");
  82. replace = true;
  83. }
  84. }
  85. if (replace) {
  86. modified = true;
  87. if (last_line_dbg_inst == nullptr) {
  88. ReplaceInstruction(inst, nullptr, 0, 0);
  89. } else {
  90. // Get the name of the source file.
  91. Instruction* file_name = context()->get_def_use_mgr()->GetDef(
  92. last_line_dbg_inst->GetSingleWordInOperand(0));
  93. const char* source = reinterpret_cast<const char*>(
  94. &file_name->GetInOperand(0).words[0]);
  95. // Get the line number and column number.
  96. uint32_t line_number =
  97. last_line_dbg_inst->GetSingleWordInOperand(1);
  98. uint32_t col_number = last_line_dbg_inst->GetSingleWordInOperand(2);
  99. // Replace the instruction.
  100. ReplaceInstruction(inst, source, line_number, col_number);
  101. }
  102. }
  103. },
  104. /* run_on_debug_line_insts = */ true);
  105. return modified;
  106. }
  107. bool ReplaceInvalidOpcodePass::IsFragmentShaderOnlyInstruction(
  108. Instruction* inst) {
  109. switch (inst->opcode()) {
  110. case SpvOpDPdx:
  111. case SpvOpDPdy:
  112. case SpvOpFwidth:
  113. case SpvOpDPdxFine:
  114. case SpvOpDPdyFine:
  115. case SpvOpFwidthFine:
  116. case SpvOpDPdxCoarse:
  117. case SpvOpDPdyCoarse:
  118. case SpvOpFwidthCoarse:
  119. case SpvOpImageSampleImplicitLod:
  120. case SpvOpImageSampleDrefImplicitLod:
  121. case SpvOpImageSampleProjImplicitLod:
  122. case SpvOpImageSampleProjDrefImplicitLod:
  123. case SpvOpImageSparseSampleImplicitLod:
  124. case SpvOpImageSparseSampleDrefImplicitLod:
  125. case SpvOpImageQueryLod:
  126. // TODO: Teach |ReplaceInstruction| to handle block terminators. Then
  127. // uncomment the OpKill case.
  128. // case SpvOpKill:
  129. // case SpvOpTerminateInstruction:
  130. return true;
  131. default:
  132. return false;
  133. }
  134. }
  135. void ReplaceInvalidOpcodePass::ReplaceInstruction(Instruction* inst,
  136. const char* source,
  137. uint32_t line_number,
  138. uint32_t column_number) {
  139. if (inst->result_id() != 0) {
  140. uint32_t const_id = GetSpecialConstant(inst->type_id());
  141. context()->KillNamesAndDecorates(inst);
  142. context()->ReplaceAllUsesWith(inst->result_id(), const_id);
  143. }
  144. assert(!inst->IsBlockTerminator() &&
  145. "We cannot simply delete a block terminator. It must be replaced "
  146. "with something.");
  147. if (consumer()) {
  148. std::string message = BuildWarningMessage(inst->opcode());
  149. consumer()(SPV_MSG_WARNING, source, {line_number, column_number, 0},
  150. message.c_str());
  151. }
  152. context()->KillInst(inst);
  153. }
  154. uint32_t ReplaceInvalidOpcodePass::GetSpecialConstant(uint32_t type_id) {
  155. const analysis::Constant* special_const = nullptr;
  156. analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
  157. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  158. Instruction* type = context()->get_def_use_mgr()->GetDef(type_id);
  159. if (type->opcode() == SpvOpTypeVector) {
  160. uint32_t component_const =
  161. GetSpecialConstant(type->GetSingleWordInOperand(0));
  162. std::vector<uint32_t> ids;
  163. for (uint32_t i = 0; i < type->GetSingleWordInOperand(1); ++i) {
  164. ids.push_back(component_const);
  165. }
  166. special_const = const_mgr->GetConstant(type_mgr->GetType(type_id), ids);
  167. } else {
  168. assert(type->opcode() == SpvOpTypeInt || type->opcode() == SpvOpTypeFloat);
  169. std::vector<uint32_t> literal_words;
  170. for (uint32_t i = 0; i < type->GetSingleWordInOperand(0); i += 32) {
  171. literal_words.push_back(0xDEADBEEF);
  172. }
  173. special_const =
  174. const_mgr->GetConstant(type_mgr->GetType(type_id), literal_words);
  175. }
  176. assert(special_const != nullptr);
  177. return const_mgr->GetDefiningInstruction(special_const)->result_id();
  178. }
  179. std::string ReplaceInvalidOpcodePass::BuildWarningMessage(SpvOp opcode) {
  180. spv_opcode_desc opcode_info;
  181. context()->grammar().lookupOpcode(opcode, &opcode_info);
  182. std::string message = "Removing ";
  183. message += opcode_info->name;
  184. message += " instruction because of incompatible execution model.";
  185. return message;
  186. }
  187. } // namespace opt
  188. } // namespace spvtools