[llvm] [BOLT][RISCV] Precommit test for a conditional branch to end of the function (PR #218419)

Kito Cheng via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 24 07:05:31 PDT 2026


https://github.com/kito-cheng created https://github.com/llvm/llvm-project/pull/218419

A conditional branch whose target lies outside of the function is modeled as a
conditional tail call, and it tune it into a unconditional call
incorrectly.

We found this case during optimize clang with bolt.

>From 9d9925c958b0da9bb082f2a72abc15f23219622f Mon Sep 17 00:00:00 2001
From: Kito Cheng <npickito at gmail.com>
Date: Mon, 17 Aug 2026 22:19:44 +0800
Subject: [PATCH] [BOLT][RISCV] Precommit test for a conditional branch to end
 of the function

A conditional branch whose target lies outside of the function is modeled as a
conditional tail call, and it tune it into a unconditional call
incorrectly.

We found this case during optimize clang with bolt.
---
 bolt/test/RISCV/cond-tail-call.s | 40 ++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 bolt/test/RISCV/cond-tail-call.s

diff --git a/bolt/test/RISCV/cond-tail-call.s b/bolt/test/RISCV/cond-tail-call.s
new file mode 100644
index 0000000000000..7034617431b5f
--- /dev/null
+++ b/bolt/test/RISCV/cond-tail-call.s
@@ -0,0 +1,40 @@
+// RUN: %clang %cflags64 -o %t %s
+// RUN: llvm-bolt -o %t.bolt %t
+// RUN: llvm-objdump -d --disassemble-symbols=f %t.bolt | FileCheck %s
+
+  .text
+
+  .globl _start
+  .p2align 1
+_start:
+  call f
+  .size _start, .-_start
+  .globl f
+  .p2align 1
+// CHECK-LABEL: <f>:
+f:
+  /// An indirect branch BOLT cannot analyze, which makes the function
+  /// non-simple. This stands in for a jump table.
+// CHECK-NEXT: beqz a1,
+  beqz a1, .Lcont
+// CHECK-NEXT: jr a2
+  jr a2
+.Lcont:
+/// FIXME: the branch below is currently replaced by an unconditional tail call,
+/// which drops both the condition and the fall-through path.
+/// NOTE: This seems not reasonable in general, however this might created when
+///       program has __builtin_unreachable or undefined behavior.
+// CHECK-NEXT: j {{.*}} <g>
+  beqz a0, .Lend
+// CHECK-NEXT: li a0, 0x1
+  li a0, 1
+// CHECK-NEXT: ret
+  ret
+.Lend:
+  .size f, .-f
+
+  .globl g
+  .p2align 1
+g:
+  ret
+  .size g, .-g



More information about the llvm-commits mailing list