Skip to content

Commit 1343895

Browse files
committed
Phase 3 Batch 2: findExceptionHandler port + Class B closure
Port HIRBuilder::findExceptionHandler to a pure-C body that scans the exception_table_ via two new C++-side read bridges (size + entry field-by-value access). C++ shim preserves caller-contract by returning &exception_table_[idx] from the matching index. Per spec §5 Class B-kept disposition for exception_table_ now CLOSED (read+write both via bridge): - Batch 1: hir_builder_state_exception_table_push_cpp (write) - Batch 2: hir_builder_state_exception_table_size_cpp (read meta) - Batch 2: hir_builder_state_exception_table_entry_cpp (read field) - Batch 2: hir_builder_state_find_exception_handler_c (C body) Pattern propagatable to similar Class B-kept members (pending_b2_blocks_, etc.) in subsequent batches. Bridges added (3, within W25b ≤5/batch): hir_builder_state_exception_table_size_cpp(builder) -> int hir_builder_state_exception_table_entry_cpp(builder, idx, *out_5fields) hir_builder_state_find_exception_handler_c(state, builder, off, *out_idx) -> int Cumulative exception_table_ bridge surface = 4 (push + size + entry + find); all under cumulative budget for the member. Numstat (vs HEAD c905827): Python/jit/hir/builder.cpp +29 -4 (size + entry impls; shim) Python/jit/hir/builder.h +5 -0 (2 friend decls + comment) Python/jit/hir/builder_state_c.h +27 -0 (3 new bridge decls) Python/jit/hir/builder_state_c.c +19 -0 (find_exception_handler_c) scripts/w45_bridge_drift_falsifier.sh +8 -4 (3 new fixtures + non-void return-type regex update) TOTAL: NET +80L (forecast +74L; +6L variance ~8%, within theologian 15% threshold for forecast accuracy). W45 fixtures: 3 new (state_exception_table_size_cpp, state_exception_table_entry_cpp, state_find_exception_handler_c) added per shepard 22:46:33Z same-commit discipline. W45 perl regex updated to match any return type (was void-only) since size_cpp returns int. Dry-run: 12/12 fixtures stage cleanly. Pre-commit compile-check: testkeeper 23:27:11Z BUILD_EXIT=0, 3-test sanity green (partial_conversions + W22 + W44). W44 gate: PASS. Authorization: theologian 23:22:59Z + supervisor 23:23:10Z. Per spec §6 python#9 + amendment 23:09:50Z, per-commit benchmark gate runs post- commit (geo-mean must stay >=1.0x parity floor).
1 parent c905827 commit 1343895

5 files changed

Lines changed: 88 additions & 8 deletions

File tree

Python/jit/hir/builder.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,16 +1243,41 @@ extern "C" void hir_builder_state_exception_table_push_cpp(
12431243
lasti != 0});
12441244
}
12451245

1246+
extern "C" int hir_builder_state_exception_table_size_cpp(void *builder) {
1247+
HIRBuilder *self = static_cast<HIRBuilder*>(builder);
1248+
return static_cast<int>(self->exception_table_.size());
1249+
}
1250+
1251+
extern "C" void hir_builder_state_exception_table_entry_cpp(
1252+
void *builder,
1253+
int idx,
1254+
int *out_start,
1255+
int *out_end,
1256+
int *out_target,
1257+
int *out_depth,
1258+
int *out_lasti) {
1259+
HIRBuilder *self = static_cast<HIRBuilder*>(builder);
1260+
const auto &e = self->exception_table_[idx];
1261+
*out_start = e.start.value();
1262+
*out_end = e.end.value();
1263+
*out_target = e.target.value();
1264+
*out_depth = e.depth;
1265+
*out_lasti = e.lasti ? 1 : 0;
1266+
}
1267+
12461268
void HIRBuilder::parseExceptionTable() {
12471269
hir_builder_state_parse_exception_table_c(&state_, this);
12481270
}
12491271

12501272
const HIRBuilder::ExceptionTableEntry* HIRBuilder::findExceptionHandler(
12511273
BCOffset off) const {
1252-
for (const auto& entry : exception_table_) {
1253-
if (off >= entry.start && off < entry.end) {
1254-
return &entry;
1255-
}
1274+
int idx = -1;
1275+
if (hir_builder_state_find_exception_handler_c(
1276+
const_cast<PhxHirBuilderState*>(&state_),
1277+
const_cast<HIRBuilder*>(this),
1278+
off.value(),
1279+
&idx)) {
1280+
return &exception_table_[idx];
12561281
}
12571282
return nullptr;
12581283
}

Python/jit/hir/builder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ class HIRBuilder {
212212
// private std::vector<ExceptionTableEntry> exception_table_.
213213
friend void ::hir_builder_state_exception_table_push_cpp(
214214
void*, int, int, int, int, int);
215+
// Phase 3 Batch 2 (theologian 23:22:59Z): read-side bridges close the
216+
// Class B-kept disposition for exception_table_ (read+write via bridge).
217+
friend int ::hir_builder_state_exception_table_size_cpp(void*);
218+
friend void ::hir_builder_state_exception_table_entry_cpp(
219+
void*, int, int*, int*, int*, int*, int*);
215220
public:
216221
const Preloader& preloader() const { return preloader_; }
217222
explicit HIRBuilder(const Preloader& preloader)

Python/jit/hir/builder_state_c.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,22 @@ void hir_builder_state_parse_exception_table_c(
6262
depth_lasti & 1);
6363
}
6464
}
65+
66+
int hir_builder_state_find_exception_handler_c(
67+
PhxHirBuilderState *state,
68+
void *builder,
69+
int off,
70+
int *out_idx) {
71+
(void)state; /* state unused (vector access via _cpp bridge). */
72+
int n = hir_builder_state_exception_table_size_cpp(builder);
73+
for (int i = 0; i < n; i++) {
74+
int start, end, target, depth, lasti;
75+
hir_builder_state_exception_table_entry_cpp(
76+
builder, i, &start, &end, &target, &depth, &lasti);
77+
if (off >= start && off < end) {
78+
*out_idx = i;
79+
return 1;
80+
}
81+
}
82+
return 0;
83+
}

Python/jit/hir/builder_state_c.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,33 @@ void hir_builder_state_exception_table_push_cpp(
6161
int depth,
6262
int lasti);
6363

64+
/* Phase 3 Batch 2: Class B read-side bridges for exception_table_.
65+
* Closes Class B-kept disposition (read+write both via bridge). */
66+
67+
/* Returns current vector size (number of entries). */
68+
int hir_builder_state_exception_table_size_cpp(void *builder);
69+
70+
/* Reads entry fields by-value via flat ptr-args (matches push_cpp pattern).
71+
* idx must be < size returned by _size_cpp. */
72+
void hir_builder_state_exception_table_entry_cpp(
73+
void *builder,
74+
int idx,
75+
int *out_start,
76+
int *out_end,
77+
int *out_target,
78+
int *out_depth,
79+
int *out_lasti);
80+
81+
/* Pure-C port of HIRBuilder::findExceptionHandler. Linear scan via
82+
* size+entry bridges; on match, writes index of first matching entry
83+
* to *out_idx and returns 1; else 0. C++ shim converts index → pointer
84+
* via &exception_table_[idx] preserving caller-contract. */
85+
int hir_builder_state_find_exception_handler_c(
86+
PhxHirBuilderState *state,
87+
void *builder,
88+
int off,
89+
int *out_idx);
90+
6491
#ifdef __cplusplus
6592
} /* extern "C" */
6693
#endif

scripts/w45_bridge_drift_falsifier.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,22 @@ FIXTURES=(
9696
"hir_builder_state_init|Phase 3 Batch 1 state init (3 args)"
9797
"hir_builder_state_parse_exception_table_c|Phase 3 Batch 1 parseExceptionTable C body (2 args)"
9898
"hir_builder_state_exception_table_push_cpp|Phase 3 Batch 1 exception_table push bridge (6 args)"
99+
"hir_builder_state_exception_table_size_cpp|Phase 3 Batch 2 exception_table size bridge (1 arg)"
100+
"hir_builder_state_exception_table_entry_cpp|Phase 3 Batch 2 exception_table entry bridge (7 args)"
101+
"hir_builder_state_find_exception_handler_c|Phase 3 Batch 2 findExceptionHandler C body (4 args)"
99102
)
100103

101104
# Mutation: append ', int phx_w45_drift' before the closing paren of the
102105
# bridge function decl + def. Uses perl -0 (slurp) for multi-line regex
103106
# since extern decls span 2 lines and definitions span more.
104107
mutate_bridge() {
105108
local symbol="$1"
106-
# Match: void <symbol>( ... ) — capture inner contents, append drift param.
107-
# Multi-line via perl -0777. Apply across all source files containing
108-
# bridge decls / defs.
109+
# Match: <return-type> <symbol>( ... ) — capture inner contents, append
110+
# drift param. Multi-line via perl -0777. Return type is any non-paren
111+
# token sequence (int, void, void*, bool, etc.). Apply across all
112+
# source files containing bridge decls / defs.
109113
perl -i -0777 -pe \
110-
"s/(void\s+${symbol}\s*\([^)]*?)\)/\${1}, int phx_w45_drift)/g" \
114+
"s/(\b\w[\w\s\*]*?\s+${symbol}\s*\([^)]*?)\)/\${1}, int phx_w45_drift)/g" \
111115
"$BUILDER_CPP" "$BUILDER_EMIT_C" "$BUILDER_STATE_C_H" "$BUILDER_STATE_C"
112116
}
113117

0 commit comments

Comments
 (0)