diff --git a/llvm/test/tools/llvm-reduce/run-ir-passes.ll b/llvm/test/tools/llvm-reduce/run-ir-passes.ll new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-reduce/run-ir-passes.ll @@ -0,0 +1,28 @@ +; RUN: llvm-reduce --delta-passes=ir-passes --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t +; RUN: FileCheck --check-prefixes=CHECK-FINAL --input-file=%t %s + +; CHECK-INTERESTINGNESS-LABEL: @f1 +; CHECK-INTERESTINGNESS: add +; CHECK-INTERESTINGNESS-LABEL: @f2 +; CHECK-INTERESTINGNESS: add +; CHECK-INTERESTINGNESS: add + +; CHECK-FINAL-LABEL: @f1 +; CHECK-FINAL: add i32 %a, 10 +; CHECK-FINAL-LABEL: @f2 +; CHECK-FINAL: add i32 %a, 5 +; CHECK-FINAL: add i32 %b, 5 + +define i32 @f1(i32 %a) { + %b = add i32 %a, 5 + %c = add i32 %b, 5 + ret i32 %c +} + +define i32 @f2(i32 %a) { + %b = add i32 %a, 5 + %c = add i32 %b, 5 + ret i32 %c +} + +declare void @f3() \ No newline at end of file diff --git a/llvm/tools/llvm-reduce/CMakeLists.txt b/llvm/tools/llvm-reduce/CMakeLists.txt --- a/llvm/tools/llvm-reduce/CMakeLists.txt +++ b/llvm/tools/llvm-reduce/CMakeLists.txt @@ -11,6 +11,7 @@ IRReader MC MIRParser + Passes Support Target TransformUtils @@ -48,6 +49,7 @@ deltas/ReduceRegisterMasks.cpp deltas/ReduceRegisterDefs.cpp deltas/ReduceRegisterUses.cpp + deltas/RunIRPasses.cpp deltas/SimplifyInstructions.cpp llvm-reduce.cpp diff --git a/llvm/tools/llvm-reduce/DeltaManager.cpp b/llvm/tools/llvm-reduce/DeltaManager.cpp --- a/llvm/tools/llvm-reduce/DeltaManager.cpp +++ b/llvm/tools/llvm-reduce/DeltaManager.cpp @@ -40,6 +40,7 @@ #include "deltas/ReduceRegisterUses.h" #include "deltas/ReduceSpecialGlobals.h" #include "deltas/ReduceVirtualRegisters.h" +#include "deltas/RunIRPasses.h" #include "deltas/SimplifyInstructions.h" #include "llvm/Support/CommandLine.h" @@ -67,6 +68,7 @@ DELTA_PASS("arguments", reduceArgumentsDeltaPass) \ DELTA_PASS("instructions", reduceInstructionsDeltaPass) \ DELTA_PASS("simplify-instructions", simplifyInstructionsDeltaPass) \ + DELTA_PASS("ir-passes", runIRPassesDeltaPass) \ DELTA_PASS("operands-zero", reduceOperandsZeroDeltaPass) \ DELTA_PASS("operands-one", reduceOperandsOneDeltaPass) \ DELTA_PASS("operands-nan", reduceOperandsNaNDeltaPass) \ diff --git a/llvm/tools/llvm-reduce/deltas/RunIRPasses.h b/llvm/tools/llvm-reduce/deltas/RunIRPasses.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/RunIRPasses.h @@ -0,0 +1,18 @@ +//===- RunIRPasses.h ------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_RUNPASSES_H +#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_RUNPASSES_H + +#include "Delta.h" + +namespace llvm { +void runIRPassesDeltaPass(TestRunner &Test); +} // namespace llvm + +#endif diff --git a/llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp b/llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp @@ -0,0 +1,53 @@ +//===- RunIRPasses.cpp ----------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "RunIRPasses.h" +#include "Delta.h" +#include "llvm/Passes/PassBuilder.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/ErrorHandling.h" + +using namespace llvm; + +static cl::opt PassPipeline( + "ir-passes", + cl::desc("A textual description of the pass pipeline, same as " + "what's passed to `opt -passes`."), + cl::init( + "function(sroa,instcombine,gvn,simplifycfg,infer-address-spaces)")); + +static void runPasses(Oracle &O, Module &Program) { + LoopAnalysisManager LAM; + FunctionAnalysisManager FAM; + CGSCCAnalysisManager CGAM; + ModuleAnalysisManager MAM; + + PassInstrumentationCallbacks PIC; + PIC.registerShouldRunOptionalPassCallback( + [&](StringRef, Any) { return !O.shouldKeep(); }); + PassBuilder PB(nullptr, PipelineTuningOptions(), None, &PIC); + + PB.registerModuleAnalyses(MAM); + PB.registerCGSCCAnalyses(CGAM); + PB.registerFunctionAnalyses(FAM); + PB.registerLoopAnalyses(LAM); + PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); + + ModulePassManager MPM; + if (auto Err = PB.parsePassPipeline(MPM, PassPipeline)) { + errs() << toString(std::move(Err)) << "\n"; + report_fatal_error("Error constructing pass pipeline"); + } + MPM.run(Program, MAM); +} + +void llvm::runIRPassesDeltaPass(TestRunner &Test) { + errs() << "*** Running passes ...\n"; + runDeltaPass(Test, runPasses); + errs() << "----------------------------\n"; +} diff --git a/llvm/utils/gn/secondary/llvm/tools/llvm-reduce/BUILD.gn b/llvm/utils/gn/secondary/llvm/tools/llvm-reduce/BUILD.gn --- a/llvm/utils/gn/secondary/llvm/tools/llvm-reduce/BUILD.gn +++ b/llvm/utils/gn/secondary/llvm/tools/llvm-reduce/BUILD.gn @@ -3,6 +3,7 @@ "//llvm/lib/CodeGen/MIRParser", "//llvm/lib/IR", "//llvm/lib/IRReader", + "//llvm/lib/Passes", "//llvm/lib/Support", "//llvm/lib/Target", "//llvm/lib/Target:TargetsToBuild", @@ -40,6 +41,7 @@ "deltas/ReduceRegisterUses.cpp", "deltas/ReduceSpecialGlobals.cpp", "deltas/ReduceVirtualRegisters.cpp", + "deltas/RunIRPasses.cpp", "deltas/SimplifyInstructions.cpp", "deltas/Utils.cpp", "llvm-reduce.cpp",