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