[llvm] e420c0c - [ThinLTO] Fix importing of writeonly variables in distributed ThinLTO

Teresa Johnson via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 4 14:59:47 PST 2019


Author: Teresa Johnson
Date: 2019-12-04T14:59:27-08:00
New Revision: e420c0c78eb0700989c8ba80e845b6306d66bb5f

URL: https://github.com/llvm/llvm-project/commit/e420c0c78eb0700989c8ba80e845b6306d66bb5f
DIFF: https://github.com/llvm/llvm-project/commit/e420c0c78eb0700989c8ba80e845b6306d66bb5f.diff

LOG: [ThinLTO] Fix importing of writeonly variables in distributed ThinLTO

Summary:
D69561/dde5893 enabled importing of readonly variables with references,
however, it introduced a bug relating to importing/internalization of
writeonly variables with references.

A fix for this was added in D70006/7f92d66. But this didn't work in
distributed ThinLTO mode. The reason is that the fix (importing the
writeonly var with a zeroinitializer) was only applied when there were
references on the writeonly var summary. In distributed ThinLTO mode,
where we only have a small slice of the index, we will not have the
references on the importing side if we are not importing those
referenced values. Rather than changing this handshaking (which will
require a lot of other changes, since that's how we know what to import
in the distributed backend clang invocation), we can simply always give
the writeonly variable a zero initializer.

Reviewers: evgeny777, steven_wu

Subscribers: mehdi_amini, inglorion, hiraditya, dexonsmith, arphaman, llvm-commits

Tags: #llvm

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

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/FunctionImportUtils.cpp
    llvm/test/ThinLTO/X86/index-const-prop2.ll
    llvm/test/ThinLTO/X86/writeonly-with-refs.ll
    llvm/test/ThinLTO/X86/writeonly.ll
    llvm/test/ThinLTO/X86/writeonly2.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp b/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp
index 71aa585dfe5d..26d48ee0d23f 100644
--- a/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp
+++ b/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp
@@ -258,7 +258,7 @@ void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
         // references in IR module (not in combined index), so we can
         // ignore them when computing import. We do not export references
         // of writeonly object. See computeImportForReferencedGlobals
-        if (ImportIndex.isWriteOnly(GVS) && GVS->refs().size())
+        if (ImportIndex.isWriteOnly(GVS))
           V->setInitializer(Constant::getNullValue(V->getValueType()));
       }
     }

diff  --git a/llvm/test/ThinLTO/X86/index-const-prop2.ll b/llvm/test/ThinLTO/X86/index-const-prop2.ll
index 430c7e8156d2..928d00adc9a2 100644
--- a/llvm/test/ThinLTO/X86/index-const-prop2.ll
+++ b/llvm/test/ThinLTO/X86/index-const-prop2.ll
@@ -36,6 +36,8 @@
 ; RUN:  -o %t4
 ; RUN: llvm-dis %t4.1.3.import.bc -o - | FileCheck %s --check-prefix=IMPORT2
 
+; Run again but with main2 exported instead of main to check that write only
+; variables are optimized out.
 ; RUN: llvm-lto2 run %t1.bc %t2.bc -save-temps \
 ; RUN:  -r=%t2.bc,foo,pl \
 ; RUN:  -r=%t2.bc,bar,pl \
@@ -49,7 +51,7 @@
 ; RUN:  -r=%t1.bc,baz, \
 ; RUN:  -r=%t1.bc,gBar, \
 ; RUN:  -o %t5
-; RUN: llvm-dis %t5.1.3.import.bc -o - | FileCheck %s --check-prefix=IMPORT
+; RUN: llvm-dis %t5.1.3.import.bc -o - | FileCheck %s --check-prefix=IMPORT-WRITEONLY
 ; RUN: llvm-dis %t5.1.5.precodegen.bc -o - | FileCheck %s --check-prefix=CODEGEN2
 ; Check that gFoo and gBar were eliminated from source module together
 ; with corresponsing stores
@@ -59,6 +61,10 @@
 ; IMPORT-NEXT:  @gBar = internal local_unnamed_addr global i32 2, align 4
 ; IMPORT:       !DICompileUnit({{.*}})
 
+; Write only variables are imported with a zero initializer.
+; IMPORT-WRITEONLY:  @gFoo.llvm.0 = internal unnamed_addr global i32 0
+; IMPORT-WRITEONLY:  @gBar = internal local_unnamed_addr global i32 0
+
 ; CODEGEN:        i32 @main()
 ; CODEGEN-NEXT:     ret i32 3
 

diff  --git a/llvm/test/ThinLTO/X86/writeonly-with-refs.ll b/llvm/test/ThinLTO/X86/writeonly-with-refs.ll
index 63f75762c39b..787d97758221 100644
--- a/llvm/test/ThinLTO/X86/writeonly-with-refs.ll
+++ b/llvm/test/ThinLTO/X86/writeonly-with-refs.ll
@@ -7,10 +7,22 @@
 ; RUN:    -r=%t2,outer,pl
 
 ; @outer should have been internalized and converted to zeroinitilizer.
-; RUN: llvm-dis %t-out.1.5.precodegen.bc -o - | FileCheck %s
-; RUN: llvm-dis %t-out.2.5.precodegen.bc -o - | FileCheck %s
+; RUN: llvm-dis %t-out.1.3.import.bc -o - | FileCheck %s
+; RUN: llvm-dis %t-out.2.3.import.bc -o - | FileCheck %s
 
-; CHECK: @outer = internal unnamed_addr global %struct.Q zeroinitializer
+; CHECK: @outer = internal local_unnamed_addr global %struct.Q zeroinitializer
+
+; Test again in distributed ThinLTO mode.
+; RUN: llvm-lto2 run -save-temps %t1 %t2 -o %t-out \
+; RUN:    -thinlto-distributed-indexes \
+; RUN:    -r=%t1,main,plx \
+; RUN:    -r=%t1,_Z3foov,l \
+; RUN:    -r=%t2,_Z3foov,pl \
+; RUN:    -r=%t2,outer,pl
+; RUN: opt -function-import -import-all-index -enable-import-metadata -summary-file %t1.thinlto.bc %t1 -o %t1.out
+; RUN: opt -function-import -import-all-index -summary-file %t2.thinlto.bc %t2 -o %t2.out
+; RUN: llvm-dis %t1.out -o - | FileCheck %s
+; RUN: llvm-dis %t2.out -o - | FileCheck %s
 
 source_filename = "main.cpp"
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

diff  --git a/llvm/test/ThinLTO/X86/writeonly.ll b/llvm/test/ThinLTO/X86/writeonly.ll
index 20f4533efe68..27305e160ea7 100644
--- a/llvm/test/ThinLTO/X86/writeonly.ll
+++ b/llvm/test/ThinLTO/X86/writeonly.ll
@@ -11,8 +11,8 @@
 ; RUN: llvm-dis %t1.imported.bc -o - | FileCheck %s --check-prefix=IMPORT
 ; RUN: llvm-lto -thinlto-action=optimize %t1.imported.bc -o - | llvm-dis - -o - | FileCheck %s --check-prefix=OPTIMIZE
 
-; IMPORT: @gFoo.llvm.0 = internal unnamed_addr global i32 1, align 4, !dbg !0
-; IMPORT-NEXT: @gBar = internal local_unnamed_addr global i32 2, align 4, !dbg !5
+; IMPORT: @gFoo.llvm.0 = internal unnamed_addr global i32 0, align 4, !dbg !0
+; IMPORT-NEXT: @gBar = internal local_unnamed_addr global i32 0, align 4, !dbg !5
 ; IMPORT: !DICompileUnit({{.*}})
 
 ; STATS:  2 module-summary-index - Number of live global variables marked write only 

diff  --git a/llvm/test/ThinLTO/X86/writeonly2.ll b/llvm/test/ThinLTO/X86/writeonly2.ll
index a7383f25b482..2648727f0997 100644
--- a/llvm/test/ThinLTO/X86/writeonly2.ll
+++ b/llvm/test/ThinLTO/X86/writeonly2.ll
@@ -19,8 +19,8 @@
 ; with corresponsing stores
 ; RUN: llvm-dis %t3.2.5.precodegen.bc -o - | FileCheck %s --check-prefix=CODEGEN-SRC
 
-; IMPORT:       @gFoo.llvm.0 = internal unnamed_addr global i32 1, align 4
-; IMPORT-NEXT:  @gBar = internal local_unnamed_addr global i32 2, align 4
+; IMPORT:       @gFoo.llvm.0 = internal unnamed_addr global i32 0, align 4
+; IMPORT-NEXT:  @gBar = internal local_unnamed_addr global i32 0, align 4
 ; IMPORT:       !DICompileUnit({{.*}})
 
 ; CODEGEN-NOT:  gFoo


        


More information about the llvm-commits mailing list