Delta.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //===- Delta.h - Delta Debugging Algorithm Implementation -----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains the implementation for the Delta Debugging Algorithm:
  10. // it splits a given set of Targets (i.e. Functions, Instructions, BBs, etc.)
  11. // into chunks and tries to reduce the number chunks that are interesting.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_DELTA_H
  15. #define LLVM_TOOLS_LLVM_REDUCE_DELTAS_DELTA_H
  16. #include "TestRunner.h"
  17. #include "llvm/ADT/ScopeExit.h"
  18. #include <functional>
  19. #include <utility>
  20. #include <vector>
  21. namespace llvm {
  22. struct Chunk {
  23. int Begin;
  24. int End;
  25. /// Helper function to verify if a given Target-index is inside the Chunk
  26. bool contains(int Index) const { return Index >= Begin && Index <= End; }
  27. void print() const {
  28. errs() << "[" << Begin;
  29. if (End - Begin != 0)
  30. errs() << "," << End;
  31. errs() << "]";
  32. }
  33. /// Operator when populating CurrentChunks in Generic Delta Pass
  34. friend bool operator!=(const Chunk &C1, const Chunk &C2) {
  35. return C1.Begin != C2.Begin || C1.End != C2.End;
  36. }
  37. /// Operator used for sets
  38. friend bool operator<(const Chunk &C1, const Chunk &C2) {
  39. return std::tie(C1.Begin, C1.End) < std::tie(C2.Begin, C2.End);
  40. }
  41. };
  42. /// Provides opaque interface for querying into ChunksToKeep without having to
  43. /// actually understand what is going on.
  44. class Oracle {
  45. /// Out of all the features that we promised to be,
  46. /// how many have we already processed?
  47. int Index = 0;
  48. /// The actual workhorse, contains the knowledge whether or not
  49. /// some particular feature should be preserved this time.
  50. ArrayRef<Chunk> ChunksToKeep;
  51. public:
  52. explicit Oracle(ArrayRef<Chunk> ChunksToKeep) : ChunksToKeep(ChunksToKeep) {}
  53. /// Should be called for each feature on which we are operating.
  54. /// Name is self-explanatory - if returns true, then it should be preserved.
  55. bool shouldKeep() {
  56. if (ChunksToKeep.empty()) {
  57. ++Index;
  58. return false; // All further features are to be discarded.
  59. }
  60. // Does the current (front) chunk contain such a feature?
  61. bool ShouldKeep = ChunksToKeep.front().contains(Index);
  62. // Is this the last feature in the chunk?
  63. if (ChunksToKeep.front().End == Index)
  64. ChunksToKeep = ChunksToKeep.drop_front(); // Onto next chunk.
  65. ++Index;
  66. return ShouldKeep;
  67. }
  68. int count() { return Index; }
  69. };
  70. /// This function implements the Delta Debugging algorithm, it receives a
  71. /// number of Targets (e.g. Functions, Instructions, Basic Blocks, etc.) and
  72. /// splits them in half; these chunks of targets are then tested while ignoring
  73. /// one chunk, if a chunk is proven to be uninteresting (i.e. fails the test)
  74. /// it is removed from consideration. The algorithm will attempt to split the
  75. /// Chunks in half and start the process again until it can't split chunks
  76. /// anymore.
  77. ///
  78. /// This function is intended to be called by each specialized delta pass (e.g.
  79. /// RemoveFunctions) and receives three key parameters:
  80. /// * Test: The main TestRunner instance which is used to run the provided
  81. /// interesting-ness test, as well as to store and access the reduced Program.
  82. /// * ExtractChunksFromModule: A function used to tailor the main program so it
  83. /// only contains Targets that are inside Chunks of the given iteration.
  84. /// Note: This function is implemented by each specialized Delta pass
  85. ///
  86. /// Other implementations of the Delta Debugging algorithm can also be found in
  87. /// the CReduce, Delta, and Lithium projects.
  88. void runDeltaPass(
  89. TestRunner &Test,
  90. function_ref<void(Oracle &, Module &)> ExtractChunksFromModule);
  91. void runDeltaPass(
  92. TestRunner &Test,
  93. function_ref<void(Oracle &, MachineFunction &)> ExtractChunksFromModule);
  94. } // namespace llvm
  95. #endif