transformation_add_synonym.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (c) 2020 Vasyl Teliman
  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_ADD_SYNONYM_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_ADD_SYNONYM_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 TransformationAddSynonym : public Transformation {
  23. public:
  24. explicit TransformationAddSynonym(
  25. protobufs::TransformationAddSynonym message);
  26. TransformationAddSynonym(
  27. uint32_t result_id,
  28. protobufs::TransformationAddSynonym::SynonymType synonym_type,
  29. uint32_t synonym_fresh_id,
  30. const protobufs::InstructionDescriptor& insert_before);
  31. // - |result_id| must be a valid result id of some instruction in the module.
  32. // - |result_id| may not be an irrelevant id.
  33. // - |synonym_type| is a type of the synonymous instruction that will be
  34. // created.
  35. // - |synonym_fresh_id| is a fresh id.
  36. // - |insert_before| must be a valid instruction descriptor and we must be
  37. // able to insert a new synonymous instruction before |insert_before|.
  38. // - |result_id| must be available before |insert_before|.
  39. bool IsApplicable(
  40. opt::IRContext* ir_context,
  41. const TransformationContext& transformation_context) const override;
  42. // Creates a new synonymous instruction according to the |synonym_type| with
  43. // result id |synonym_fresh_id|.
  44. // Inserts that instruction before |insert_before| and creates a fact
  45. // that the |synonym_fresh_id| and the |result_id| are synonymous.
  46. void Apply(opt::IRContext* ir_context,
  47. TransformationContext* transformation_context) const override;
  48. std::unordered_set<uint32_t> GetFreshIds() const override;
  49. protobufs::Transformation ToMessage() const override;
  50. // Returns true if we can create a synonym of |inst| according to the
  51. // |synonym_type|.
  52. static bool IsInstructionValid(
  53. opt::IRContext* ir_context,
  54. const TransformationContext& transformation_context,
  55. opt::Instruction* inst,
  56. protobufs::TransformationAddSynonym::SynonymType synonym_type);
  57. // Returns true if |synonym_type| requires an additional constant instruction
  58. // to be present in the module.
  59. static bool IsAdditionalConstantRequired(
  60. protobufs::TransformationAddSynonym::SynonymType synonym_type);
  61. private:
  62. // Returns a new instruction which is synonymous to |message_.result_id|.
  63. std::unique_ptr<opt::Instruction> MakeSynonymousInstruction(
  64. opt::IRContext* ir_context,
  65. const TransformationContext& transformation_context) const;
  66. // Returns a result id of a constant instruction that is required to be
  67. // present in some synonym types (e.g. returns a result id of a zero constant
  68. // for ADD_ZERO synonym type). Returns 0 if no such instruction is present in
  69. // the module. This method should only be called when
  70. // IsAdditionalConstantRequired returns true.
  71. uint32_t MaybeGetConstantId(
  72. opt::IRContext* ir_context,
  73. const TransformationContext& transformation_context) const;
  74. protobufs::TransformationAddSynonym message_;
  75. };
  76. } // namespace fuzz
  77. } // namespace spvtools
  78. #endif // SOURCE_FUZZ_TRANSFORMATION_ADD_SYNONYM_H_