diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -1586,6 +1586,16 @@ if (!Function.isSymbolValidInScope(Symbol, SymbolSize)) break; + // Skip basic block labels. This happens on RISC-V with linker relaxation + // enabled because every branch needs a relocation and corresponding + // symbol. We don't want to add such symbols as entry points. + const auto PrivateLabelPrefix = BC->AsmInfo->getPrivateLabelPrefix(); + if (!PrivateLabelPrefix.empty() && + cantFail(Symbol.getName()).starts_with(PrivateLabelPrefix)) { + ++NextSymRefI; + continue; + } + // This is potentially another entry point into the function. uint64_t EntryOffset = NextSymRefI->first - Function.getAddress(); LLVM_DEBUG(dbgs() << "BOLT-DEBUG: adding entry point to function " diff --git a/bolt/test/RISCV/branch-no-secondary-entry.s b/bolt/test/RISCV/branch-no-secondary-entry.s new file mode 100644 --- /dev/null +++ b/bolt/test/RISCV/branch-no-secondary-entry.s @@ -0,0 +1,18 @@ +/// Test that no secondary entry points are created for basic block labels used +/// by branches. +// RUN: %clang %cflags -o %t %s +// RUN: llvm-bolt -print-cfg -o /dev/null %t 2>&1 | FileCheck %s + +// CHECK: Binary Function "_start" after building cfg { +// CHECK: IsMultiEntry: 0 +// CHECK: beq t0, t1, .Ltmp0 +// CHECK: {{^}}.Ltmp0 +// CHECK: ret + + .globl _start +_start: + beq t0, t1, 1f +1: + ret + .size _start, .-_start +