transformation_inline_function.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) 2020 André Perez Maselco
  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_FUZZ_TRANSFORMATION_INLINE_FUNCTION_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_INLINE_FUNCTION_H_
  16. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  17. #include "source/fuzz/transformation.h"
  18. #include "source/fuzz/transformation_context.h"
  19. #include "source/opt/ir_context.h"
  20. namespace spvtools {
  21. namespace fuzz {
  22. class TransformationInlineFunction : public Transformation {
  23. public:
  24. explicit TransformationInlineFunction(
  25. protobufs::TransformationInlineFunction message);
  26. TransformationInlineFunction(
  27. uint32_t function_call_id,
  28. const std::map<uint32_t, uint32_t>& result_id_map);
  29. // - |message_.result_id_map| must map the instructions of the called function
  30. // to fresh ids, unless overflow ids are available.
  31. // - |message_.function_call_id| must be an OpFunctionCall instruction.
  32. // It must not have an early return and must not use OpUnreachable or
  33. // OpKill. This is to guard against making the module invalid when the
  34. // caller is inside a continue construct.
  35. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3735):
  36. // Allow functions that use OpKill or OpUnreachable to be inlined if the
  37. // function call is not part of a continue construct.
  38. bool IsApplicable(
  39. opt::IRContext* ir_context,
  40. const TransformationContext& transformation_context) const override;
  41. // Replaces the OpFunctionCall instruction, identified by
  42. // |message_.function_call_id|, with a copy of the function's body.
  43. // |message_.result_id_map| is used to provide fresh ids for duplicate
  44. // instructions.
  45. void Apply(opt::IRContext* ir_context,
  46. TransformationContext* transformation_context) const override;
  47. std::unordered_set<uint32_t> GetFreshIds() const override;
  48. protobufs::Transformation ToMessage() const override;
  49. // Returns true if |function_call_instruction| is defined, is an
  50. // OpFunctionCall instruction, has no uses if its return type is void, has no
  51. // early returns and has no uses of OpKill or OpUnreachable.
  52. static bool IsSuitableForInlining(
  53. opt::IRContext* ir_context, opt::Instruction* function_call_instruction);
  54. private:
  55. protobufs::TransformationInlineFunction message_;
  56. // Inline |instruction_to_be_inlined| by setting its ids to the corresponding
  57. // ids in |result_id_map|.
  58. void AdaptInlinedInstruction(
  59. const std::map<uint32_t, uint32_t>& result_id_map,
  60. opt::IRContext* ir_context, opt::Instruction* instruction) const;
  61. };
  62. } // namespace fuzz
  63. } // namespace spvtools
  64. #endif // SOURCE_FUZZ_TRANSFORMATION_INLINE_FUNCTION_H_