| 1 |
"""Generate the cases for the tier 2 optimizer. |
| 2 |
Reads the instruction definitions from bytecodes.c and optimizer_bytecodes.c |
| 3 |
Writes the cases to optimizer_cases.c.h, which is #included in Python/optimizer_analysis.c. |
| 4 |
""" |
| 5 |
|
| 6 |
import argparse |
| 7 |
|
| 8 |
from analyzer import ( |
| 9 |
Analysis, |
| 10 |
Instruction, |
| 11 |
Uop, |
| 12 |
analyze_files, |
| 13 |
StackItem, |
| 14 |
analysis_error, |
| 15 |
) |
| 16 |
from generators_common import ( |
| 17 |
DEFAULT_INPUT, |
| 18 |
ROOT, |
| 19 |
write_header, |
| 20 |
Emitter, |
| 21 |
TokenIterator, |
| 22 |
) |
| 23 |
from cwriter import CWriter |
| 24 |
from typing import TextIO |
| 25 |
from lexer import Token |
| 26 |
from stack import Local, Stack, StackError, Storage |
| 27 |
|
| 28 |
DEFAULT_OUTPUT = ROOT / "Python/optimizer_cases.c.h" |
| 29 |
DEFAULT_ABSTRACT_INPUT = (ROOT / "Python/optimizer_bytecodes.c").absolute().as_posix() |
| 30 |
|
| 31 |
|
| 32 |
def validate_uop(override: Uop, uop: Uop) -> None: |
| 33 |
# To do |
| 34 |
pass |
| 35 |
|
| 36 |
|
| 37 |
def type_name(var: StackItem) -> str: |
| 38 |
if var.is_array(): |
| 39 |
return f"JitOptSymbol **" |
| 40 |
if var.type: |
| 41 |
return var.type |
| 42 |
return f"JitOptSymbol *" |
| 43 |
|
| 44 |
|
| 45 |
def declare_variables(uop: Uop, out: CWriter, skip_inputs: bool) -> None: |
| 46 |
variables = {"unused"} |
| 47 |
if not skip_inputs: |
| 48 |
for var in reversed(uop.stack.inputs): |
| 49 |
if var.used and var.name not in variables: |
| 50 |
variables.add(var.name) |
| 51 |
out.emit(f"{type_name(var)}{var.name};\n") |
| 52 |
for var in uop.stack.outputs: |
| 53 |
if var.peek: |
| 54 |
continue |
| 55 |
if var.name not in variables: |
| 56 |
variables.add(var.name) |
| 57 |
out.emit(f"{type_name(var)}{var.name};\n") |
| 58 |
|
| 59 |
|
| 60 |
def decref_inputs( |
| 61 |
out: CWriter, |
| 62 |
tkn: Token, |
| 63 |
tkn_iter: TokenIterator, |
| 64 |
uop: Uop, |
| 65 |
stack: Stack, |
| 66 |
inst: Instruction | None, |
| 67 |
) -> None: |
| 68 |
next(tkn_iter) |
| 69 |
next(tkn_iter) |
| 70 |
next(tkn_iter) |
| 71 |
out.emit_at("", tkn) |
| 72 |
|
| 73 |
|
| 74 |
def emit_default(out: CWriter, uop: Uop, stack: Stack) -> None: |
| 75 |
null = CWriter.null() |
| 76 |
for var in reversed(uop.stack.inputs): |
| 77 |
stack.pop(var, null) |
| 78 |
offset = stack.base_offset - stack.physical_sp |
| 79 |
for var in uop.stack.outputs: |
| 80 |
if var.is_array() and not var.peek and not var.name == "unused": |
| 81 |
c_offset = offset.to_c() |
| 82 |
out.emit(f"{var.name} = &stack_pointer[{c_offset}];\n") |
| 83 |
offset = offset.push(var) |
| 84 |
for var in uop.stack.outputs: |
| 85 |
local = Local.undefined(var) |
| 86 |
stack.push(local) |
| 87 |
if var.name != "unused" and not var.peek: |
| 88 |
local.in_local = True |
| 89 |
if var.is_array(): |
| 90 |
if var.size == "1": |
| 91 |
out.emit(f"{var.name}[0] = sym_new_not_null(ctx);\n") |
| 92 |
else: |
| 93 |
out.emit(f"for (int _i = {var.size}; --_i >= 0;) {{\n") |
| 94 |
out.emit(f"{var.name}[_i] = sym_new_not_null(ctx);\n") |
| 95 |
out.emit("}\n") |
| 96 |
elif var.name == "null": |
| 97 |
out.emit(f"{var.name} = sym_new_null(ctx);\n") |
| 98 |
else: |
| 99 |
out.emit(f"{var.name} = sym_new_not_null(ctx);\n") |
| 100 |
|
| 101 |
|
| 102 |
class OptimizerEmitter(Emitter): |
| 103 |
|
| 104 |
def emit_save(self, storage: Storage) -> None: |
| 105 |
storage.flush(self.out) |
| 106 |
|
| 107 |
def emit_reload(self, storage: Storage) -> None: |
| 108 |
pass |
| 109 |
|
| 110 |
def goto_label(self, goto: Token, label: Token, storage: Storage) -> None: |
| 111 |
self.out.emit(goto) |
| 112 |
self.out.emit(label) |
| 113 |
|
| 114 |
def write_uop( |
| 115 |
override: Uop | None, |
| 116 |
uop: Uop, |
| 117 |
out: CWriter, |
| 118 |
stack: Stack, |
| 119 |
debug: bool, |
| 120 |
skip_inputs: bool, |
| 121 |
) -> None: |
| 122 |
locals: dict[str, Local] = {} |
| 123 |
prototype = override if override else uop |
| 124 |
try: |
| 125 |
out.start_line() |
| 126 |
if override: |
| 127 |
storage = Storage.for_uop(stack, prototype, out, check_liveness=False) |
| 128 |
if debug: |
| 129 |
args = [] |
| 130 |
for input in prototype.stack.inputs: |
| 131 |
if not input.peek or override: |
| 132 |
args.append(input.name) |
| 133 |
out.emit(f'DEBUG_PRINTF({", ".join(args)});\n') |
| 134 |
if override: |
| 135 |
for cache in uop.caches: |
| 136 |
if cache.name != "unused": |
| 137 |
if cache.size == 4: |
| 138 |
type = cast = "PyObject *" |
| 139 |
else: |
| 140 |
type = f"uint{cache.size*16}_t " |
| 141 |
cast = f"uint{cache.size*16}_t" |
| 142 |
out.emit(f"{type}{cache.name} = ({cast})this_instr->operand0;\n") |
| 143 |
if override: |
| 144 |
emitter = OptimizerEmitter(out, {}) |
| 145 |
# No reference management of inputs needed. |
| 146 |
for var in storage.inputs: # type: ignore[possibly-undefined] |
| 147 |
var.in_local = False |
| 148 |
_, storage = emitter.emit_tokens(override, storage, None, False) |
| 149 |
out.start_line() |
| 150 |
storage.flush(out) |
| 151 |
else: |
| 152 |
emit_default(out, uop, stack) |
| 153 |
out.start_line() |
| 154 |
stack.flush(out) |
| 155 |
except StackError as ex: |
| 156 |
raise analysis_error(ex.args[0], prototype.body.open) # from None |
| 157 |
|
| 158 |
|
| 159 |
SKIPS = ("_EXTENDED_ARG",) |
| 160 |
|
| 161 |
|
| 162 |
def generate_abstract_interpreter( |
| 163 |
filenames: list[str], |
| 164 |
abstract: Analysis, |
| 165 |
base: Analysis, |
| 166 |
outfile: TextIO, |
| 167 |
debug: bool, |
| 168 |
) -> None: |
| 169 |
write_header(__file__, filenames, outfile) |
| 170 |
out = CWriter(outfile, 2, False) |
| 171 |
out.emit("\n") |
| 172 |
base_uop_names = set([uop.name for uop in base.uops.values()]) |
| 173 |
for abstract_uop_name in abstract.uops: |
| 174 |
assert ( |
| 175 |
abstract_uop_name in base_uop_names |
| 176 |
), f"All abstract uops should override base uops, but {abstract_uop_name} is not." |
| 177 |
|
| 178 |
for uop in base.uops.values(): |
| 179 |
override: Uop | None = None |
| 180 |
if uop.name in abstract.uops: |
| 181 |
override = abstract.uops[uop.name] |
| 182 |
validate_uop(override, uop) |
| 183 |
if uop.properties.tier == 1: |
| 184 |
continue |
| 185 |
if uop.replicates: |
| 186 |
continue |
| 187 |
if uop.is_super(): |
| 188 |
continue |
| 189 |
if not uop.is_viable(): |
| 190 |
out.emit(f"/* {uop.name} is not a viable micro-op for tier 2 */\n\n") |
| 191 |
continue |
| 192 |
out.emit(f"case {uop.name}: {{\n") |
| 193 |
if override: |
| 194 |
declare_variables(override, out, skip_inputs=False) |
| 195 |
else: |
| 196 |
declare_variables(uop, out, skip_inputs=True) |
| 197 |
stack = Stack(extract_bits=False, cast_type="JitOptSymbol *") |
| 198 |
write_uop(override, uop, out, stack, debug, skip_inputs=(override is None)) |
| 199 |
out.start_line() |
| 200 |
out.emit("break;\n") |
| 201 |
out.emit("}") |
| 202 |
out.emit("\n\n") |
| 203 |
|
| 204 |
|
| 205 |
def generate_tier2_abstract_from_files( |
| 206 |
filenames: list[str], outfilename: str, debug: bool = False |
| 207 |
) -> None: |
| 208 |
assert len(filenames) == 2, "Need a base file and an abstract cases file." |
| 209 |
base = analyze_files([filenames[0]]) |
| 210 |
abstract = analyze_files([filenames[1]]) |
| 211 |
with open(outfilename, "w") as outfile: |
| 212 |
generate_abstract_interpreter(filenames, abstract, base, outfile, debug) |
| 213 |
|
| 214 |
|
| 215 |
arg_parser = argparse.ArgumentParser( |
| 216 |
description="Generate the code for the tier 2 interpreter.", |
| 217 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 218 |
) |
| 219 |
|
| 220 |
arg_parser.add_argument( |
| 221 |
"-o", "--output", type=str, help="Generated code", default=DEFAULT_OUTPUT |
| 222 |
) |
| 223 |
|
| 224 |
|
| 225 |
arg_parser.add_argument("input", nargs="*", help="Abstract interpreter definition file") |
| 226 |
|
| 227 |
arg_parser.add_argument( |
| 228 |
"base", nargs="*", help="The base instruction definition file(s)" |
| 229 |
) |
| 230 |
|
| 231 |
arg_parser.add_argument("-d", "--debug", help="Insert debug calls", action="store_true") |
| 232 |
|
| 233 |
if __name__ == "__main__": |
| 234 |
args = arg_parser.parse_args() |
| 235 |
if not args.input: |
| 236 |
args.base.append(DEFAULT_INPUT) |
| 237 |
args.input.append(DEFAULT_ABSTRACT_INPUT) |
| 238 |
else: |
| 239 |
args.base.append(args.input[-1]) |
| 240 |
args.input.pop() |
| 241 |
abstract = analyze_files(args.input) |
| 242 |
base = analyze_files(args.base) |
| 243 |
with open(args.output, "w") as outfile: |
| 244 |
generate_abstract_interpreter(args.input, abstract, base, outfile, args.debug) |