wrap_opkill.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #ifndef SOURCE_OPT_WRAP_OPKILL_H_
  15. #define SOURCE_OPT_WRAP_OPKILL_H_
  16. #include "source/opt/pass.h"
  17. namespace spvtools {
  18. namespace opt {
  19. // Documented in optimizer.hpp
  20. class WrapOpKill : public Pass {
  21. public:
  22. WrapOpKill() : void_type_id_(0) {}
  23. const char* name() const override { return "wrap-opkill"; }
  24. Status Process() override;
  25. IRContext::Analysis GetPreservedAnalyses() override {
  26. return IRContext::kAnalysisDefUse |
  27. IRContext::kAnalysisInstrToBlockMapping |
  28. IRContext::kAnalysisDecorations | IRContext::kAnalysisCombinators |
  29. IRContext::kAnalysisNameMap | IRContext::kAnalysisBuiltinVarId |
  30. IRContext::kAnalysisConstants | IRContext::kAnalysisTypes;
  31. }
  32. private:
  33. // Replaces the OpKill or OpTerminateInvocation instruction |inst| with a
  34. // function call to a function that contains a single instruction, a clone of
  35. // |inst|. An OpUnreachable instruction will be placed after the function
  36. // call. Return true if successful.
  37. bool ReplaceWithFunctionCall(Instruction* inst);
  38. // Returns the id of the void type.
  39. uint32_t GetVoidTypeId();
  40. // Returns the id of the function type for a void function with no parameters.
  41. uint32_t GetVoidFunctionTypeId();
  42. // Return the id of a function that has return type void, has no parameters,
  43. // and contains a single instruction, which is |opcode|, either OpKill or
  44. // OpTerminateInvocation. Returns 0 if the function could not be generated.
  45. uint32_t GetKillingFuncId(spv::Op opcode);
  46. // Returns the id of the return type for the function that contains |inst|.
  47. // Returns 0 if |inst| is not in a function.
  48. uint32_t GetOwningFunctionsReturnType(Instruction* inst);
  49. // The id of the void type. If its value is 0, then the void type has not
  50. // been found or created yet.
  51. uint32_t void_type_id_;
  52. // The function that is a single instruction, which is an OpKill. The
  53. // function has a void return type and takes no parameters. If the function is
  54. // |nullptr|, then the function has not been generated.
  55. std::unique_ptr<Function> opkill_function_;
  56. // The function that is a single instruction, which is an
  57. // OpTerminateInvocation. The function has a void return type and takes no
  58. // parameters. If the function is |nullptr|, then the function has not been
  59. // generated.
  60. std::unique_ptr<Function> opterminateinvocation_function_;
  61. };
  62. } // namespace opt
  63. } // namespace spvtools
  64. #endif // SOURCE_OPT_WRAP_OPKILL_H_