2
0

replace_invalid_opc.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #ifndef SOURCE_OPT_REPLACE_INVALID_OPC_H_
  15. #define SOURCE_OPT_REPLACE_INVALID_OPC_H_
  16. #include <string>
  17. #include "source/opt/pass.h"
  18. namespace spvtools {
  19. namespace opt {
  20. // This pass will runs on shader modules only. It will replace the result of
  21. // instructions that are valid for shader modules, but not the current shader
  22. // stage, with a constant value. If the instruction does not have a return
  23. // value, the instruction will simply be deleted.
  24. class ReplaceInvalidOpcodePass : public Pass {
  25. public:
  26. const char* name() const override { return "replace-invalid-opcode"; }
  27. Status Process() override;
  28. private:
  29. // Returns the execution model that is used by every entry point in the
  30. // module. If more than one execution model is used in the module, then the
  31. // return value is spv::ExecutionModel::Max.
  32. spv::ExecutionModel GetExecutionModel();
  33. // Replaces all instructions in |function| that are invalid with execution
  34. // model |mode|, but valid for another shader model, with a special constant
  35. // value. See |GetSpecialConstant|.
  36. bool RewriteFunction(Function* function, spv::ExecutionModel mode);
  37. // Returns true if |inst| is valid for fragment shaders only.
  38. bool IsFragmentShaderOnlyInstruction(Instruction* inst);
  39. // Replaces all uses of the result of |inst|, if there is one, with the id of
  40. // a special constant. Then |inst| is killed. |inst| cannot be a block
  41. // terminator because the basic block will then become invalid. |inst| is no
  42. // longer valid after calling this function.
  43. void ReplaceInstruction(Instruction* inst, const char* source,
  44. uint32_t line_number, uint32_t column_number);
  45. // Returns the id of a constant with type |type_id|. The type must be an
  46. // integer, float, or vector. For scalar types, the hex representation of the
  47. // constant will be the concatenation of 0xDEADBEEF with itself until the
  48. // width of the type has been reached. For a vector, each element of the
  49. // constant will be constructed the same way.
  50. uint32_t GetSpecialConstant(uint32_t type_id);
  51. std::string BuildWarningMessage(spv::Op opcode);
  52. };
  53. } // namespace opt
  54. } // namespace spvtools
  55. #endif // SOURCE_OPT_REPLACE_INVALID_OPC_H_