transformation_replace_irrelevant_id.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2020 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_FUZZ_TRANSFORMATION_REPLACE_IRRELEVANT_ID_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_REPLACE_IRRELEVANT_ID_H_
  16. #include "source/fuzz/transformation.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. class TransformationReplaceIrrelevantId : public Transformation {
  20. public:
  21. explicit TransformationReplaceIrrelevantId(
  22. protobufs::TransformationReplaceIrrelevantId message);
  23. TransformationReplaceIrrelevantId(
  24. const protobufs::IdUseDescriptor& id_use_descriptor,
  25. uint32_t replacement_id);
  26. // - The id of interest in |message_.id_use_descriptor| is irrelevant
  27. // according to the fact manager.
  28. // - The types of the original id and of the replacement ids are the same.
  29. // - The replacement must not be the result id of an OpFunction instruction.
  30. // - |message_.replacement_id| is available to use at the enclosing
  31. // instruction of |message_.id_use_descriptor|.
  32. // - The original id is in principle replaceable with any other id of the same
  33. // type. See fuzzerutil::IdUseCanBeReplaced for details.
  34. bool IsApplicable(
  35. opt::IRContext* ir_context,
  36. const TransformationContext& transformation_context) const override;
  37. // Replaces the use of an irrelevant id identified by
  38. // |message_.id_use_descriptor| with the id |message_.replacement_id|, which
  39. // has the same type as the id of interest.
  40. void Apply(opt::IRContext* ir_context,
  41. TransformationContext* transformation_context) const override;
  42. std::unordered_set<uint32_t> GetFreshIds() const override;
  43. protobufs::Transformation ToMessage() const override;
  44. // Returns true if and only if |use_instruction| is OpVariable and
  45. // |replacement_for_use| is not a constant instruction - i.e., if it would be
  46. // illegal to replace the variable's initializer with the given instruction.
  47. static bool AttemptsToReplaceVariableInitializerWithNonConstant(
  48. const opt::Instruction& use_instruction,
  49. const opt::Instruction& replacement_for_use);
  50. private:
  51. protobufs::TransformationReplaceIrrelevantId message_;
  52. };
  53. } // namespace fuzz
  54. } // namespace spvtools
  55. #endif // SOURCE_FUZZ_TRANSFORMATION_REPLACE_IRRELEVANT_ID_H_