diff --git a/llvm/test/Reduce/remove-dso-local.ll b/llvm/test/Reduce/remove-dso-local.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Reduce/remove-dso-local.ll @@ -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) + 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 @@ -19,6 +19,7 @@ deltas/ReduceBasicBlocks.cpp deltas/ReduceFunctionBodies.cpp deltas/ReduceFunctions.cpp + deltas/ReduceGlobalValues.cpp deltas/ReduceGlobalVarInitializers.cpp deltas/ReduceGlobalVars.cpp deltas/ReduceInstructions.cpp diff --git a/llvm/tools/llvm-reduce/DeltaManager.h b/llvm/tools/llvm-reduce/DeltaManager.h --- a/llvm/tools/llvm-reduce/DeltaManager.h +++ b/llvm/tools/llvm-reduce/DeltaManager.h @@ -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 @@ reduceFunctionBodiesDeltaPass(Tester); reduceFunctionsDeltaPass(Tester); reduceBasicBlocksDeltaPass(Tester); + reduceGlobalValuesDeltaPass(Tester); reduceGlobalsInitializersDeltaPass(Tester); reduceGlobalsDeltaPass(Tester); reduceMetadataDeltaPass(Tester); diff --git a/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.h b/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.h @@ -0,0 +1,19 @@ +//===- 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 in order +// to reduce Global Values. +// +//===----------------------------------------------------------------------===// + +#include "Delta.h" + +namespace llvm { +void reduceGlobalValuesDeltaPass(TestRunner &Test); +} // namespace llvm diff --git a/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp b/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp @@ -0,0 +1,51 @@ +//===- 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 in order +// to reduce Global Values. +// +//===----------------------------------------------------------------------===// + +#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 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/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 @@ -17,6 +17,7 @@ "deltas/ReduceBasicBlocks.cpp", "deltas/ReduceFunctionBodies.cpp", "deltas/ReduceFunctions.cpp", + "deltas/ReduceGlobalValues.cpp", "deltas/ReduceGlobalVarInitializers.cpp", "deltas/ReduceGlobalVars.cpp", "deltas/ReduceInstructions.cpp",