2021-04-01 20:38:39 -07:00
|
|
|
//===- DeltaManager.cpp - Runs Delta Passes to reduce Input ---------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file calls each specialized Delta pass in order to reduce the input IR
|
|
|
|
// file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DeltaManager.h"
|
2022-06-03 16:32:04 -07:00
|
|
|
#include "ReducerWorkItem.h"
|
2021-04-01 20:38:39 -07:00
|
|
|
#include "TestRunner.h"
|
|
|
|
#include "deltas/Delta.h"
|
|
|
|
#include "deltas/ReduceAliases.h"
|
|
|
|
#include "deltas/ReduceArguments.h"
|
|
|
|
#include "deltas/ReduceAttributes.h"
|
|
|
|
#include "deltas/ReduceBasicBlocks.h"
|
2022-10-06 14:20:13 -07:00
|
|
|
#include "deltas/ReduceDIMetadata.h"
|
2021-04-01 20:38:39 -07:00
|
|
|
#include "deltas/ReduceFunctionBodies.h"
|
|
|
|
#include "deltas/ReduceFunctions.h"
|
2021-10-30 19:27:35 -07:00
|
|
|
#include "deltas/ReduceGlobalObjects.h"
|
2021-04-01 20:38:39 -07:00
|
|
|
#include "deltas/ReduceGlobalValues.h"
|
|
|
|
#include "deltas/ReduceGlobalVarInitializers.h"
|
|
|
|
#include "deltas/ReduceGlobalVars.h"
|
2022-04-19 12:10:38 -04:00
|
|
|
#include "deltas/ReduceIRReferences.h"
|
2022-10-21 10:37:52 -07:00
|
|
|
#include "deltas/ReduceInstructionFlags.h"
|
2022-04-19 16:42:27 -04:00
|
|
|
#include "deltas/ReduceInstructionFlagsMIR.h"
|
2021-04-01 20:38:39 -07:00
|
|
|
#include "deltas/ReduceInstructions.h"
|
2021-11-02 10:11:54 +01:00
|
|
|
#include "deltas/ReduceInstructionsMIR.h"
|
2023-01-01 21:29:20 -05:00
|
|
|
#include "deltas/ReduceInvokes.h"
|
2022-10-21 14:03:51 -07:00
|
|
|
#include "deltas/ReduceMemoryOperations.h"
|
2021-04-01 20:38:39 -07:00
|
|
|
#include "deltas/ReduceMetadata.h"
|
2021-08-23 10:58:16 -07:00
|
|
|
#include "deltas/ReduceModuleData.h"
|
2022-10-10 19:42:17 -07:00
|
|
|
#include "deltas/ReduceOpcodes.h"
|
2021-04-01 20:38:39 -07:00
|
|
|
#include "deltas/ReduceOperandBundles.h"
|
2021-09-17 12:18:03 -07:00
|
|
|
#include "deltas/ReduceOperands.h"
|
[llvm-reduce] Introduce operands-skip pass.
Add a new "operands-skip" pass whose goal is to remove instructions in the middle of dependency chains. For instance:
```
%baseptr = alloca i32
%arrayidx = getelementptr i32, i32* %baseptr, i32 %idxprom
store i32 42, i32* %arrayidx
```
might be reducible to
```
%baseptr = alloca i32
%arrayidx = getelementptr ... ; now dead, together with the computation of %idxprom
store i32 42, i32* %baseptr
```
Other passes would either replace `%baseptr` with undef (operands, instructions) or move it to become a function argument (operands-to-args), both of which might fail the interestingness check.
In principle the implementation allows operand replacement with any value or instruction in the function that passes the filter constraints (same type, dominance, "more reduced"), but is limited in this patch to values that are directly or indirectly used to compute the current operand value, motivated by the example above. Additionally, function arguments are added to the candidate set which helps reducing the number of relevant arguments mitigating a concern of too many arguments mentioned in https://reviews.llvm.org/D110274#3025013.
Possible future extensions:
* Instead of requiring the same type, bitcast/trunc/zext could be automatically inserted for some more flexibility.
* If undef is added to the candidate set, "operands-skip"is able to produce any reduction that "operands" can do. Additional candidates might be zero and one, where the "reductive power" classification can prefer one over the other. If undefined behaviour should not be introduced, undef can be removed from the candidate set.
Recommit after resolving conflict with D112651 and reusing
shouldReduceOperand from D113532.
Reviewed By: aeubanks
Differential Revision: https://reviews.llvm.org/D111818
2021-11-11 18:23:27 -06:00
|
|
|
#include "deltas/ReduceOperandsSkip.h"
|
2021-10-13 09:05:54 -05:00
|
|
|
#include "deltas/ReduceOperandsToArgs.h"
|
2022-06-27 14:28:33 -04:00
|
|
|
#include "deltas/ReduceRegisterDefs.h"
|
2022-06-22 13:13:17 -04:00
|
|
|
#include "deltas/ReduceRegisterMasks.h"
|
2022-04-22 11:44:37 -04:00
|
|
|
#include "deltas/ReduceRegisterUses.h"
|
2021-04-01 20:38:39 -07:00
|
|
|
#include "deltas/ReduceSpecialGlobals.h"
|
2022-08-15 15:45:20 -06:00
|
|
|
#include "deltas/ReduceUsingSimplifyCFG.h"
|
2022-04-20 11:37:53 -04:00
|
|
|
#include "deltas/ReduceVirtualRegisters.h"
|
2022-04-13 17:36:58 -07:00
|
|
|
#include "deltas/RunIRPasses.h"
|
2022-06-08 22:09:47 -04:00
|
|
|
#include "deltas/SimplifyInstructions.h"
|
2022-10-18 15:42:14 -07:00
|
|
|
#include "deltas/StripDebugInfo.h"
|
2021-04-30 11:48:03 -07:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2021-04-01 20:38:39 -07:00
|
|
|
|
2021-04-30 11:48:03 -07:00
|
|
|
using namespace llvm;
|
|
|
|
|
2022-10-10 11:02:47 -07:00
|
|
|
using SmallStringSet = SmallSet<StringRef, 8>;
|
|
|
|
|
2022-02-02 09:31:29 +01:00
|
|
|
extern cl::OptionCategory LLVMReduceOptions;
|
2022-10-10 11:02:47 -07:00
|
|
|
static cl::list<std::string>
|
2021-04-30 11:48:03 -07:00
|
|
|
DeltaPasses("delta-passes",
|
|
|
|
cl::desc("Delta passes to run, separated by commas. By "
|
2022-02-02 09:31:29 +01:00
|
|
|
"default, run all delta passes."),
|
2022-10-10 11:02:47 -07:00
|
|
|
cl::cat(LLVMReduceOptions), cl::CommaSeparated);
|
|
|
|
|
|
|
|
static cl::list<std::string>
|
|
|
|
SkipDeltaPasses("skip-delta-passes",
|
|
|
|
cl::desc("Delta passes to not run, separated by commas. By "
|
|
|
|
"default, run all delta passes."),
|
|
|
|
cl::cat(LLVMReduceOptions), cl::CommaSeparated);
|
2021-04-30 11:48:03 -07:00
|
|
|
|
|
|
|
#define DELTA_PASSES \
|
2022-07-16 20:57:44 -07:00
|
|
|
do { \
|
2022-10-18 15:42:14 -07:00
|
|
|
DELTA_PASS("strip-debug-info", stripDebugInfoDeltaPass) \
|
2022-08-02 10:32:49 -06:00
|
|
|
DELTA_PASS("functions", reduceFunctionsDeltaPass) \
|
|
|
|
DELTA_PASS("function-bodies", reduceFunctionBodiesDeltaPass) \
|
2022-07-16 20:57:44 -07:00
|
|
|
DELTA_PASS("special-globals", reduceSpecialGlobalsDeltaPass) \
|
|
|
|
DELTA_PASS("aliases", reduceAliasesDeltaPass) \
|
2022-11-22 16:55:44 -05:00
|
|
|
DELTA_PASS("ifuncs", reduceIFuncsDeltaPass) \
|
2022-10-13 14:42:15 -07:00
|
|
|
DELTA_PASS("simplify-conditionals-true", reduceConditionalsTrueDeltaPass) \
|
|
|
|
DELTA_PASS("simplify-conditionals-false", reduceConditionalsFalseDeltaPass)\
|
2023-01-01 21:29:20 -05:00
|
|
|
DELTA_PASS("invokes", reduceInvokesDeltaPass) \
|
2022-10-03 22:06:09 -07:00
|
|
|
DELTA_PASS("unreachable-basic-blocks", reduceUnreachableBasicBlocksDeltaPass) \
|
2022-07-16 20:57:44 -07:00
|
|
|
DELTA_PASS("basic-blocks", reduceBasicBlocksDeltaPass) \
|
2022-10-03 22:06:09 -07:00
|
|
|
DELTA_PASS("simplify-cfg", reduceUsingSimplifyCFGDeltaPass) \
|
2023-01-02 08:25:26 -05:00
|
|
|
DELTA_PASS("function-data", reduceFunctionDataDeltaPass) \
|
2022-07-16 20:57:44 -07:00
|
|
|
DELTA_PASS("global-values", reduceGlobalValuesDeltaPass) \
|
|
|
|
DELTA_PASS("global-objects", reduceGlobalObjectsDeltaPass) \
|
|
|
|
DELTA_PASS("global-initializers", reduceGlobalsInitializersDeltaPass) \
|
|
|
|
DELTA_PASS("global-variables", reduceGlobalsDeltaPass) \
|
2022-10-06 14:20:13 -07:00
|
|
|
DELTA_PASS("di-metadata", reduceDIMetadataDeltaPass) \
|
2022-07-16 20:57:44 -07:00
|
|
|
DELTA_PASS("metadata", reduceMetadataDeltaPass) \
|
2023-01-01 10:52:02 -05:00
|
|
|
DELTA_PASS("named-metadata", reduceNamedMetadataDeltaPass) \
|
2022-07-16 20:57:44 -07:00
|
|
|
DELTA_PASS("arguments", reduceArgumentsDeltaPass) \
|
|
|
|
DELTA_PASS("instructions", reduceInstructionsDeltaPass) \
|
|
|
|
DELTA_PASS("simplify-instructions", simplifyInstructionsDeltaPass) \
|
2022-04-13 17:36:58 -07:00
|
|
|
DELTA_PASS("ir-passes", runIRPassesDeltaPass) \
|
2022-07-16 20:57:44 -07:00
|
|
|
DELTA_PASS("operands-zero", reduceOperandsZeroDeltaPass) \
|
|
|
|
DELTA_PASS("operands-one", reduceOperandsOneDeltaPass) \
|
|
|
|
DELTA_PASS("operands-nan", reduceOperandsNaNDeltaPass) \
|
|
|
|
DELTA_PASS("operands-to-args", reduceOperandsToArgsDeltaPass) \
|
|
|
|
DELTA_PASS("operands-skip", reduceOperandsSkipDeltaPass) \
|
|
|
|
DELTA_PASS("operand-bundles", reduceOperandBundesDeltaPass) \
|
|
|
|
DELTA_PASS("attributes", reduceAttributesDeltaPass) \
|
|
|
|
DELTA_PASS("module-data", reduceModuleDataDeltaPass) \
|
2022-10-10 19:42:17 -07:00
|
|
|
DELTA_PASS("opcodes", reduceOpcodesDeltaPass) \
|
2022-10-21 14:03:51 -07:00
|
|
|
DELTA_PASS("volatile", reduceVolatileInstructionsDeltaPass) \
|
2022-10-21 14:59:07 -07:00
|
|
|
DELTA_PASS("atomic-ordering", reduceAtomicOrderingDeltaPass) \
|
2022-10-21 14:45:28 -07:00
|
|
|
DELTA_PASS("syncscopes", reduceAtomicSyncScopesDeltaPass) \
|
2022-10-21 10:37:52 -07:00
|
|
|
DELTA_PASS("instruction-flags", reduceInstructionFlagsDeltaPass) \
|
|
|
|
} while (false)
|
2021-04-30 11:48:03 -07:00
|
|
|
|
2021-11-02 10:11:54 +01:00
|
|
|
#define DELTA_PASSES_MIR \
|
2022-07-16 20:57:44 -07:00
|
|
|
do { \
|
|
|
|
DELTA_PASS("instructions", reduceInstructionsMIRDeltaPass) \
|
|
|
|
DELTA_PASS("ir-instruction-references", \
|
|
|
|
reduceIRInstructionReferencesDeltaPass) \
|
|
|
|
DELTA_PASS("ir-block-references", reduceIRBlockReferencesDeltaPass) \
|
|
|
|
DELTA_PASS("ir-function-references", reduceIRFunctionReferencesDeltaPass) \
|
|
|
|
DELTA_PASS("instruction-flags", reduceInstructionFlagsMIRDeltaPass) \
|
|
|
|
DELTA_PASS("register-uses", reduceRegisterUsesMIRDeltaPass) \
|
2022-06-27 14:28:33 -04:00
|
|
|
DELTA_PASS("register-defs", reduceRegisterDefsMIRDeltaPass) \
|
2022-07-16 20:57:44 -07:00
|
|
|
DELTA_PASS("register-hints", reduceVirtualRegisterHintsDeltaPass) \
|
2022-06-22 13:13:17 -04:00
|
|
|
DELTA_PASS("register-masks", reduceRegisterMasksMIRDeltaPass) \
|
2022-07-16 20:57:44 -07:00
|
|
|
} while (false)
|
2021-11-02 10:11:54 +01:00
|
|
|
|
2022-10-10 11:02:47 -07:00
|
|
|
static void runAllDeltaPasses(TestRunner &Tester,
|
|
|
|
const SmallStringSet &SkipPass) {
|
|
|
|
#define DELTA_PASS(NAME, FUNC) \
|
|
|
|
if (!SkipPass.count(NAME)) { \
|
|
|
|
FUNC(Tester); \
|
|
|
|
}
|
2021-11-02 10:11:54 +01:00
|
|
|
if (Tester.getProgram().isMIR()) {
|
2022-07-16 20:57:44 -07:00
|
|
|
DELTA_PASSES_MIR;
|
2021-11-02 10:11:54 +01:00
|
|
|
} else {
|
2022-07-16 20:57:44 -07:00
|
|
|
DELTA_PASSES;
|
2021-11-02 10:11:54 +01:00
|
|
|
}
|
2021-04-30 11:48:03 -07:00
|
|
|
#undef DELTA_PASS
|
|
|
|
}
|
|
|
|
|
|
|
|
static void runDeltaPassName(TestRunner &Tester, StringRef PassName) {
|
|
|
|
#define DELTA_PASS(NAME, FUNC) \
|
|
|
|
if (PassName == NAME) { \
|
|
|
|
FUNC(Tester); \
|
|
|
|
return; \
|
|
|
|
}
|
2021-11-02 10:11:54 +01:00
|
|
|
if (Tester.getProgram().isMIR()) {
|
2022-07-16 20:57:44 -07:00
|
|
|
DELTA_PASSES_MIR;
|
2021-11-02 10:11:54 +01:00
|
|
|
} else {
|
2022-07-16 20:57:44 -07:00
|
|
|
DELTA_PASSES;
|
2021-11-02 10:11:54 +01:00
|
|
|
}
|
2021-04-30 11:48:03 -07:00
|
|
|
#undef DELTA_PASS
|
2022-10-10 11:02:47 -07:00
|
|
|
|
|
|
|
// We should have errored on unrecognized passes before trying to run
|
|
|
|
// anything.
|
|
|
|
llvm_unreachable("unknown delta pass");
|
2021-04-30 11:48:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::printDeltaPasses(raw_ostream &OS) {
|
|
|
|
OS << "Delta passes (pass to `--delta-passes=` as a comma separated list):\n";
|
|
|
|
#define DELTA_PASS(NAME, FUNC) OS << " " << NAME << "\n";
|
2021-11-02 10:11:54 +01:00
|
|
|
OS << " IR:\n";
|
2022-07-16 20:57:44 -07:00
|
|
|
DELTA_PASSES;
|
2021-11-02 10:11:54 +01:00
|
|
|
OS << " MIR:\n";
|
2022-07-16 20:57:44 -07:00
|
|
|
DELTA_PASSES_MIR;
|
2021-04-30 11:48:03 -07:00
|
|
|
#undef DELTA_PASS
|
|
|
|
}
|
|
|
|
|
2022-10-10 11:02:47 -07:00
|
|
|
// Built a set of available delta passes.
|
|
|
|
static void collectPassNames(const TestRunner &Tester,
|
|
|
|
SmallStringSet &NameSet) {
|
|
|
|
#define DELTA_PASS(NAME, FUNC) NameSet.insert(NAME);
|
|
|
|
if (Tester.getProgram().isMIR()) {
|
|
|
|
DELTA_PASSES_MIR;
|
|
|
|
} else {
|
|
|
|
DELTA_PASSES;
|
|
|
|
}
|
|
|
|
#undef DELTA_PASS
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify all requested or skipped passes are valid names, and return them in a
|
|
|
|
/// set.
|
|
|
|
static SmallStringSet handlePassList(const TestRunner &Tester,
|
|
|
|
const cl::list<std::string> &PassList) {
|
|
|
|
SmallStringSet AllPasses;
|
|
|
|
collectPassNames(Tester, AllPasses);
|
|
|
|
|
|
|
|
SmallStringSet PassSet;
|
|
|
|
for (StringRef PassName : PassList) {
|
|
|
|
if (!AllPasses.count(PassName)) {
|
|
|
|
errs() << "unknown pass \"" << PassName << "\"\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
PassSet.insert(PassName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PassSet;
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:24:23 -07:00
|
|
|
void llvm::runDeltaPasses(TestRunner &Tester, int MaxPassIterations) {
|
2022-04-19 17:19:36 -04:00
|
|
|
uint64_t OldComplexity = Tester.getProgram().getComplexityScore();
|
2022-10-10 11:02:47 -07:00
|
|
|
|
|
|
|
SmallStringSet RunPassSet, SkipPassSet;
|
|
|
|
|
|
|
|
if (!DeltaPasses.empty())
|
|
|
|
RunPassSet = handlePassList(Tester, DeltaPasses);
|
|
|
|
|
|
|
|
if (!SkipDeltaPasses.empty())
|
|
|
|
SkipPassSet = handlePassList(Tester, SkipDeltaPasses);
|
|
|
|
|
2022-01-10 22:24:23 -07:00
|
|
|
for (int Iter = 0; Iter < MaxPassIterations; ++Iter) {
|
|
|
|
if (DeltaPasses.empty()) {
|
2022-10-10 11:02:47 -07:00
|
|
|
runAllDeltaPasses(Tester, SkipPassSet);
|
2022-01-10 22:24:23 -07:00
|
|
|
} else {
|
2022-10-10 11:02:47 -07:00
|
|
|
for (StringRef PassName : DeltaPasses) {
|
|
|
|
if (!SkipPassSet.count(PassName))
|
|
|
|
runDeltaPassName(Tester, PassName);
|
2022-01-10 22:24:23 -07:00
|
|
|
}
|
2021-04-30 11:48:03 -07:00
|
|
|
}
|
2022-10-10 11:02:47 -07:00
|
|
|
|
2022-04-19 17:19:36 -04:00
|
|
|
uint64_t NewComplexity = Tester.getProgram().getComplexityScore();
|
|
|
|
if (NewComplexity >= OldComplexity)
|
2022-01-10 22:24:23 -07:00
|
|
|
break;
|
2022-04-19 17:19:36 -04:00
|
|
|
OldComplexity = NewComplexity;
|
2021-04-30 11:48:03 -07:00
|
|
|
}
|
2021-04-01 20:38:39 -07:00
|
|
|
}
|