transformation_access_chain.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_ACCESS_CHAIN_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_ACCESS_CHAIN_H_
  16. #include <utility>
  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 TransformationAccessChain : public Transformation {
  24. public:
  25. explicit TransformationAccessChain(
  26. protobufs::TransformationAccessChain message);
  27. TransformationAccessChain(
  28. uint32_t fresh_id, uint32_t pointer_id,
  29. const std::vector<uint32_t>& index_id,
  30. const protobufs::InstructionDescriptor& instruction_to_insert_before,
  31. const std::vector<std::pair<uint32_t, uint32_t>>& fresh_ids_for_clamping =
  32. {});
  33. // - |message_.fresh_id| must be fresh.
  34. // - |message_.instruction_to_insert_before| must identify an instruction
  35. // before which it is legitimate to insert an OpAccessChain instruction.
  36. // - |message_.pointer_id| must be a result id with pointer type that is
  37. // available (according to dominance rules) at the insertion point.
  38. // - The pointer must not be OpConstantNull or OpUndef.
  39. // - |message_.index_id| must be a sequence of ids of 32-bit integers
  40. // such that it is possible to walk the pointee type of
  41. // |message_.pointer_id| using these indices.
  42. // - All indices used to access a struct must be OpConstant.
  43. // - The indices used to index non-struct composites will be clamped to be
  44. // in bound. Enough fresh ids must be given in
  45. // |message_.fresh_id_for_clamping| to perform clamping (2 for
  46. // each index accessing a non-struct). This requires the bool type and
  47. // a constant of value (bound - 1) to be declared in the module.
  48. // - If type t is the final type reached by walking these indices, the module
  49. // must include an instruction "OpTypePointer SC %t" where SC is the storage
  50. // class associated with |message_.pointer_id|.
  51. bool IsApplicable(
  52. opt::IRContext* ir_context,
  53. const TransformationContext& transformation_context) const override;
  54. // Adds an instruction of the form:
  55. // |message_.fresh_id| = OpAccessChain %ptr |message_.index_id|
  56. // where %ptr is the result if of an instruction declaring a pointer to the
  57. // type reached by walking the pointee type of |message_.pointer_id| using
  58. // the indices in |message_.index_id|, and with the same storage class as
  59. // |message_.pointer_id|.
  60. //
  61. // For each of the indices traversing non-struct composites, two clamping
  62. // instructions are added using ids in |message_.fresh_id_for_clamping|.
  63. //
  64. // If the fact manager in |transformation_context| reports that
  65. // |message_.pointer_id| has an irrelevant pointee value, then the fact that
  66. // |message_.fresh_id| (the result of the access chain) also has an irrelevant
  67. // pointee value is also recorded.
  68. void Apply(opt::IRContext* ir_context,
  69. TransformationContext* transformation_context) const override;
  70. std::unordered_set<uint32_t> GetFreshIds() const override;
  71. protobufs::Transformation ToMessage() const override;
  72. private:
  73. // Returns {false, 0} in each of the following cases:
  74. // - |index_id| does not correspond to a 32-bit integer constant
  75. // - |object_type_id| must be a struct type
  76. // - the constant at |index_id| is out of bounds.
  77. // Otherwise, returns {true, value}, where value is the value of the constant
  78. // at |index_id|.
  79. std::pair<bool, uint32_t> GetStructIndexValue(opt::IRContext* ir_context,
  80. uint32_t index_id,
  81. uint32_t object_type_id) const;
  82. // Returns true if |index_id| corresponds, in the given context, to a 32-bit
  83. // integer which can be used to index an object of the type specified by
  84. // |object_type_id|. Returns false otherwise.
  85. static bool ValidIndexToComposite(opt::IRContext* ir_context,
  86. uint32_t index_id, uint32_t object_type_id);
  87. protobufs::TransformationAccessChain message_;
  88. };
  89. } // namespace fuzz
  90. } // namespace spvtools
  91. #endif // SOURCE_FUZZ_TRANSFORMATION_ACCESS_CHAIN_H_