[llvm] r324242 - [ThinLTO] Convert dead alias to declarations
Teresa Johnson via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 5 07:44:27 PST 2018
Author: tejohnson
Date: Mon Feb 5 07:44:27 2018
New Revision: 324242
URL: http://llvm.org/viewvc/llvm-project?rev=324242&view=rev
Log:
[ThinLTO] Convert dead alias to declarations
Summary:
This complements the fixes in r323633 and r324075 which drop the
definitions of dead functions and variables, respectively.
Fixes PR36208.
Reviewers: grimar, rafael
Subscribers: mehdi_amini, llvm-commits, inglorion
Differential Revision: https://reviews.llvm.org/D42856
Added:
llvm/trunk/test/LTO/Resolution/X86/not-prevailing-alias.ll
Modified:
llvm/trunk/include/llvm/Transforms/IPO/FunctionImport.h
llvm/trunk/lib/LTO/LTOBackend.cpp
llvm/trunk/lib/Transforms/IPO/FunctionImport.cpp
Modified: llvm/trunk/include/llvm/Transforms/IPO/FunctionImport.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO/FunctionImport.h?rev=324242&r1=324241&r2=324242&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Transforms/IPO/FunctionImport.h (original)
+++ llvm/trunk/include/llvm/Transforms/IPO/FunctionImport.h Mon Feb 5 07:44:27 2018
@@ -122,8 +122,9 @@ void computeDeadSymbols(
const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing);
-/// Converts value \p GV to declaration.
-void convertToDeclaration(GlobalValue &GV);
+/// Converts value \p GV to declaration, or replaces with a declaration if
+/// it is an alias. Returns true if converted, false if replaced.
+bool convertToDeclaration(GlobalValue &GV);
/// Compute the set of summaries needed for a ThinLTO backend compilation of
/// \p ModulePath.
Modified: llvm/trunk/lib/LTO/LTOBackend.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOBackend.cpp?rev=324242&r1=324241&r2=324242&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/LTOBackend.cpp (original)
+++ llvm/trunk/lib/LTO/LTOBackend.cpp Mon Feb 5 07:44:27 2018
@@ -401,18 +401,14 @@ Error lto::backend(Config &C, AddStreamF
static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
const ModuleSummaryIndex &Index) {
- auto MaybeDrop = [&](GlobalValue &GV) {
+ std::vector<GlobalValue *> ReplacedGlobals;
+ for (auto &GV : Mod.global_values())
if (GlobalValueSummary *GVS = DefinedGlobals.lookup(GV.getGUID()))
- if (!Index.isGlobalValueLive(GVS))
- convertToDeclaration(GV);
- };
+ if (!Index.isGlobalValueLive(GVS) && !convertToDeclaration(GV))
+ ReplacedGlobals.push_back(&GV);
- // Process functions and global now.
- // FIXME: add support for aliases (needs support in convertToDeclaration).
- for (auto &GV : Mod)
- MaybeDrop(GV);
- for (auto &GV : Mod.globals())
- MaybeDrop(GV);
+ for (GlobalValue *GV : ReplacedGlobals)
+ GV->eraseFromParent();
}
Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream,
Modified: llvm/trunk/lib/Transforms/IPO/FunctionImport.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/FunctionImport.cpp?rev=324242&r1=324241&r2=324242&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/FunctionImport.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/FunctionImport.cpp Mon Feb 5 07:44:27 2018
@@ -613,7 +613,7 @@ llvm::EmitImportsFiles(StringRef ModuleP
return std::error_code();
}
-void llvm::convertToDeclaration(GlobalValue &GV) {
+bool llvm::convertToDeclaration(GlobalValue &GV) {
DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName() << "\n");
if (Function *F = dyn_cast<Function>(&GV)) {
F->deleteBody();
@@ -624,14 +624,24 @@ void llvm::convertToDeclaration(GlobalVa
V->setLinkage(GlobalValue::ExternalLinkage);
V->clearMetadata();
V->setComdat(nullptr);
- } else
- // For now we don't resolve or drop aliases. Once we do we'll
- // need to add support here for creating either a function or
- // variable declaration, and return the new GlobalValue* for
- // the caller to use.
- // Support of dropping aliases is required for correct dead code
- // elimination performed in thin LTO backends (see 'dropDeadSymbols').
- llvm_unreachable("Expected function or variable");
+ } else {
+ GlobalValue *NewGV;
+ if (GV.getValueType()->isFunctionTy())
+ NewGV =
+ Function::Create(cast<FunctionType>(GV.getValueType()),
+ GlobalValue::ExternalLinkage, "", GV.getParent());
+ else
+ NewGV =
+ new GlobalVariable(*GV.getParent(), GV.getValueType(),
+ /*isConstant*/ false, GlobalValue::ExternalLinkage,
+ /*init*/ nullptr, "",
+ /*insertbefore*/ nullptr, GV.getThreadLocalMode(),
+ GV.getType()->getAddressSpace());
+ NewGV->takeName(&GV);
+ GV.replaceAllUsesWith(NewGV);
+ return false;
+ }
+ return true;
}
/// Fixup WeakForLinker linkages in \p TheModule based on summary analysis.
@@ -666,9 +676,13 @@ void llvm::thinLTOResolveWeakForLinkerMo
// interposable property and possibly get inlined. Simply drop
// the definition in that case.
if (GlobalValue::isAvailableExternallyLinkage(NewLinkage) &&
- GlobalValue::isInterposableLinkage(GV.getLinkage()))
- convertToDeclaration(GV);
- else {
+ GlobalValue::isInterposableLinkage(GV.getLinkage())) {
+ if (!convertToDeclaration(GV))
+ // FIXME: Change this to collect replaced GVs and later erase
+ // them from the parent module once thinLTOResolveWeakForLinkerGUID is
+ // changed to enable this for aliases.
+ llvm_unreachable("Expected GV to be converted");
+ } else {
DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
<< GV.getLinkage() << " to " << NewLinkage << "\n");
GV.setLinkage(NewLinkage);
Added: llvm/trunk/test/LTO/Resolution/X86/not-prevailing-alias.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/LTO/Resolution/X86/not-prevailing-alias.ll?rev=324242&view=auto
==============================================================================
--- llvm/trunk/test/LTO/Resolution/X86/not-prevailing-alias.ll (added)
+++ llvm/trunk/test/LTO/Resolution/X86/not-prevailing-alias.ll Mon Feb 5 07:44:27 2018
@@ -0,0 +1,43 @@
+; Test to ensure that dead alias are dropped by converting to a declaration
+; RUN: opt -module-summary %s -o %t1.bc
+; RUN: llvm-lto2 run %t1.bc -r %t1.bc,barAlias,x \
+; RUN: -r %t1.bc,bar,x -r %t1.bc,zed,px \
+; RUN: -r %t1.bc,var,x -r %t1.bc,varAlias,x \
+; RUN: -o %t2.o -save-temps
+
+; Check that bar and barAlias were dropped to declarations
+; RUN: llvm-dis %t2.o.1.1.promote.bc -o - | FileCheck %s --check-prefix=DROP
+; DROP-DAG: declare void @bar()
+; DROP-DAG: declare void @barAlias()
+; DROP-DAG: @var = external global i32
+; DROP-DAG: @varAlias = external global i32
+
+; Check that 'barAlias' and 'varAlias' were not inlined.
+; RUN: llvm-objdump -d %t2.o.1 | FileCheck %s
+; CHECK: zed:
+; CHECK-NEXT: {{.*}} pushq
+; CHECK-NEXT: {{.*}} callq 0
+; CHECK-NEXT: movq (%rip), %rax
+
+; Check that 'barAlias' and 'varAlias' produced as undefined.
+; RUN: llvm-readelf --symbols %t2.o.1 | FileCheck %s --check-prefix=SYMBOLS
+; SYMBOLS: NOTYPE GLOBAL DEFAULT UND barAlias
+; SYMBOLS: NOTYPE GLOBAL DEFAULT UND varAlias
+; SYMBOLS: FUNC GLOBAL DEFAULT 2 zed
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at barAlias = alias void(), void()* @bar
+define void @bar() {
+ ret void
+}
+
+ at var = global i32 99
+ at varAlias = alias i32, i32* @var
+
+define i32 @zed() {
+ call void @barAlias()
+ %1 = load i32, i32* @varAlias, align 4
+ ret i32 %1
+}
More information about the llvm-commits
mailing list