[llvm] [llvm] don't merge constants with COMDAT (PR #192477)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 16 08:43:38 PDT 2026
https://github.com/8051Enthusiast created https://github.com/llvm/llvm-project/pull/192477
After merging #190995 (now reverted) the CI failed because ConstantMerge currently merges constants with differing COMDATs. This can result in a function referencing globals with a different COMDAT than before the merge, producing a linking error when that global gets discarded.
It might be possible to remove the comdat if one of the globals to be merged doesn't have comdat and the comdat type of the other one is `any`, but I'm not 100% sure on that, so for now this just bails out when it finds global constants with comdat (which are relatively rare anyway).
>From 5d64e20b80327083d1fe28fd27545ee023755786 Mon Sep 17 00:00:00 2001
From: 8051enthusiast <8051enthusiast at protonmail.com>
Date: Thu, 16 Apr 2026 17:20:23 +0200
Subject: [PATCH] [llvm] don't merge constants with COMDAT
After merging #190995 (now reverted) the CI failed because
ConstantMerge currently merges constants with differing COMDATs.
This can result in a function referencing globals with a different
COMDAT than before the merge, producing a linking error when that
global gets discarded.
---
llvm/lib/Transforms/IPO/ConstantMerge.cpp | 1 +
.../Transforms/ConstantMerge/dont-merge.ll | 19 +++++++++++++++++++
2 files changed, 20 insertions(+)
diff --git a/llvm/lib/Transforms/IPO/ConstantMerge.cpp b/llvm/lib/Transforms/IPO/ConstantMerge.cpp
index 282bcabc34534..172cdcbf368d1 100644
--- a/llvm/lib/Transforms/IPO/ConstantMerge.cpp
+++ b/llvm/lib/Transforms/IPO/ConstantMerge.cpp
@@ -85,6 +85,7 @@ isUnmergeableGlobal(GlobalVariable *GV,
// Only process constants with initializers in the default address space.
return !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
+ GV->hasComdat() ||
// Don't touch thread-local variables.
GV->isThreadLocal() ||
// Don't touch values marked with attribute(used).
diff --git a/llvm/test/Transforms/ConstantMerge/dont-merge.ll b/llvm/test/Transforms/ConstantMerge/dont-merge.ll
index 0d90d82809693..7d5c981c2deda 100644
--- a/llvm/test/Transforms/ConstantMerge/dont-merge.ll
+++ b/llvm/test/Transforms/ConstantMerge/dont-merge.ll
@@ -92,3 +92,22 @@ define void @test5(ptr %P1, ptr %P2) {
store ptr @T5ua, ptr %P2
ret void
}
+
+$test6a = comdat any
+$test6b = comdat any
+
+ at T6a = private unnamed_addr constant i32 666, comdat($test6a)
+ at T6b = private unnamed_addr constant i32 666, comdat($test6b)
+
+; CHECK: @T6a
+; CHECK: @T6b
+
+define void @test6a(ptr %P) comdat {
+ store ptr @T6a, ptr %P
+ ret void
+}
+
+define void @test6b(ptr %P) comdat {
+ store ptr @T6b, ptr %P
+ ret void
+}
More information about the llvm-commits
mailing list