transformation.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (c) 2019 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_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_H_
  16. #include <memory>
  17. #include <unordered_set>
  18. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  19. #include "source/fuzz/transformation_context.h"
  20. #include "source/opt/ir_context.h"
  21. namespace spvtools {
  22. namespace fuzz {
  23. // Rules for transformations
  24. // -------------------------
  25. //
  26. // - Immutability: a transformation must be immutable.
  27. // - Ability to copy and serialize: to ensure that a copy of a transformation,
  28. // possibly saved out to disk and read back again, is indistinguishable
  29. // from the original transformation, thus a transformation must depend
  30. // only on well-defined pieces of state, such as instruction ids. It must
  31. // not rely on state such as pointers to instructions and blocks.
  32. // - Determinism: the effect of a transformation on a module be a deterministic
  33. // function of the module and the transformation. Any randomization should
  34. // be applied before creating the transformation, not during its
  35. // application.
  36. // - Well-defined and precondition: the 'IsApplicable' method should only
  37. // return true if the transformation can be cleanly applied to the given
  38. // module, to mutate it into a valid and semantically-equivalent module, as
  39. // long as the module is initially valid.
  40. // - Ability to test precondition on any valid module: 'IsApplicable' should be
  41. // designed so that it is safe to ask whether a transformation is
  42. // applicable to an arbitrary valid module. For example, if a
  43. // transformation involves a block id, 'IsApplicable' should check whether
  44. // the module indeed has a block with that id, and return false if not. It
  45. // must not assume that there is such a block.
  46. // - Documented precondition: while the implementation of 'IsApplicable' should
  47. // should codify the precondition, the method should be commented in the
  48. // header file for a transformation with a precise English description of
  49. // the precondition.
  50. // - Documented effect: while the implementation of 'Apply' should codify the
  51. // effect of the transformation, the method should be commented in the
  52. // header file for a transformation with a precise English description of
  53. // the effect.
  54. class Transformation {
  55. public:
  56. virtual ~Transformation();
  57. // Factory method to obtain a transformation object from the protobuf
  58. // representation of a transformation given by |message|.
  59. static std::unique_ptr<Transformation> FromMessage(
  60. const protobufs::Transformation& message);
  61. // A precondition that determines whether the transformation can be cleanly
  62. // applied in a semantics-preserving manner to the SPIR-V module given by
  63. // |ir_context|, in the presence of facts and other contextual information
  64. // captured by |transformation_context|.
  65. //
  66. // Preconditions for individual transformations must be documented in the
  67. // associated header file using precise English. The transformation context
  68. // provides access to facts about the module that are known to be true, on
  69. // which the precondition may depend.
  70. virtual bool IsApplicable(
  71. opt::IRContext* ir_context,
  72. const TransformationContext& transformation_context) const = 0;
  73. // Requires that IsApplicable(ir_context, *transformation_context) holds.
  74. // Applies the transformation, mutating |ir_context| and possibly updating
  75. // |transformation_context| with new facts established by the transformation.
  76. virtual void Apply(opt::IRContext* ir_context,
  77. TransformationContext* transformation_context) const = 0;
  78. // Returns the set of fresh ids that appear in the transformation's protobuf
  79. // message.
  80. virtual std::unordered_set<uint32_t> GetFreshIds() const = 0;
  81. // Turns the transformation into a protobuf message for serialization.
  82. virtual protobufs::Transformation ToMessage() const = 0;
  83. // Helper that returns true if and only if (a) |id| is a fresh id for the
  84. // module, and (b) |id| is not in |ids_used_by_this_transformation|, a set of
  85. // ids already known to be in use by a transformation. This is useful when
  86. // checking id freshness for a transformation that uses many ids, all of which
  87. // must be distinct.
  88. static bool CheckIdIsFreshAndNotUsedByThisTransformation(
  89. uint32_t id, opt::IRContext* ir_context,
  90. std::set<uint32_t>* ids_used_by_this_transformation);
  91. };
  92. } // namespace fuzz
  93. } // namespace spvtools
  94. #endif // SOURCE_FUZZ_TRANSFORMATION_H_