[llvm] [BOLT] ICF: Use nameStartsWith instead of getName (PR #206999)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 2 04:11:52 PDT 2026
https://github.com/DmitriiMartynov updated https://github.com/llvm/llvm-project/pull/206999
>From 1d338d758eb3680a196e03697406c6a624615f5e Mon Sep 17 00:00:00 2001
From: Dmitrii Martynov <martynovdmitryvladimirovich at yandex.ru>
Date: Wed, 1 Jul 2026 16:40:37 +0300
Subject: [PATCH] [BOLT] ICF: Use nameStartsWith instead of getName
The getName method returns the name of the first symbol for a given BinaryData.
If a symbol is associated with the same address as a vtable symbol, the ICF
check for vtable mangled names may behave unexpectedly. The correct approach is
to use nameStartsWith, which checks all symbols associated with the BinaryData.
---
bolt/lib/Passes/IdenticalCodeFolding.cpp | 4 ++--
bolt/test/safe-icf-relative-vtable.cpp | 10 ++++++++--
2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/bolt/lib/Passes/IdenticalCodeFolding.cpp b/bolt/lib/Passes/IdenticalCodeFolding.cpp
index cebb7638fa7c6..18c901e45116f 100644
--- a/bolt/lib/Passes/IdenticalCodeFolding.cpp
+++ b/bolt/lib/Passes/IdenticalCodeFolding.cpp
@@ -384,8 +384,8 @@ namespace bolt {
void IdenticalCodeFolding::initVTableReferences(const BinaryContext &BC) {
for (const auto &[Address, Data] : BC.getBinaryData()) {
// Filter out all symbols that are not vtables.
- if (!Data->getName().starts_with("_ZTV") && // vtable
- !Data->getName().starts_with("_ZTCN")) // construction vtable
+ if (!Data->nameStartsWith("_ZTV") && // vtable
+ !Data->nameStartsWith("_ZTCN")) // construction vtable
continue;
for (uint64_t I = Address, End = I + Data->getSize(); I < End;
I += VTableAddressGranularity)
diff --git a/bolt/test/safe-icf-relative-vtable.cpp b/bolt/test/safe-icf-relative-vtable.cpp
index 59ddcc656a5d1..86ffe1838b891 100644
--- a/bolt/test/safe-icf-relative-vtable.cpp
+++ b/bolt/test/safe-icf-relative-vtable.cpp
@@ -6,9 +6,9 @@
// RUN: llvm-bolt %t.so -o %t.bolt --no-threads --icf=safe \
// RUN: --debug-only=bolt-icf 2>&1 | FileCheck %s
-// RUN: %clang %cxxflags -o %t.so %s -Wl,-q -fno-rtti \
+// RUN: %clang %cxxflags -o %t.exp.so %s -Wl,-q -fno-rtti \
// RUN: -fexperimental-relative-c++-abi-vtables
-// RUN: llvm-bolt %t.so -o %t.bolt --no-threads --icf=safe \
+// RUN: llvm-bolt %t.exp.so -o %t.exp.bolt --no-threads --icf=safe \
// RUN: --debug-only=bolt-icf 2>&1 | FileCheck %s
// CHECK: folding {{.*bar.*}} into {{.*foo.*}}
@@ -20,6 +20,12 @@ class TT {
virtual int bar(int a) { return ++a; }
};
+// __init_array_end was added to check that ICF works correctly even if the
+// symbol has the same address as a vtable symbol
+extern void (*__init_array_end[])();
+__attribute__((used)) static void *dummy__init_array_end = __init_array_end;
+__attribute__((constructor)) void dummy_ctor() {};
+
int main() {
TT T;
return T.foo(0) + T.bar(1);
More information about the llvm-commits
mailing list