From 38ba2824c85606e0e7bb2071055d0c7aceb463c1 Mon Sep 17 00:00:00 2001 From: Job Noorman Date: Thu, 22 Jun 2023 09:33:32 +0200 Subject: [PATCH] [BOLT] Don't register internal func relocs as external references Currently, all relocations that point inside a function are registered as external references. If these relocations cannot be resolved as jump tables or computed gotos, the containing function gets marked as not-simple and excluded from optimizations. RISC-V uses relocations for branches and jumps (to support linker relaxation) and as such, almost no functions get marked as simple. This patch fixes this by only registering relocations that originate outside of the referenced function as external references. Reviewed By: rafauler Differential Revision: https://reviews.llvm.org/D153345 --- bolt/lib/Rewrite/RewriteInstance.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp index 58da9f4..066503e 100644 --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -2875,7 +2875,13 @@ void RewriteInstance::handleRelocation(const SectionRef &RelocatedSection, ReferencedSymbol = ReferencedBF->getOrCreateLocalLabel(Address, /*CreatePastEnd =*/true); - ReferencedBF->registerReferencedOffset(RefFunctionOffset); + + // If ContainingBF != nullptr, it equals ReferencedBF (see + // if-condition above) so we're handling a relocation from a function + // to itself. RISC-V uses such relocations for branches, for example. + // These should not be registered as externally references offsets. + if (!ContainingBF) + ReferencedBF->registerReferencedOffset(RefFunctionOffset); } if (opts::Verbosity > 1 && BinarySection(*BC, RelocatedSection).isWritable()) -- 2.7.4