[llvm] r371567 - llvm-reduce: Add pass to reduce parameters
David Blaikie via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 10 16:10:10 PDT 2019
Author: dblaikie
Date: Tue Sep 10 16:10:10 2019
New Revision: 371567
URL: http://llvm.org/viewvc/llvm-project?rev=371567&view=rev
Log:
llvm-reduce: Add pass to reduce parameters
Patch by Diego TreviƱo!
Differential Revision: https://reviews.llvm.org/D65479
Added:
llvm/trunk/test/Reduce/Inputs/remove-args.py (with props)
llvm/trunk/test/Reduce/remove-args.ll
llvm/trunk/tools/llvm-reduce/deltas/ReduceArguments.cpp
llvm/trunk/tools/llvm-reduce/deltas/ReduceArguments.h
Modified:
llvm/trunk/tools/llvm-reduce/CMakeLists.txt
llvm/trunk/tools/llvm-reduce/DeltaManager.h
Added: llvm/trunk/test/Reduce/Inputs/remove-args.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Reduce/Inputs/remove-args.py?rev=371567&view=auto
==============================================================================
--- llvm/trunk/test/Reduce/Inputs/remove-args.py (added)
+++ llvm/trunk/test/Reduce/Inputs/remove-args.py Tue Sep 10 16:10:10 2019
@@ -0,0 +1,16 @@
+import sys
+
+InterestingArgumentPresent = False
+FunctionCallPresent = False
+
+input = open(sys.argv[1], "r")
+for line in input:
+ if "%interesting" in line:
+ InterestingArgumentPresent = True
+ if "call void @interesting" in line:
+ FunctionCallPresent = True
+
+if InterestingArgumentPresent and FunctionCallPresent:
+ sys.exit(0) # Interesting!
+
+sys.exit(1)
Propchange: llvm/trunk/test/Reduce/Inputs/remove-args.py
------------------------------------------------------------------------------
svn:executable = *
Added: llvm/trunk/test/Reduce/remove-args.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Reduce/remove-args.ll?rev=371567&view=auto
==============================================================================
--- llvm/trunk/test/Reduce/remove-args.ll (added)
+++ llvm/trunk/test/Reduce/remove-args.ll Tue Sep 10 16:10:10 2019
@@ -0,0 +1,22 @@
+; Test that llvm-reduce can remove uninteresting function arguments from function definitions as well as their calls.
+;
+; RUN: rm -rf %t
+; RUN: mkdir %t
+; copy the test file to preserve executable bit
+; RUN: cp %p/Inputs/remove-args.py %t/test.py
+; get the python path from lit
+; RUN: echo "#!" %python > %t/test.py
+; then include the rest of the test script
+; RUN: cat %p/Inputs/remove-args.py >> %t/test.py
+
+; RUN: llvm-reduce --test %t/test.py %s -o %t/out.ll
+; RUN: cat %t/out.ll | FileCheck -implicit-check-not=uninteresting %s
+; REQUIRES: plugins
+
+; CHECK: @interesting(i32 %interesting)
+define void @interesting(i32 %uninteresting1, i32 %interesting, i32 %uninteresting2) {
+entry:
+ ; CHECK: call void @interesting(i32 0)
+ call void @interesting(i32 -1, i32 0, i32 -1)
+ ret void
+}
Modified: llvm/trunk/tools/llvm-reduce/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-reduce/CMakeLists.txt?rev=371567&r1=371566&r2=371567&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-reduce/CMakeLists.txt (original)
+++ llvm/trunk/tools/llvm-reduce/CMakeLists.txt Tue Sep 10 16:10:10 2019
@@ -20,6 +20,7 @@ add_llvm_tool(llvm-reduce
deltas/ReduceFunctions.cpp
deltas/ReduceGlobalVars.cpp
deltas/ReduceMetadata.cpp
+ deltas/ReduceArguments.cpp
DEPENDS
intrinsics_gen
Modified: llvm/trunk/tools/llvm-reduce/DeltaManager.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-reduce/DeltaManager.h?rev=371567&r1=371566&r2=371567&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-reduce/DeltaManager.h (original)
+++ llvm/trunk/tools/llvm-reduce/DeltaManager.h Tue Sep 10 16:10:10 2019
@@ -13,6 +13,7 @@
#include "TestRunner.h"
#include "deltas/Delta.h"
+#include "deltas/ReduceArguments.h"
#include "deltas/ReduceFunctions.h"
#include "deltas/ReduceGlobalVars.h"
#include "deltas/ReduceMetadata.h"
@@ -24,6 +25,7 @@ inline void runDeltaPasses(TestRunner &T
reduceFunctionsDeltaPass(Tester);
reduceGlobalsDeltaPass(Tester);
reduceMetadataDeltaPass(Tester);
+ reduceArgumentsDeltaPass(Tester);
// TODO: Implement the remaining Delta Passes
}
Added: llvm/trunk/tools/llvm-reduce/deltas/ReduceArguments.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-reduce/deltas/ReduceArguments.cpp?rev=371567&view=auto
==============================================================================
--- llvm/trunk/tools/llvm-reduce/deltas/ReduceArguments.cpp (added)
+++ llvm/trunk/tools/llvm-reduce/deltas/ReduceArguments.cpp Tue Sep 10 16:10:10 2019
@@ -0,0 +1,125 @@
+//===- ReduceArguments.cpp - Specialized Delta Pass -----------------------===//
+//
+// 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 implements a function which calls the Generic Delta pass in order
+// to reduce uninteresting Arguments from defined functions.
+//
+//===----------------------------------------------------------------------===//
+
+#include "ReduceArguments.h"
+#include "Delta.h"
+#include "llvm/ADT/SmallVector.h"
+#include <set>
+#include <vector>
+
+using namespace llvm;
+
+/// Goes over OldF calls and replaces them with a call to NewF
+static void replaceFunctionCalls(Function &OldF, Function &NewF,
+ const std::set<int> &ArgIndexesToKeep) {
+ for (auto *U : OldF.users())
+ if (auto *CI = dyn_cast<CallInst>(U)) {
+ SmallVector<Value *, 8> Args;
+ for (auto ArgI = CI->arg_begin(), E = CI->arg_end(); ArgI != E; ++ArgI)
+ if (ArgIndexesToKeep.count(ArgI - CI->arg_begin()))
+ Args.push_back(*ArgI);
+
+ CallInst *NewCI = CallInst::Create(&NewF, Args);
+ NewCI->setCallingConv(NewF.getCallingConv());
+ if (!CI->use_empty())
+ CI->replaceAllUsesWith(NewCI);
+ ReplaceInstWithInst(CI, NewCI);
+ }
+}
+
+/// Removes out-of-chunk arguments from functions, and modifies their calls
+/// accordingly. It also removes allocations of out-of-chunk arguments.
+/// @returns the Module stripped of out-of-chunk functions
+static void extractArgumentsFromModule(std::vector<Chunk> ChunksToKeep,
+ Module *Program) {
+ unsigned I = 0, ArgCount = 0;
+ std::set<Argument *> ArgsToKeep;
+ std::vector<Function *> Funcs;
+ // Get inside-chunk arguments, as well as their parent function
+ for (auto &F : *Program)
+ if (!F.isDeclaration()) {
+ Funcs.push_back(&F);
+ for (auto &A : F.args())
+ if (I < ChunksToKeep.size()) {
+ if (ChunksToKeep[I].contains(++ArgCount))
+ ArgsToKeep.insert(&A);
+ if (ChunksToKeep[I].end == ArgCount)
+ ++I;
+ }
+ }
+
+ for (auto *F : Funcs) {
+ ValueToValueMapTy VMap;
+ std::vector<Instruction *> InstToDelete;
+ for (auto &A : F->args())
+ if (!ArgsToKeep.count(&A)) {
+ // By adding undesired arguments to the VMap, CloneFunction will remove
+ // them from the resulting Function
+ VMap[&A] = UndefValue::get(A.getType());
+ for (auto *U : A.users())
+ if (auto *I = dyn_cast<Instruction>(*&U))
+ InstToDelete.push_back(I);
+ }
+ // Delete any instruction that uses the argument
+ for (auto *I : InstToDelete) {
+ I->replaceAllUsesWith(UndefValue::get(I->getType()));
+ I->eraseFromParent();
+ }
+
+ // No arguments to reduce
+ if (VMap.empty())
+ continue;
+
+ std::set<int> ArgIndexesToKeep;
+ int ArgI = 0;
+ for (auto &Arg : F->args())
+ if (ArgsToKeep.count(&Arg))
+ ArgIndexesToKeep.insert(++ArgI);
+
+ auto *ClonedFunc = CloneFunction(F, VMap);
+ // In order to preserve function order, we move Clone after old Function
+ ClonedFunc->removeFromParent();
+ Program->getFunctionList().insertAfter(F->getIterator(), ClonedFunc);
+
+ replaceFunctionCalls(*F, *ClonedFunc, ArgIndexesToKeep);
+ // Rename Cloned Function to Old's name
+ auto FName = F->getName();
+ F->eraseFromParent();
+ ClonedFunc->setName(FName);
+ }
+}
+
+/// Counts the amount of arguments in non-declaration functions and prints their
+/// respective name, index, and parent function name
+static int countArguments(Module *Program) {
+ // TODO: Silence index with --quiet flag
+ outs() << "----------------------------\n";
+ outs() << "Param Index Reference:\n";
+ int ArgsCount = 0;
+ for (auto &F : *Program)
+ if (!F.isDeclaration() && F.arg_size()) {
+ outs() << " " << F.getName() << "\n";
+ for (auto &A : F.args())
+ outs() << "\t" << ++ArgsCount << ": " << A.getName() << "\n";
+
+ outs() << "----------------------------\n";
+ }
+
+ return ArgsCount;
+}
+
+void llvm::reduceArgumentsDeltaPass(TestRunner &Test) {
+ outs() << "*** Reducing Arguments...\n";
+ unsigned ArgCount = countArguments(Test.getProgram());
+ runDeltaPass(Test, ArgCount, extractArgumentsFromModule);
+}
Added: llvm/trunk/tools/llvm-reduce/deltas/ReduceArguments.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-reduce/deltas/ReduceArguments.h?rev=371567&view=auto
==============================================================================
--- llvm/trunk/tools/llvm-reduce/deltas/ReduceArguments.h (added)
+++ llvm/trunk/tools/llvm-reduce/deltas/ReduceArguments.h Tue Sep 10 16:10:10 2019
@@ -0,0 +1,21 @@
+//===- ReduceArguments.h - Specialized Delta Pass -------------------------===//
+//
+// 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 implements a function which calls the Generic Delta pass in order
+// to reduce uninteresting Arguments from defined functions.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Delta.h"
+#include "llvm/IR/Argument.h"
+#include "llvm/Transforms/Utils/BasicBlockUtils.h"
+#include "llvm/Transforms/Utils/Cloning.h"
+
+namespace llvm {
+void reduceArgumentsDeltaPass(TestRunner &Test);
+} // namespace llvm
More information about the llvm-commits
mailing list