transformation_add_parameter.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_ADD_PARAMETER_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_ADD_PARAMETER_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 TransformationAddParameter : public Transformation {
  23. public:
  24. explicit TransformationAddParameter(
  25. protobufs::TransformationAddParameter message);
  26. TransformationAddParameter(uint32_t function_id, uint32_t parameter_fresh_id,
  27. uint32_t parameter_type_id,
  28. std::map<uint32_t, uint32_t> call_parameter_ids,
  29. uint32_t function_type_fresh_id);
  30. // - |function_id| must be a valid result id of some non-entry-point function
  31. // in the module.
  32. // - |parameter_type_id| is a type id of the new parameter. The type must be
  33. // supported by this transformation as specified by IsParameterTypeSupported
  34. // function.
  35. // - |call_parameter_id| must map from every id of an OpFunctionCall
  36. // instruction of this function to the id that will be passed as the new
  37. // parameter at that call site. There could be no callers, therefore this
  38. // map can be empty.
  39. // - |parameter_fresh_id| and |function_type_fresh_id| are fresh ids and are
  40. // not equal.
  41. bool IsApplicable(
  42. opt::IRContext* ir_context,
  43. const TransformationContext& transformation_context) const override;
  44. // - Creates a new OpFunctionParameter instruction with result id
  45. // |parameter_fresh_id| for the function with |function_id|.
  46. // - Adjusts function's type to include a new parameter.
  47. // - Adds an argument to every caller of the function to account for the added
  48. // parameter. The argument is the value in |call_parameter_id| map.
  49. void Apply(opt::IRContext* ir_context,
  50. TransformationContext* transformation_context) const override;
  51. std::unordered_set<uint32_t> GetFreshIds() const override;
  52. protobufs::Transformation ToMessage() const override;
  53. // Returns true if the type of the parameter is supported by this
  54. // transformation.
  55. static bool IsParameterTypeSupported(opt::IRContext* ir_context,
  56. uint32_t type_id);
  57. private:
  58. protobufs::TransformationAddParameter message_;
  59. };
  60. } // namespace fuzz
  61. } // namespace spvtools
  62. #endif // SOURCE_FUZZ_TRANSFORMATION_ADD_PARAMETER_H_