[lld] [lld] Migrate away from PointerUnion::dyn_cast (NFC) (PR #228356)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 2 00:54:45 PDT 2026
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/228356
Note that PointerUnion::dyn_cast has been soft deprecated in
PointerUnion.h:
// FIXME: Replace the uses of is(), get() and dyn_cast() with
// isa<T>, cast<T> and the llvm::dyn_cast<T>
Literal migration would result in dyn_cast_if_present (see the
definition of PointerUnion::dyn_cast), but this patch uses dyn_cast
where the operands are known to be nonnull.
The operands are known to be nonnull in one of two ways:
- Preceding checks: In BPSectionOrderer.cpp,
reloc.getReferentInputSection() is called a few lines above (and the
caller checks r.referent.isNull()), guaranteeing the PointerUnion is
nonnull.
- Downstream code: The else or fallthrough branch unconditionally
calls cast<...> or dyn_cast<...> on the pointer, or dereferences it.
Assisted-by: Antigravity
>From 286c3d29f4fdae3443b7b0f88c425d3592f2b723 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 20 Sep 2026 17:42:10 -0700
Subject: [PATCH] [lld] Migrate away from PointerUnion::dyn_cast (NFC)
Note that PointerUnion::dyn_cast has been soft deprecated in
PointerUnion.h:
// FIXME: Replace the uses of is(), get() and dyn_cast() with
// isa<T>, cast<T> and the llvm::dyn_cast<T>
Literal migration would result in dyn_cast_if_present (see the
definition of PointerUnion::dyn_cast), but this patch uses dyn_cast
where the operands are known to be nonnull.
The operands are known to be nonnull in one of two ways:
- Preceding checks: In BPSectionOrderer.cpp,
reloc.getReferentInputSection() is called a few lines above (and the
caller checks r.referent.isNull()), guaranteeing the PointerUnion is
nonnull.
- Downstream code: The else or fallthrough branch unconditionally
calls cast<...> or dyn_cast<...> on the pointer, or dereferences it.
Assisted-by: Antigravity
---
lld/MachO/BPSectionOrderer.cpp | 2 +-
lld/MachO/InputFiles.cpp | 4 ++--
lld/MachO/InputSection.cpp | 2 +-
lld/MachO/MarkLive.cpp | 4 ++--
lld/MachO/Relocations.cpp | 2 +-
lld/MachO/SyntheticSections.cpp | 2 +-
6 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/lld/MachO/BPSectionOrderer.cpp b/lld/MachO/BPSectionOrderer.cpp
index 8d1f491ddc9cc..54f2fb2f03727 100644
--- a/lld/MachO/BPSectionOrderer.cpp
+++ b/lld/MachO/BPSectionOrderer.cpp
@@ -104,7 +104,7 @@ struct BPOrdererMachO : lld::BPOrderer<BPOrdererMachO> {
if (isec)
kind = uint64_t(isec->kind());
- if (auto *sym = reloc.referent.dyn_cast<Symbol *>()) {
+ if (auto *sym = dyn_cast<Symbol *>(reloc.referent)) {
kind = (kind << 8) | uint8_t(sym->kind());
if (auto *d = llvm::dyn_cast<Defined>(sym))
value = d->value;
diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index abf7cf4e01b62..635a636b35c85 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -1217,7 +1217,7 @@ void ObjFile::registerCompactUnwind(Section &compactUnwindSection) {
continue;
}
uint64_t add = r.addend;
- if (auto *sym = cast_or_null<Defined>(r.referent.dyn_cast<Symbol *>())) {
+ if (auto *sym = cast_or_null<Defined>(dyn_cast<Symbol *>(r.referent))) {
// Check whether the symbol defined in this file is the prevailing one.
// Skip if it is e.g. a weak def that didn't prevail.
if (sym->getFile() != this) {
@@ -1228,7 +1228,7 @@ void ObjFile::registerCompactUnwind(Section &compactUnwindSection) {
referentIsec = cast<ConcatInputSection>(sym->isec());
} else {
referentIsec =
- cast<ConcatInputSection>(r.referent.dyn_cast<InputSection *>());
+ cast<ConcatInputSection>(dyn_cast<InputSection *>(r.referent));
}
// Unwind info lives in __DATA, and finalization of __TEXT will occur
// before finalization of __DATA. Moreover, the finalization of unwind
diff --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp
index beeeb8e7d5c64..6ea4b0ddd58e4 100644
--- a/lld/MachO/InputSection.cpp
+++ b/lld/MachO/InputSection.cpp
@@ -240,7 +240,7 @@ void ConcatInputSection::writeTo(uint8_t *buf) {
const Symbol *fromSym = cast<Symbol *>(r.referent);
const Relocation &minuend = relocs[++i];
uint64_t minuendVA;
- if (const Symbol *toSym = minuend.referent.dyn_cast<Symbol *>())
+ if (const Symbol *toSym = dyn_cast<Symbol *>(minuend.referent))
minuendVA = toSym->getVA() + minuend.addend;
else {
auto *referentIsec = cast<InputSection *>(minuend.referent);
diff --git a/lld/MachO/MarkLive.cpp b/lld/MachO/MarkLive.cpp
index a51c800bf4ef1..4134c48ad48ad 100644
--- a/lld/MachO/MarkLive.cpp
+++ b/lld/MachO/MarkLive.cpp
@@ -155,7 +155,7 @@ void MarkLiveImpl<RecordWhyLive>::markTransitively() {
// Mark all symbols listed in the relocation table for this section.
for (const Relocation &r : isec->relocs) {
- if (auto *s = r.referent.dyn_cast<Symbol *>())
+ if (auto *s = dyn_cast<Symbol *>(r.referent))
addSym(s, entry);
else
enqueue(cast<InputSection *>(r.referent), r.addend, entry);
@@ -173,7 +173,7 @@ void MarkLiveImpl<RecordWhyLive>::markTransitively() {
continue;
for (const Relocation &r : isec->relocs) {
- if (auto *s = r.referent.dyn_cast<Symbol *>()) {
+ if (auto *s = dyn_cast<Symbol *>(r.referent)) {
if (s->isLive()) {
InputSection *referentIsec = nullptr;
if (auto *d = dyn_cast<Defined>(s))
diff --git a/lld/MachO/Relocations.cpp b/lld/MachO/Relocations.cpp
index b926d74bbded1..240b9cdf5ee15 100644
--- a/lld/MachO/Relocations.cpp
+++ b/lld/MachO/Relocations.cpp
@@ -22,7 +22,7 @@ static_assert(sizeof(void *) != 8 || sizeof(Relocation) == 24,
"Try to minimize Reloc's size; we create many instances");
InputSection *Relocation::getReferentInputSection() const {
- if (const auto *sym = referent.dyn_cast<Symbol *>()) {
+ if (const auto *sym = dyn_cast<Symbol *>(referent)) {
if (const auto *d = dyn_cast<Defined>(sym))
return d->isec();
return nullptr;
diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index b3d6ce2e9a351..808f32fb2b094 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -2055,7 +2055,7 @@ void InitOffsetsSection::setUp() {
error(isec->getLocation(rel.offset) +
": unexpected section relocation");
- Symbol *sym = rel.referent.dyn_cast<Symbol *>();
+ Symbol *sym = dyn_cast<Symbol *>(rel.referent);
if (auto *undefined = dyn_cast<Undefined>(sym))
treatUndefinedSymbol(*undefined, isec, rel.offset);
if (needsBinding(sym))
More information about the llvm-commits
mailing list