[llvm] r324075 - [ThinLTO] - Fix for "ThinLTO inlines variables that should be discarded".

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 2 04:17:33 PST 2018


Author: grimar
Date: Fri Feb  2 04:17:33 2018
New Revision: 324075

URL: http://llvm.org/viewvc/llvm-project?rev=324075&view=rev
Log:
[ThinLTO] - Fix for "ThinLTO inlines variables that should be discarded".

This fixes PR36187.

Patch teaches ThinLTO to drop non-prevailing variables, 
just like we recently did for functions (in r323633).

Differential revision: https://reviews.llvm.org/D42798

Added:
    llvm/trunk/test/LTO/Resolution/X86/not-prevailing-variables.ll
Modified:
    llvm/trunk/lib/LTO/LTOBackend.cpp
    llvm/trunk/test/tools/gold/X86/global_with_section.ll

Modified: llvm/trunk/lib/LTO/LTOBackend.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOBackend.cpp?rev=324075&r1=324074&r2=324075&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/LTOBackend.cpp (original)
+++ llvm/trunk/lib/LTO/LTOBackend.cpp Fri Feb  2 04:17:33 2018
@@ -401,13 +401,19 @@ Error lto::backend(Config &C, AddStreamF
 
 static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
                             const ModuleSummaryIndex &Index) {
-  for (auto &GV : Mod) {
+  auto MaybeDrop = [&](GlobalValue &GV) {
     auto It = DefinedGlobals.find(GV.getGUID());
-    if (It == DefinedGlobals.end())
-      continue;
-    if (!Index.isGlobalValueLive(It->second))
-      convertToDeclaration(GV);
-  }
+    if (It != DefinedGlobals.end())
+      if (!Index.isGlobalValueLive(It->second))
+        convertToDeclaration(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);
 }
 
 Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream,

Added: llvm/trunk/test/LTO/Resolution/X86/not-prevailing-variables.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/LTO/Resolution/X86/not-prevailing-variables.ll?rev=324075&view=auto
==============================================================================
--- llvm/trunk/test/LTO/Resolution/X86/not-prevailing-variables.ll (added)
+++ llvm/trunk/test/LTO/Resolution/X86/not-prevailing-variables.ll Fri Feb  2 04:17:33 2018
@@ -0,0 +1,35 @@
+; RUN: opt -module-summary %s -o %t1.o
+; RUN: llvm-lto2 run -save-temps -o %t2.o %t1.o      \
+; RUN:   -r %t1.o,testVar1,plx -r %t1.o,testVar2,plx \
+; RUN:   -r %t1.o,var1,pl -r %t1.o,var2,lx
+
+; Test contains two variables: var1 and var2.
+; var2 is not prevailing and here we check it is not inlined.
+
+; Check 'var2' was not inlined.
+; RUN: llvm-objdump -d %t2.o.1 | FileCheck %s
+; CHECK:      testVar1:
+; CHECK-NEXT:   movl $10, %eax
+; CHECK-NEXT:   retq
+; CHECK:      testVar2:
+; CHECK-NEXT:   movl  (%rip), %eax
+; CHECK-NEXT:   retq
+
+; Check 'var2' is undefined.
+; RUN: llvm-readelf --symbols %t2.o.1 | FileCheck %s --check-prefix=UND
+; UND: NOTYPE  GLOBAL DEFAULT UND var2
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at var1 = global i32 10, align 4
+define i32 @testVar1() {
+  %1 = load i32, i32* @var1, align 4
+  ret i32 %1
+}
+
+ at var2 = global i32 11, align 4
+define i32 @testVar2() {
+  %1 = load i32, i32* @var2, align 4
+  ret i32 %1
+}

Modified: llvm/trunk/test/tools/gold/X86/global_with_section.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/gold/X86/global_with_section.ll?rev=324075&r1=324074&r2=324075&view=diff
==============================================================================
--- llvm/trunk/test/tools/gold/X86/global_with_section.ll (original)
+++ llvm/trunk/test/tools/gold/X86/global_with_section.ll Fri Feb  2 04:17:33 2018
@@ -45,7 +45,9 @@ target triple = "x86_64-unknown-linux-gn
 
 ; Confirm via a variable with a non-C identifier section that we are getting
 ; the expected internalization.
-; CHECK-DAG: @var_with_nonC_section = internal global i32 0, section ".nonCsection"
+; CHECK-REGULARLTO-DAG: @var_with_nonC_section = internal global i32 0, section ".nonCsection"
+; Check we dropped definition of dead variable.
+; CHECK-THINLTO-DAG: @var_with_nonC_section = external dso_local global i32, section ".nonCsection"
 @var_with_nonC_section = global i32 0, section ".nonCsection"
 
 ; We should not internalize @deadfunc_with_section due to section




More information about the llvm-commits mailing list