Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement debug strings #1540

Merged
merged 1 commit into from
Jan 22, 2025
Merged

Implement debug strings #1540

merged 1 commit into from
Jan 22, 2025

Conversation

vext01
Copy link
Contributor

@vext01 vext01 commented Jan 10, 2025

[This requires a compiler change and a llvm sync]

@vext01
Implement debug strings.
6fb181e

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:

--- 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().

@vext01 vext01 changed the title Implement debug anchors. Implement debug strings Jan 16, 2025
@vext01
Copy link
Contributor Author

vext01 commented Jan 16, 2025

Force pushed the renaming and fixed conflicts.

@vext01
Copy link
Contributor Author

vext01 commented Jan 22, 2025

That last push syncs with master and adds the missing outlining logic (and a test for it).

@vext01
Copy link
Contributor Author

vext01 commented Jan 22, 2025

If you are OK with the deubgging logic, OK to squash this?

@ltratt
Copy link
Contributor

ltratt commented Jan 22, 2025

Please squash.

@vext01 vext01 force-pushed the debug-anchors branch 2 times, most recently from 6a91f73 to 617f1e9 Compare January 22, 2025 16:27
@vext01
Copy link
Contributor Author

vext01 commented Jan 22, 2025

That last force push syncs ykllvm and this is now ready to merge.

@ltratt ltratt added this pull request to the merge queue Jan 22, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 22, 2025
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()`.
@vext01
Copy link
Contributor Author

vext01 commented Jan 22, 2025

Force pushed to fix clippy warnings.

@ltratt ltratt added this pull request to the merge queue Jan 22, 2025
Merged via the queue into ykjit:master with commit 94e9c56 Jan 22, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants