-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows the interpreter author to insert special comments into the trace IR for helping with debugging. In the interpreter source code you can add a call like `yk_debug_str(debug_string)` and when it is traced, a special `DebugStr` bytecode is inserted into the JIT IR, which when printed, displays the dynamic value of `debug_string` at the time the instruction was traced. Under the hood, this works a lot like promotion. Calls to `yk_debug_str` (when tracing is enabled) causes `debug_string` to be copied into a vector which is passed down the pipeline. Here's an example for yklua: ```diff --- a/src/lvm.c +++ b/src/lvm.c @@ -27,6 +27,7 @@ #include "lgc.h" #include "lobject.h" #include "lopcodes.h" +#include "lopnames.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" @@ -1234,6 +1235,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p); /* invalidate top for instructions not expecting it */ lua_assert(isIT(i) || (cast_void(L->top.p = base), 1)); + char dbgmsg[128]; + snprintf(dbgmsg, 128, "%s", opnames[GET_OPCODE(i)]); + yk_debug_str(dbgmsg); + vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { StkId ra = RA(i); ``` Will give a trace with debug strings inside like: ``` ... %408: ptr = ptr_add %361, 152 %409: ptr = load %408 %410: i32 = call @snprintf(%320, 128i64, %364, %409) ; debug_str: NEWTABLE %412: ptr = ptr_add %351, 592 %413: i32 = shl 1i32, 4294967295i32 ... ``` The strings are formatted as a comment. (Obviously you'd probably want to guard the debug string logic in the interpreter somehow so the overhead doesn't arise in production) For this facility to be useful, the `DebugStr` bytecode must never be optimised out. We tell the trace optimiser that this bytecode is an "internal" bytecode, which stops DCE from killing it. When adding the code to outline the contents of `yk_debug_str()` I needed to copy logic from `handle_promote()` and found several other copies of that same code in other places in the trace builder. Thic change also de-duplicates the copies into a function `outline_until_after_call()`.
- Loading branch information
Showing
13 changed files
with
341 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Run-time: | ||
// env-var: YKD_LOG_IR=aot,jit-pre-opt,jit-post-opt | ||
// env-var: YKD_SERIALISE_COMPILATION=1 | ||
// env-var: YK_LOG=4 | ||
// stderr: | ||
// ... | ||
// --- Begin aot --- | ||
// ... | ||
// debug_str %{{10_0}} | ||
// ... | ||
// ...call fprintf(... | ||
// ... | ||
// debug_str %{{13_0}} | ||
// ... | ||
// --- End aot --- | ||
// ... | ||
// --- Begin jit-pre-opt --- | ||
// ... | ||
// ; debug_str: before fprintf: 4 | ||
// ... | ||
// ...call @fprintf(... | ||
// ... | ||
// ; debug_str: after fprintf: 5 | ||
// ... | ||
// --- End jit-pre-opt --- | ||
// ... | ||
// --- Begin jit-post-opt --- | ||
// ... | ||
// ; debug_str: before fprintf: 4 | ||
// ... | ||
// ...call @fprintf(... | ||
// ... | ||
// ; debug_str: after fprintf: 5 | ||
// ... | ||
// --- End jit-post-opt --- | ||
// ... | ||
|
||
// Check that yk_debug_str() works and is not optimised out. | ||
|
||
#include <assert.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <yk.h> | ||
#include <yk_testing.h> | ||
|
||
#define MAX_MSG 128 | ||
|
||
int main(int argc, char **argv) { | ||
YkMT *mt = yk_mt_new(NULL); | ||
yk_mt_hot_threshold_set(mt, 0); | ||
YkLocation loc = yk_location_new(); | ||
char msg[MAX_MSG]; | ||
|
||
int i = 4; | ||
NOOPT_VAL(loc); | ||
NOOPT_VAL(i); | ||
while (i > 0) { | ||
yk_mt_control_point(mt, &loc); | ||
snprintf(msg, MAX_MSG, "before fprintf: %d", i); | ||
yk_debug_str(msg); | ||
fprintf(stderr, "%d\n", i); | ||
snprintf(msg, MAX_MSG, "after fprintf: %d", i + 1); | ||
yk_debug_str(msg); | ||
i--; | ||
} | ||
yk_location_drop(loc); | ||
yk_mt_shutdown(mt); | ||
return (EXIT_SUCCESS); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Run-time: | ||
// env-var: YKD_LOG_IR=aot,jit-pre-opt,jit-post-opt | ||
// env-var: YKD_SERIALISE_COMPILATION=1 | ||
// env-var: YK_LOG=4 | ||
// stderr: | ||
// ... | ||
// --- Begin aot --- | ||
// ... | ||
// debug_str %{{10_0}} | ||
// ... | ||
// ...call fprintf(... | ||
// ... | ||
// debug_str %{{13_0}} | ||
// ... | ||
// --- End aot --- | ||
// ... | ||
// --- Begin jit-pre-opt --- | ||
// ... | ||
// ; debug_str: before fprintf: 4 | ||
// ... | ||
// ...call @fprintf(... | ||
// ... | ||
// ; debug_str: after fprintf: 5 | ||
// ... | ||
// --- End jit-pre-opt --- | ||
// ... | ||
// --- Begin jit-post-opt --- | ||
// ... | ||
// ; debug_str: before fprintf: 4 | ||
// ... | ||
// ...call @fprintf(... | ||
// ... | ||
// ; debug_str: after fprintf: 5 | ||
// ... | ||
// --- End jit-post-opt --- | ||
// ... | ||
|
||
// Check that yk_debug_str() works and is not optimised out. | ||
|
||
#include <assert.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <yk.h> | ||
#include <yk_testing.h> | ||
|
||
#define MAX_MSG 128 | ||
|
||
__attribute__((yk_outline,noinline)) | ||
void f() { | ||
yk_debug_str("inside f"); | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
YkMT *mt = yk_mt_new(NULL); | ||
yk_mt_hot_threshold_set(mt, 0); | ||
YkLocation loc = yk_location_new(); | ||
char msg[MAX_MSG]; | ||
|
||
int i = 4; | ||
NOOPT_VAL(loc); | ||
NOOPT_VAL(i); | ||
while (i > 0) { | ||
yk_mt_control_point(mt, &loc); | ||
snprintf(msg, MAX_MSG, "before fprintf: %d", i); | ||
yk_debug_str(msg); | ||
f(); | ||
fprintf(stderr, "%d\n", i); | ||
snprintf(msg, MAX_MSG, "after fprintf: %d", i + 1); | ||
yk_debug_str(msg); | ||
i--; | ||
} | ||
yk_location_drop(loc); | ||
yk_mt_shutdown(mt); | ||
return (EXIT_SUCCESS); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.