[llvm] [BOLT] Not use hlt as split point when build the CFG (PR #150963)
Haibo Jiang via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 29 07:40:42 PDT 2025
https://github.com/Jianghibo updated https://github.com/llvm/llvm-project/pull/150963
>From 536fec3857d5a29701283c70a8a052157a5e361a Mon Sep 17 00:00:00 2001
From: jianghaibo <jianghaibo9 at huawei.com>
Date: Mon, 28 Jul 2025 22:43:43 +0800
Subject: [PATCH] [BOLT] Not use hlt as split point when build the CFG
For x86, the halt instruction is defined as a terminator instruction.
When building the CFG, the instruction sequence following the hlt
instruction is treated as an independent MBB. Since there is no
jump information, the predecessor of this MBB cannot be identified,
and it is considered an unreachable MBB that will be removed.
Using this fix, the instruction sequences before and after hlt are
refused to be placed in different blocks.
---
bolt/include/bolt/Core/MCPlusBuilder.h | 3 +++
bolt/lib/Core/MCPlusBuilder.cpp | 5 +++--
bolt/lib/Target/X86/X86MCPlusBuilder.cpp | 4 ++++
bolt/test/X86/Inputs/cfg_build_hlt.s | 11 +++++++++++
bolt/test/X86/cfg_build_hlt.test | 13 +++++++++++++
5 files changed, 34 insertions(+), 2 deletions(-)
create mode 100644 bolt/test/X86/Inputs/cfg_build_hlt.s
create mode 100644 bolt/test/X86/cfg_build_hlt.test
diff --git a/bolt/include/bolt/Core/MCPlusBuilder.h b/bolt/include/bolt/Core/MCPlusBuilder.h
index f902a8c43cd1d..b698ce54d6899 100644
--- a/bolt/include/bolt/Core/MCPlusBuilder.h
+++ b/bolt/include/bolt/Core/MCPlusBuilder.h
@@ -740,6 +740,9 @@ class MCPlusBuilder {
return false;
}
+ /// Return true if the hlt instruction is defind as terminator
+ virtual bool isTerminatorHLT(const MCInst &Inst) const { return false; }
+
/// Return the width, in bytes, of the memory access performed by \p Inst, if
/// this is a pop instruction. Return zero otherwise.
virtual int getPopSize(const MCInst &Inst) const {
diff --git a/bolt/lib/Core/MCPlusBuilder.cpp b/bolt/lib/Core/MCPlusBuilder.cpp
index fa8f4d1df308b..e731e041b4d83 100644
--- a/bolt/lib/Core/MCPlusBuilder.cpp
+++ b/bolt/lib/Core/MCPlusBuilder.cpp
@@ -132,8 +132,9 @@ bool MCPlusBuilder::equals(const MCSpecifierExpr &A, const MCSpecifierExpr &B,
}
bool MCPlusBuilder::isTerminator(const MCInst &Inst) const {
- return Analysis->isTerminator(Inst) ||
- (opts::TerminalTrap && Info->get(Inst.getOpcode()).isTrap());
+ return (opts::TerminalTrap && Info->get(Inst.getOpcode()).isTrap()) ||
+ Analysis->isTerminator(Inst) ? !isTerminatorHLT(Inst)
+ : false;
}
void MCPlusBuilder::setTailCall(MCInst &Inst) const {
diff --git a/bolt/lib/Target/X86/X86MCPlusBuilder.cpp b/bolt/lib/Target/X86/X86MCPlusBuilder.cpp
index a60c1a6bf156e..090d933a1e66a 100644
--- a/bolt/lib/Target/X86/X86MCPlusBuilder.cpp
+++ b/bolt/lib/Target/X86/X86MCPlusBuilder.cpp
@@ -223,6 +223,10 @@ class X86MCPlusBuilder : public MCPlusBuilder {
return Inst.getOpcode() == X86::ENDBR32 || Inst.getOpcode() == X86::ENDBR64;
}
+ bool isTerminatorHLT(const MCInst &Inst) const override {
+ return Inst.getOpcode() == X86::HLT;
+ }
+
int getPopSize(const MCInst &Inst) const override {
switch (Inst.getOpcode()) {
case X86::POP16r:
diff --git a/bolt/test/X86/Inputs/cfg_build_hlt.s b/bolt/test/X86/Inputs/cfg_build_hlt.s
new file mode 100644
index 0000000000000..de14ad7a70348
--- /dev/null
+++ b/bolt/test/X86/Inputs/cfg_build_hlt.s
@@ -0,0 +1,11 @@
+ .global main
+ .type main, %function
+main:
+ pushq %rbp
+ movq %rsp, %rbp
+ hlt
+ movl $0x0, %eax
+ popq %rbp
+ retq
+.size main, .-main
+
diff --git a/bolt/test/X86/cfg_build_hlt.test b/bolt/test/X86/cfg_build_hlt.test
new file mode 100644
index 0000000000000..4139b64c66c03
--- /dev/null
+++ b/bolt/test/X86/cfg_build_hlt.test
@@ -0,0 +1,13 @@
+## Check CFG for halt instruction
+RUN: %clang %cflags %S/Inputs/cfg_build_hlt.s -static -o %t.exe -nostdlib
+RUN: llvm-bolt %t.exe --print-cfg -o %t 2>&1 | FileCheck %s --check-prefix=CHECK-CFG
+RUN: llvm-objdump -d %t --print-imm-hex | FileCheck %s --check-prefix=CHECK-BIN
+
+CHECK-CFG: BB Count : 1
+CHECK-BIN: <main>:
+CHECK-BIN-NEXT: 55 pushq %rbp
+CHECK-BIN-NEXT: 48 89 e5 movq %rsp, %rbp
+CHECK-BIN-NEXT: f4 hlt
+CHECK-BIN-NEXT: b8 00 00 00 00 movl $0x0, %eax
+CHECK-BIN-NEXT: 5d popq %rbp
+CHECK-BIN-NEXT: c3 retq
More information about the llvm-commits
mailing list