Skip to content

Commit 2433905

Browse files
saeubankaeubanks
authored andcommitted
[llvm-reduce] Remove dso_local when possible
Add a new delta pass to llvm-reduce that removes dso_local when possible Reviewed By: aeubanks Differential Revision: https://reviews.llvm.org/D98673
1 parent bc4d3ca commit 2433905

File tree

6 files changed

+101
-0
lines changed

6 files changed

+101
-0
lines changed

llvm/test/Reduce/remove-dso-local.ll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
; Test that llvm-reduce can remove dso_local.
2+
;
3+
; RUN: llvm-reduce --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
4+
; RUN: FileCheck --check-prefix=CHECK-FINAL %s < %t
5+
6+
; CHECK-INTERESTINGNESS: declare
7+
; CHECK-INTERESTINGNESS-SAME: void @f0
8+
; CHECK-INTERESTINGNESS-SAME: i32
9+
; CHECK-INTERESTINGNESS-SAME: i32
10+
11+
; CHECK-FINAL: declare void @f0(i32, i32)
12+
13+
declare dso_local void @f0(i32, i32)
14+
15+
; CHECK-INTERESTINGNESS: declare
16+
; CHECK-INTERESTINGNESS-SAME: dso_local
17+
; CHECK-INTERESTINGNESS-SAME: void @f1
18+
; CHECK-INTERESTINGNESS-SAME: i32
19+
; CHECK-INTERESTINGNESS-SAME: i32
20+
21+
; CHECK-FINAL: declare dso_local void @f1(i32, i32)
22+
23+
declare dso_local void @f1(i32, i32)
24+

llvm/tools/llvm-reduce/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ add_llvm_tool(llvm-reduce
1919
deltas/ReduceBasicBlocks.cpp
2020
deltas/ReduceFunctionBodies.cpp
2121
deltas/ReduceFunctions.cpp
22+
deltas/ReduceGlobalValues.cpp
2223
deltas/ReduceGlobalVarInitializers.cpp
2324
deltas/ReduceGlobalVars.cpp
2425
deltas/ReduceInstructions.cpp

llvm/tools/llvm-reduce/DeltaManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "deltas/ReduceBasicBlocks.h"
2020
#include "deltas/ReduceFunctionBodies.h"
2121
#include "deltas/ReduceFunctions.h"
22+
#include "deltas/ReduceGlobalValues.h"
2223
#include "deltas/ReduceGlobalVarInitializers.h"
2324
#include "deltas/ReduceGlobalVars.h"
2425
#include "deltas/ReduceInstructions.h"
@@ -35,6 +36,7 @@ inline void runDeltaPasses(TestRunner &Tester) {
3536
reduceFunctionBodiesDeltaPass(Tester);
3637
reduceFunctionsDeltaPass(Tester);
3738
reduceBasicBlocksDeltaPass(Tester);
39+
reduceGlobalValuesDeltaPass(Tester);
3840
reduceGlobalsInitializersDeltaPass(Tester);
3941
reduceGlobalsDeltaPass(Tester);
4042
reduceMetadataDeltaPass(Tester);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===- ReduceGlobalValues.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 to reduce
10+
// global value attributes/specifiers.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "ReduceGlobalValues.h"
15+
#include "llvm/IR/Constants.h"
16+
#include "llvm/IR/GlobalValue.h"
17+
18+
using namespace llvm;
19+
20+
/// Sets dso_local to false for all global values.
21+
static void extractGVsFromModule(std::vector<Chunk> ChunksToKeep,
22+
Module *Program) {
23+
Oracle O(ChunksToKeep);
24+
25+
// remove dso_local from global values
26+
for (auto &GV : Program->global_values())
27+
if (GV.isDSOLocal() && !O.shouldKeep()) {
28+
GV.setDSOLocal(false);
29+
}
30+
}
31+
32+
/// Counts the amount of global values with dso_local and displays their
33+
/// respective name & index
34+
static int countGVs(Module *Program) {
35+
// TODO: Silence index with --quiet flag
36+
outs() << "----------------------------\n";
37+
outs() << "GlobalValue Index Reference:\n";
38+
int GVCount = 0;
39+
for (auto &GV : Program->global_values())
40+
if (GV.isDSOLocal())
41+
outs() << "\t" << ++GVCount << ": " << GV.getName() << "\n";
42+
outs() << "----------------------------\n";
43+
return GVCount;
44+
}
45+
46+
void llvm::reduceGlobalValuesDeltaPass(TestRunner &Test) {
47+
outs() << "*** Reducing GlobalValues...\n";
48+
int GVCount = countGVs(Test.getProgram());
49+
runDeltaPass(Test, GVCount, extractGVsFromModule);
50+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===- ReduceGlobalValues.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 to reduce
10+
// global value attributes/specifiers.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEGLOBALVALUES_H
15+
#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEGLOBALVALUES_H
16+
17+
#include "Delta.h"
18+
19+
namespace llvm {
20+
void reduceGlobalValuesDeltaPass(TestRunner &Test);
21+
} // namespace llvm
22+
23+
#endif

llvm/utils/gn/secondary/llvm/tools/llvm-reduce/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ executable("llvm-reduce") {
1717
"deltas/ReduceBasicBlocks.cpp",
1818
"deltas/ReduceFunctionBodies.cpp",
1919
"deltas/ReduceFunctions.cpp",
20+
"deltas/ReduceGlobalValues.cpp",
2021
"deltas/ReduceGlobalVarInitializers.cpp",
2122
"deltas/ReduceGlobalVars.cpp",
2223
"deltas/ReduceInstructions.cpp",

0 commit comments

Comments
 (0)