[llvm] [ThinLTO] Do not duplicate import a function that is actually defined in the current module (PR #110064)

William Junda Huang via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 25 17:26:16 PDT 2024


https://github.com/huangjd created https://github.com/llvm/llvm-project/pull/110064

Doing so could cause a bug where the linker tries to remap a function "reimported" from the current module when materializing it, causing a lookup assert in the type mappings.

>From a00ac362a6d0d2b2247c841331f2cd93c6af5114 Mon Sep 17 00:00:00 2001
From: William Huang <williamjhuang at google.com>
Date: Wed, 25 Sep 2024 17:50:20 -0400
Subject: [PATCH] [ThinLTO] Do not duplicate import a function that is actually
 defined in the current module.

Doing so could cause a bug where the linker tries to remap a function
"reimported" from the current module when materializing it, causing a
lookup assert in the type mappings.
---
 llvm/lib/Linker/IRMover.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp
index 3a6c2678cd157f..5bd05d86a949c3 100644
--- a/llvm/lib/Linker/IRMover.cpp
+++ b/llvm/lib/Linker/IRMover.cpp
@@ -595,11 +595,15 @@ Value *IRLinker::materialize(Value *V, bool ForIndirectSymbol) {
   if (!SGV)
     return nullptr;
 
+  // If SGV is from dest, it is already materialized when dest was loaded.
+  if (SGV->getParent() == &DstM)
+    return nullptr;
+
   // When linking a global from other modules than source & dest, skip
   // materializing it because it would be mapped later when its containing
   // module is linked. Linking it now would potentially pull in many types that
   // may not be mapped properly.
-  if (SGV->getParent() != &DstM && SGV->getParent() != SrcM.get())
+  if (SGV->getParent() != SrcM.get())
     return nullptr;
 
   Expected<Constant *> NewProto = linkGlobalValueProto(SGV, ForIndirectSymbol);



More information about the llvm-commits mailing list