available_instructions.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2021 Alastair F. Donaldson
  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_AVAILABLE_INSTRUCTIONS_H_
  15. #define SOURCE_FUZZ_AVAILABLE_INSTRUCTIONS_H_
  16. #include <unordered_map>
  17. #include <vector>
  18. #include "source/opt/instruction.h"
  19. #include "source/opt/ir_context.h"
  20. namespace spvtools {
  21. namespace fuzz {
  22. // A class for allowing efficient querying of the instruction that satisfy a
  23. // particular predicate that are available before a given instruction.
  24. // Availability information is only computed for instructions in *reachable*
  25. // basic blocks.
  26. class AvailableInstructions {
  27. public:
  28. // The outer class captures availability information for a whole module, and
  29. // each instance of this inner class captures availability for a particular
  30. // instruction.
  31. class AvailableBeforeInstruction {
  32. public:
  33. AvailableBeforeInstruction(
  34. const AvailableInstructions& available_instructions,
  35. opt::Instruction* inst);
  36. // Returns the number of instructions that are available before the
  37. // instruction associated with this class.
  38. uint32_t size() const;
  39. // Returns true if and only if |size()| is 0.
  40. bool empty() const;
  41. // Requires |index| < |size()|. Returns the ith available instruction.
  42. opt::Instruction* operator[](uint32_t index) const;
  43. private:
  44. // A references to an instance of the outer class.
  45. const AvailableInstructions& available_instructions_;
  46. // The instruction for which availability information is captured.
  47. opt::Instruction* inst_;
  48. // A cache to improve the efficiency of the [] operator. The [] operator
  49. // requires walking the instruction's dominator tree to find an instruction
  50. // at a particular index, which is a linear time operation. By inserting all
  51. // instructions that are traversed during this search into a cache, future
  52. // lookups will take constant time unless they require traversing the
  53. // dominator tree more deeply.
  54. mutable std::unordered_map<uint32_t, opt::Instruction*> index_cache;
  55. };
  56. // Constructs availability instructions for |ir_context|, where instructions
  57. // are only available if they satisfy |predicate|.
  58. AvailableInstructions(
  59. opt::IRContext* ir_context,
  60. const std::function<bool(opt::IRContext*, opt::Instruction*)>& predicate);
  61. // Yields instruction availability for |inst|.
  62. AvailableBeforeInstruction GetAvailableBeforeInstruction(
  63. opt::Instruction* inst) const;
  64. private:
  65. // The module in which all instructions are contained.
  66. opt::IRContext* ir_context_;
  67. // The global instructions that satisfy the predicate.
  68. std::vector<opt::Instruction*> available_globals_;
  69. // Per function, the parameters that satisfy the predicate.
  70. std::unordered_map<opt::Function*, std::vector<opt::Instruction*>>
  71. available_params_;
  72. // The number of instructions that satisfy the predicate and that are
  73. // available at the entry to a block. For the entry block of a function this
  74. // is the number of available globals + the number of available function
  75. // parameters. For any other block it is the number of available instructions
  76. // for the blocks immediate dominator + the number of instructions generated
  77. // by the immediate dominator.
  78. std::unordered_map<opt::BasicBlock*, uint32_t> num_available_at_block_entry_;
  79. // For each block this records those instructions in the block that satisfy
  80. // the predicate.
  81. std::unordered_map<opt::BasicBlock*, std::vector<opt::Instruction*>>
  82. generated_by_block_;
  83. // For each instruction this records how many instructions satisfying the
  84. // predicate are available before the instruction.
  85. std::unordered_map<opt::Instruction*, uint32_t>
  86. num_available_before_instruction_;
  87. };
  88. } // namespace fuzz
  89. } // namespace spvtools
  90. #endif // SOURCE_FUZZ_AVAILABLE_INSTRUCTIONS_H_