-
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.
Detect and abort tracing if "early return" occurs.
If we start tracing a loop and the function-with-a-control-point returns whilst tracing is ongoing, we are in dangerous territory. For quite a while we assumed that "dangerous territory" meant (gongs of doom!) "UB". Actually, we just hadn't created very good examples: when I did, it turns out that an `unwrap` in trace_builder fails if early return happens. We *could*, therefore, detect early returns there, but that means that we've done a lot of work before realising this. This commit instead has `mt.rs` detect early returns, and abort tracing, as soon as we reasonably can. [Note, however, that if a user annotates `return`s from the function, they could stop things even earlier. Is this worth it? It is quite complicated to explain and, as we'll see in a bit, it's not a very common case] All that said, it turns out early returns aren't that common: you have to start tracing on the same iteration that you "early return" from. This is definitely possible -- indeed the two C tests in the PR do that, one "normally" and one when side-tracing -- but it seems unlikely that it will happen very often. So although we could force the user to help us detect this even sooner, I'm not sure it's worth burdening them or us with that responsibility right now: the cases where it would save serious resources are borderline pathological. Indeed, the only such case I can think of is: if you start tracing from the "root" control point call frame, and then return to the main program (i.e. you will never go back to the interpreter), then tracing is never turned off. One day we can fix this, but I don't think we will encounter it very soon.
- Loading branch information
Showing
9 changed files
with
371 additions
and
67 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,65 @@ | ||
// Run-time: | ||
// env-var: YKD_SERIALISE_COMPILATION=1 | ||
// env-var: YKD_LOG_IR=jit-pre-opt | ||
// env-var: YK_LOG=4 | ||
// stderr: | ||
// yk-jit-event: start-tracing | ||
// early return | ||
// 5 | ||
// yk-jit-event: tracing-aborted | ||
// 4 | ||
// yk-jit-event: start-tracing | ||
// 3 | ||
// yk-jit-event: stop-tracing | ||
// --- Begin jit-pre-opt --- | ||
// ... | ||
// --- End jit-pre-opt --- | ||
// 2 | ||
// yk-jit-event: enter-jit-code | ||
// 1 | ||
// yk-jit-event: deoptimise | ||
// return | ||
// exit | ||
|
||
// Check that early return from a recursive interpreter loop aborts tracing, | ||
// but doesn't stop a location being retraced. | ||
|
||
#include <assert.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <yk.h> | ||
#include <yk_testing.h> | ||
|
||
void loop(YkMT *, YkLocation *, int); | ||
|
||
void loop(YkMT *mt, YkLocation *loc, int i) { | ||
NOOPT_VAL(i); | ||
while (i > 0) { | ||
yk_mt_control_point(mt, loc); | ||
if (i == 6) { | ||
loop(mt, loc, i - 1); | ||
i--; | ||
} else if (i == 5) { | ||
fprintf(stderr, "early return\n"); | ||
return; | ||
} | ||
fprintf(stderr, "%d\n", i); | ||
i--; | ||
} | ||
fprintf(stderr, "return\n"); | ||
return; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
YkMT *mt = yk_mt_new(NULL); | ||
yk_mt_hot_threshold_set(mt, 1); | ||
YkLocation loc = yk_location_new(); | ||
|
||
NOOPT_VAL(loc); | ||
loop(mt, &loc, 6); | ||
fprintf(stderr, "exit\n"); | ||
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,118 @@ | ||
|
||
// Run-time: | ||
// env-var: YKD_SERIALISE_COMPILATION=1 | ||
// env-var: YKD_LOG_IR=jit-pre-opt | ||
// env-var: YK_LOG=4 | ||
// stderr: | ||
// yk-jit-event: start-tracing | ||
// b3 | ||
// 8 | ||
// yk-jit-event: stop-tracing | ||
// --- Begin jit-pre-opt --- | ||
// ... | ||
// --- End jit-pre-opt --- | ||
// b1 | ||
// 7 | ||
// yk-jit-event: enter-jit-code | ||
// yk-jit-event: deoptimise | ||
// yk-jit-event: start-side-tracing | ||
// b1 | ||
// 9 | ||
// yk-jit-event: tracing-aborted | ||
// b3 | ||
// 8 | ||
// yk-jit-event: enter-jit-code | ||
// yk-jit-event: deoptimise | ||
// yk-jit-event: start-side-tracing | ||
// b3 | ||
// 7 | ||
// yk-jit-event: stop-tracing | ||
// --- Begin jit-pre-opt --- | ||
// ... | ||
// --- End jit-pre-opt --- | ||
// b3 | ||
// 6 | ||
// yk-jit-event: enter-jit-code | ||
// yk-jit-event: execute-side-trace | ||
// yk-jit-event: deoptimise | ||
// yk-jit-event: start-side-tracing | ||
// b2 | ||
// 5 | ||
// yk-jit-event: stop-tracing | ||
// --- Begin jit-pre-opt --- | ||
// ... | ||
// --- End jit-pre-opt --- | ||
// b2 | ||
// 4 | ||
// yk-jit-event: enter-jit-code | ||
// yk-jit-event: execute-side-trace | ||
// yk-jit-event: execute-side-trace | ||
// b2 | ||
// 3 | ||
// yk-jit-event: execute-side-trace | ||
// yk-jit-event: execute-side-trace | ||
// b2 | ||
// 2 | ||
// yk-jit-event: execute-side-trace | ||
// yk-jit-event: execute-side-trace | ||
// b2 | ||
// 1 | ||
// yk-jit-event: execute-side-trace | ||
// yk-jit-event: execute-side-trace | ||
// b2 | ||
// 0 | ||
// yk-jit-event: deoptimise | ||
// yk-jit-event: start-side-tracing | ||
// exit | ||
|
||
// Check that early return from a recursive interpreter loop aborts tracing, | ||
// but doesn't stop a location being retraced. | ||
|
||
#include <assert.h> | ||
#include <stdio.h> | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <yk.h> | ||
#include <yk_testing.h> | ||
|
||
void loop(YkMT *, YkLocation *, int, bool); | ||
|
||
void loop(YkMT *mt, YkLocation *loc, int i, bool inner) { | ||
NOOPT_VAL(i); | ||
while (i > 0) { | ||
yk_mt_control_point(mt, loc); | ||
if (i == 10) { | ||
loop(mt, loc, i - 1, true); | ||
i--; | ||
} else if (inner && i <= 8) { | ||
fprintf(stderr, "b1\n"); | ||
i--; | ||
} else if (!inner && i <= 6) { | ||
fprintf(stderr, "b2\n"); | ||
i--; | ||
} else { | ||
fprintf(stderr, "b3\n"); | ||
i--; | ||
} | ||
if (inner && i == 6) { | ||
return; | ||
} | ||
fprintf(stderr, "%d\n", i); | ||
} | ||
return; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
YkMT *mt = yk_mt_new(NULL); | ||
yk_mt_hot_threshold_set(mt, 1); | ||
yk_mt_sidetrace_threshold_set(mt, 1); | ||
YkLocation loc = yk_location_new(); | ||
|
||
NOOPT_VAL(loc); | ||
loop(mt, &loc, 10, false); | ||
fprintf(stderr, "exit\n"); | ||
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
Oops, something went wrong.