[llvm-reduce] Remove dso_local when possible
authorSamuel <swamulism@gmail.com>
Mon, 29 Mar 2021 04:18:45 +0000 (21:18 -0700)
committerArthur Eubanks <aeubanks@google.com>
Mon, 29 Mar 2021 19:00:10 +0000 (12:00 -0700)
Add a new delta pass to llvm-reduce that removes dso_local when possible

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D98673

llvm/test/Reduce/remove-dso-local.ll [new file with mode: 0644]
llvm/tools/llvm-reduce/CMakeLists.txt
llvm/tools/llvm-reduce/DeltaManager.h
llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp [new file with mode: 0644]
llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.h [new file with mode: 0644]
llvm/utils/gn/secondary/llvm/tools/llvm-reduce/BUILD.gn

diff --git a/llvm/test/Reduce/remove-dso-local.ll b/llvm/test/Reduce/remove-dso-local.ll
new file mode 100644 (file)
index 0000000..6589057
--- /dev/null
@@ -0,0 +1,24 @@
+; Test that llvm-reduce can remove dso_local.
+;
+; RUN: llvm-reduce --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
+; RUN: FileCheck --check-prefix=CHECK-FINAL %s < %t
+
+; CHECK-INTERESTINGNESS: declare
+; CHECK-INTERESTINGNESS-SAME: void @f0
+; CHECK-INTERESTINGNESS-SAME: i32
+; CHECK-INTERESTINGNESS-SAME: i32
+
+; CHECK-FINAL: declare void @f0(i32, i32)
+
+declare dso_local void @f0(i32, i32)
+
+; CHECK-INTERESTINGNESS: declare
+; CHECK-INTERESTINGNESS-SAME: dso_local
+; CHECK-INTERESTINGNESS-SAME: void @f1
+; CHECK-INTERESTINGNESS-SAME: i32
+; CHECK-INTERESTINGNESS-SAME: i32
+
+; CHECK-FINAL: declare dso_local void @f1(i32, i32)
+
+declare dso_local void @f1(i32, i32)
+
index b6db920..0f91677 100644 (file)
@@ -19,6 +19,7 @@ add_llvm_tool(llvm-reduce
   deltas/ReduceBasicBlocks.cpp
   deltas/ReduceFunctionBodies.cpp
   deltas/ReduceFunctions.cpp
+  deltas/ReduceGlobalValues.cpp
   deltas/ReduceGlobalVarInitializers.cpp
   deltas/ReduceGlobalVars.cpp
   deltas/ReduceInstructions.cpp
index 18a6b0d..5970a3a 100644 (file)
@@ -19,6 +19,7 @@
 #include "deltas/ReduceBasicBlocks.h"
 #include "deltas/ReduceFunctionBodies.h"
 #include "deltas/ReduceFunctions.h"
+#include "deltas/ReduceGlobalValues.h"
 #include "deltas/ReduceGlobalVarInitializers.h"
 #include "deltas/ReduceGlobalVars.h"
 #include "deltas/ReduceInstructions.h"
@@ -35,6 +36,7 @@ inline void runDeltaPasses(TestRunner &Tester) {
   reduceFunctionBodiesDeltaPass(Tester);
   reduceFunctionsDeltaPass(Tester);
   reduceBasicBlocksDeltaPass(Tester);
+  reduceGlobalValuesDeltaPass(Tester);
   reduceGlobalsInitializersDeltaPass(Tester);
   reduceGlobalsDeltaPass(Tester);
   reduceMetadataDeltaPass(Tester);
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp b/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp
new file mode 100644 (file)
index 0000000..73aa15d
--- /dev/null
@@ -0,0 +1,50 @@
+//===- ReduceGlobalValues.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 to reduce
+// global value attributes/specifiers.
+//
+//===----------------------------------------------------------------------===//
+
+#include "ReduceGlobalValues.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/GlobalValue.h"
+
+using namespace llvm;
+
+/// Sets dso_local to false for all global values.
+static void extractGVsFromModule(std::vector<Chunk> ChunksToKeep,
+                                 Module *Program) {
+  Oracle O(ChunksToKeep);
+
+  // remove dso_local from global values
+  for (auto &GV : Program->global_values())
+    if (GV.isDSOLocal() && !O.shouldKeep()) {
+      GV.setDSOLocal(false);
+    }
+}
+
+/// Counts the amount of global values with dso_local and displays their
+/// respective name & index
+static int countGVs(Module *Program) {
+  // TODO: Silence index with --quiet flag
+  outs() << "----------------------------\n";
+  outs() << "GlobalValue Index Reference:\n";
+  int GVCount = 0;
+  for (auto &GV : Program->global_values())
+    if (GV.isDSOLocal())
+      outs() << "\t" << ++GVCount << ": " << GV.getName() << "\n";
+  outs() << "----------------------------\n";
+  return GVCount;
+}
+
+void llvm::reduceGlobalValuesDeltaPass(TestRunner &Test) {
+  outs() << "*** Reducing GlobalValues...\n";
+  int GVCount = countGVs(Test.getProgram());
+  runDeltaPass(Test, GVCount, extractGVsFromModule);
+}
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.h b/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.h
new file mode 100644 (file)
index 0000000..ea32a6c
--- /dev/null
@@ -0,0 +1,23 @@
+//===- ReduceGlobalValues.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 to reduce
+// global value attributes/specifiers.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEGLOBALVALUES_H
+#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEGLOBALVALUES_H
+
+#include "Delta.h"
+
+namespace llvm {
+void reduceGlobalValuesDeltaPass(TestRunner &Test);
+} // namespace llvm
+
+#endif
index 7bdae5e..a00f00c 100644 (file)
@@ -17,6 +17,7 @@ executable("llvm-reduce") {
     "deltas/ReduceBasicBlocks.cpp",
     "deltas/ReduceFunctionBodies.cpp",
     "deltas/ReduceFunctions.cpp",
+    "deltas/ReduceGlobalValues.cpp",
     "deltas/ReduceGlobalVarInitializers.cpp",
     "deltas/ReduceGlobalVars.cpp",
     "deltas/ReduceInstructions.cpp",