transformation_replace_params_with_struct.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_REPLACE_PARAMS_WITH_STRUCT_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_REPLACE_PARAMS_WITH_STRUCT_H_
  16. #include <map>
  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 TransformationReplaceParamsWithStruct : public Transformation {
  24. public:
  25. explicit TransformationReplaceParamsWithStruct(
  26. protobufs::TransformationReplaceParamsWithStruct message);
  27. TransformationReplaceParamsWithStruct(
  28. const std::vector<uint32_t>& parameter_id,
  29. uint32_t fresh_function_type_id, uint32_t fresh_parameter_id,
  30. const std::map<uint32_t, uint32_t>& caller_id_to_fresh_composite_id);
  31. // - Each element of |parameter_id| is a valid result id of some
  32. // OpFunctionParameter instruction. All parameter ids must correspond to
  33. // parameters of the same function. That function may not be an entry-point
  34. // function.
  35. // - Types of all parameters must be supported by this transformation (see
  36. // IsParameterTypeSupported method).
  37. // - |parameter_id| may not be empty or contain duplicates.
  38. // - There must exist an OpTypeStruct instruction containing types of all
  39. // replaced parameters. Type of the i'th component of the struct is equal
  40. // to the type of the instruction with result id |parameter_id[i]|.
  41. // - |caller_id_to_fresh_composite_id| should contain a key for at least every
  42. // result id of an OpFunctionCall instruction that calls the function.
  43. // - |fresh_function_type_id|, |fresh_parameter_id|,
  44. // |caller_id_to_fresh_composite_id| are all fresh and unique ids.
  45. bool IsApplicable(
  46. opt::IRContext* ir_context,
  47. const TransformationContext& transformation_context) const override;
  48. // - Creates a new function parameter with result id |fresh_parameter_id|.
  49. // Parameter's type is OpTypeStruct with each components type equal to the
  50. // type of the replaced parameter.
  51. // - OpCompositeConstruct with result id from |fresh_composite_id| is inserted
  52. // before each OpFunctionCall instruction.
  53. // - OpCompositeExtract with result id equal to the result id of the replaced
  54. // parameter is created in the function.
  55. void Apply(opt::IRContext* ir_context,
  56. TransformationContext* transformation_context) const override;
  57. std::unordered_set<uint32_t> GetFreshIds() const override;
  58. protobufs::Transformation ToMessage() const override;
  59. // Returns true if parameter's type is supported by this transformation.
  60. static bool IsParameterTypeSupported(opt::IRContext* ir_context,
  61. uint32_t param_type_id);
  62. private:
  63. // Returns a result id of the OpTypeStruct instruction required by this
  64. // transformation (see docs on the IsApplicable method to learn more).
  65. uint32_t MaybeGetRequiredStructType(opt::IRContext* ir_context) const;
  66. // Returns a vector of indices of parameters to replace. Concretely, i'th
  67. // element is the index of the parameter with result id |parameter_id[i]| in
  68. // its function.
  69. std::vector<uint32_t> ComputeIndicesOfReplacedParameters(
  70. opt::IRContext* ir_context) const;
  71. protobufs::TransformationReplaceParamsWithStruct message_;
  72. };
  73. } // namespace fuzz
  74. } // namespace spvtools
  75. #endif // SOURCE_FUZZ_TRANSFORMATION_REPLACE_PARAMS_WITH_STRUCT_H_