From 1db57f77e9c627bc9c00f9e2f827aa72b425b739 Mon Sep 17 00:00:00 2001 From: zinc Date: Thu, 15 Oct 2026 06:15:36 +0330 Subject: find_replace: cover o3 function prologues on every backend arm64, llvm and the region-interpreter rdrop path now watch the two prologue words the way the x86-64 jit already did, so icon.replace_function's lit+jmp patch retires a compiled callee (and clears fn_nat) instead of leaving a stale stub. three new icon regressions: repeated o3 replace, failed publication keeps the old entry, and a direct call after warming the region. --- src/core/core.c | 51 +++++++++++++++++++--- tests/icon/regression/o3_direct_replace.expected | 1 + tests/icon/regression/o3_direct_replace.zc | 17 ++++++++ tests/icon/regression/o3_failed_keeps_old.expected | 1 + tests/icon/regression/o3_failed_keeps_old.zc | 19 ++++++++ tests/icon/regression/o3_repeated_replace.expected | 1 + tests/icon/regression/o3_repeated_replace.zc | 16 +++++++ 7 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 tests/icon/regression/o3_direct_replace.expected create mode 100644 tests/icon/regression/o3_direct_replace.zc create mode 100644 tests/icon/regression/o3_failed_keeps_old.expected create mode 100644 tests/icon/regression/o3_failed_keeps_old.zc create mode 100644 tests/icon/regression/o3_repeated_replace.expected create mode 100644 tests/icon/regression/o3_repeated_replace.zc diff --git a/src/core/core.c b/src/core/core.c index 748c508..3133c55 100644 --- a/src/core/core.c +++ b/src/core/core.c @@ -4000,8 +4000,21 @@ static void rforget(Rg *R) if ((unsigned)R->fn < (unsigned)code_words) fn_nat[R->fn].stub = 0; R->nat = 0; } -static void rmark(Rg *R) { R->marked = 1; cinc(cov, R->lo, R->hi); } -static void runmark(Rg *R) { cdec(cov, R->lo, R->hi); } +static void rmark(Rg *R) +{ + R->marked = 1; cinc(cov, R->lo, R->hi); + /* cover the function prologue: icon.replace_function patches it with lit+jmp */ + if ((unsigned)R->fn < (unsigned)code_words) { + cinc(cov, R->fn, R->fn + 2); + tab[R->fn] = 0; + if ((unsigned)(R->fn + 1) < (unsigned)code_words) tab[R->fn + 1] = 0; + } +} +static void runmark(Rg *R) +{ + cdec(cov, R->lo, R->hi); + if ((unsigned)R->fn < (unsigned)code_words) cdec(cov, R->fn, R->fn + 2); +} static int ralloc(Rg *R, RB *B) { int i, j, k, n = B->n; @@ -5232,7 +5245,11 @@ static int rdrop(int a) /* a store landed on region code: drop those regions */ { int j, hit = 0; for (j = 0; j < nrg; j++) - if (rgs[j]->lifted && rgs[j]->lo <= a && a < rgs[j]->hi) { + if (rgs[j]->lifted && ((rgs[j]->lo <= a && a < rgs[j]->hi) || + ((unsigned)rgs[j]->fn < (unsigned)code_words && a >= rgs[j]->fn && a < rgs[j]->fn + 2))) { + /* prologue patch (icon.replace_function) retires the region; a body store only clears it */ + if ((unsigned)rgs[j]->fn < (unsigned)code_words && a >= rgs[j]->fn && a < rgs[j]->fn + 2) + rgs[j]->dead = 1; if (trace) fprintf(stderr, "o3: dropped %d..%d (a store to %d: %d)\n", rgs[j]->lo, rgs[j]->hi - 1, a, m[a]); rclear(rgs[j]); hit = 1; } @@ -5346,8 +5363,16 @@ static void runmark(Rg *R) } #else static void rforget(Rg *R) { (void)R; } -static void rmark(Rg *R) { R->marked = 1; cinc(rcov, R->lo, R->hi); } -static void runmark(Rg *R) { cdec(rcov, R->lo, R->hi); } +static void rmark(Rg *R) +{ + R->marked = 1; cinc(rcov, R->lo, R->hi); + if ((unsigned)R->fn < (unsigned)code_words) cinc(rcov, R->fn, R->fn + 2); +} +static void runmark(Rg *R) +{ + cdec(rcov, R->lo, R->hi); + if ((unsigned)R->fn < (unsigned)code_words) cdec(rcov, R->fn, R->fn + 2); +} static void rcompile(Rg *R, int eager) { (void)R; (void)eager; } #endif #endif @@ -5359,8 +5384,20 @@ static void rcompile(Rg *R, int eager) { (void)R; (void)eager; } typedef Cell (*RFn)(Cell *mem, Cell *regs, int ent); static VM_TLS int nregions; static void rforget(Rg *R) { rt_queue(R->rt); R->rt = 0; R->nat = 0; R->dnat = 0; } -static void rmark(Rg *R) { R->marked = 1; cinc(rcov, R->lo, R->hi); cinc(lcov, R->lo, R->hi); } -static void runmark(Rg *R) { cdec(rcov, R->lo, R->hi); cdec(lcov, R->lo, R->hi); } +static void rmark(Rg *R) +{ + R->marked = 1; cinc(rcov, R->lo, R->hi); cinc(lcov, R->lo, R->hi); + if ((unsigned)R->fn < (unsigned)code_words) { + cinc(rcov, R->fn, R->fn + 2); cinc(lcov, R->fn, R->fn + 2); + } +} +static void runmark(Rg *R) +{ + cdec(rcov, R->lo, R->hi); cdec(lcov, R->lo, R->hi); + if ((unsigned)R->fn < (unsigned)code_words) { + cdec(rcov, R->fn, R->fn + 2); cdec(lcov, R->fn, R->fn + 2); + } +} static LLVMValueRef as_f64(LLVMValueRef v, int fp) { return fp ? v : LLVMBuildBitCast(bd, v, f64t, ""); } static LLVMValueRef as_i64(LLVMValueRef v, int fp) { return fp ? LLVMBuildBitCast(bd, v, i64t, "") : v; } /* the variables go back to their frame slots. before a call from inside the diff --git a/tests/icon/regression/o3_direct_replace.expected b/tests/icon/regression/o3_direct_replace.expected new file mode 100644 index 0000000..5dda402 --- /dev/null +++ b/tests/icon/regression/o3_direct_replace.expected @@ -0,0 +1 @@ +100 1 200 diff --git a/tests/icon/regression/o3_direct_replace.zc b/tests/icon/regression/o3_direct_replace.zc new file mode 100644 index 0000000..efcd0be --- /dev/null +++ b/tests/icon/regression/o3_direct_replace.zc @@ -0,0 +1,17 @@ +include rt; +include run; +include icon; +// replace an o3 function and call it directly (entry via the patched prologue), +// after warming the compiled region so the store must drop it. +o3 int target() { return 1; } +fn main() { + int i, s = 0; + for (i = 0; i < 100; i++) s += target(); + rt.put_n(s); rt.put_c(' '); + let one = run.tokens("1"), two = run.tokens("2"); + rt.put_n(icon.find_replace(target, one, two)); rt.put_c(' '); + s = 0; + for (i = 0; i < 100; i++) s += target(); + rt.put_n(s); rt.put_c(10); + return 0; +} diff --git a/tests/icon/regression/o3_failed_keeps_old.expected b/tests/icon/regression/o3_failed_keeps_old.expected new file mode 100644 index 0000000..68cd311 --- /dev/null +++ b/tests/icon/regression/o3_failed_keeps_old.expected @@ -0,0 +1 @@ +8 1 10 0 6 10 diff --git a/tests/icon/regression/o3_failed_keeps_old.zc b/tests/icon/regression/o3_failed_keeps_old.zc new file mode 100644 index 0000000..f750a84 --- /dev/null +++ b/tests/icon/regression/o3_failed_keeps_old.zc @@ -0,0 +1,19 @@ +include rt; +include run; +include icon; +typecheck on; +// a rejected publication must leave the previous entry (and any o3 caller of it) +// unchanged: status 6, still returning the last successfully published body. +o3 int target() { return 4; } +o3 int caller() { return target() + target(); } +fn main() { + let four = run.tokens("4"), five = run.tokens("5"); + rt.put_n(caller()); rt.put_c(' '); + rt.put_n(icon.find_replace(target, four, five)); rt.put_c(' '); + rt.put_n(caller()); rt.put_c(' '); + let bad_pat = run.tokens("return $int;"), bad_rep = run.tokens("return \"x\";"); + rt.put_n(icon.find_replace(target, bad_pat, bad_rep)); rt.put_c(' '); + rt.put_n(icon.error()); rt.put_c(' '); + rt.put_n(caller()); rt.put_c(10); + return 0; +} diff --git a/tests/icon/regression/o3_repeated_replace.expected b/tests/icon/regression/o3_repeated_replace.expected new file mode 100644 index 0000000..970ce07 --- /dev/null +++ b/tests/icon/regression/o3_repeated_replace.expected @@ -0,0 +1 @@ +21 1 24 1 27 diff --git a/tests/icon/regression/o3_repeated_replace.zc b/tests/icon/regression/o3_repeated_replace.zc new file mode 100644 index 0000000..5cad477 --- /dev/null +++ b/tests/icon/regression/o3_repeated_replace.zc @@ -0,0 +1,16 @@ +include rt; +include run; +include icon; +// repeated find_replace on an o3 callee must keep the o3 caller's published view +// current (prologue coverage / generation guard), not a stale compiled stub. +o3 int target() { return 7; } +o3 int caller() { int s = 0; for (int i = 0; i < 3; i++) s += target(); return s; } +fn main() { + let seven = run.tokens("7"), eight = run.tokens("8"), nine = run.tokens("9"); + rt.put_n(caller()); rt.put_c(' '); + rt.put_n(icon.find_replace(target, seven, eight)); rt.put_c(' '); + rt.put_n(caller()); rt.put_c(' '); + rt.put_n(icon.find_replace(target, eight, nine)); rt.put_c(' '); + rt.put_n(caller()); rt.put_c(10); + return 0; +} -- cgit v1.2.3