[llvm] [ConstantMerge] Check local linkage while merging (PR #124220)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 23 18:57:43 PST 2025


https://github.com/gulfemsavrun created https://github.com/llvm/llvm-project/pull/124220

Avoid merging a constant with a local linkage into a constant with a non-local linkage because it might be used in pointer arithmetics like in relative lookup tables, where it uses pointer arithmetics that is only valid with local linkage.

>From b3f9047097ec95591c3ea63ea45298e660c9baa9 Mon Sep 17 00:00:00 2001
From: Gulfem Savrun Yeniceri <gulfem at google.com>
Date: Thu, 23 Jan 2025 18:36:12 -0800
Subject: [PATCH] [ConstantMerge] Check local linkage while merging

Avoid merging a constant with a local linkage into a
constant with a non-local linkage because it might be
used in pointer arithmetics like in relative lookup
tables, where it uses pointer arithmetics that is only
valid with local linkage.
---
 llvm/lib/Transforms/IPO/ConstantMerge.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/llvm/lib/Transforms/IPO/ConstantMerge.cpp b/llvm/lib/Transforms/IPO/ConstantMerge.cpp
index a1face0a6a9c3a..85686f8f62874a 100644
--- a/llvm/lib/Transforms/IPO/ConstantMerge.cpp
+++ b/llvm/lib/Transforms/IPO/ConstantMerge.cpp
@@ -109,6 +109,8 @@ static CanMerge makeMergeable(GlobalVariable *Old, GlobalVariable *New) {
   assert(!hasMetadataOtherThanDebugLoc(New));
   if (!Old->hasGlobalUnnamedAddr())
     New->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
+  if (Old->hasLocalLinkage() && !New->hasLocalLinkage())
+    return CanMerge::No;
   return CanMerge::Yes;
 }
 



More information about the llvm-commits mailing list