Skip to content

Commit

Permalink
gh-107557: Setup abstract interpretation (#107847)
Browse files Browse the repository at this point in the history
Co-authored-by: Guido van Rossum <gvanrossum@users.noreply.github.com>
Co-authored-by: Jules <57632293+juliapoo@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 15, 2023
1 parent 34e1917 commit e28b0dc
Show file tree
Hide file tree
Showing 21 changed files with 1,118 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Expand Up @@ -90,6 +90,7 @@ Programs/test_frozenmain.h generated
Python/Python-ast.c generated
Python/executor_cases.c.h generated
Python/generated_cases.c.h generated
Python/abstract_interp_cases.c.h generated
Python/opcode_targets.h generated
Python/stdlib_module_names.h generated
Tools/peg_generator/pegen/grammar_parser.py generated
Expand Down
2 changes: 1 addition & 1 deletion Include/cpython/optimizer.h
Expand Up @@ -22,7 +22,7 @@ typedef struct _PyExecutorObject {
typedef struct _PyOptimizerObject _PyOptimizerObject;

/* Should return > 0 if a new executor is created. O if no executor is produced and < 0 if an error occurred. */
typedef int (*optimize_func)(_PyOptimizerObject* self, PyCodeObject *code, _Py_CODEUNIT *instr, _PyExecutorObject **);
typedef int (*optimize_func)(_PyOptimizerObject* self, PyCodeObject *code, _Py_CODEUNIT *instr, _PyExecutorObject **, int curr_stackentries);

typedef struct _PyOptimizerObject {
PyObject_HEAD
Expand Down
142 changes: 142 additions & 0 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Include/internal/pycore_optimizer.h
@@ -0,0 +1,20 @@
#ifndef Py_INTERNAL_OPTIMIZER_H
#define Py_INTERNAL_OPTIMIZER_H
#ifdef __cplusplus
extern "C" {
#endif

#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif

#include "pycore_uops.h"

int _Py_uop_analyze_and_optimize(PyCodeObject *code,
_PyUOpInstruction *trace, int trace_len, int curr_stackentries);


#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_OPTIMIZER_H */
2 changes: 1 addition & 1 deletion Include/internal/pycore_uops.h
Expand Up @@ -8,7 +8,7 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

#define _Py_UOP_MAX_TRACE_LENGTH 32
#define _Py_UOP_MAX_TRACE_LENGTH 64

typedef struct {
uint32_t opcode;
Expand Down
12 changes: 11 additions & 1 deletion Makefile.pre.in
Expand Up @@ -405,6 +405,7 @@ PYTHON_OBJS= \
Python/mysnprintf.o \
Python/mystrtoul.o \
Python/optimizer.o \
Python/optimizer_analysis.o \
Python/pathconfig.o \
Python/preconfig.o \
Python/pyarena.o \
Expand Down Expand Up @@ -1552,10 +1553,12 @@ regen-cases:
-m $(srcdir)/Include/internal/pycore_opcode_metadata.h.new \
-e $(srcdir)/Python/executor_cases.c.h.new \
-p $(srcdir)/Lib/_opcode_metadata.py.new \
-a $(srcdir)/Python/abstract_interp_cases.c.h.new \
$(srcdir)/Python/bytecodes.c
$(UPDATE_FILE) $(srcdir)/Python/generated_cases.c.h $(srcdir)/Python/generated_cases.c.h.new
$(UPDATE_FILE) $(srcdir)/Include/internal/pycore_opcode_metadata.h $(srcdir)/Include/internal/pycore_opcode_metadata.h.new
$(UPDATE_FILE) $(srcdir)/Python/executor_cases.c.h $(srcdir)/Python/executor_cases.c.h.new
$(UPDATE_FILE) $(srcdir)/Python/abstract_interp_cases.c.h $(srcdir)/Python/abstract_interp_cases.c.h.new
$(UPDATE_FILE) $(srcdir)/Lib/_opcode_metadata.py $(srcdir)/Lib/_opcode_metadata.py.new

Python/compile.o: $(srcdir)/Include/internal/pycore_opcode_metadata.h
Expand All @@ -1568,6 +1571,7 @@ Python/ceval.o: \

Python/executor.o: \
$(srcdir)/Include/internal/pycore_opcode_metadata.h \
$(srcdir)/Include/internal/pycore_optimizer.h \
$(srcdir)/Python/ceval_macros.h \
$(srcdir)/Python/executor_cases.c.h

Expand All @@ -1576,7 +1580,12 @@ Python/flowgraph.o: \

Python/optimizer.o: \
$(srcdir)/Python/executor_cases.c.h \
$(srcdir)/Include/internal/pycore_opcode_metadata.h
$(srcdir)/Include/internal/pycore_opcode_metadata.h \
$(srcdir)/Include/internal/pycore_optimizer.h

Python/optimizer_analysis.o: \
$(srcdir)/Include/internal/pycore_opcode_metadata.h \
$(srcdir)/Include/internal/pycore_optimizer.h

Python/frozen.o: $(FROZEN_FILES_OUT)

Expand Down Expand Up @@ -1786,6 +1795,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/pycore_obmalloc_init.h \
$(srcdir)/Include/internal/pycore_opcode.h \
$(srcdir)/Include/internal/pycore_opcode_utils.h \
$(srcdir)/Include/internal/pycore_optimizer.h \
$(srcdir)/Include/internal/pycore_pathconfig.h \
$(srcdir)/Include/internal/pycore_pyarena.h \
$(srcdir)/Include/internal/pycore_pyerrors.h \
Expand Down
@@ -0,0 +1 @@
Generate the cases needed for the barebones tier 2 abstract interpreter for optimization passes in CPython.
1 change: 1 addition & 0 deletions PCbuild/_freeze_module.vcxproj
Expand Up @@ -218,6 +218,7 @@
<ClCompile Include="..\Python\mysnprintf.c" />
<ClCompile Include="..\Python\mystrtoul.c" />
<ClCompile Include="..\Python\optimizer.c" />
<ClCompile Include="..\Python\optimizer_analysis.c" />
<ClCompile Include="..\Python\pathconfig.c" />
<ClCompile Include="..\Python\perf_trampoline.c" />
<ClCompile Include="..\Python\preconfig.c" />
Expand Down

0 comments on commit e28b0dc

Please sign in to comment.