|
1 | 1 | # asm2arm_tool |
2 | 2 |
|
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及以上优化等级 |
0 commit comments