0

我在 x86_64 上使用 Ubuntu。我首先从 clang 生成未优化的 WebAssembly 代码。WebAssembly 代码被编译成机器代码wasmtimecranelift用作编译后端。

我正在做一个简单的矩阵乘法:

int first[m][n], second[p][q], multiply[m][q];

这是三个循环中第一个循环的机器代码

for ( int c = 0 ; c < m ; c++ ){}

由 生成wasmtime。我找到了许多必要的说明,但我找不到起重机升降机的调用约定来需要这些说明。

汇编代码

  1. 我的问题主要是关于esi寄存器,编译器是否必须使用它来获取c使用esi. 的价值必须esi保留吗?不然为什么不直接用它来做cmp。如果是,为什么1ad我们使用 esi 来存储来自r14d哪个来自的0 eax?此外,0 值保存在 中eax,eax 也是非易失性寄存器吗?

  2. 1b0,如果我们直接cmp esi, r12d根据结果设置 esi 0 或 1,就不需要复杂的步骤了1ba——1c3对吧?

  3. 如果我使用优化的 WebAssembly 生成优化的机器代码

     wasm32-wasi-clang -O2
    

    我不认为cranelift本身提供了优化标志,我发现机器代码更短但也很难理解。有什么方法可以帮助我阅读优化的机器代码?而且,如果我的机器代码来自未优化的 wasm。根据我们刚才对这个for循环例子的分析,可以说我找到了wasm编译器cranelift的缺陷吗?

非常感谢你的帮助!我知道代码可能读起来不愉快,你可以放大页面看图片

  195: rex mov DWORD PTR [rbx+rdx*1+0xc],eax         ;int c = 0, eax preserves 0. 

  19a: rex mov edx,ecx                              ; edx = ecx = [rdi+0x60]  + 0xffffffc0  

  19d: rex mov esi,DWORD PTR [rbx+rdx*1+0xc]         ; esi = [0xc]  = c,  get c into register? 

  1a2: mov    r12d,DWORD PTR [rbx+rdx*1+0x38]       ; r12d = m 

  1a7: mov    r14d,eax               ;r14d = eax = 0         
  
  1aa: rex mov eax,esi               ;     eax = esi = [c] = c [[!!! abuse register, you can directly use esi for cmp, eax remains 0, instead of store eax value 0 in r14d, 
                                                        ;use eax for cmp, and at the end restore eax value 0 by esi (esi = r14d)]

  1ad: mov    esi,r14d               ; esi = r14d = 0;  

  1b0: cmp    eax,r12d               ; cmp if eax (c) < r12d (m), if yes, al = 1, otherwise al = 0; {can use esi?? cmp usage ?}

  1b3: setl   al               

  1b6: movzx  eax,al               ; eax = al = 1 (if <)   [movzx already uses zero-extension!!]

  1ba: and    eax,0x1              ; eax = 1 if (if <). [al is low 8 bits, set only supports one byte, Byte Set On Condition (setcc), why not directrly set r12d also has low 8 bit !!!! ]

  1bd: mov    r12d,eax             ; r12d = eax = 1 (if <)

  1c0: rex mov eax,esi                ;eax = esi =  r14d = 0;   [restore eax = 0, but why use esi rather than r14d, actually you dont even need to restore eax 
                                                        ;if choose to replace eax with another register in CMP   ]

  1c3: mov    esi,r12d             ; esi = r12d =  eax = 1 (if <) [!!if directly use esi for CMP, there is no need of mov 12d, eax then mov esi, r12d]

  1c6: rex test esi,esi ;if esi=0 jump

  1c9: je     2f0 <_wasm_function_3+0x227>
4

0 回答 0