Skip to content

Commit 318c57c

Browse files
authored
Mgsc5 align (#768)
* bpu: tage not enable bank conflict Change-Id: Ie8aef098adea8dab3d0dd7db34fa163517a850d7 * bpu: enable all SC Change-Id: I7d179632f0614a65654bc67e07ceca9991ee9d61 * bpu: only enable imli + path + bias Change-Id: Idad43903835915098fcbecb2c78e3bb1442638ec * bpu: sc: ensure threadhold is positive Change-Id: I9aa1d222463d6411b7481d3c93e7fd4fec17a039 * bpu: sc align RTL: update conds( sum < total/2) Change-Id: I7718a8e97835f34ff360850446f65b24740a0040 * bpu: test path + bias Change-Id: Iaf2a2f9504b6fddcd18f8c0ff70eb8fa981e4c21 * util: add 2 skills: frontend pmu + run cpt regression - Added a new skill for extracting and summarizing BPU counters from gem5 simulation statistics, producing machine-readable JSON and CSV outputs. - Created a default configuration file for BPU counters, listing essential raw counter names. - Introduced a script to analyze BPU counters, allowing users to specify custom counter files and debug directories. Change-Id: Ie483baabbebfdef5e135738efc6e7b9579e67d57 * util: add branch predictability triage skills - Introduced a new skill for analyzing branch PC semantics, determining if branches are naturally hard to predict or if the predictor needs improvement. - The skill includes guidelines for input prioritization, address classification, and a standard analysis workflow. - Outputs a comprehensive report on branch predictability, including code classification and semantic patterns. Change-Id: I65fe0f714dbea8b9eaf8a75543176c3fda76beb1 * util: introduce mgsc-table-probe skill for SC analysis - Added a new skill for analyzing the effectiveness of SC tables in front-end micro-testing, including detailed usage instructions and output formats. - Implemented scripts and configuration files to facilitate batch analysis of SC sub-table effects and branch-level fixes. - Included reference materials for constructing effective micro-tests and evaluating their performance. Change-Id: I93290ac30d047d2251c7891f7512d7ecaae4f341 * bpu: test path + bias + imli again Change-Id: I85fe49785c272649ebee96e844375a46a0c4a2f8 * bpu: update BP db switches handling and add focusBranchPC - Changed the way BP db switches are constructed in the xiangshan configuration to ensure proper list handling. - Introduced a new parameter `focusBranchPC` in the BTBMGSC class to control trace writing for specific branch PCs. - Updated constructor and methods in BTBMGSC to accommodate the new parameter, enhancing branch prediction traceability. Change-Id: I4869bcd1f2b168ca341de4e066df67cfd6de738f * bpu: disable tage bank conflict as default RTL bank conflict only decrease 0.1 score, no need to simulate it, code is dirty and complex Change-Id: I111af20037e6559d8abfdce5fa9c961775b91a19
1 parent 7613ec7 commit 318c57c

File tree

15 files changed

+1682
-5
lines changed

15 files changed

+1682
-5
lines changed
Lines changed: 355 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,355 @@
1+
---
2+
name: branch-predictability-triage
3+
description: "用于分析分支 PC 对应的 ELF / 函数 / 源码语义,并判断该分支更像是语义上天然难预测,还是更像预测器没有学好。适用于 SPEC06 checkpoint、benchmark ELF、topMispredictsByBranch.csv、单个 branch PC 归因。"
4+
---
5+
6+
# 分支可预测性归因技能
7+
8+
## 何时使用
9+
10+
- 你手里有一个或多个 branch PC,想知道它们属于:
11+
- benchmark 主体
12+
- runtime / toolchain(如 `libgcc``glibc``jemalloc`
13+
- `bbl` / kernel / 高地址运行时
14+
- 你想把 branch PC 尽量对应到:
15+
- ELF
16+
- 函数名
17+
- 源码文件 / 代码块
18+
- 你想判断一条分支更像:
19+
- 语义上天然难预测
20+
- 结构上本应较容易预测,但 predictor 没抓住
21+
- 你想分析 SPEC06 切片,例如:
22+
- 新 profile:`/nfs/home/share/checkpoints_profiles/.../checkpoint-0-0-0`
23+
- 老 profile:`/nfs/share/zyy/spec06_rv64gcb_O3_20m_gcc12.2.0-intFpcOff-jeMalloc/...`
24+
25+
## 目标
26+
27+
输出一份面向分支预测分析的结论,而不是单纯做地址翻译。最终结论至少要回答:
28+
29+
1. 这条 branch PC 属于哪类代码。
30+
2. 能否对应到 benchmark 自身源码。
31+
3. 这条分支的控制流模式是什么。
32+
4. 更像是“天然难预测”还是“predictor 还有提升空间”。
33+
34+
## 输入优先级
35+
36+
优先收集以下信息:
37+
38+
1. branch PC 列表
39+
2. 切片名或 benchmark 名
40+
3. 切片根目录
41+
4. 可能的 `topMispredictsByBranch.csv` / `topMisrateByBranch.csv`
42+
5. 如有,用户本地 benchmark 源码树
43+
44+
如果用户只给了 PC,没有给切片名,也可以先按地址区间做粗分类。
45+
46+
## 地址分类规则
47+
48+
先判断 PC 是否属于 benchmark ELF 的装载范围,不要一上来就找源码行。
49+
50+
### A. 低地址、落在 benchmark ELF `.text`
51+
52+
常见表现:
53+
54+
- `0x10000` 左右开始的静态可执行映像
55+
- `llvm-symbolizer` / `nm` 能解析到 benchmark 函数
56+
57+
处理策略:
58+
59+
- 优先认为是 benchmark 主体或静态链接进 benchmark 的 runtime/helper
60+
61+
### B. 高地址,如 `0x8000xxxx`
62+
63+
常见表现:
64+
65+
- 不落在 benchmark ELF 的 LOAD 段
66+
- 更像 `bbl` / opensbi / kernel / 其他 runtime
67+
68+
处理策略:
69+
70+
- 不要强行拿 benchmark ELF 解
71+
- 先说明该地址大概率不属于 benchmark 主体
72+
- 如果没有对应 runtime ELF,只能停在“非 benchmark 代码”这一层
73+
74+
## 仓库内常见路径
75+
76+
### 新 profile
77+
78+
- checkpoint 根目录:`/nfs/home/share/checkpoints_profiles/<profile>/checkpoint-0-0-0`
79+
- ELF 目录:`/nfs/home/share/checkpoints_profiles/<profile>/elf`
80+
81+
常见映射:
82+
83+
- `gcc_typeck` / `gcc_scilab` / `gcc_expr2` / `gcc_200` -> `elf/gcc`
84+
- `perlbench_splitmail` / `perlbench_diffmail` -> `elf/perlbench`
85+
- `bzip2_*` -> `elf/bzip2`
86+
- `gobmk_*` -> `elf/gobmk`
87+
- `astar_*` -> `elf/astar`
88+
- `gamess_*` -> `elf/gamess`
89+
- `mcf` -> `elf/mcf`
90+
- `sjeng` -> `elf/sjeng`
91+
92+
### 老 profile
93+
94+
- 根目录:`/nfs/share/zyy/spec06_rv64gcb_O3_20m_gcc12.2.0-intFpcOff-jeMalloc`
95+
- benchmark ELF:`elf/<bench>_base.riscv64-linux-gnu-gcc12.2.0`
96+
- 运行镜像:`bin/*-bbl-linux-spec.bin`
97+
98+
注意:
99+
100+
- `bin/*-bbl-linux-spec.bin` 往往不是 ELF,不能直接 `addr2line`
101+
- 真正可用于静态语义分析的通常是 `elf/` 下的 benchmark ELF
102+
103+
### 本地源码树
104+
105+
常见 SPEC2006 源码路径:
106+
107+
- `/nfs/home/yanyue/tools/cpu2006_analyze/benchspec/CPU2006`
108+
109+
例如:
110+
111+
- `400.perlbench/src`
112+
- `403.gcc/src`
113+
- `429.mcf/src`
114+
- `458.sjeng/src`
115+
116+
## 推荐工具
117+
118+
优先使用:
119+
120+
- `file`
121+
- `readelf -S`
122+
- `readelf -Wl`
123+
- `nm -n`
124+
- `llvm-symbolizer`
125+
- `llvm-objdump -d --line-numbers --source`
126+
- `rg`
127+
128+
必要时使用:
129+
130+
- `gdb -batch -ex 'info line *ADDR'`
131+
- `readelf --debug-dump=decodedline`
132+
133+
不建议默认依赖系统自带 `addr2line`,因为某些 RISC-V + DWARF 组合下它可能只能给函数名,不能稳定给源码行。
134+
135+
## 标准分析流程
136+
137+
### 第一步:确认 ELF 是否可用
138+
139+
先检查:
140+
141+
```bash
142+
file <elf>
143+
readelf -S <elf> | rg 'debug|symtab|strtab'
144+
readelf -Wl <elf>
145+
```
146+
147+
目标:
148+
149+
- 确认是否是 ELF
150+
- 是否带 `debug_info`
151+
- 代码装载地址范围是什么
152+
153+
### 第二步:判断 PC 是否属于该 ELF
154+
155+
如果 PC 明显不在 LOAD 段范围内:
156+
157+
- 直接标记为“非该 benchmark ELF 主体地址”
158+
- 不要继续做伪映射
159+
160+
### 第三步:先到函数级
161+
162+
优先拿到函数名:
163+
164+
```bash
165+
llvm-symbolizer --obj=<elf> 0xPC
166+
nm -n <elf> | rg '<附近符号>'
167+
```
168+
169+
如果只能到函数名,也不要停。函数级 + 本地源码通常已经足够做语义分析。
170+
171+
### 第四步:查看函数内分支上下文
172+
173+
```bash
174+
llvm-objdump -d --line-numbers --source \
175+
--start-address=<pc附近起点> \
176+
--stop-address=<pc附近终点> \
177+
<elf>
178+
```
179+
180+
重点看:
181+
182+
- 比较指令前的 load / and / shift / compare
183+
- branch 是:
184+
- `beqz/bnez`
185+
- `blt/bge`
186+
- 循环回边
187+
- 早退条件
188+
- “刷新最大值”类选择分支
189+
190+
### 第五步:映射到本地源码块
191+
192+
如果 line table 不够稳定:
193+
194+
- 用函数名在本地源码树里找定义
195+
- 再用汇编语义对到源码块
196+
197+
示例:
198+
199+
```bash
200+
rg -n '^.*\\bpush_slidE\\b\\s*\\(' /nfs/home/yanyue/tools/cpu2006_analyze/benchspec/CPU2006/458.sjeng/src/*.c
201+
```
202+
203+
这一步的目标不是强行制造“精确某一行”,而是定位到:
204+
205+
- 哪个函数
206+
- 哪个 `if/else/loop`
207+
- 它的输入依赖是什么
208+
209+
### 第六步:判断分支类型
210+
211+
每条分支至少归到以下一种:
212+
213+
- `loop-exit`
214+
- `guard / fastpath`
215+
- `predicate-result`
216+
- `max/min update`
217+
- `pointer/null/empty check`
218+
- `state-machine / parser / regex`
219+
- `runtime/helper`
220+
221+
### 第七步:输出预测性结论
222+
223+
结论至少包含:
224+
225+
- 该分支更像“结构型易预测”还是“语义型难预测”
226+
- 如果 predictor 表现差,更该怀疑:
227+
- predictor 模型 / 历史建模 / alias / 容量
228+
- 还是输入分布本身导致的不可规整
229+
230+
## 预测性判断准则
231+
232+
下面是默认启发式,不是绝对规则。
233+
234+
### 通常偏容易预测
235+
236+
- `for/while` 循环退出条件
237+
- 连续扫描直到边界/空值/哨兵值
238+
- 长度下界检查
239+
- 空指针 / 空格 / `npiece` / `frame` / null-check
240+
- 稳定模式位,例如 `captures``mode``flag` 长时间不变
241+
- 明显偏置的错误路径 / 稀有路径
242+
243+
常见表现:
244+
245+
- 连续若干次 taken,然后一次 not-taken
246+
- 连续若干次 not-taken,然后一次 taken
247+
- 同一 phase 下高度偏置
248+
249+
如果这类分支 mispredict 很高,更值得怀疑:
250+
251+
- predictor 没学住简单结构
252+
- 同一 PC 混入太多上下文
253+
- 表项别名或容量冲突
254+
255+
### 通常更难预测
256+
257+
- regex / parser / symbol-table / search-state 驱动的判断
258+
- `if (value > best)` 这种“刷新最大值/最小值”类分支
259+
- 依赖 `load` 出来的动态值,再做分类/比较
260+
- 依赖输入真假分布的 filter / predicate 结果
261+
- 依赖多重全局状态的启发式判断
262+
- 匹配成功/失败、查表命中/未命中、搜索剪枝命中/未命中
263+
264+
常见表现:
265+
266+
- 同一 PC 在不同 phase 下行为变化很大
267+
- taken ratio 接近中间值
268+
- 结果高度依赖输入内容或状态机位置
269+
270+
如果这类分支 mispredict 很高,不一定说明 predictor 有明显问题;可能是语义上本来就更难。
271+
272+
## 典型案例模板
273+
274+
### 案例 A:滑动子走子生成
275+
276+
类似:
277+
278+
- `board[target] == npiece`
279+
- `board[target] != frame`
280+
281+
判断:
282+
283+
- 这是典型扫描型分支
284+
- 通常结构规整,偏容易预测
285+
- 如果预测差,优先怀疑 predictor 没把 ray 长度/phase 模式学好
286+
287+
### 案例 B:搜索排序中的“刷新最大值”
288+
289+
类似:
290+
291+
- `if (move_ordering[i] > best)`
292+
293+
判断:
294+
295+
- 这是数据相关分支
296+
- 依赖 move ordering 分布
297+
- 比 loop-exit 明显更难
298+
- 预测差未必是 predictor bug
299+
300+
### 案例 C:regex / match 成败
301+
302+
类似:
303+
304+
- `if (!s) goto nope;`
305+
- `if (CALLREGEXEC(...))`
306+
307+
判断:
308+
309+
- 强依赖输入文本、状态、匹配位置
310+
- 通常比长度检查更难预测
311+
312+
## 输出格式建议
313+
314+
对每个 branch PC,建议输出以下字段:
315+
316+
- `pc`
317+
- `benchmark`
318+
- `elf`
319+
- `belongs_to`
320+
- `benchmark`
321+
- `runtime/toolchain`
322+
- `bbl/high-address`
323+
- `function`
324+
- `source_candidate`
325+
- `semantic_pattern`
326+
- `predictability`
327+
- `easy`
328+
- `medium`
329+
- `hard`
330+
- `why`
331+
- `tage_interpretation`
332+
- `more_like_predictor_issue`
333+
- `more_like_semantically_hard`
334+
- `mixed`
335+
336+
## 使用时的注意事项
337+
338+
- 不要把“无法精确到行号”误判为“完全无法分析”。
339+
- 对 benchmark 主体,函数级定位 + 本地源码块通常已经足够做预测性判断。
340+
-`0x8000xxxx` 这类地址,先排除 runtime/bbl,再谈源码。
341+
-`libgcc/glibc` helper,要明确告诉用户:这不是 benchmark 自身算法分支。
342+
- 如果用户的目标是比较 predictor 设计优劣,优先找:
343+
- 结构上本应好预测,但 mispredict 很高的分支
344+
- 如果用户的目标是解释 workload 本身难度,优先找:
345+
- 语义上强数据相关的分支
346+
347+
## 默认结论风格
348+
349+
回答时优先给出:
350+
351+
1. 该 PC 属于什么代码
352+
2. 它大致对应哪段源码逻辑
353+
3. 它属于哪种分支模式
354+
4. 我为什么判断它偏 easy / hard
355+
5. 我更倾向把责任归到 predictor 还是 workload 语义

0 commit comments

Comments
 (0)