[PATCH] D95943: [ThinLTO] Fix import of a global variable to modules contain its weak def
Kristina Bessonova via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 4 08:18:57 PST 2021
krisb updated this revision to Diff 321443.
krisb added a comment.
Add the comment and FIXME.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D95943/new/
https://reviews.llvm.org/D95943
Files:
llvm/lib/Transforms/IPO/FunctionImport.cpp
llvm/test/ThinLTO/X86/Inputs/weak_globals_import.ll
llvm/test/ThinLTO/X86/weak_globals_import.ll
Index: llvm/test/ThinLTO/X86/weak_globals_import.ll
===================================================================
--- /dev/null
+++ llvm/test/ThinLTO/X86/weak_globals_import.ll
@@ -0,0 +1,30 @@
+; RUN: opt -module-summary %s -o %t1.bc
+; RUN: opt -module-summary %p/Inputs/weak_globals_import.ll -o %t2.bc
+
+; RUN: llvm-lto2 run -save-temps %t1.bc %t2.bc -o %t.out \
+; RUN: -r=%t1.bc,main,plx \
+; RUN: -r=%t1.bc,_Z3foov,l \
+; RUN: -r=%t1.bc,G \
+; RUN: -r=%t2.bc,_Z3foov,pl \
+; RUN: -r=%t2.bc,G,pl
+; RUN: llvm-dis %t.out.1.3.import.bc -o - | FileCheck %s
+; RUN: llvm-dis %t.out.2.3.import.bc -o - | FileCheck %s
+
+; Test that a prevailing def for a global variable got imported even if
+; a destination module already contains non-prevailing one.
+
+; CHECK: @G = internal local_unnamed_addr global i32 42
+
+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-unknown-linux-gnu"
+
+ at G = weak dso_local local_unnamed_addr global i32 0, align 4
+
+define dso_local i32 @main() local_unnamed_addr {
+ %1 = load i32, i32* @G, align 4
+ %2 = tail call i32 @_Z3foov()
+ %3 = add nsw i32 %2, %1
+ ret i32 %3
+}
+
+declare dso_local i32 @_Z3foov() local_unnamed_addr
Index: llvm/test/ThinLTO/X86/Inputs/weak_globals_import.ll
===================================================================
--- /dev/null
+++ llvm/test/ThinLTO/X86/Inputs/weak_globals_import.ll
@@ -0,0 +1,11 @@
+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-unknown-linux-gnu"
+
+ at G = dso_local local_unnamed_addr global i32 42, align 4
+
+; Function Attrs: noinline
+define dso_local i32 @_Z3foov() local_unnamed_addr #0 {
+ ret i32 0
+}
+
+attributes #0 = { noinline }
Index: llvm/lib/Transforms/IPO/FunctionImport.cpp
===================================================================
--- llvm/lib/Transforms/IPO/FunctionImport.cpp
+++ llvm/lib/Transforms/IPO/FunctionImport.cpp
@@ -282,10 +282,25 @@
FunctionImporter::ImportMapTy &ImportList,
StringMap<FunctionImporter::ExportSetTy> *ExportLists) {
for (auto &VI : Summary.refs()) {
- if (DefinedGVSummaries.count(VI.getGUID())) {
- LLVM_DEBUG(
- dbgs() << "Ref ignored! Target already in destination module.\n");
- continue;
+ // Find if the module already contains a suitable definition.
+ const auto &GVS = DefinedGVSummaries.find(VI.getGUID());
+ if (GVS != DefinedGVSummaries.end()) {
+ if (VI.getSummaryList().size() > 1 &&
+ GlobalValue::isInterposableLinkage(GVS->second->linkage())) {
+ // If the module contains a non-prevailing def of a multiply defined
+ // global, it still requires a prevailing one to be imported. Otherwise,
+ // it may result in an undefined symbol because if the prevailing copy
+ // is read-only it will be internalized thus become not available for
+ // linking.
+ // FIXME: Instead of checking for linkage type, use prevailing symbol
+ // information and ensure the prevailing copy is read-only before allow
+ // importing.
+ ;
+ } else {
+ LLVM_DEBUG(
+ dbgs() << "Ref ignored! Target already in destination module.\n");
+ continue;
+ }
}
LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n");
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95943.321443.patch
Type: text/x-patch
Size: 3441 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210204/85a80a60/attachment.bin>
More information about the llvm-commits
mailing list