comparator_deep_blocks_first.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_COMPARATOR_BLOCKS_DEEP_FIRST_H_
  15. #define SOURCE_FUZZ_COMPARATOR_BLOCKS_DEEP_FIRST_H_
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/opt/ir_context.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. // Comparator for blocks, comparing them based on how deep they are nested
  21. // inside selection or loop constructs. Deeper blocks are considered less than
  22. // ones that are not as deep. The blocks are required to be in the same
  23. // function.
  24. class ComparatorDeepBlocksFirst {
  25. public:
  26. explicit ComparatorDeepBlocksFirst(opt::IRContext* ir_context)
  27. : ir_context_(ir_context) {}
  28. bool operator()(uint32_t bb1, uint32_t bb2) const {
  29. return this->operator()(fuzzerutil::MaybeFindBlock(ir_context_, bb1),
  30. fuzzerutil::MaybeFindBlock(ir_context_, bb2));
  31. }
  32. bool operator()(const opt::BasicBlock* bb1, opt::BasicBlock* bb2) const {
  33. assert(bb1 && bb2 && "The blocks must exist.");
  34. assert(bb1->GetParent() == bb2->GetParent() &&
  35. "The blocks must be in the same functions.");
  36. return ir_context_->GetStructuredCFGAnalysis()->NestingDepth(bb1->id()) >
  37. ir_context_->GetStructuredCFGAnalysis()->NestingDepth(bb2->id());
  38. }
  39. private:
  40. opt::IRContext* ir_context_;
  41. };
  42. } // namespace fuzz
  43. } // namespace spvtools
  44. #endif // SOURCE_FUZZ_COMPARATOR_BLOCKS_DEEP_FIRST_H_