Skip to content

Commit 136c8f5

Browse files
committed
[Reduce] Try turning function definitions into declarations first, NFCI-ish
ReduceFunctions could do it, but it also replaces *all* calls with undef, so if any of undef replacements makes reduction uninteresting, it won't work. ReduceBasicBlocks also could do it, but well, it may take many guesses for all the blocks of a function to happen to be out-of-chunk, which is not a very efficient way to go about it. So let's just do this first.
1 parent 1d9b860 commit 136c8f5

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; RUN: llvm-reduce --test FileCheck --test-arg --check-prefixes=CHECK-ALL,CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
2+
; RUN: cat %t | FileCheck --check-prefixes=CHECK-ALL,CHECK-FINAL %s
3+
4+
; CHECK-INTERESTINGNESS: @callee(
5+
; CHECK-FINAL: declare void @callee()
6+
define void @callee() {
7+
ret void
8+
}
9+
10+
; CHECK-ALL: define void @caller()
11+
define void @caller() {
12+
entry:
13+
; CHECK-ALL: call void @callee()
14+
; CHECK-ALL: ret void
15+
call void @callee()
16+
ret void
17+
}

llvm/tools/llvm-reduce/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_llvm_tool(llvm-reduce
1616
deltas/ReduceArguments.cpp
1717
deltas/ReduceAttributes.cpp
1818
deltas/ReduceBasicBlocks.cpp
19+
deltas/ReduceFunctionBodies.cpp
1920
deltas/ReduceFunctions.cpp
2021
deltas/ReduceGlobalVars.cpp
2122
deltas/ReduceInstructions.cpp

llvm/tools/llvm-reduce/DeltaManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "deltas/ReduceArguments.h"
1717
#include "deltas/ReduceAttributes.h"
1818
#include "deltas/ReduceBasicBlocks.h"
19+
#include "deltas/ReduceFunctionBodies.h"
1920
#include "deltas/ReduceFunctions.h"
2021
#include "deltas/ReduceGlobalVars.h"
2122
#include "deltas/ReduceInstructions.h"
@@ -26,6 +27,7 @@ namespace llvm {
2627

2728
// TODO: Add CLI option to run only specified Passes (for unit tests)
2829
inline void runDeltaPasses(TestRunner &Tester) {
30+
reduceFunctionBodiesDeltaPass(Tester);
2931
reduceFunctionsDeltaPass(Tester);
3032
reduceBasicBlocksDeltaPass(Tester);
3133
reduceGlobalsDeltaPass(Tester);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===- ReduceFunctions.cpp - Specialized Delta Pass -----------------------===//
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 implements a function which calls the Generic Delta pass in order
10+
// to reduce function bodies in the provided Module.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "ReduceFunctionBodies.h"
15+
#include "Delta.h"
16+
17+
using namespace llvm;
18+
19+
/// Removes all the bodies of defined functions that aren't inside any of the
20+
/// desired Chunks.
21+
static void
22+
extractFunctionBodiesFromModule(const std::vector<Chunk> &ChunksToKeep,
23+
Module *Program) {
24+
Oracle O(ChunksToKeep);
25+
26+
// Delete out-of-chunk function bodies
27+
std::vector<Function *> FuncDefsToReduce;
28+
for (auto &F : *Program)
29+
if (!F.isDeclaration() && !O.shouldKeep())
30+
F.deleteBody();
31+
}
32+
33+
/// Counts the amount of non-declaration functions and prints their
34+
/// respective name & index
35+
static int countFunctionDefinitions(Module *Program) {
36+
// TODO: Silence index with --quiet flag
37+
errs() << "----------------------------\n";
38+
errs() << "Function Definition Index Reference:\n";
39+
int FunctionDefinitionCount = 0;
40+
for (auto &F : *Program)
41+
if (!F.isDeclaration())
42+
errs() << "\t" << ++FunctionDefinitionCount << ": " << F.getName()
43+
<< "\n";
44+
45+
errs() << "----------------------------\n";
46+
return FunctionDefinitionCount;
47+
}
48+
49+
void llvm::reduceFunctionBodiesDeltaPass(TestRunner &Test) {
50+
errs() << "*** Reducing Function Bodies...\n";
51+
int Functions = countFunctionDefinitions(Test.getProgram());
52+
runDeltaPass(Test, Functions, extractFunctionBodiesFromModule);
53+
errs() << "----------------------------\n";
54+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===- ReduceFunctionBodies.h - Specialized Delta Pass --------------------===//
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 implements a function which calls the Generic Delta pass in order
10+
// to reduce function bodies in the provided Module.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "Delta.h"
15+
16+
namespace llvm {
17+
void reduceFunctionBodiesDeltaPass(TestRunner &Test);
18+
} // namespace llvm

0 commit comments

Comments
 (0)