Skip to content

Commit cabb100

Browse files
committed
refcount_insertion: add RC_DIFF differential logging + RC_USE_CPP switch
RC_DIFF=1: logs every Incref/Decref with register ID and block ID - C pass: "C +ref vN bbM" / "C -ref vN bbM" - C++ pass: "CPP +ref vN bbM" / "CPP -ref vN bbM" RC_USE_CPP=1: forces C++ pass instead of C pass (for A/B comparison) Usage: run same test with both passes, sort output, diff. First divergence on alternating_sum: v47 Decref in bb2 (C) vs bb1 (C++).
1 parent 14b80aa commit cabb100

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

Python/jit/hir/refcount_insertion.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,18 @@ bool isUncounted(const Register* reg) {
490490
}
491491

492492
// Insert an Incref of `reg` before `cursor`.
493+
static int g_cpp_rc_log = -1;
494+
static int cpp_rc_log_enabled() {
495+
if (g_cpp_rc_log < 0) g_cpp_rc_log = getenv("RC_DIFF") != nullptr;
496+
return g_cpp_rc_log;
497+
}
498+
493499
void insertIncref(Env& env, Register* reg, Instr& cursor) {
494500
JIT_DCHECK(env.mutate, "Attempt to insert incref with mutate == false");
495501
JIT_DCHECK(!isUncounted(reg), "Attempt to incref an uncounted value");
502+
if (cpp_rc_log_enabled()) {
503+
fprintf(stderr, "CPP +ref v%d bb%d\n", reg->id(), cursor.block()->id);
504+
}
496505
Instr* incref;
497506
if (reg->type() <= TObject) {
498507
incref = static_cast<Instr*>(hir_c_create_incref(reg));
@@ -512,6 +521,9 @@ void insertIncref(Env& env, Register* reg, Instr& cursor) {
512521
void insertDecref(Env& env, Register* reg, Instr& cursor) {
513522
JIT_DCHECK(env.mutate, "Attempt to insert decref with mutate == false");
514523
JIT_DCHECK(!isUncounted(reg), "Attempt to decref an uncounted value");
524+
if (cpp_rc_log_enabled()) {
525+
fprintf(stderr, "CPP -ref v%d bb%d\n", reg->id(), cursor.block()->id);
526+
}
515527
Instr* decref;
516528
if (reg->type() <= TObject) {
517529
decref = static_cast<Instr*>(hir_c_create_decref(reg));
@@ -1305,12 +1317,14 @@ void RefcountInsertion::Run(Function& func) {
13051317
bindGuards(func);
13061318
func.cfg.splitCriticalEdges();
13071319

1308-
PhxRefcountEnv *c_env = phx_rc_env_create(static_cast<void*>(&func));
1309-
phx_rc_run(c_env);
1310-
phx_rc_env_destroy(c_env);
1311-
removeTrampolineBlocks(&func.cfg);
1312-
optimizeLongDecrefRuns(func);
1313-
return;
1320+
if (!getenv("RC_USE_CPP")) {
1321+
PhxRefcountEnv *c_env = phx_rc_env_create(static_cast<void*>(&func));
1322+
phx_rc_run(c_env);
1323+
phx_rc_env_destroy(c_env);
1324+
removeTrampolineBlocks(&func.cfg);
1325+
optimizeLongDecrefRuns(func);
1326+
return;
1327+
}
13141328

13151329
TRACE(
13161330
"Starting refcount insertion for '{}':\n{}",

Python/jit/hir/refcount_pass_c.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,27 @@
1212
#include "cinderx/Common/jit_log_c.h"
1313
#include "Python.h"
1414

15+
#include <stdio.h>
16+
#include <stdlib.h>
1517
#include <string.h>
1618

19+
static int g_c_rc_log = -1;
20+
static int c_rc_log_enabled(void) {
21+
if (g_c_rc_log < 0) g_c_rc_log = getenv("RC_DIFF") != NULL;
22+
return g_c_rc_log;
23+
}
24+
1725
/* AliasClass bits for managed heap (AManagedHeapAny) */
1826
#define ALIAS_MANAGED_HEAP_ANY 0x3FC
1927

2028
/* Bridge: check if register type is subtype of TObject (needs C++ constant) */
2129
extern int phx_rc_reg_is_object(void *reg);
2230

2331
void phx_rc_insert_incref(PhxRefcountEnv *env, void *reg, void *cursor) {
32+
if (env->mutate && c_rc_log_enabled()) {
33+
const HirBasicBlock *blk = (const HirBasicBlock *)((HirInstrLayout *)cursor)->block;
34+
fprintf(stderr, "C +ref v%d bb%d\n", hir_reg_id(reg), hir_bb_id(blk));
35+
}
2436
void *incref = phx_rc_reg_is_object(reg)
2537
? hir_c_create_incref(reg)
2638
: hir_c_create_xincref(reg);
@@ -30,6 +42,10 @@ void phx_rc_insert_incref(PhxRefcountEnv *env, void *reg, void *cursor) {
3042
}
3143

3244
void phx_rc_insert_decref(PhxRefcountEnv *env, void *reg, void *cursor) {
45+
if (env->mutate && c_rc_log_enabled()) {
46+
const HirBasicBlock *blk = (const HirBasicBlock *)((HirInstrLayout *)cursor)->block;
47+
fprintf(stderr, "C -ref v%d bb%d\n", hir_reg_id(reg), hir_bb_id(blk));
48+
}
3349
void *decref = phx_rc_reg_is_object(reg)
3450
? hir_c_create_decref(reg)
3551
: hir_c_create_xdecref(reg);

0 commit comments

Comments
 (0)