Skip to content

Commit 0a1fedb

Browse files
authored
gh-126835: Rename ast_opt.c to ast_preprocess.c and related stuff after moving const folding to the peephole optimizier (#131830)
1 parent 8eaaf16 commit 0a1fedb

14 files changed

+59
-57
lines changed

.github/CODEOWNERS

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Include/internal/pycore_time.h @pganssle @abalkin
188188

189189
# AST
190190
Python/ast.c @isidentical @JelleZijlstra @eclips4
191-
Python/ast_opt.c @isidentical @eclips4
191+
Python/ast_preprocess.c @isidentical @eclips4
192192
Parser/asdl.py @isidentical @JelleZijlstra @eclips4
193193
Parser/asdl_c.py @isidentical @JelleZijlstra @eclips4
194194
Lib/ast.py @isidentical @JelleZijlstra @eclips4

Include/internal/pycore_compile.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ PyAPI_FUNC(PyCodeObject*) _PyAST_Compile(
3434
int optimize,
3535
struct _arena *arena);
3636

37-
/* AST optimizations */
38-
extern int _PyCompile_AstOptimize(
37+
/* AST preprocessing */
38+
extern int _PyCompile_AstPreprocess(
3939
struct _mod *mod,
4040
PyObject *filename,
4141
PyCompilerFlags *flags,
4242
int optimize,
4343
struct _arena *arena,
4444
int syntax_check_only);
4545

46-
extern int _PyAST_Optimize(
46+
extern int _PyAST_Preprocess(
4747
struct _mod *,
4848
struct _arena *arena,
4949
PyObject *filename,

InternalDocs/compiler.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,8 @@ Important files
505505
* [Python/ast.c](../Python/ast.c):
506506
Used for validating the AST.
507507

508-
* [Python/ast_opt.c](../Python/ast_opt.c):
509-
Optimizes the AST.
508+
* [Python/ast_preprocess.c](../Python/ast_preprocess.c):
509+
Preprocesses the AST before compiling.
510510

511511
* [Python/ast_unparse.c](../Python/ast_unparse.c):
512512
Converts the AST expression node back into a string (for string annotations).

Makefile.pre.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ PYTHON_OBJS= \
426426
Python/asdl.o \
427427
Python/assemble.o \
428428
Python/ast.o \
429-
Python/ast_opt.o \
429+
Python/ast_preprocess.o \
430430
Python/ast_unparse.o \
431431
Python/bltinmodule.o \
432432
Python/brc.o \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Move constant folding to the peephole optimizer. Rename AST optimization
2+
related files (Python/ast_opt.c -> Python/ast_preprocess.c), structs (_PyASTOptimizeState -> _PyASTPreprocessState)
3+
and functions (_PyAST_Optimize -> _PyAST_Preprocess, _PyCompile_AstOptimize -> _PyCompile_AstPreprocess).

PCbuild/_freeze_module.vcxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
<ClCompile Include="..\Python\asdl.c" />
191191
<ClCompile Include="..\Python\assemble.c" />
192192
<ClCompile Include="..\Python\ast.c" />
193-
<ClCompile Include="..\Python\ast_opt.c" />
193+
<ClCompile Include="..\Python\ast_preprocess.c" />
194194
<ClCompile Include="..\Python\ast_unparse.c" />
195195
<ClCompile Include="..\Python\bltinmodule.c" />
196196
<ClCompile Include="..\Python\brc.c" />

PCbuild/_freeze_module.vcxproj.filters

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<ClCompile Include="..\Python\ast.c">
3535
<Filter>Source Files</Filter>
3636
</ClCompile>
37-
<ClCompile Include="..\Python\ast_opt.c">
37+
<ClCompile Include="..\Python\ast_preprocess.c">
3838
<Filter>Source Files</Filter>
3939
</ClCompile>
4040
<ClCompile Include="..\Python\ast_unparse.c">

PCbuild/pythoncore.vcxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@
580580
<ClCompile Include="..\Python\asdl.c" />
581581
<ClCompile Include="..\Python\assemble.c" />
582582
<ClCompile Include="..\Python\ast.c" />
583-
<ClCompile Include="..\Python\ast_opt.c" />
583+
<ClCompile Include="..\Python\ast_preprocess.c" />
584584
<ClCompile Include="..\Python\ast_unparse.c" />
585585
<ClCompile Include="..\Python\bltinmodule.c" />
586586
<ClCompile Include="..\Python\bootstrap_hash.c" />

PCbuild/pythoncore.vcxproj.filters

+1-1
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@
13251325
<ClCompile Include="..\Python\ast.c">
13261326
<Filter>Python</Filter>
13271327
</ClCompile>
1328-
<ClCompile Include="..\Python\ast_opt.c">
1328+
<ClCompile Include="..\Python\ast_preprocess.c">
13291329
<Filter>Python</Filter>
13301330
</ClCompile>
13311331
<ClCompile Include="..\Python\ast_unparse.c">

Python/ast_opt.c renamed to Python/ast_preprocess.c

+39-39
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* AST Optimizer */
1+
/* AST pre-processing */
22
#include "Python.h"
33
#include "pycore_ast.h" // _PyAST_GetDocString()
44
#include "pycore_c_array.h" // _Py_CArray_EnsureCapacity()
@@ -22,7 +22,7 @@ typedef struct {
2222

2323
_Py_c_array_t cf_finally; /* context for PEP 765 check */
2424
int cf_finally_used;
25-
} _PyASTOptimizeState;
25+
} _PyASTPreprocessState;
2626

2727
#define ENTER_RECURSIVE() \
2828
if (Py_EnterRecursiveCall(" during compilation")) { \
@@ -32,14 +32,14 @@ if (Py_EnterRecursiveCall(" during compilation")) { \
3232
#define LEAVE_RECURSIVE() Py_LeaveRecursiveCall();
3333

3434
static ControlFlowInFinallyContext*
35-
get_cf_finally_top(_PyASTOptimizeState *state)
35+
get_cf_finally_top(_PyASTPreprocessState *state)
3636
{
3737
int idx = state->cf_finally_used;
3838
return ((ControlFlowInFinallyContext*)state->cf_finally.array) + idx;
3939
}
4040

4141
static int
42-
push_cf_context(_PyASTOptimizeState *state, stmt_ty node, bool finally, bool funcdef, bool loop)
42+
push_cf_context(_PyASTPreprocessState *state, stmt_ty node, bool finally, bool funcdef, bool loop)
4343
{
4444
if (_Py_CArray_EnsureCapacity(&state->cf_finally, state->cf_finally_used+1) < 0) {
4545
return 0;
@@ -55,14 +55,14 @@ push_cf_context(_PyASTOptimizeState *state, stmt_ty node, bool finally, bool fun
5555
}
5656

5757
static void
58-
pop_cf_context(_PyASTOptimizeState *state)
58+
pop_cf_context(_PyASTPreprocessState *state)
5959
{
6060
assert(state->cf_finally_used > 0);
6161
state->cf_finally_used--;
6262
}
6363

6464
static int
65-
control_flow_in_finally_warning(const char *kw, stmt_ty n, _PyASTOptimizeState *state)
65+
control_flow_in_finally_warning(const char *kw, stmt_ty n, _PyASTPreprocessState *state)
6666
{
6767
PyObject *msg = PyUnicode_FromFormat("'%s' in a 'finally' block", kw);
6868
if (msg == NULL) {
@@ -76,7 +76,7 @@ control_flow_in_finally_warning(const char *kw, stmt_ty n, _PyASTOptimizeState *
7676
}
7777

7878
static int
79-
before_return(_PyASTOptimizeState *state, stmt_ty node_)
79+
before_return(_PyASTPreprocessState *state, stmt_ty node_)
8080
{
8181
if (state->cf_finally_used > 0) {
8282
ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
@@ -90,7 +90,7 @@ before_return(_PyASTOptimizeState *state, stmt_ty node_)
9090
}
9191

9292
static int
93-
before_loop_exit(_PyASTOptimizeState *state, stmt_ty node_, const char *kw)
93+
before_loop_exit(_PyASTPreprocessState *state, stmt_ty node_, const char *kw)
9494
{
9595
if (state->cf_finally_used > 0) {
9696
ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
@@ -365,7 +365,7 @@ optimize_format(expr_ty node, PyObject *fmt, asdl_expr_seq *elts, PyArena *arena
365365
}
366366

367367
static int
368-
fold_binop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
368+
fold_binop(expr_ty node, PyArena *arena, _PyASTPreprocessState *state)
369369
{
370370
if (state->syntax_check_only) {
371371
return 1;
@@ -389,18 +389,18 @@ fold_binop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
389389
return 1;
390390
}
391391

392-
static int astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
393-
static int astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
394-
static int astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
395-
static int astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
396-
static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
397-
static int astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
398-
static int astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
399-
static int astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
400-
static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
401-
static int astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
402-
static int astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
403-
static int astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
392+
static int astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
393+
static int astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
394+
static int astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
395+
static int astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
396+
static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
397+
static int astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
398+
static int astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
399+
static int astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
400+
static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
401+
static int astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
402+
static int astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
403+
static int astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
404404

405405
#define CALL(FUNC, TYPE, ARG) \
406406
if (!FUNC((ARG), ctx_, state)) \
@@ -436,7 +436,7 @@ stmt_seq_remove_item(asdl_stmt_seq *stmts, Py_ssize_t idx)
436436
}
437437

438438
static int
439-
astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTOptimizeState *state)
439+
astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTPreprocessState *state)
440440
{
441441
int docstring = _PyAST_GetDocString(stmts) != NULL;
442442
if (docstring && (state->optimize >= 2)) {
@@ -466,7 +466,7 @@ astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTOptimizeState *state)
466466
}
467467

468468
static int
469-
astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
469+
astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
470470
{
471471
switch (node_->kind) {
472472
case Module_kind:
@@ -488,7 +488,7 @@ astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
488488
}
489489

490490
static int
491-
astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
491+
astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
492492
{
493493
ENTER_RECURSIVE();
494494
switch (node_->kind) {
@@ -613,14 +613,14 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
613613
}
614614

615615
static int
616-
astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
616+
astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
617617
{
618618
CALL(astfold_expr, expr_ty, node_->value);
619619
return 1;
620620
}
621621

622622
static int
623-
astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
623+
astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
624624
{
625625
CALL(astfold_expr, expr_ty, node_->target);
626626
CALL(astfold_expr, expr_ty, node_->iter);
@@ -629,7 +629,7 @@ astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState
629629
}
630630

631631
static int
632-
astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
632+
astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
633633
{
634634
CALL_SEQ(astfold_arg, arg, node_->posonlyargs);
635635
CALL_SEQ(astfold_arg, arg, node_->args);
@@ -642,7 +642,7 @@ astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
642642
}
643643

644644
static int
645-
astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
645+
astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
646646
{
647647
if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
648648
CALL_OPT(astfold_expr, expr_ty, node_->annotation);
@@ -651,7 +651,7 @@ astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
651651
}
652652

653653
static int
654-
astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
654+
astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
655655
{
656656
ENTER_RECURSIVE();
657657
switch (node_->kind) {
@@ -806,7 +806,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
806806
}
807807

808808
static int
809-
astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
809+
astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
810810
{
811811
switch (node_->kind) {
812812
case ExceptHandler_kind:
@@ -820,15 +820,15 @@ astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState
820820
}
821821

822822
static int
823-
astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
823+
astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
824824
{
825825
CALL(astfold_expr, expr_ty, node_->context_expr);
826826
CALL_OPT(astfold_expr, expr_ty, node_->optional_vars);
827827
return 1;
828828
}
829829

830830
static int
831-
fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTOptimizeState *state)
831+
fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTPreprocessState *state)
832832
{
833833
if (state->syntax_check_only) {
834834
return 1;
@@ -869,7 +869,7 @@ fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTOptimizeState *stat
869869
}
870870

871871
static int
872-
astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
872+
astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
873873
{
874874
// Currently, this is really only used to form complex/negative numeric
875875
// constants in MatchValue and MatchMapping nodes
@@ -911,7 +911,7 @@ astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
911911
}
912912

913913
static int
914-
astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
914+
astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
915915
{
916916
CALL(astfold_pattern, expr_ty, node_->pattern);
917917
CALL_OPT(astfold_expr, expr_ty, node_->guard);
@@ -920,7 +920,7 @@ astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
920920
}
921921

922922
static int
923-
astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
923+
astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
924924
{
925925
switch (node_->kind) {
926926
case TypeVar_kind:
@@ -942,11 +942,11 @@ astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
942942
#undef CALL_SEQ
943943

944944
int
945-
_PyAST_Optimize(mod_ty mod, PyArena *arena, PyObject *filename, int optimize,
946-
int ff_features, int syntax_check_only)
945+
_PyAST_Preprocess(mod_ty mod, PyArena *arena, PyObject *filename, int optimize,
946+
int ff_features, int syntax_check_only)
947947
{
948-
_PyASTOptimizeState state;
949-
memset(&state, 0, sizeof(_PyASTOptimizeState));
948+
_PyASTPreprocessState state;
949+
memset(&state, 0, sizeof(_PyASTPreprocessState));
950950
state.filename = filename;
951951
state.optimize = optimize;
952952
state.ff_features = ff_features;

Python/bltinmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
845845
goto error;
846846
}
847847
int syntax_check_only = ((flags & PyCF_OPTIMIZED_AST) == PyCF_ONLY_AST); /* unoptiomized AST */
848-
if (_PyCompile_AstOptimize(mod, filename, &cf, optimize,
848+
if (_PyCompile_AstPreprocess(mod, filename, &cf, optimize,
849849
arena, syntax_check_only) < 0) {
850850
_PyArena_Free(arena);
851851
goto error;

Python/compile.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ compiler_setup(compiler *c, mod_ty mod, PyObject *filename,
135135
c->c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
136136
c->c_save_nested_seqs = false;
137137

138-
if (!_PyAST_Optimize(mod, arena, filename, c->c_optimize, merged, 0)) {
138+
if (!_PyAST_Preprocess(mod, arena, filename, c->c_optimize, merged, 0)) {
139139
return ERROR;
140140
}
141141
c->c_st = _PySymtable_Build(mod, filename, &c->c_future);
@@ -1481,8 +1481,8 @@ _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags,
14811481
}
14821482

14831483
int
1484-
_PyCompile_AstOptimize(mod_ty mod, PyObject *filename, PyCompilerFlags *cf,
1485-
int optimize, PyArena *arena, int no_const_folding)
1484+
_PyCompile_AstPreprocess(mod_ty mod, PyObject *filename, PyCompilerFlags *cf,
1485+
int optimize, PyArena *arena, int no_const_folding)
14861486
{
14871487
_PyFutureFeatures future;
14881488
if (!_PyFuture_FromAST(mod, filename, &future)) {
@@ -1492,7 +1492,7 @@ _PyCompile_AstOptimize(mod_ty mod, PyObject *filename, PyCompilerFlags *cf,
14921492
if (optimize == -1) {
14931493
optimize = _Py_GetConfig()->optimization_level;
14941494
}
1495-
if (!_PyAST_Optimize(mod, arena, filename, optimize, flags, no_const_folding)) {
1495+
if (!_PyAST_Preprocess(mod, arena, filename, optimize, flags, no_const_folding)) {
14961496
return -1;
14971497
}
14981498
return 0;

Python/pythonrun.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,7 @@ Py_CompileStringObject(const char *str, PyObject *filename, int start,
14981498
}
14991499
if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
15001500
int syntax_check_only = ((flags->cf_flags & PyCF_OPTIMIZED_AST) == PyCF_ONLY_AST); /* unoptiomized AST */
1501-
if (_PyCompile_AstOptimize(mod, filename, flags, optimize, arena, syntax_check_only) < 0) {
1501+
if (_PyCompile_AstPreprocess(mod, filename, flags, optimize, arena, syntax_check_only) < 0) {
15021502
_PyArena_Free(arena);
15031503
return NULL;
15041504
}

Tools/c-analyzer/cpython/ignored.tsv

-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ Objects/unicodeobject.c unicode_translate_call_errorhandler argparse -
349349
Parser/parser.c - reserved_keywords -
350350
Parser/parser.c - soft_keywords -
351351
Parser/lexer/lexer.c - type_comment_prefix -
352-
Python/ast_opt.c fold_unaryop ops -
353352
Python/ceval.c - _PyEval_BinaryOps -
354353
Python/ceval.c - _Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS -
355354
Python/codecs.c - Py_hexdigits -

0 commit comments

Comments
 (0)