diff --git a/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp b/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp --- a/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp +++ b/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp @@ -258,7 +258,7 @@ // 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 --- 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 --- 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 --- 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 --- 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