|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +// |
| 3 | +// W3 R4 ORACLE ADAPTER — re-creates deleted analysis.h C++ API as a thin |
| 4 | +// wrapper around the HirLivenessState C API. Used by docs/oracle_scratch/ |
| 5 | +// rc_oracle.cpp (a snapshot of cc4a18e7e5:Python/jit/hir/refcount_insertion.cpp) |
| 6 | +// to compile against current jit/hir/ infrastructure. |
| 7 | +// |
| 8 | +// SCOPE: oracle scratch lib ONLY. Pinned to push 35 HEAD d81e5806c3 per |
| 9 | +// theologian 23:34:14Z risk-mitigation. Not for production use. |
| 10 | +// |
| 11 | +// PHASE 0 AUDIT findings: |
| 12 | +// - cc4a18e7e5:analysis.h had `class LivenessAnalysis` (line 108) + |
| 13 | +// `extern const RegisterSet kEmptyRegSet` (line 20). |
| 14 | +// - HEAD analysis.h has only `using RegisterSet = std::unordered_set<Register*>`. |
| 15 | +// - liveness_c.h provides HirLivenessState C API that this adapter wraps. |
| 16 | +// |
| 17 | +// BRIDGE SPEC TEMPLATE (LITE form): |
| 18 | +// Bridge: _rc_oracle_adapter.h (LivenessAnalysis adapter class + kEmptyRegSet) |
| 19 | +// Purpose: expose deleted-from-HEAD LivenessAnalysis C++ class as wrapper |
| 20 | +// around HirLivenessState C API |
| 21 | +// C++ source: cc4a18e7e5:Python/jit/hir/analysis.h (deleted at HEAD) |
| 22 | +// PRIOR DECISIONS: |
| 23 | +// - D-1776820568: W3 MEDIUM priority, oracle-pinned to d81e5806c3 |
| 24 | +// - 14-bug R3b session: RC_DIFF found bugs #1-#14 by C-vs-C++ runtime diff |
| 25 | +// - R4 deletion timing: refcount_insertion.cpp deleted before corpus |
| 26 | +// methodology adopted; W3 closes the historical gap |
| 27 | +// INVARIANTS PRESERVED: |
| 28 | +// 1. LivenessAnalysis API surface: Run() + GetLastUses() + GetIn(block) — |
| 29 | +// the 3 methods refcount_insertion.cpp actually uses (verified via grep) |
| 30 | +// 2. LastUses iteration order: refcount_insertion uses iteration order to |
| 31 | +// decide WHERE Decref instructions are inserted; adapter delegates to |
| 32 | +// HirLivenessState which preserves the underlying analysis order |
| 33 | +// 3. kEmptyRegSet sentinel: empty-set value used as fallback in |
| 34 | +// ?: expressions; defined in _rc_oracle_adapter.cpp as static const |
| 35 | +// empty RegisterSet (operator== / contains / size all OK out of box) |
| 36 | +// 4. RegisterSet semantics: identical typedef (std::unordered_set<Register*>) |
| 37 | +// — adapter just passes Register* pointers through, no marshalling |
| 38 | +// 5. Oracle pin: this header is in docs/oracle_scratch/, included only by |
| 39 | +// rc_oracle.cpp; CMakeLists.txt (Step 4) git-checkouts d81e5806c3 for |
| 40 | +// the underlying jit/hir headers |
| 41 | +// Falsifier (Step 5 deliverable): inject synthetic refcount divergence into |
| 42 | +// C path, scripts/rc_diff_oracle.sh produces non-empty output. If empty |
| 43 | +// under injection, oracle is non-functional. |
| 44 | + |
| 45 | +#pragma once |
| 46 | + |
| 47 | +#include "cinderx/Jit/hir/analysis.h" // RegisterSet (HEAD) |
| 48 | +#include "cinderx/Jit/hir/liveness_c.h" // HirLivenessState C API |
| 49 | +#include "cinderx/Jit/hir/hir.h" // Function, BasicBlock, Instr, Register |
| 50 | + |
| 51 | +#include <ostream> |
| 52 | +#include <unordered_map> |
| 53 | +#include <unordered_set> |
| 54 | + |
| 55 | +namespace jit::hir { |
| 56 | + |
| 57 | +// Sentinel: empty RegisterSet used as fallback in refcount_insertion.cpp's |
| 58 | +// ?: expressions. Defined in _rc_oracle_adapter.cpp. |
| 59 | +extern const RegisterSet kEmptyRegSet; |
| 60 | + |
| 61 | +// cc4a18e7e5:analysis.h had operator<< for RegisterSet (used by |
| 62 | +// fmt::streamed in rc_oracle.cpp's TRACE). HEAD's analysis.h dropped it. |
| 63 | +// Adapter restores the formatter so rc_oracle.cpp compiles unmodified. |
| 64 | +// Format is debug-only; not load-bearing for the post-pass HIR diff. |
| 65 | +std::ostream &operator<<(std::ostream &os, const RegisterSet &set); |
| 66 | + |
| 67 | +// LivenessAnalysis adapter. Constructor takes Function& (matches cc4a18e7e5 |
| 68 | +// API). Run() builds the underlying HirLivenessState. GetLastUses() and |
| 69 | +// GetIn(block) project the C state into the C++ container types |
| 70 | +// refcount_insertion.cpp expects. |
| 71 | +class LivenessAnalysis { |
| 72 | + public: |
| 73 | + explicit LivenessAnalysis(const Function& irfunc) : func_(irfunc) {} |
| 74 | + |
| 75 | + ~LivenessAnalysis() { |
| 76 | + if (state_ != nullptr) { |
| 77 | + hir_liveness_destroy(state_); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Non-copyable (state_ is unique-owned). |
| 82 | + LivenessAnalysis(const LivenessAnalysis&) = delete; |
| 83 | + LivenessAnalysis& operator=(const LivenessAnalysis&) = delete; |
| 84 | + |
| 85 | + void Run() { |
| 86 | + // const_cast: HirFunction is an opaque void* alias; the C API only reads. |
| 87 | + state_ = hir_liveness_create( |
| 88 | + reinterpret_cast<HirFunction>(const_cast<Function*>(&func_))); |
| 89 | + } |
| 90 | + |
| 91 | + using LastUses = |
| 92 | + std::unordered_map<const Instr*, std::unordered_set<Register*>>; |
| 93 | + |
| 94 | + // Iterate every Instr in the function; for each, collect dying registers |
| 95 | + // via hir_liveness_get_dying_regs. Result map matches cc4a18e7e5 typedef. |
| 96 | + LastUses GetLastUses() { |
| 97 | + LastUses result; |
| 98 | + for (auto& block : func_.cfg.blocks) { |
| 99 | + for (auto& instr : block) { |
| 100 | + constexpr size_t kCap = 64; // refcount_insertion's deepest fn ≤32 dying |
| 101 | + void* dying[kCap]; |
| 102 | + size_t n = hir_liveness_get_dying_regs( |
| 103 | + state_, |
| 104 | + reinterpret_cast<HirInstr>(const_cast<Instr*>(&instr)), |
| 105 | + dying, kCap); |
| 106 | + if (n == 0) continue; |
| 107 | + auto& set = result[&instr]; |
| 108 | + for (size_t i = 0; i < n; i++) { |
| 109 | + set.insert(reinterpret_cast<Register*>(dying[i])); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return result; |
| 114 | + } |
| 115 | + |
| 116 | + // Live-in set for a basic block. Builds via foreach callback (no per-reg |
| 117 | + // iteration on caller side). |
| 118 | + RegisterSet GetIn(const BasicBlock* block) { |
| 119 | + RegisterSet result; |
| 120 | + auto cb = [](void* reg, void* ctx) { |
| 121 | + static_cast<RegisterSet*>(ctx)->insert(static_cast<Register*>(reg)); |
| 122 | + }; |
| 123 | + hir_liveness_foreach_live_in(state_, block, cb, &result); |
| 124 | + return result; |
| 125 | + } |
| 126 | + |
| 127 | + private: |
| 128 | + const Function& func_; |
| 129 | + HirLivenessState* state_{nullptr}; |
| 130 | +}; |
| 131 | + |
| 132 | +} // namespace jit::hir |
0 commit comments