Sfondo:
Durante l'ottimizzazione del codice Pascal con linguaggio assembly incorporato, ho notato un'istruzione non necessaria MOVe l'ho rimosso.
Con mia sorpresa, la rimozione delle istruzioni non necessarie ha fatto rallentare il mio programma .
Ho scoperto che l' aggiunta di MOVistruzioni arbitrarie e inutili ha aumentato ulteriormente le prestazioni .
L'effetto è irregolare e cambia in base all'ordine di esecuzione: le stesse istruzioni spazzatura trasposte su o giù da una singola riga producono un rallentamento .
Capisco che la CPU fa tutti i tipi di ottimizzazioni e ottimizzazione, ma sembra più una magia nera.
I dati:
Una versione del mio codice compila in modo condizionale tre operazioni spazzatura nel mezzo di un ciclo che esegue i 2**20==1048576tempi. (Il programma circostante calcola solo gli hash SHA-256 ).
I risultati sulla mia macchina piuttosto vecchia (Intel (R) Core (TM) 2 CPU 6400 a 2,13 GHz):
avg time (ms) with -dJUNKOPS: 1822.84 ms
avg time (ms) without: 1836.44 ms
I programmi sono stati eseguiti 25 volte in un ciclo, con l'ordine di esecuzione che cambia ogni volta in modo casuale.
Estratto:
{$asmmode intel}
procedure example_junkop_in_sha256;
var s1, t2 : uint32;
begin
// Here are parts of the SHA-256 algorithm, in Pascal:
// s0 {r10d} := ror(a, 2) xor ror(a, 13) xor ror(a, 22)
// s1 {r11d} := ror(e, 6) xor ror(e, 11) xor ror(e, 25)
// Here is how I translated them (side by side to show symmetry):
asm
MOV r8d, a ; MOV r9d, e
ROR r8d, 2 ; ROR r9d, 6
MOV r10d, r8d ; MOV r11d, r9d
ROR r8d, 11 {13 total} ; ROR r9d, 5 {11 total}
XOR r10d, r8d ; XOR r11d, r9d
ROR r8d, 9 {22 total} ; ROR r9d, 14 {25 total}
XOR r10d, r8d ; XOR r11d, r9d
// Here is the extraneous operation that I removed, causing a speedup
// s1 is the uint32 variable declared at the start of the Pascal code.
//
// I had cleaned up the code, so I no longer needed this variable, and
// could just leave the value sitting in the r11d register until I needed
// it again later.
//
// Since copying to RAM seemed like a waste, I removed the instruction,
// only to discover that the code ran slower without it.
{$IFDEF JUNKOPS}
MOV s1, r11d
{$ENDIF}
// The next part of the code just moves on to another part of SHA-256,
// maj { r12d } := (a and b) xor (a and c) xor (b and c)
mov r8d, a
mov r9d, b
mov r13d, r9d // Set aside a copy of b
and r9d, r8d
mov r12d, c
and r8d, r12d { a and c }
xor r9d, r8d
and r12d, r13d { c and b }
xor r12d, r9d
// Copying the calculated value to the same s1 variable is another speedup.
// As far as I can tell, it doesn't actually matter what register is copied,
// but moving this line up or down makes a huge difference.
{$IFDEF JUNKOPS}
MOV s1, r9d // after mov r12d, c
{$ENDIF}
// And here is where the two calculated values above are actually used:
// T2 {r12d} := S0 {r10d} + Maj {r12d};
ADD r12d, r10d
MOV T2, r12d
end
end;
Provate voi stessi:
Il codice è online su GitHub se vuoi provarlo tu stesso.
Le mie domande:
- Perché copiare inutilmente i contenuti di un registro nella RAM aumenterebbe mai le prestazioni?
- Perché la stessa inutile istruzione fornirebbe un aumento di velocità su alcune linee e un rallentamento su altre?
- Questo comportamento è qualcosa che potrebbe essere sfruttato in modo prevedibile da un compilatore?