transformation_wrap_vector_synonym.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2021 Shiyu Liu
  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_WRAP_VECTOR_SYNONYM_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_WRAP_VECTOR_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 TransformationWrapVectorSynonym : public Transformation {
  23. public:
  24. explicit TransformationWrapVectorSynonym(
  25. protobufs::TransformationWrapVectorSynonym message);
  26. TransformationWrapVectorSynonym(uint32_t instruction_id,
  27. uint32_t vector_operand1,
  28. uint32_t vector_operand2, uint32_t fresh_id,
  29. uint32_t pos);
  30. // - |instruction_id| must be the id of a supported arithmetic operation
  31. // and must be relevant.
  32. // - |vector_operand1| and |vector_operand2| represents the result ids of the
  33. // two vector operands.
  34. // - |fresh_id| is an unused id that will be used as a result id of the
  35. // created instruction.
  36. // - |vector_operand1| and |vector_operand2| must have compatible vector types
  37. // that are supported by this transformation.
  38. // - |pos| is an index of the operands of |instruction_id| in the
  39. // |vector_operand1| and |vector_operand2|. It must be less than the size
  40. // of those vector operands.
  41. // - A vector type with the same width as the types of the vector operands,
  42. // and element type matching the type of |instruction_id|, must exist in the
  43. // module.
  44. bool IsApplicable(
  45. opt::IRContext* ir_context,
  46. const TransformationContext& transformation_context) const override;
  47. // Adds a new instruction before the |instruction_id| with |fresh_id|
  48. // result id and |instruction_id|'s opcode. The added instruction has
  49. // two operands: |vector_operand1| and |vector_operand2| and its type
  50. // id is equal to the type ids of those operands. A new fact is added
  51. // to the fact manager specifying that |fresh_id[pos]| is synonymous
  52. // to |instruction_id|.
  53. void Apply(opt::IRContext* ir_context,
  54. TransformationContext* transformation_context) const override;
  55. std::unordered_set<uint32_t> GetFreshIds() const override;
  56. protobufs::Transformation ToMessage() const override;
  57. // Checks whether the instruction given is supported by the transformation.
  58. // A valid instruction must:
  59. // - has both result id and type id.
  60. // - is a supported scalar operation instruction.
  61. // - has a supported type that is either int or float.
  62. static bool IsInstructionSupported(opt::IRContext* ir_context,
  63. const opt::Instruction& instruction);
  64. private:
  65. protobufs::TransformationWrapVectorSynonym message_;
  66. };
  67. } // namespace fuzz
  68. } // namespace spvtools
  69. #endif // SOURCE_FUZZ_TRANSFORMATION_WRAP_VECTOR_SYNONYM_H_