x86/x64最適化勉強会4
@Constellation
一般的なVM
for (...) {
// opcode fetch
opcode = ...
switch (opcode) {
case MV: {
...
continue; // next opcode
}
case THROW: {
...
break; // go to exception handler
}
}
// handling exception
...
}
opcodeを入れていたところを, labelに(codeへのpointerへ)して全て関節ジャンプへ
for (...) {
switch (opcode) {
case MV: {
...
// opcode fetch
opcode = ...
goto *opcode; // next opcode
}
...
breaker::Compiler#EmitMV
// opcode | (dst | src)
void EmitMV(const Instruction* instr) {
const int16_t dst = Reg(instr[1].i16[0]);
const int16_t src = Reg(instr[1].i16[1]);
LoadVR(asm_->rax, src);
asm_->mov(asm_->qword[asm_->r13 + dst * kJSValSize], asm_->rax);
set_last_used_candidate(dst);
// type propagation
type_record_[dst] = type_record_[src];
}
JS
function test() {
for (var i = 0; i < 100; ++i) {
print(i);
}
}
bytecode
[code] local: 1 heap: 0 registers: 4
000000: LOAD_CONST r0 0
000002: LOAD_CONST r1 1
000004: IF_FALSE_BINARY_LT 18 r0 r1 ; => 22
000006: LOAD_GLOBAL r1 1 0x0 0
000010: LOAD_UNDEFINED r3
000012: MV r2 r0
000014: CALL r1 r2 2
000016: INCREMENT r0
000018: LOAD_CONST r1 1
000020: IF_TRUE_BINARY_LT -14 r0 r1 ; => 6
000022: LOAD_UNDEFINED r1
000024: RETURN r1
こういうふうだと
BINARY_LT r0, r1, r2
IF_TRUE r0, ...
JITとしては
Register VM Bytecode Compilerが融合命令をemit
IF_TRUE_BINARY_LT r1, r2
IfStatementのconditionは保存が要らない場合がほとんど
test
...
LoadVRs(asm_->rsi, lhs, asm_->rdx, rhs);
Int32Guard(lhs, asm_->rsi, asm_->rax, ".BINARY_LT_SLOW");
Int32Guard(rhs, asm_->rdx, asm_->rax, ".BINARY_LT_SLOW");
asm_->cmp(asm_->esi, asm_->edx);
if (fused != OP::NOP) {
// fused jump opcode
const std::string label = MakeLabel(instr);
if (fused == OP::IF_TRUE) {
asm_->jl(label.c_str(), Xbyak::CodeGenerator::T_NEAR);
} else {
asm_->jge(label.c_str(), Xbyak::CodeGenerator::T_NEAR);
}
SunSpider benchmark result (spent time. smaller is faster)
V8 Suite (spent time. smaller is faster)
参考画像
JS
if (i < 100) {
...
}
bytecode
000002: LOAD_CONST r1 1
000004: IF_FALSE_BINARY_LT 12 r0 r1 ; => 16
r1に一度constantをloadして, その後使う方式
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |