[PATCH] D31443: [LTO] Do not reorder global variables unnecessarily during merging

Tobias Edler von Koch via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 28 15:02:29 PDT 2017


tobiasvk created this revision.

The new resolution-based LTO API leads to global variables being re-ordered
during module merging for regular LTO. With the old LTO API and in the non-LTO
case, the source order is preserved.

The new order is semi-random; it is caused by recursively materializing
variables as their uses are discovered during materialization of functions. In
practice, it is often the exact reverse of source order.

While the C standard makes no guarantee about the relative ordering of global
variables, there is no discernable benefit to re-ordering them in this way.
Worse, it can negatively affect performance by degrading cache locality and
introduces unnecessary variation in behavior and performance between the two
LTO APIs, and the non-LTO case.

This fixes PR32441.


https://reviews.llvm.org/D31443

Files:
  lib/Object/ModuleSymbolTable.cpp
  test/LTO/Resolution/X86/globalorder.ll


Index: test/LTO/Resolution/X86/globalorder.ll
===================================================================
--- /dev/null
+++ test/LTO/Resolution/X86/globalorder.ll
@@ -0,0 +1,29 @@
+; Check that the order of global variables is not unnecessarily
+; perturbed by LTO during module merging.
+;
+; RUN: llvm-as %s -o %t.bc
+; RUN: llvm-lto2 %t.bc -O0 -save-temps -o %t.o -r %t.bc,var1,px -r %t.bc,var2,px -r %t.bc,var3,px -r %t.bc,var4,px -r %t.bc,foo,px
+; RUN: llvm-dis -o - %t.o.0.0.preopt.bc | FileCheck %s
+;
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; CHECK: @var1 =
+ at var1 = global i32 0, align 4
+; CHECK: @var2 =
+ at var2 = global i32 0, align 4
+; CHECK: @var3 =
+ at var3 = global i32* @var1, align 4
+; CHECK: @var4 =
+ at var4 = global i32* @var2, align 4
+
+define i32 @foo() {
+entry:
+  %0 = load i32*, i32** @var3, align 4
+  %1 = load i32, i32* %0, align 4
+  %2 = load i32*, i32** @var4, align 4
+  %3 = load i32, i32* %2, align 4
+  %add = add nsw i32 %3, %1
+  ret i32 %add
+}
Index: lib/Object/ModuleSymbolTable.cpp
===================================================================
--- lib/Object/ModuleSymbolTable.cpp
+++ lib/Object/ModuleSymbolTable.cpp
@@ -43,10 +43,10 @@
   else
     FirstMod = M;
 
-  for (Function &F : *M)
-    SymTab.push_back(&F);
   for (GlobalVariable &GV : M->globals())
     SymTab.push_back(&GV);
+  for (Function &F : *M)
+    SymTab.push_back(&F);
   for (GlobalAlias &GA : M->aliases())
     SymTab.push_back(&GA);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31443.93307.patch
Type: text/x-patch
Size: 1540 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170328/3fb27f52/attachment.bin>


More information about the llvm-commits mailing list