8.05.2010

pipeline bubble

利用 assembly 來optimize speed 時,要注意,並不是 instruction line 短就夠了。
還要注意 pipline 的特性。

pipeline 將指令分成幾個 stage:
  • fetch,
  • decode/register read,
  • alu,
  • memory write,
  • register update.
如果這樣個 assembly codes:
ldr r0,#1
add r3,r0,r1
第二行指令在 decode/register read 時,r0 還沒update,因為上一行才執行到alu operation。所以在pipeline中的第二行指令就要停下來,等兩個clock,等上一行指令執行到register update後,才可以繼續。
這樣,就白白浪費 2 個 clock 了。

為了避免這樣的情況,寫 assembly 的時候,就可以在中間插入下面要作的 assembly code,不要白白浪費這兩個clock

在 (很久)前面的文章:yuv - rgb color space convert 的 assembly code 可以看到。最後一個作者的assembly code 就有做到這個(所以整個 code 的 flow 變得不好trace)。實測的結果,也是最快的。

在 Wiki 里也有說明
instruction scheduling
Instruction scheduling is an important optimization for modern pipelined processors, which avoids stalls or bubbles in the pipeline by clustering instructions with no dependencies together, while being careful to preserve the original semantics.

2 則留言:

sunflier 提到...

讚,收下了...
不過要一行一行自己對,除非眼力很好...

這件事聰明一點的 compiler 應該也會想到?

checko 提到...

的確是這樣,所以我想這會不會就是有時候手寫assembly code 結果還是比 compile 出來得慢的原因? >_<