replace_invalid_opc.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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(spv::Capability::Linkage)) {
  22. return Status::SuccessWithoutChange;
  23. }
  24. spv::ExecutionModel execution_model = GetExecutionModel();
  25. if (execution_model == spv::ExecutionModel::Kernel) {
  26. // We do not handle kernels.
  27. return Status::SuccessWithoutChange;
  28. }
  29. if (execution_model == spv::ExecutionModel::Max) {
  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. spv::ExecutionModel ReplaceInvalidOpcodePass::GetExecutionModel() {
  40. spv::ExecutionModel result = spv::ExecutionModel::Max;
  41. bool first = true;
  42. for (Instruction& entry_point : get_module()->entry_points()) {
  43. if (first) {
  44. result = static_cast<spv::ExecutionModel>(
  45. entry_point.GetSingleWordInOperand(0));
  46. first = false;
  47. } else {
  48. spv::ExecutionModel current_model = static_cast<spv::ExecutionModel>(
  49. entry_point.GetSingleWordInOperand(0));
  50. if (current_model != result) {
  51. result = spv::ExecutionModel::Max;
  52. break;
  53. }
  54. }
  55. }
  56. return result;
  57. }
  58. bool ReplaceInvalidOpcodePass::RewriteFunction(Function* function,
  59. spv::ExecutionModel 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() == spv::Op::OpLabel || inst->IsNoLine()) {
  66. last_line_dbg_inst = nullptr;
  67. return;
  68. } else if (inst->IsLine()) {
  69. last_line_dbg_inst = inst;
  70. return;
  71. }
  72. bool replace = false;
  73. if (model != spv::ExecutionModel::Fragment &&
  74. IsFragmentShaderOnlyInstruction(inst)) {
  75. replace = true;
  76. }
  77. if (model != spv::ExecutionModel::TessellationControl &&
  78. model != spv::ExecutionModel::GLCompute &&
  79. !context()->IsTargetEnvAtLeast(SPV_ENV_UNIVERSAL_1_3)) {
  80. if (inst->opcode() == spv::Op::OpControlBarrier) {
  81. assert(model != spv::ExecutionModel::Kernel &&
  82. "Expecting to be working on a shader module.");
  83. replace = true;
  84. }
  85. }
  86. if (replace) {
  87. modified = true;
  88. if (last_line_dbg_inst == nullptr) {
  89. ReplaceInstruction(inst, nullptr, 0, 0);
  90. } else {
  91. // Get the name of the source file.
  92. uint32_t file_name_id = 0;
  93. if (last_line_dbg_inst->opcode() == spv::Op::OpLine) {
  94. file_name_id = last_line_dbg_inst->GetSingleWordInOperand(0);
  95. } else { // Shader100::DebugLine
  96. uint32_t debug_source_id =
  97. last_line_dbg_inst->GetSingleWordInOperand(2);
  98. Instruction* debug_source_inst =
  99. context()->get_def_use_mgr()->GetDef(debug_source_id);
  100. file_name_id = debug_source_inst->GetSingleWordInOperand(2);
  101. }
  102. Instruction* file_name =
  103. context()->get_def_use_mgr()->GetDef(file_name_id);
  104. const std::string source = file_name->GetInOperand(0).AsString();
  105. // Get the line number and column number.
  106. uint32_t line_number =
  107. last_line_dbg_inst->GetSingleWordInOperand(1);
  108. uint32_t col_number = last_line_dbg_inst->GetSingleWordInOperand(2);
  109. // Replace the instruction.
  110. ReplaceInstruction(inst, source.c_str(), line_number, col_number);
  111. }
  112. }
  113. },
  114. /* run_on_debug_line_insts = */ true);
  115. return modified;
  116. }
  117. bool ReplaceInvalidOpcodePass::IsFragmentShaderOnlyInstruction(
  118. Instruction* inst) {
  119. switch (inst->opcode()) {
  120. case spv::Op::OpDPdx:
  121. case spv::Op::OpDPdy:
  122. case spv::Op::OpFwidth:
  123. case spv::Op::OpDPdxFine:
  124. case spv::Op::OpDPdyFine:
  125. case spv::Op::OpFwidthFine:
  126. case spv::Op::OpDPdxCoarse:
  127. case spv::Op::OpDPdyCoarse:
  128. case spv::Op::OpFwidthCoarse:
  129. case spv::Op::OpImageSampleImplicitLod:
  130. case spv::Op::OpImageSampleDrefImplicitLod:
  131. case spv::Op::OpImageSampleProjImplicitLod:
  132. case spv::Op::OpImageSampleProjDrefImplicitLod:
  133. case spv::Op::OpImageSparseSampleImplicitLod:
  134. case spv::Op::OpImageSparseSampleDrefImplicitLod:
  135. case spv::Op::OpImageQueryLod:
  136. // TODO: Teach |ReplaceInstruction| to handle block terminators. Then
  137. // uncomment the OpKill case.
  138. // case spv::Op::OpKill:
  139. // case spv::Op::OpTerminateInstruction:
  140. return true;
  141. default:
  142. return false;
  143. }
  144. }
  145. void ReplaceInvalidOpcodePass::ReplaceInstruction(Instruction* inst,
  146. const char* source,
  147. uint32_t line_number,
  148. uint32_t column_number) {
  149. if (inst->result_id() != 0) {
  150. uint32_t const_id = GetSpecialConstant(inst->type_id());
  151. context()->KillNamesAndDecorates(inst);
  152. context()->ReplaceAllUsesWith(inst->result_id(), const_id);
  153. }
  154. assert(!inst->IsBlockTerminator() &&
  155. "We cannot simply delete a block terminator. It must be replaced "
  156. "with something.");
  157. if (consumer()) {
  158. std::string message = BuildWarningMessage(inst->opcode());
  159. consumer()(SPV_MSG_WARNING, source, {line_number, column_number, 0},
  160. message.c_str());
  161. }
  162. context()->KillInst(inst);
  163. }
  164. uint32_t ReplaceInvalidOpcodePass::GetSpecialConstant(uint32_t type_id) {
  165. const analysis::Constant* special_const = nullptr;
  166. analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
  167. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  168. Instruction* type = context()->get_def_use_mgr()->GetDef(type_id);
  169. if (type->opcode() == spv::Op::OpTypeVector) {
  170. uint32_t component_const =
  171. GetSpecialConstant(type->GetSingleWordInOperand(0));
  172. std::vector<uint32_t> ids;
  173. for (uint32_t i = 0; i < type->GetSingleWordInOperand(1); ++i) {
  174. ids.push_back(component_const);
  175. }
  176. special_const = const_mgr->GetConstant(type_mgr->GetType(type_id), ids);
  177. } else {
  178. assert(type->opcode() == spv::Op::OpTypeInt ||
  179. type->opcode() == spv::Op::OpTypeFloat);
  180. std::vector<uint32_t> literal_words;
  181. for (uint32_t i = 0; i < type->GetSingleWordInOperand(0); i += 32) {
  182. literal_words.push_back(0xDEADBEEF);
  183. }
  184. special_const =
  185. const_mgr->GetConstant(type_mgr->GetType(type_id), literal_words);
  186. }
  187. assert(special_const != nullptr);
  188. return const_mgr->GetDefiningInstruction(special_const)->result_id();
  189. }
  190. std::string ReplaceInvalidOpcodePass::BuildWarningMessage(spv::Op opcode) {
  191. spv_opcode_desc opcode_info;
  192. context()->grammar().lookupOpcode(opcode, &opcode_info);
  193. std::string message = "Removing ";
  194. message += opcode_info->name;
  195. message += " instruction because of incompatible execution model.";
  196. return message;
  197. }
  198. } // namespace opt
  199. } // namespace spvtools