transformation_mutate_pointer.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_MUTATE_POINTER_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_MUTATE_POINTER_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 TransformationMutatePointer : public Transformation {
  23. public:
  24. explicit TransformationMutatePointer(
  25. protobufs::TransformationMutatePointer message);
  26. explicit TransformationMutatePointer(
  27. uint32_t pointer_id, uint32_t fresh_id,
  28. const protobufs::InstructionDescriptor& insert_before);
  29. // - |fresh_id| must be fresh.
  30. // - |insert_before| must be a valid instruction descriptor of some
  31. // instruction in the module.
  32. // - It should be possible to insert OpLoad and OpStore before
  33. // |insert_before|.
  34. // - |pointer_id| must be a result id of some instruction in the module.
  35. // - Instruction with result id |pointer_id| must be valid (see
  36. // IsValidPointerInstruction method).
  37. // - There must exist an irrelevant constant in the module. Type of the
  38. // constant must be equal to the type of the |pointer_id|'s pointee.
  39. // - |pointer_id| must be available (according to the dominance rules) before
  40. // |insert_before|.
  41. bool IsApplicable(
  42. opt::IRContext* ir_context,
  43. const TransformationContext& transformation_context) const override;
  44. // Inserts the following instructions before |insert_before|:
  45. // %fresh_id = OpLoad %pointee_type_id %pointer_id
  46. // OpStore %pointer_id %constant_id
  47. // OpStore %pointer_id %fresh_id
  48. void Apply(opt::IRContext* ir_context,
  49. TransformationContext* transformation_context) const override;
  50. std::unordered_set<uint32_t> GetFreshIds() const override;
  51. protobufs::Transformation ToMessage() const override;
  52. // Returns true if |inst| valid pointer according to the following:
  53. // - |inst| has result id and type id.
  54. // - |inst| is neither OpUndef nor OpConstantNull.
  55. // - |inst| has a pointer type.
  56. // - |inst|'s storage class is either Private, Function or Workgroup.
  57. // - |inst|'s pointee type and all its constituents are either scalar or
  58. // composite.
  59. static bool IsValidPointerInstruction(opt::IRContext* ir_context,
  60. const opt::Instruction& inst);
  61. private:
  62. protobufs::TransformationMutatePointer message_;
  63. };
  64. } // namespace fuzz
  65. } // namespace spvtools
  66. #endif // SOURCE_FUZZ_TRANSFORMATION_MUTATE_POINTER_H_