transformation_equation_instruction.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_EQUATION_INSTRUCTION_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_EQUATION_INSTRUCTION_H_
  16. #include <vector>
  17. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  18. #include "source/fuzz/transformation.h"
  19. #include "source/fuzz/transformation_context.h"
  20. #include "source/opt/ir_context.h"
  21. namespace spvtools {
  22. namespace fuzz {
  23. class TransformationEquationInstruction : public Transformation {
  24. public:
  25. explicit TransformationEquationInstruction(
  26. protobufs::TransformationEquationInstruction message);
  27. TransformationEquationInstruction(
  28. uint32_t fresh_id, spv::Op opcode,
  29. const std::vector<uint32_t>& in_operand_id,
  30. const protobufs::InstructionDescriptor& instruction_to_insert_before);
  31. // - |message_.fresh_id| must be fresh.
  32. // - |message_.instruction_to_insert_before| must identify an instruction
  33. // before which an equation instruction can legitimately be inserted.
  34. // - Each id in |message_.in_operand_id| must exist, not be an OpUndef, and
  35. // be available before |message_.instruction_to_insert_before|.
  36. // - |message_.opcode| must be an opcode for which we know how to handle
  37. // equations, the types of the ids in |message_.in_operand_id| must be
  38. // suitable for use with this opcode, and the module must contain an
  39. // appropriate result type id.
  40. bool IsApplicable(
  41. opt::IRContext* ir_context,
  42. const TransformationContext& transformation_context) const override;
  43. // Adds an instruction to the module, right before
  44. // |message_.instruction_to_insert_before|, of the form:
  45. //
  46. // |message_.fresh_id| = |message_.opcode| %type |message_.in_operand_ids|
  47. //
  48. // where %type is a type id that already exists in the module and that is
  49. // compatible with the opcode and input operands.
  50. //
  51. // The fact manager is also updated to inform it of this equation fact.
  52. void Apply(opt::IRContext* ir_context,
  53. TransformationContext* transformation_context) const override;
  54. std::unordered_set<uint32_t> GetFreshIds() const override;
  55. protobufs::Transformation ToMessage() const override;
  56. private:
  57. // Returns type id for the equation instruction. Returns 0 if result type does
  58. // not exist.
  59. uint32_t MaybeGetResultTypeId(opt::IRContext* ir_context) const;
  60. protobufs::TransformationEquationInstruction message_;
  61. };
  62. } // namespace fuzz
  63. } // namespace spvtools
  64. #endif // SOURCE_FUZZ_TRANSFORMATION_EQUATION_INSTRUCTION_H_