Skip to content

Commit

Permalink
Use BCOffset for DebugInfo
Browse files Browse the repository at this point in the history
Summary:
It's using raw integers for bytecode offsets and indices, but it doesn't have
to.

Reviewed By: swtaarrs

Differential Revision: D56534784

fbshipit-source-id: 50f5ec68bc9514addf661c12ee5e72839e044159
  • Loading branch information
Alex Malyshev authored and facebook-github-bot committed Apr 26, 2024
1 parent bf659ba commit 3602baa
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
4 changes: 2 additions & 2 deletions cinderx/Jit/debug_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void DebugInfo::addUnitCallStack(
const jit::hir::FrameState* caller_frame_state) {
uint16_t caller_id = getCallerID(caller_frame_state);
uint16_t code_obj_id = getCodeObjID(code);
addr_locs_.emplace(addr, LocNode{code_obj_id, caller_id, bc_off.value()});
addr_locs_.emplace(addr, LocNode{code_obj_id, caller_id, bc_off});
}

uint16_t DebugInfo::getCodeObjID(BorrowedRef<PyCodeObject> code_obj) {
Expand All @@ -162,7 +162,7 @@ uint16_t DebugInfo::getCallerID(const jit::hir::FrameState* caller) {
LocNode node{
getCodeObjID(caller->code),
getCallerID(caller->parent),
caller->instr_offset().value()};
caller->instr_offset()};
for (uint16_t i = 0; i < inlined_calls_.size(); i++) {
if (inlined_calls_[i] == node) {
return i;
Expand Down
27 changes: 11 additions & 16 deletions cinderx/Jit/debug_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ class Instr;
// A location in a code object
struct CodeObjLoc {
CodeObjLoc(BorrowedRef<PyFrameObject> py_frame)
: code(py_frame->f_code),
instr_offset(py_frame->f_lasti * int{sizeof(_Py_CODEUNIT)}) {}
CodeObjLoc(BorrowedRef<PyCodeObject> code_, int lasti)
: code(code_), instr_offset(lasti) {}
: code{py_frame->f_code},
instr_offset{BCIndex{py_frame->f_lasti}.asOffset()} {}
CodeObjLoc(BorrowedRef<PyCodeObject> code, BCOffset instr_offset)
: code{code}, instr_offset{instr_offset} {}

BorrowedRef<PyCodeObject> code;

// Bytecode offset. A value less than 0 indicates the position is unknown.
int instr_offset{-1};
BCOffset instr_offset{-1};

int lineNo() const {
return PyCode_Addr2Line(code, instr_offset);
return PyCode_Addr2Line(code, instr_offset.value());
}
};

Expand Down Expand Up @@ -86,7 +87,7 @@ class DebugInfo {
// Given a node, the location information for its call stack is specified
// by the node and the chain of inlined calls reachable from it.
struct LocNode {
LocNode(uint16_t cobj_id, uint16_t clr_id, int bco)
LocNode(uint16_t cobj_id, uint16_t clr_id, BCOffset bco)
: code_obj_id(cobj_id), caller_id(clr_id), bc_off(bco) {}
// Index into code_objs of the PyCodeObject for this entry.
uint16_t code_obj_id;
Expand All @@ -96,20 +97,14 @@ class DebugInfo {
uint16_t caller_id;

// Current bytecode offset.
int bc_off;
BCOffset bc_off;

bool hasCaller() const {
return caller_id != kNoCallerID;
}

bool operator==(const LocNode& other) const {
return (code_obj_id == other.code_obj_id) &&
(caller_id == other.caller_id) && (bc_off == other.bc_off);
}

bool operator!=(const LocNode& other) const {
return !(*this == other);
}
bool operator==(const LocNode& other) const = default;
bool operator!=(const LocNode& other) const = default;
};

static const uint16_t kNoCallerID = UINT16_MAX;
Expand Down
7 changes: 4 additions & 3 deletions cinderx/Jit/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ PyFrameState getPyFrameStateForJITGen(PyGenObject* gen) {
BorrowedRef<PyFrameObject> materializePyFrame(
PyThreadState* tstate,
_PyShadowFrame* shadow_frame,
int last_instr_offset,
BCOffset last_instr_offset,
std::optional<BorrowedRef<PyFrameObject>> cursor) {
// Make sure a PyFrameObject exists at the correct location in the call
// stack.
Expand Down Expand Up @@ -276,7 +276,7 @@ BorrowedRef<PyFrameObject> materializePyFrame(
}
}
// Update the PyFrameObject to refect the state of the JIT function
py_frame->f_lasti = last_instr_offset / sizeof(_Py_CODEUNIT);
py_frame->f_lasti = last_instr_offset.asIndex().value();
if (is_shadow_frame_for_gen(shadow_frame)) {
PyGenObject* gen = _PyShadowFrame_GetGen(shadow_frame);
py_frame->f_state = getPyFrameStateForJITGen(gen);
Expand Down Expand Up @@ -411,7 +411,8 @@ UnitState getUnitState(_PyShadowFrame* shadow_frame) {
JIT_DABORT("No debug info for addr {:x}", ip);
for (std::size_t i = 0; i < unit_frames.size(); i++) {
_PyShadowFrame* sf = unit_frames[i];
unit_state.emplace_back(sf, CodeObjLoc{_PyShadowFrame_GetCode(sf), -1});
unit_state.emplace_back(
sf, CodeObjLoc{_PyShadowFrame_GetCode(sf), BCOffset{-1}});
}
}

Expand Down

0 comments on commit 3602baa

Please sign in to comment.