Skip to content

Commit 76dcf4a

Browse files
double_freeliuq19
authored andcommitted
chore: add asm2arm_tool execution and test scripts
1 parent ccddb06 commit 76dcf4a

10 files changed

Lines changed: 992 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ fuzz/testdata
5656
tools/venv/*
5757
tools/asm2arm_tool/.cache
5858
tools/asm2arm_tool/build
59+
tools/asm2arm_tool/output

internal/native/sve_linkname/recover_arm64_test.go

Lines changed: 40 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/native/sve_wrapgoc/recover_arm64_test.go

Lines changed: 40 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/asm2arm_tool/README.md

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,140 @@
11
# asm2arm_tool
22

3-
asm2arm_tool 是一个用于将 ARM64 汇编代码转换为 Go 语言可调用代码的工具,支持 JIT 和静态链接两种模式。
3+
asm2arm_tool 是一个用于将 ARM64 汇编代码转换为 Go 语言可调用代码的工具,支持 JIT 和静态链接两种模式。
4+
5+
## 构建与测试
6+
7+
### 工具依赖
8+
9+
- **LLVM19**:依赖特定版本的LLVM,需要在生成汇编时同时支持goframe与SVE指令,见**build_tool.sh**中的链接
10+
- 测试依赖sonic的go环境
11+
12+
### 脚本说明
13+
14+
1. **build_tool.sh**:负责拉取LLVM、simde依赖并构建LLVM和工具本身
15+
2. **generate_native_go.sh**:负责编译native目录下的C源码为汇编并输入给工具生成对应模式的Go文件
16+
3. **test_native_recover.sh**:测试生成文件功能正确性的脚本
17+
4. **test_encoder_api.sh**:测试encoder与api功能正确性的脚本
18+
19+
### 构建步骤
20+
21+
0. **构建依赖**
22+
23+
编译器需要支持C++17与完整的SVE支持,推荐GCC9或者CLANG9以上版本
24+
25+
1. **构建工具**
26+
27+
```bash
28+
cd tools/asm2arm_tool/scripts
29+
30+
bash build_tool.sh
31+
```
32+
33+
构建脚本会:
34+
- 克隆LLVM仓库到 `tools/asm2arm_tool/llvm-project`
35+
-`tools/asm2arm_tool/build/llvm-build`构建LLVM(仅启用clang和lld)
36+
- 安装LLVM到 `tools/asm2arm_tool/build/llvm-install` 目录
37+
-`tools/asm2arm_tool/build`构建asm2arm_tool工具
38+
39+
2. **运行测试**
40+
41+
`native``recover`测试:
42+
```bash
43+
cd tools/asm2arm_tool/scripts
44+
45+
bash test_native_recover.sh
46+
```
47+
48+
`test_native_recover.sh`测试脚本会:
49+
- 拷贝 `internal/native/neon` 下的文件到 `output/neon` 目录
50+
- 拷贝 `internal/native/sve_linkname` 下的文件到 `output/sve_linkname` 目录
51+
- 拷贝 `internal/native/sve_wrapgoc` 下的文件到 `output/sve_wrapgoc` 目录
52+
- 调用 `generate_native_go.sh` 生成对应平台的go代码文件到上述`output/neon``output/sve_linkname``output/sve_wrapgoc`目录
53+
- 在三个输出目录下分别运行go的测试
54+
55+
`encoder``api`测试:
56+
```bash
57+
cd tools/asm2arm_tool/scripts
58+
59+
bash test_encoder_api.sh
60+
```
61+
`test_encoder_api.sh`测试脚本会:
62+
- 将生成文件拷贝到internal/native下的目录中
63+
- 执行encoder、api的测试
64+
65+
### 注意事项
66+
67+
- 首次构建会拉取和编译LLVM,过程可能需要较长时间和较大磁盘空间
68+
- 构建过程依赖网络连接(拉取LLVM和子模块)
69+
- generate_native_go.sh脚本执行工具时,默认开启了--debug选项,会将debug输出重定向到对应的log文件
70+
- generate_native_go.sh脚本执行工具时,默认设置sve vector length为32,可通过--vl选项指定其他值;当不指定--vl选项时,会使用运行机器的/proc/sys/abi/sve_default_vector_length值作为默认值
71+
72+
73+
## 使用方法
74+
75+
### 命令行选项
76+
77+
#### 通用选项
78+
79+
| 选项 | 描述 | 必需 |
80+
|------|------|------|
81+
| `--mode` | 工具模式:`JIT``SL` ||
82+
| `--source` | 输入汇编文件路径 ||
83+
| `--output` | 生成的 Go 文件输出路径 ||
84+
| `--link-ld` | 链接脚本路径 ||
85+
| `--package` | 生成的 Go 文件所属的包名 ||
86+
| `--features` | 特性,如 `+sve,+aes` ||
87+
| `--debug` | 启用调试输出 ||
88+
89+
#### JIT 模式选项
90+
91+
| 选项 | 描述 | 必需 |
92+
|------|------|------|
93+
| `--tmpl` | 模板文件路径 ||
94+
95+
#### 静态链接 (SL) 模式选项
96+
97+
| 选项 | 描述 | 必需 |
98+
|------|------|------|
99+
| `--goproto` | Go 函数接口声明文件路径 ||
100+
101+
### 使用示例
102+
103+
#### 输出帮助
104+
105+
```bash
106+
cd tools/asm2arm_tool
107+
108+
./build/asm2arm_tool --help
109+
```
110+
111+
#### JIT 模式
112+
113+
```bash
114+
./build/asm2arm_tool --mode JIT --source input.s --output output_dir --link-ld scripts/link.ld --package sve_wrapgoc --tmpl input.tmpl
115+
```
116+
117+
执行完成后,会在output_dir下生成对应的`input_subr.go``input_text_arm64.go``input.go`三个文件
118+
119+
#### 静态链接模式
120+
121+
```bash
122+
./build/asm2arm_tool --mode SL --source input.s --output output_dir --link-ld scripts/link.ld --package sve_linkname --goproto input_arm64.go
123+
```
124+
125+
执行完成后,会在output_dir下生成对应的`input_subr_arm64.go``input_arm64.s`两个文件
126+
127+
#### 汇编编译选项
128+
129+
输入给工具的汇编文件,在生成时可参考下列选项:
130+
131+
```bash
132+
${CLANG_PATH} \
133+
-g0 -fverbose-asm -fstack-usage -fsigned-char -Wa,--no-size-directive -fno-ident -fno-jump-tables \
134+
-ffixed-x28 -ffixed-x9 -Wno-error -Wno-nullability-completeness -Wno-incompatible-pointer-types \
135+
-mllvm=--go-frame -mllvm=--enable-shrink-wrap=0 -mno-red-zone \
136+
-fno-stack-protector -nostdlib -O3 -fno-asynchronous-unwind-tables -fno-builtin -fno-exceptions \
137+
-march=armv8-a -I${SIMDE_INCLUDE_DIR} -S -o "${asm_file}" "${src_file}"
138+
```
139+
140+
- clang在-O0、-O1的优化级别下,生成的汇编可能存在栈空间过大的非法汇编,推荐开启-O2及以上优化等级

tools/asm2arm_tool/scripts/build_tool.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ set -e
77

88
# 获取脚本所在目录的绝对路径
99
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
10+
source "$SCRIPT_DIR/cpu_detect.sh"
11+
check_kunpeng_cpu
12+
1013
TOOL_DIR="$(dirname "$SCRIPT_DIR")" # asm2arm_tool
1114
BUILD_DIR="$TOOL_DIR/build"
1215
LLVM_DIR="$TOOL_DIR/llvm-project"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
# 检测是否是鲲鹏CPU
4+
is_kunpeng_cpu() {
5+
# 尝试从 /proc/cpuinfo 读取CPU信息
6+
if [ -f "/proc/cpuinfo" ]; then
7+
# 检查CPU part字段
8+
if grep -q "CPU part" "/proc/cpuinfo"; then
9+
local cpu_part=$(grep "CPU part" "/proc/cpuinfo" | head -1 | awk -F: '{print $2}' | tr -d ' ')
10+
if [ "$cpu_part" = "0xd02" ] || [ "$cpu_part" = "0xd06" ]; then
11+
return 0
12+
fi
13+
fi
14+
fi
15+
return 1
16+
}
17+
18+
check_kunpeng_cpu() {
19+
if is_kunpeng_cpu; then
20+
echo "检测到鲲鹏CPU"
21+
else
22+
echo "不支持的CPU"
23+
exit 1
24+
fi
25+
}

0 commit comments

Comments
 (0)