DeltaManager.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //===- DeltaManager.cpp - Runs Delta Passes to reduce Input ---------------===//
  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 calls each specialized Delta pass in order to reduce the input IR
  10. // file.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "DeltaManager.h"
  14. #include "TestRunner.h"
  15. #include "deltas/Delta.h"
  16. #include "deltas/ReduceAliases.h"
  17. #include "deltas/ReduceArguments.h"
  18. #include "deltas/ReduceAttributes.h"
  19. #include "deltas/ReduceBasicBlocks.h"
  20. #include "deltas/ReduceFunctionBodies.h"
  21. #include "deltas/ReduceFunctions.h"
  22. #include "deltas/ReduceGlobalObjects.h"
  23. #include "deltas/ReduceGlobalValues.h"
  24. #include "deltas/ReduceGlobalVarInitializers.h"
  25. #include "deltas/ReduceGlobalVars.h"
  26. #include "deltas/ReduceInstructions.h"
  27. #include "deltas/ReduceInstructionsMIR.h"
  28. #include "deltas/ReduceMetadata.h"
  29. #include "deltas/ReduceModuleData.h"
  30. #include "deltas/ReduceOperandBundles.h"
  31. #include "deltas/ReduceOperands.h"
  32. #include "deltas/ReduceOperandsSkip.h"
  33. #include "deltas/ReduceOperandsToArgs.h"
  34. #include "deltas/ReduceSpecialGlobals.h"
  35. #include "llvm/Support/CommandLine.h"
  36. using namespace llvm;
  37. static cl::opt<std::string>
  38. DeltaPasses("delta-passes",
  39. cl::desc("Delta passes to run, separated by commas. By "
  40. "default, run all delta passes."));
  41. #define DELTA_PASSES \
  42. DELTA_PASS("special-globals", reduceSpecialGlobalsDeltaPass) \
  43. DELTA_PASS("aliases", reduceAliasesDeltaPass) \
  44. DELTA_PASS("function-bodies", reduceFunctionBodiesDeltaPass) \
  45. DELTA_PASS("functions", reduceFunctionsDeltaPass) \
  46. DELTA_PASS("basic-blocks", reduceBasicBlocksDeltaPass) \
  47. DELTA_PASS("global-values", reduceGlobalValuesDeltaPass) \
  48. DELTA_PASS("global-objects", reduceGlobalObjectsDeltaPass) \
  49. DELTA_PASS("global-initializers", reduceGlobalsInitializersDeltaPass) \
  50. DELTA_PASS("global-variables", reduceGlobalsDeltaPass) \
  51. DELTA_PASS("metadata", reduceMetadataDeltaPass) \
  52. DELTA_PASS("arguments", reduceArgumentsDeltaPass) \
  53. DELTA_PASS("instructions", reduceInstructionsDeltaPass) \
  54. DELTA_PASS("operands-zero", reduceOperandsZeroDeltaPass) \
  55. DELTA_PASS("operands-one", reduceOperandsOneDeltaPass) \
  56. DELTA_PASS("operands-undef", reduceOperandsUndefDeltaPass) \
  57. DELTA_PASS("operands-to-args", reduceOperandsToArgsDeltaPass) \
  58. DELTA_PASS("operands-skip", reduceOperandsSkipDeltaPass) \
  59. DELTA_PASS("operand-bundles", reduceOperandBundesDeltaPass) \
  60. DELTA_PASS("attributes", reduceAttributesDeltaPass) \
  61. DELTA_PASS("module-data", reduceModuleDataDeltaPass)
  62. #define DELTA_PASSES_MIR \
  63. DELTA_PASS("instructions", reduceInstructionsMIRDeltaPass)
  64. static void runAllDeltaPasses(TestRunner &Tester) {
  65. #define DELTA_PASS(NAME, FUNC) FUNC(Tester);
  66. if (Tester.getProgram().isMIR()) {
  67. DELTA_PASSES_MIR
  68. } else {
  69. DELTA_PASSES
  70. }
  71. #undef DELTA_PASS
  72. }
  73. static void runDeltaPassName(TestRunner &Tester, StringRef PassName) {
  74. #define DELTA_PASS(NAME, FUNC) \
  75. if (PassName == NAME) { \
  76. FUNC(Tester); \
  77. return; \
  78. }
  79. if (Tester.getProgram().isMIR()) {
  80. DELTA_PASSES_MIR
  81. } else {
  82. DELTA_PASSES
  83. }
  84. #undef DELTA_PASS
  85. errs() << "unknown pass \"" << PassName << "\"\n";
  86. exit(1);
  87. }
  88. void llvm::printDeltaPasses(raw_ostream &OS) {
  89. OS << "Delta passes (pass to `--delta-passes=` as a comma separated list):\n";
  90. #define DELTA_PASS(NAME, FUNC) OS << " " << NAME << "\n";
  91. OS << " IR:\n";
  92. DELTA_PASSES
  93. OS << " MIR:\n";
  94. DELTA_PASSES_MIR
  95. #undef DELTA_PASS
  96. }
  97. // FIXME: We might want to use a different metric than "number of
  98. // bytes in serialized IR" to detect non-progress of the main delta
  99. // loop
  100. static int getIRSize(TestRunner &Tester) {
  101. std::string Str;
  102. raw_string_ostream SS(Str);
  103. Tester.getProgram().print(SS, /*AnnotationWriter=*/nullptr);
  104. return Str.length();
  105. }
  106. void llvm::runDeltaPasses(TestRunner &Tester, int MaxPassIterations) {
  107. int OldSize = getIRSize(Tester);
  108. for (int Iter = 0; Iter < MaxPassIterations; ++Iter) {
  109. if (DeltaPasses.empty()) {
  110. runAllDeltaPasses(Tester);
  111. } else {
  112. StringRef Passes = DeltaPasses;
  113. while (!Passes.empty()) {
  114. auto Split = Passes.split(",");
  115. runDeltaPassName(Tester, Split.first);
  116. Passes = Split.second;
  117. }
  118. }
  119. int NewSize = getIRSize(Tester);
  120. if (NewSize >= OldSize)
  121. break;
  122. OldSize = NewSize;
  123. }
  124. }