diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -1382,7 +1382,8 @@ void dumpSCCs(raw_ostream &OS); /// Analyze index and detect unmodified globals - void propagateAttributes(const DenseSet &PreservedSymbols); + void propagateAttributes(const DenseSet &PreservedSymbols, + bool ImportEnabled); /// Checks if we can import global variable from another module. bool canImportGlobalVar(GlobalValueSummary *S, bool AnalyzeRefs) const; diff --git a/llvm/lib/IR/ModuleSummaryIndex.cpp b/llvm/lib/IR/ModuleSummaryIndex.cpp --- a/llvm/lib/IR/ModuleSummaryIndex.cpp +++ b/llvm/lib/IR/ModuleSummaryIndex.cpp @@ -15,6 +15,7 @@ #include "llvm/ADT/SCCIterator.h" #include "llvm/ADT/Statistic.h" #include "llvm/ADT/StringMap.h" +#include "llvm/Support/CommandLine.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -26,6 +27,10 @@ STATISTIC(WriteOnlyLiveGVars, "Number of live global variables marked write only"); +static cl::opt PropagateAttrs("propagate-attrs", cl::init(true), + cl::Hidden, + cl::desc("Propagate attributes in index")); + FunctionSummary FunctionSummary::ExternalNode = FunctionSummary::makeDummyFunctionSummary({}); @@ -156,7 +161,23 @@ // Internalization itself happens in the backend after import is finished // See internalizeGVsAfterImport. void ModuleSummaryIndex::propagateAttributes( - const DenseSet &GUIDPreservedSymbols) { + const DenseSet &GUIDPreservedSymbols, + bool ImportEnabled) { + setWithAttributePropagation(); + + if (!PropagateAttrs || !ImportEnabled) { + // If import is disabled we should drop read/write-only attribute + // from all summaries to prevent incorrect internalization. Do the + // same if we have disabled attribute propagation. + for (auto &P : *this) + for (auto &S : P.second.SummaryList) + if (auto *GVS = dyn_cast(S.get())) { + GVS->setReadOnly(false); + GVS->setWriteOnly(false); + } + return; + } + for (auto &P : *this) for (auto &S : P.second.SummaryList) { if (!isGlobalValueLive(S.get())) diff --git a/llvm/lib/Transforms/IPO/FunctionImport.cpp b/llvm/lib/Transforms/IPO/FunctionImport.cpp --- a/llvm/lib/Transforms/IPO/FunctionImport.cpp +++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp @@ -901,19 +901,7 @@ function_ref isPrevailing, bool ImportEnabled) { computeDeadSymbols(Index, GUIDPreservedSymbols, isPrevailing); - if (ImportEnabled) { - Index.propagateAttributes(GUIDPreservedSymbols); - } else { - // If import is disabled we should drop read/write-only attribute - // from all summaries to prevent internalization. - for (auto &P : Index) - for (auto &S : P.second.SummaryList) - if (auto *GVS = dyn_cast(S.get())) { - GVS->setReadOnly(false); - GVS->setWriteOnly(false); - } - } - Index.setWithAttributePropagation(); + Index.propagateAttributes(GUIDPreservedSymbols, ImportEnabled); } /// Compute the set of summaries needed for a ThinLTO backend compilation of 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 @@ -25,6 +25,13 @@ ; OPTIMIZE-NEXT: %2 = tail call i32 @rand() ; OPTIMIZE-NEXT: ret i32 0 +; Confirm that with -propagate-attrs=false we no longer do write-only importing +; RUN: llvm-lto -propagate-attrs=false -thinlto-action=import -exported-symbol=main %t1.bc -thinlto-index=%t3.index.bc -o %t1.imported.bc -stats 2>&1 | FileCheck %s --check-prefix=STATS-NOPROP +; RUN: llvm-dis %t1.imported.bc -o - | FileCheck %s --check-prefix=IMPORT-NOPROP +; STATS-NOPROP-NOT: Number of live global variables marked write only +; IMPORT-NOPROP: @gFoo.llvm.0 = available_externally +; IMPORT-NOPROP-NEXT: @gBar = available_externally + target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-pc-linux-gnu"