Mercurial > hg > Members > anatofuz > slides
comparison slides/2018/07/03/slide.md @ 48:bc8b0482c14f
auto-Update generated slides by script
author | Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 03 Jul 2018 20:32:57 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
47:32e35be2ce71 | 48:bc8b0482c14f |
---|---|
1 title: CbCによるMoarVMの改良 | |
2 author: Takahiro Shimizu | |
3 profile: | |
4 lang: Japanese | |
5 | |
6 | |
7 # 研究目的 | |
8 - Perl5の後継言語として開発されているPerl6はMoarVMと呼ばれるVMを搭載している. | |
9 - Perl6はMoarVM,JVM,JavaScript上で動くRakudoと呼ばれる実装と,コンパイラ開発者用のサブセットであるNQPが主な実装となっている. | |
10 - 現在Perl6及びMoarVMは全体的な速度がPerl5と比較し低下しており,実務として利用できるレベルに達していない. | |
11 - さらにPerl6の実装自体巨大なcase-switch文など見通しが悪くなっている. | |
12 - この問題を解決するために現在当研究室で開発している継続を中心にしたContinuation based Cを用いて改良を行う | |
13 - CbCの設計理念からVMの実装と親和性が高い事も推測できる為,実際にCbCを用いてどのようにVMが実装できるかを検証する | |
14 | |
15 # 今週の進捗 | |
16 * 油断していたら一週間経ってました | |
17 * DynAsmに手がかりが無いかなと思って読んでいます | |
18 | |
19 # DynAsm | |
20 | |
21 * minilua経由で`dynasm.lua`などが呼ばれている模様 | |
22 * `dasm_init`でbreak pointを書けたら止まった | |
23 | |
24 ``` | |
25 (lldb) bt | |
26 * thread #2, stop reason = breakpoint 1.1 | |
27 * frame #0: 0x000000010029982f libmoar.dylib`dasm_init(compiler=0x00007000010bbc60, maxsection=2) at dasm_x86.h:93 | |
28 frame #1: 0x0000000100287d6a libmoar.dylib`MVM_jit_compiler_init(tc=0x0000000100a4b8f0, cl=0x00007000010bbc60, jg=0x000000010183ac7a) at compile.c:19 | |
29 frame #2: 0x0000000100287ff5 libmoar.dylib`MVM_jit_compile_graph(tc=0x0000000100a4b8f0, jg=0x000000010183ac7a) at compile.c:52 | |
30 frame #3: 0x00000001001b50da libmoar.dylib`MVM_spesh_candidate_add(tc=0x0000000100a4b8f0, p=0x0000000100a4c480) at candidate.c:101 | |
31 frame #4: 0x00000001001cf991 libmoar.dylib`worker(tc=0x0000000100a4b8f0, callsite=0x00000001006c9150, args=0x0000000000000000) at worker.c:16 | |
32 frame #5: 0x000000010014e8e2 libmoar.dylib`invoke_handler(tc=0x0000000100a4b8f0, invokee=0x000000010181ae40, callsite=0x00000001006c9150, args=0x0000000000000000) at MVMCFunction.c:9 | |
33 frame #6: 0x00000001000e8494 libmoar.dylib`thread_initial_invoke(tc=0x0000000100a4b8f0, data=0x0000000100a4c070) at threads.c:59 | |
34 frame #7: 0x00000001000aefee libmoar.dylib`MVM_interp_run(tc=0x0000000100a4b8f0, initial_invoke=(libmoar.dylib`thread_initial_invoke at threads.c:50), invoke_data=0x0000000100a4c070) at interp.c:93 | |
35 frame #8: 0x00000001000e7a35 libmoar.dylib`start_thread(data=0x0000000100a4c070) at threads.c:87 | |
36 frame #9: 0x00007fff7abf8661 libsystem_pthread.dylib`_pthread_body + 340 | |
37 frame #10: 0x00007fff7abf850d libsystem_pthread.dylib`_pthread_start + 377 | |
38 frame #11: 0x00007fff7abf7bf9 libsystem_pthread.dylib`thread_start + 13 | |
39 ``` | |
40 | |
41 # dasm_free | |
42 | |
43 * freeしている箇所で一度止めてupしていく | |
44 | |
45 ``` | |
46 (lldb) b dasm_free | |
47 Breakpoint 2: where = libmoar.dylib`dasm_free + 12 at dasm_x86.h:118, address = 0x0000000100299a0c | |
48 (lldb) c | |
49 Process 42568 resuming | |
50 Process 42568 stopped | |
51 * thread #2, stop reason = breakpoint 2.1 | |
52 frame #0: 0x0000000100299a0c libmoar.dylib`dasm_free(compiler=0x00007000010bbc60) at dasm_x86.h:118 | |
53 115 void | |
54 116 dasm_free (Dst_DECL) | |
55 117 { | |
56 -> 118 dasm_State *D = Dst_REF; | |
57 119 int i; | |
58 120 for (i = 0; i < D->maxsection; i++) | |
59 121 if (D->sections[i].buf) | |
60 Target 0: (moar) stopped. | |
61 (lldb) bt | |
62 * thread #2, stop reason = breakpoint 2.1 | |
63 * frame #0: 0x0000000100299a0c libmoar.dylib`dasm_free(compiler=0x00007000010bbc60) at dasm_x86.h:118 | |
64 frame #1: 0x0000000100287f39 libmoar.dylib`MVM_jit_compiler_deinit(tc=0x0000000100a4b8f0, cl=0x00007000010bbc60) at compile.c:40 | |
65 frame #2: 0x00000001002881d7 libmoar.dylib`MVM_jit_compile_graph(tc=0x0000000100a4b8f0, jg=0x000000010183ac7a) at compile.c:99 | |
66 frame #3: 0x00000001001b50da libmoar.dylib`MVM_spesh_candidate_add(tc=0x0000000100a4b8f0, p=0x0000000100a4c480) at candidate.c:101 | |
67 frame #4: 0x00000001001cf991 libmoar.dylib`worker(tc=0x0000000100a4b8f0, callsite=0x00000001006c9150, args=0x0000000000000000) at worker.c:16 | |
68 frame #5: 0x000000010014e8e2 libmoar.dylib`invoke_handler(tc=0x0000000100a4b8f0, invokee=0x000000010181ae40, callsite=0x00000001006c9150, args=0x0000000000000000) at MVMCFunction.c:9 | |
69 frame #6: 0x00000001000e8494 libmoar.dylib`thread_initial_invoke(tc=0x0000000100a4b8f0, data=0x0000000100a4c070) at threads.c:59 | |
70 frame #7: 0x00000001000aefee libmoar.dylib`MVM_interp_run(tc=0x0000000100a4b8f0, initial_invoke=(libmoar.dylib`thread_initial_invoke at threads.c:50), invoke_data=0x0000000100a4c070) at interp.c:93 | |
71 frame #8: 0x00000001000e7a35 libmoar.dylib`start_thread(data=0x0000000100a4c070) at threads.c:87 | |
72 frame #9: 0x00007fff7abf8661 libsystem_pthread.dylib`_pthread_body + 340 | |
73 frame #10: 0x00007fff7abf850d libsystem_pthread.dylib`_pthread_start + 377 | |
74 frame #11: 0x00007fff7abf7bf9 libsystem_pthread.dylib`thread_start + 13 | |
75 ``` | |
76 | |
77 # MBM_jit__compile_graph | |
78 | |
79 ``` | |
80 (lldb) f | |
81 frame #2: 0x00000001002881d7 libmoar.dylib`MVM_jit_compile_graph(tc=0x0000000100a4b8f0, jg=0x000000010183ac7a) at compile.c:99 | |
82 96 code = MVM_jit_compiler_assemble(tc, &cl, jg); | |
83 97 | |
84 98 /* Clear up the compiler */ | |
85 -> 99 MVM_jit_compiler_deinit(tc, &cl); | |
86 100 | |
87 101 /* Logging for insight */ | |
88 102 if (tc->instance->jit_bytecode_dir) { | |
89 (lldb) | |
90 ``` | |
91 | |
92 ``` | |
93 (lldb) p code | |
94 (MVMJitCode *) $2 = 0x00000001047196a0 | |
95 (lldb) p *code | |
96 (MVMJitCode) $3 = { | |
97 func_ptr = 0x00000001007ff000 | |
98 size = 1723 | |
99 bytecode = 0x0000000100560cf0 "R\x03" | |
100 sf = 0x0000000103248300 | |
101 local_types = 0x0000000104719920 | |
102 num_locals = 18 | |
103 num_labels = 7 | |
104 labels = 0x0000000102902530 | |
105 num_deopts = 0 | |
106 num_inlines = 0 | |
107 num_handlers = 0 | |
108 deopts = 0x0000000000000000 | |
109 inlines = 0x0000000000000000 | |
110 handlers = 0x0000000000000000 | |
111 spill_size = 1 | |
112 seq_nr = 0 | |
113 } | |
114 ``` | |
115 | |
116 # disassしてみる | |
117 | |
118 * `MAGIC_BYTECODE`という謎のbytecodeを吐いている | |
119 | |
120 ``` | |
121 (lldb) disass -a code->bytecode | |
122 libmoar.dylib`MAGIC_BYTECODE: | |
123 0x100560cf0 <+0>: pushq %rdx | |
124 0x100560cf1 <+1>: addl (%rax), %eax | |
125 0x100560cf3 <+3>: addb %al, (%rax) | |
126 0x100560cf5 <+5>: addb %al, (%rax) | |
127 0x100560cf7 <+7>: addb %al, (%rax) | |
128 0x100560cf9 <+9>: addb %al, (%rax) | |
129 0x100560cfb <+11>: addb %al, (%rax) | |
130 0x100560cfd <+13>: addb %al, (%rax) | |
131 0x100560cff <+15>: addb %al, (%rbx) | |
132 0x100560d01 <+17>: addb %al, (%rax) | |
133 0x100560d03 <+19>: addb %al, (%rax) | |
134 0x100560d05 <+21>: addb %al, (%rax) | |
135 0x100560d07 <+23>: addb %al, (%rax) | |
136 0x100560d09 <+25>: addb %al, (%rax) | |
137 0x100560d0b <+27>: addb %al, (%rax) | |
138 0x100560d0d <+29>: addb %al, (%rax) | |
139 0x100560d0f <+31>: addb %al, (%rax) | |
140 0x100560d11 <+33>: addb %al, (%rax) | |
141 0x100560d13 <+35>: addb %al, (%rax) | |
142 0x100560d15 <+37>: addb %al, (%rax) | |
143 0x100560d17 <+39>: addb %al, (%rax) | |
144 0x100560d19 <+41>: addb %al, (%rax) | |
145 0x100560d1b <+43>: addb %al, (%rax) | |
146 0x100560d1d <+45>: addb %al, (%rax) | |
147 ``` | |
148 | |
149 # 作っていそう | |
150 | |
151 ``` | |
152 45 MVMJitCode * MVM_jit_compile_graph(MVMThreadContext *tc, MVMJitGraph *jg) { | |
153 46 MVMJitCompiler cl; | |
154 47 MVMJitCode *code; | |
155 48 MVMJitNode *node = jg->first_node; | |
156 49 | |
157 50 MVM_jit_log(tc, "Starting compilation\n"); | |
158 (lldb) | |
159 51 /* initialation */ | |
160 52 MVM_jit_compiler_init(tc, &cl, jg); | |
161 53 /* generate code */ | |
162 54 MVM_jit_emit_prologue(tc, &cl, jg); | |
163 55 while (node) { | |
164 56 switch(node->type) { | |
165 57 case MVM_JIT_NODE_LABEL: | |
166 58 MVM_jit_emit_label(tc, &cl, jg, node->u.label.name); | |
167 59 break; | |
168 60 case MVM_JIT_NODE_PRIMITIVE: | |
169 61 MVM_jit_emit_primitive(tc, &cl, jg, &node->u.prim); | |
170 (lldb) | |
171 62 break; | |
172 63 case MVM_JIT_NODE_BRANCH: | |
173 64 MVM_jit_emit_block_branch(tc, &cl, jg, &node->u.branch); | |
174 65 break; | |
175 66 case MVM_JIT_NODE_CALL_C: | |
176 67 MVM_jit_emit_call_c(tc, &cl, jg, &node->u.call); | |
177 68 break; | |
178 69 case MVM_JIT_NODE_GUARD: | |
179 70 MVM_jit_emit_guard(tc, &cl, jg, &node->u.guard); | |
180 71 break; | |
181 72 case MVM_JIT_NODE_INVOKE: | |
182 (lldb) | |
183 73 MVM_jit_emit_invoke(tc, &cl, jg, &node->u.invoke); | |
184 74 break; | |
185 75 case MVM_JIT_NODE_JUMPLIST: | |
186 76 MVM_jit_emit_jumplist(tc, &cl, jg, &node->u.jumplist); | |
187 77 break; | |
188 78 case MVM_JIT_NODE_CONTROL: | |
189 79 MVM_jit_emit_control(tc, &cl, &node->u.control, NULL); | |
190 80 break; | |
191 81 case MVM_JIT_NODE_EXPR_TREE: | |
192 82 MVM_jit_compile_expr_tree(tc, &cl, jg, node->u.tree); | |
193 83 break; | |
194 (lldb) | |
195 84 case MVM_JIT_NODE_DATA: | |
196 85 MVM_jit_emit_data(tc, &cl, &node->u.data); | |
197 86 break; | |
198 87 case MVM_JIT_NODE_SAVE_RV: | |
199 88 MVM_jit_emit_save_rv(tc, &cl, node->u.stack.slot); | |
200 89 break; | |
201 90 } | |
202 91 node = node->next; | |
203 92 } | |
204 93 MVM_jit_emit_epilogue(tc, &cl, jg); | |
205 94 | |
206 (lldb) | |
207 95 /* Generate code */ | |
208 96 code = MVM_jit_compiler_assemble(tc, &cl, jg); | |
209 97 | |
210 98 /* Clear up the compiler */ | |
211 99 MVM_jit_compiler_deinit(tc, &cl); | |
212 100 | |
213 101 /* Logging for insight */ | |
214 102 if (tc->instance->jit_bytecode_dir) { | |
215 103 MVM_jit_log_bytecode(tc, code); | |
216 104 } | |
217 105 if (tc->instance->jit_log_fh) | |
218 (lldb) | |
219 106 fflush(tc->instance->jit_log_fh); | |
220 107 return code; | |
221 108 } | |
222 109 | |
223 ``` | |
224 | |
225 # 実際にアセンブルしている箇所 | |
226 | |
227 ``` | |
228 110 MVMJitCode * MVM_jit_compiler_assemble(MVMThreadContext *tc, MVMJitCompiler *cl, MVMJitGraph *jg) { | |
229 111 MVMJitCode * code; | |
230 112 MVMint32 i; | |
231 113 char * memory; | |
232 114 size_t codesize; | |
233 115 | |
234 116 MVMint32 dasm_error = 0; | |
235 117 | |
236 118 /* compile the function */ | |
237 119 if ((dasm_error = dasm_link(cl, &codesize)) != 0) { | |
238 120 MVM_jit_log(tc, "DynASM could not link, error: %d\n", dasm_error); | |
239 121 return NULL; | |
240 122 } | |
241 123 | |
242 124 memory = MVM_platform_alloc_pages(codesize, MVM_PAGE_READ|MVM_PAGE_WRITE); | |
243 125 if ((dasm_error = dasm_encode(cl, memory)) != 0) { | |
244 126 MVM_jit_log(tc, "DynASM could not encode, error: %d\n", dasm_error); | |
245 127 return NULL; | |
246 128 } | |
247 129 | |
248 130 /* set memory readable + executable */ | |
249 131 if (!MVM_platform_set_page_mode(memory, codesize, MVM_PAGE_READ|MVM_PAGE_EXEC)) { | |
250 132 MVM_jit_log(tc, "Setting jit page executable failed or was denied. deactivating jit.\n"); | |
251 133 /* our caller allocated the compiler and our caller must clean it up */ | |
252 134 tc->instance->jit_enabled = 0; | |
253 135 return NULL; | |
254 136 } | |
255 137 | |
256 138 MVM_jit_log(tc, "Bytecode size: %"MVM_PRSz"\n", codesize); | |
257 139 | |
258 140 /* Create code segment */ | |
259 141 code = MVM_malloc(sizeof(MVMJitCode)); | |
260 142 code->func_ptr = (void (*)(MVMThreadContext*,MVMCompUnit*,void*)) memory; | |
261 143 code->size = codesize; | |
262 144 code->bytecode = (MVMuint8*)MAGIC_BYTECODE; | |
263 145 code->sf = jg->sg->sf; | |
264 146 code->spill_size = cl->spills_num; | |
265 147 if (cl->spills_num > 0) { | |
266 148 MVMint32 sg_num_locals = jg->sg->num_locals; | |
267 149 code->num_locals = sg_num_locals + cl->spills_num; | |
268 150 code->local_types = MVM_malloc(code->num_locals * sizeof(MVMuint16)); | |
269 151 if (jg->sg->local_types != NULL) { | |
270 152 memcpy(code->local_types, jg->sg->local_types, sizeof(MVMuint16)*sg_num_locals); | |
271 153 } else { | |
272 154 memcpy(code->local_types, code->sf->body.local_types, sizeof(MVMuint16)*sg_num_locals); | |
273 155 } | |
274 156 for (i = 0; i < cl->spills_num; i++) { | |
275 157 code->local_types[sg_num_locals + i] = cl->spills[i].reg_type; | |
276 158 } | |
277 159 } else { | |
278 160 code->local_types = NULL; | |
279 161 code->num_locals = 0; | |
280 162 } | |
281 ``` | |
282 | |
283 | |
284 # メモリのプロテクトを解除 | |
285 | |
286 * `memory = MVM_platform_alloc_pages(codesize, MVM_PAGE_READ|MVM_PAGE_WRITE);`で呼び出している | |
287 * `platform/posix/mmap.c`で主に定義されている | |
288 | |
289 ``` | |
290 42 void *MVM_platform_alloc_pages(size_t size, int page_mode) | |
291 43 { | |
292 44 int prot_mode = page_mode_to_prot_mode(page_mode); | |
293 45 void *block = mmap(NULL, size, prot_mode, MVM_MAP_ANON | MAP_PRIVATE, -1, 0); | |
294 46 | |
295 47 if (block == MAP_FAILED) | |
296 48 MVM_panic(1, "MVM_platform_alloc_pages failed: %d", errno); | |
297 49 | |
298 50 return block; | |
299 51 } | |
300 ``` | |
301 | |
302 * mmapを変数をラップする用に使用している | |
303 | |
304 ``` | |
305 21 static int page_mode_to_prot_mode(int page_mode) { | |
306 22 switch (page_mode) { | |
307 23 case MVM_PAGE_READ: | |
308 24 return PROT_READ; | |
309 25 case MVM_PAGE_WRITE: | |
310 26 return PROT_WRITE; | |
311 27 case (MVM_PAGE_READ|MVM_PAGE_WRITE): | |
312 28 return PROT_READ|PROT_WRITE; | |
313 29 case MVM_PAGE_EXEC: | |
314 30 return PROT_EXEC; | |
315 31 case (MVM_PAGE_READ|MVM_PAGE_EXEC): | |
316 32 return PROT_READ|PROT_EXEC; | |
317 33 case (MVM_PAGE_WRITE|MVM_PAGE_EXEC): | |
318 34 return PROT_WRITE|PROT_EXEC; | |
319 35 case (MVM_PAGE_READ|MVM_PAGE_WRITE|MVM_PAGE_EXEC): | |
320 36 return PROT_READ|PROT_WRITE|PROT_EXEC; | |
321 37 default: | |
322 38 return PROT_NONE; | |
323 39 } | |
324 40 } | |
325 ``` | |
326 | |
327 | |
328 # JITに突入する箇所 | |
329 | |
330 * ` MVM_jit_enter_code`が怪しいが複数宣言されている | |
331 * `src/jit/compile.c` | |
332 | |
333 ``` | |
334 324 /* Enter the JIT code segment. The label is a continuation point where control | |
335 325 * is resumed after the frame is properly setup. */ | |
336 326 void MVM_jit_enter_code(MVMThreadContext *tc, MVMCompUnit *cu, | |
337 327 MVMJitCode *code) { | |
338 328 void *label = tc->cur_frame->jit_entry_label; | |
339 329 MVMint32 ofs = (char*)label - (char*)code->func_ptr; | |
340 330 if (ofs < 0 || ofs >= code->size) | |
341 331 MVM_oops(tc, "JIT entry label out of range for code!\n" | |
342 332 "(label %p, func_ptr %p, code size %lui, offset %li, frame_nr %i, seq nr %i)", | |
343 333 label, code->func_ptr, code->size, ((char*)label) - ((char*)code->func_ptr), | |
344 334 tc->cur_frame->sequence_nr, code->seq_nr); | |
345 335 code->func_ptr(tc, cu, label); | |
346 336 } | |
347 ``` | |
348 | |
349 # コード実行箇所 | |
350 | |
351 * `code->func_ptr`でgrepした | |
352 * src/core/nativecall.cがそれっぽい | |
353 | |
354 ``` | |
355 1149 void MVM_nativecall_invoke_jit(MVMThreadContext *tc, MVMObject *site) { | |
356 1150 MVMNativeCallBody *body = MVM_nativecall_get_nc_body(tc, site); | |
357 1151 MVMJitCode * const jitcode = body->jitcode; | |
358 1152 | |
359 1153 jitcode->func_ptr(tc, *tc->interp_cu, jitcode->labels[0]); | |
360 1154 } | |
361 ``` | |
362 | |
363 * ただしサンプルコードでは止まらない | |
364 * JITされてない? | |
365 * 呼ばれている箇所を探索 | |
366 * jit/graph.c | |
367 * op_to_funcで呼ばれているが巨大なcase文 | |
368 * opcodeによって判定している | |
369 * これ自体は`consume_reprop`で呼ばれているが謎 | |
370 * 多分`MVM_interp_run`をちゃんと読む必要がありそう | |
371 |