From 6faf17b7626bfdeea977a7a333c6e20ed677615d Mon Sep 17 00:00:00 2001 From: Mingming Liu Date: Mon, 2 Dec 2024 16:15:52 -0800 Subject: [ThinLTO]Supports declaration import for global variables in distributed ThinLTO (#117616) When `-import-declaration` option is enabled, declaration import is supported for functions. https://github.com/llvm/llvm-project/pull/88024 has the context for this option. This patch supports declaration import for global variables in distributed ThinLTO. The motivating use case is to propagate `dso_local` attribute of global variables across modules, to optimize global variable access when a binary is built with `-fno-direct-access-external-data`. * With `-fdirect-access-external-data`, non thread-local global variables will [have `dso_local` attributes](https://github.com/llvm/llvm-project/blob/fe3c23b439b9a2d00442d9bc6a4ca86f73066a3d/clang/lib/CodeGen/CodeGenModule.cpp#L1730-L1746). This optimizes the global variable access as shown by https://gcc.godbolt.org/z/vMzWcKdh3 --- llvm/lib/IR/ModuleSummaryIndex.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'llvm/lib/IR/ModuleSummaryIndex.cpp') diff --git a/llvm/lib/IR/ModuleSummaryIndex.cpp b/llvm/lib/IR/ModuleSummaryIndex.cpp index 12a558b..d9024b0 100644 --- a/llvm/lib/IR/ModuleSummaryIndex.cpp +++ b/llvm/lib/IR/ModuleSummaryIndex.cpp @@ -328,6 +328,13 @@ void ModuleSummaryIndex::propagateAttributes( bool ModuleSummaryIndex::canImportGlobalVar(const GlobalValueSummary *S, bool AnalyzeRefs) const { + bool CanImportDecl; + return canImportGlobalVar(S, AnalyzeRefs, CanImportDecl); +} + +bool ModuleSummaryIndex::canImportGlobalVar(const GlobalValueSummary *S, + bool AnalyzeRefs, + bool &CanImportDecl) const { auto HasRefsPreventingImport = [this](const GlobalVarSummary *GVS) { // We don't analyze GV references during attribute propagation, so // GV with non-trivial initializer can be marked either read or @@ -348,13 +355,20 @@ bool ModuleSummaryIndex::canImportGlobalVar(const GlobalValueSummary *S, }; auto *GVS = cast(S->getBaseObject()); + const bool nonInterposable = + !GlobalValue::isInterposableLinkage(S->linkage()); + const bool eligibleToImport = !S->notEligibleToImport(); + + // It's correct to import a global variable only when it is not interposable + // and eligible to import. + CanImportDecl = (nonInterposable && eligibleToImport); + // Global variable with non-trivial initializer can be imported // if it's readonly. This gives us extra opportunities for constant // folding and converting indirect calls to direct calls. We don't // analyze GV references during attribute propagation, because we // don't know yet if it is readonly or not. - return !GlobalValue::isInterposableLinkage(S->linkage()) && - !S->notEligibleToImport() && + return nonInterposable && eligibleToImport && (!AnalyzeRefs || !HasRefsPreventingImport(GVS)); } -- cgit v1.1