8000 gh-105481: add OPCODE_IS_INSTRUMENTED (generated from bytecodes.c) to replace MIN_INSTRUMENTED_OPCODE (defined in opcode.py) by iritkatriel · Pull Request #107276 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-105481: add OPCODE_IS_INSTRUMENTED (generated from bytecodes.c) to replace MIN_INSTRUMENTED_OPCODE (defined in opcode.py) #107276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load com 8000 ments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix test__opcode. Special case INSTRUMENTED_LINE in generator
  • Loading branch information
iritkatriel committed Jul 26, 2023
commit 02f7a9fc6279affc29eedba938b9e748fd2dee85
5 changes: 3 additions & 2 deletions Include/internal/pycore_opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,8 @@ enum InstructionFormat { INSTR_FMT_IB, INSTR_FMT_IBC, INSTR_FMT_IBC00, INSTR_FMT

#define IS_VALID_OPCODE(OP) \
(((OP) >= 0) && ((OP) < OPCODE_METADATA_SIZE) && \
(_PyOpcode_opcode_metadata[(OP)].valid_entry))
(_PyOpcode_opcode_metadata[(OP)].valid_entry)) || \
((OP) == INSTRUMENTED_LINE)

#define HAS_ARG_FLAG (1)
#define HAS_CONST_FLAG (2)
Expand All @@ -964,7 +965,7 @@ enum InstructionFormat { INSTR_FMT_IB, INSTR_FMT_IBC, INSTR_FMT_IBC00, INSTR_FMT
#define OPCODE_HAS_JUMP(OP) (_PyOpcode_opcode_metadata[OP].flags & (HAS_JUMP_FLAG))
#define OPCODE_HAS_FREE(OP) (_PyOpcode_opcode_metadata[OP].flags & (HAS_FREE_FLAG))
#define OPCODE_HAS_LOCAL(OP) (_PyOpcode_opcode_metadata[OP].flags & (HAS_LOCAL_FLAG))
#define OPCODE_IS_INSTRUMENTED(OP) (_PyOpcode_opcode_metadata[OP].flags & (IS_INSTRUMENTED_FLAG))
#define OPCODE_IS_INSTRUMENTED(OP) (_PyOpcode_opcode_metadata[OP].flags & (IS_INSTRUMENTED_FLAG)) || ((OP) == INSTRUMENTED_LINE)

struct opcode_metadata {
bool valid_entry;
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test__opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_stack_effect(self):
# All defined opcodes
has_arg = dis.hasarg
for name, code in filter(lambda item: item[0] not in dis.deoptmap, dis.opmap.items()):
if code >= opcode.MIN_INSTRUMENTED_OPCODE:
if _opcode.is_instrumented(code):
continue
with self.subTest(opname=name):
stack_effect(code)
Expand All @@ -83,7 +83,7 @@ def test_stack_effect_jump(self):
has_exc = dis.hasexc
has_jump = dis.hasjabs + dis.hasjrel
for name, code in filter(lambda item: item[0] not in dis.deoptmap, dis.opmap.items()):
if code >= opcode.MIN_INSTRUMENTED_OPCODE:
if _opcode.is_instrumented(code):
continue
with self.subTest(opname=name):
if code not in has_arg:
Expand Down
6 changes: 5 additions & 1 deletion Tools/cases_generator/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ def emit_macros(cls, out: Formatter):
out.emit(f"#define {name} ({value})")

for name, value in flags.bitmask.items():
if name == 'IS_INSTRUMENTED_FLAG':
or_special_case = " || ((OP) == INSTRUMENTED_LINE)"
else:
or_special_case = ""
out.emit(
f"#define OPCODE_{name[:-len('_FLAG')]}(OP) "
f"(_PyOpcode_opcode_metadata[OP].flags & ({name}))"
f"(_PyOpcode_opcode_metadata[OP].flags & ({name})){or_special_case}"
)


Expand Down
3 changes: 2 additions & 1 deletion Tools/cases_generator/generate_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ def write_metadata(self, metadata_filename: str, pymetadata_filename: str) -> No
self.out.emit(
"#define IS_VALID_OPCODE(OP) \\\n"
" (((OP) >= 0) && ((OP) < OPCODE_METADATA_SIZE) && \\\n"
" (_PyOpcode_opcode_metadata[(OP)].valid_entry))"
" (_PyOpcode_opcode_metadata[(OP)].valid_entry)) || \\\n"
" ((OP) == INSTRUMENTED_LINE)" # implemented in ceval.c, not bytecodes.c
Copy link
Member Author
@iritkatriel iritkatriel Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to special-case INSTRUMENTED_LINE because it's implemented in ceval.c rather than bytecodes.c, so doesn't appear in the metadata. We could instead do something else like add it to the instr list after bytecode.c is parsed. I'm not sure which option is less bad.

)

self.out.emit("")
Expand Down
0