Skip to content

Commit 61c4754

Browse files
committed
Implement BRANCH_TAKEN and BRANCH_NOT_TAKEN events
1 parent 7fbc4c9 commit 61c4754

16 files changed

+517
-316
lines changed

Include/cpython/code.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ extern "C" {
99
#endif
1010

1111
/* Count of all local monitoring events */
12-
#define _PY_MONITORING_LOCAL_EVENTS 10
12+
#define _PY_MONITORING_LOCAL_EVENTS 11
1313
/* Count of all "real" monitoring events (not derived from other events) */
14-
#define _PY_MONITORING_UNGROUPED_EVENTS 15
14+
#define _PY_MONITORING_UNGROUPED_EVENTS 16
1515
/* Count of all monitoring events */
16-
#define _PY_MONITORING_EVENTS 17
16+
#define _PY_MONITORING_EVENTS 19
1717

1818
/* Tables of which tools are active for each monitored event. */
1919
typedef struct _Py_LocalMonitors {

Include/cpython/monitoring.h

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,27 @@
1313
#define PY_MONITORING_EVENT_LINE 5
1414
#define PY_MONITORING_EVENT_INSTRUCTION 6
1515
#define PY_MONITORING_EVENT_JUMP 7
16-
#define PY_MONITORING_EVENT_BRANCH 8
17-
#define PY_MONITORING_EVENT_STOP_ITERATION 9
16+
#define PY_MONITORING_EVENT_BRANCH_TAKEN 8
17+
#define PY_MONITORING_EVENT_BRANCH_NOT_TAKEN 9
18+
#define PY_MONITORING_EVENT_STOP_ITERATION 10
1819

1920
#define PY_MONITORING_IS_INSTRUMENTED_EVENT(ev) \
2021
((ev) < _PY_MONITORING_LOCAL_EVENTS)
2122

2223
/* Other events, mainly exceptions */
2324

24-
#define PY_MONITORING_EVENT_RAISE 10
25-
#define PY_MONITORING_EVENT_EXCEPTION_HANDLED 11
26-
#define PY_MONITORING_EVENT_PY_UNWIND 12
27-
#define PY_MONITORING_EVENT_PY_THROW 13
28-
#define PY_MONITORING_EVENT_RERAISE 14
25+
#define PY_MONITORING_EVENT_RAISE 11
26+
#define PY_MONITORING_EVENT_EXCEPTION_HANDLED 12
27+
#define PY_MONITORING_EVENT_PY_UNWIND 13
28+
#define PY_MONITORING_EVENT_PY_THROW 14
29+
#define PY_MONITORING_EVENT_RERAISE 15
2930

3031

3132
/* Ancillary events */
3233

33-
#define PY_MONITORING_EVENT_C_RETURN 15
34-
#define PY_MONITORING_EVENT_C_RAISE 16
34+
#define PY_MONITORING_EVENT_C_RETURN 16
35+
#define PY_MONITORING_EVENT_C_RAISE 17
36+
#define PY_MONITORING_EVENT_BRANCH 18
3537

3638

3739
typedef struct _PyMonitoringState {
@@ -74,10 +76,18 @@ PyAPI_FUNC(int)
7476
_PyMonitoring_FireJumpEvent(PyMonitoringState *state, PyObject *codelike, int32_t offset,
7577
PyObject *target_offset);
7678

77-
PyAPI_FUNC(int)
79+
Py_DEPRECATED(3.14) PyAPI_FUNC(int)
7880
_PyMonitoring_FireBranchEvent(PyMonitoringState *state, PyObject *codelike, int32_t offset,
7981
PyObject *target_offset);
8082

83+
PyAPI_FUNC(int)
84+
_PyMonitoring_FireBranchTakenEvent(PyMonitoringState *state, PyObject *codelike, int32_t offset,
85+
PyObject *target_offset);
86+
87+
PyAPI_FUNC(int)
88+
_PyMonitoring_FireBranchNotTakenEvent(PyMonitoringState *state, PyObject *codelike, int32_t offset,
89+
PyObject *target_offset);
90+
8191
PyAPI_FUNC(int)
8292
_PyMonitoring_FireCReturnEvent(PyMonitoringState *state, PyObject *codelike, int32_t offset,
8393
PyObject *retval);
@@ -174,12 +184,21 @@ PyMonitoring_FireJumpEvent(PyMonitoringState *state, PyObject *codelike, int32_t
174184
}
175185

176186
static inline int
177-
PyMonitoring_FireBranchEvent(PyMonitoringState *state, PyObject *codelike, int32_t offset,
187+
PyMonitoring_FireBranchTakenEvent(PyMonitoringState *state, PyObject *codelike, int32_t offset,
188+
PyObject *target_offset)
189+
{
190+
_PYMONITORING_IF_ACTIVE(
191+
state,
192+
_PyMonitoring_FireBranchTakenEvent(state, codelike, offset, target_offset));
193+
}
194+
195+
static inline int
196+
PyMonitoring_FireBranchNotTakenEvent(PyMonitoringState *state, PyObject *codelike, int32_t offset,
178197
PyObject *target_offset)
179198
{
180199
_PYMONITORING_IF_ACTIVE(
181200
state,
182-
_PyMonitoring_FireBranchEvent(state, codelike, offset, target_offset));
201+
_PyMonitoring_FireBranchNotTakenEvent(state, codelike, offset, target_offset));
183202
}
184203

185204
static inline int

Include/internal/pycore_opcode_metadata.h

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_ids.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/opcode_ids.h

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/_opcode_metadata.py

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_compiler_codegen.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def test_if_expression(self):
2929
('LOAD_CONST', 0, 1),
3030
('TO_BOOL', 0, 1),
3131
('POP_JUMP_IF_FALSE', false_lbl := self.Label(), 1),
32+
('NOT_TAKEN', None, 1, 1),
3233
('LOAD_CONST', 1, 1),
3334
('JUMP_NO_INTERRUPT', exit_lbl := self.Label()),
3435
false_lbl,
@@ -49,6 +50,7 @@ def test_for_loop(self):
4950
('GET_ITER', None, 1),
5051
loop_lbl := self.Label(),
5152
('FOR_ITER', exit_lbl := self.Label(), 1),
53+
('NOT_TAKEN', None, 1, 1),
5254
('NOP', None, 1, 1),
5355
('STORE_NAME', 1, 1),
5456
('LOAD_NAME', 2, 2),

0 commit comments

Comments
 (0)