"""Generate C code for a Python C extension module from Python source code.""" # FIXME: Basically nothing in this file operates on the level of a # single module and it should be renamed. from __future__ import annotations import json import os import sys from collections.abc import Iterable from typing import TypeVar from mypy.build import ( BuildResult, BuildSource, State, build, compute_hash, create_metastore, get_cache_names, sorted_components, ) from mypy.errors import CompileError from mypy.fscache import FileSystemCache from mypy.nodes import MypyFile from mypy.options import Options from mypy.plugin import Plugin, ReportConfigContext from mypy.util import hash_digest, json_dumps from mypyc.analysis.capsule_deps import find_class_dependencies, find_implicit_op_dependencies from mypyc.codegen.cstring import c_string_initializer from mypyc.codegen.emit import ( Emitter, EmitterContext, HeaderDeclaration, c_array_initializer, native_function_doc_initializer, ) from mypyc.codegen.emitclass import generate_class, generate_class_reuse, generate_class_type_decl from mypyc.codegen.emitfunc import generate_native_function, native_function_header from mypyc.codegen.emitwrapper import ( generate_legacy_wrapper_function, generate_wrapper_function, legacy_wrapper_function_header, wrapper_function_header, ) from mypyc.codegen.literals import Literals from mypyc.common import ( EXT_SUFFIX, IS_FREE_THREADED, MODULE_PREFIX, PREFIX, RUNTIME_C_FILES, TOP_LEVEL_NAME, TYPE_VAR_PREFIX, shared_lib_name, short_id_from_name, ) from mypyc.errors import Errors from mypyc.ir.deps import LIBRT_BASE64, LIBRT_STRINGS, LIBRT_TIME, LIBRT_VECS, SourceDep from mypyc.ir.func_ir import FuncIR from mypyc.ir.module_ir import ModuleIR, ModuleIRs, deserialize_modules from mypyc.ir.ops import DeserMaps, LoadLiteral from mypyc.ir.rtypes import RType from mypyc.irbuild.main import build_ir from mypyc.irbuild.mapper import Mapper from mypyc.irbuild.prepare import load_type_map from mypyc.namegen import NameGenerator, exported_name from mypyc.options import CompilerOptions from mypyc.transform.copy_propagation import do_copy_propagation from mypyc.transform.exceptions import insert_exception_handling from mypyc.transform.flag_elimination import do_flag_elimination from mypyc.transform.log_trace import insert_event_trace_logging from mypyc.transform.lower import lower_ir from mypyc.transform.refcount import insert_ref_count_opcodes from mypyc.transform.spill import insert_spills from mypyc.transform.uninit import insert_uninit_checks # All the modules being compiled are divided into "groups". A group # is a set of modules that are placed into the same shared library. # Two common configurations are that every module is placed in a group # by itself (fully separate compilation) and that every module is # placed in the same group (fully whole-program compilation), but we # support finer-grained control of the group as well. # # In fully whole-program compilation, we will generate N+1 extension # modules: one shim per module and one shared library containing all # the actual code. # In fully separate compilation, we (unfortunately) will generate 2*N # extension modules: one shim per module and also one library containing # each module's actual code. (This might be fixable in the future, # but allows a clean separation between setup of the export tables # (see generate_export_table) and running module top levels.) # # A group is represented as a list of BuildSources containing all of # its modules along with the name of the group. (Which can be None # only if we are compiling only a single group with a single file in it # and not using shared libraries). Group = tuple[list[BuildSource], str | None] Groups = list[Group] # A list of (file name, file contents) pairs. FileContents = list[tuple[str, str]] class MarkedDeclaration: """Add a mark, useful for topological sort.""" def __init__(self, declaration: HeaderDeclaration, mark: bool) -> None: self.declaration = declaration self.mark = False class MypycPlugin(Plugin): """Plugin for making mypyc interoperate properly with mypy incremental mode. Basically the point of this plugin is to force mypy to recheck things based on the demands of mypyc in a couple situations: * Any modules in the same group must be compiled together, so we tell mypy that modules depend on all their groupmates. * If the IR metadata is missing or stale or any of the generated C source files associated missing or stale, then we need to recompile the module so we mark it as stale. """ def __init__( self, options: Options, compiler_options: CompilerOptions, groups: Groups ) -> None: super().__init__(options) self.group_map: dict[str, tuple[str | None, list[str]]] = {} for sources, name in groups: modules = sorted(source.module for source in sources) for id in modules: self.group_map[id] = (name, modules) self.compiler_options = compiler_options self.metastore = create_metastore(options, parallel_worker=False) def report_config_data(self, ctx: ReportConfigContext) -> tuple[str | None, list[str]] | None: # The config data we report is the group map entry for the module. # If the data is being used to check validity, we do additional checks # that the IR cache exists and matches the metadata cache and all # output source files exist and are up to date. id, path, is_check = ctx.id, ctx.path, ctx.is_check if id not in self.group_map: return None # If we aren't doing validity checks, just return the cache data if not is_check: return self.group_map[id] # Load the metadata and IR cache meta_path, _, _ = get_cache_names(id, path, self.options) ir_path = get_ir_cache_name(id, path, self.options) try: meta_json = self.metastore.read(meta_path) ir_json = self.metastore.read(ir_path) except FileNotFoundError: # This could happen if mypyc failed after mypy succeeded # in the previous run or if some cache files got # deleted. No big deal, just fail to load the cache. return None ir_data = json.loads(ir_json) # Check that the IR cache matches the metadata cache if hash_digest(meta_json) != ir_data["meta_hash"]: return None # Check that all the source files are present and as # expected. The main situation where this would come up is the # user deleting the build directory without deleting # .mypy_cache, which we should handle gracefully. for path, hash in ir_data["src_hashes"].items(): try: with open(os.path.join(self.compiler_options.target_dir, path), "rb") as f: contents = f.read() except FileNotFoundError: return None real_hash = hash_digest(contents) if hash != real_hash: return None return self.group_map[id] def get_additional_deps(self, file: MypyFile) -> list[tuple[int, str, int]]: # Report dependency on modules in the module's group return [(10, id, -1) for id in self.group_map.get(file.fullname, (None, []))[1]] def parse_and_typecheck( sources: list[BuildSource], options: Options, compiler_options: CompilerOptions, groups: Groups, fscache: FileSystemCache | None = None, alt_lib_path: str | None = None, ) -> BuildResult: assert options.strict_optional, "strict_optional must be turned on" mypyc_plugin = MypycPlugin(options, compiler_options, groups) result = build( sources=sources, options=options, alt_lib_path=alt_lib_path, fscache=fscache, extra_plugins=[mypyc_plugin], ) mypyc_plugin.metastore.close() if result.errors: raise CompileError(result.errors) return result def compile_scc_to_ir( scc: list[MypyFile], result: BuildResult, mapper: Mapper, compiler_options: CompilerOptions, errors: Errors, ) -> ModuleIRs: """Compile an SCC into ModuleIRs. Any modules that this SCC depends on must have either been compiled, type checked, or loaded from a cache into mapper. Arguments: scc: The list of MypyFiles to compile result: The BuildResult from the mypy front-end mapper: The Mapper object mapping mypy ASTs to class and func IRs compiler_options: The compilation options errors: Where to report any errors encountered Returns the IR of the modules. """ if compiler_options.verbose: print("Compiling {}".format(", ".join(x.name for x in scc))) # Generate basic IR, with missing exception and refcount handling. modules = build_ir(scc, result.graph, result.types, mapper, compiler_options, errors) if errors.num_errors > 0: return modules env_user_functions = {} for module in modules.values(): for cls in module.classes: if cls.env_user_function: env_user_functions[cls.env_user_function] = cls for module in modules.values(): for fn in module.functions: # Insert checks for uninitialized values. insert_uninit_checks(fn, compiler_options.strict_traceback_checks) # Insert exception handling. insert_exception_handling(fn, compiler_options.strict_traceback_checks) # Insert reference count handling. insert_ref_count_opcodes(fn) if fn in env_user_functions: insert_spills(fn, env_user_functions[fn]) if compiler_options.log_trace: insert_event_trace_logging(fn, compiler_options) # Switch to lower abstraction level IR. lower_ir(fn, compiler_options) # Calculate implicit module dependencies (needed for librt) deps = find_implicit_op_dependencies(fn) if deps is not None: module.dependencies.update(deps) # Perform optimizations. do_copy_propagation(fn, compiler_options) do_flag_elimination(fn, compiler_options) # Calculate implicit dependencies from class attribute types for cl in module.classes: deps = find_class_dependencies(cl) if deps is not None: module.dependencies.update(deps) return modules def compile_modules_to_ir( result: BuildResult, mapper: Mapper, compiler_options: CompilerOptions, errors: Errors ) -> ModuleIRs: """Compile a collection of modules into ModuleIRs. The modules to compile are specified as part of mapper's group_map. Returns the IR of the modules. """ deser_ctx = DeserMaps({}, {}) modules = {} # Process the graph by SCC in topological order, like we do in mypy.build for scc in sorted_components(result.graph): scc_states = [result.graph[id] for id in scc.mod_ids] trees = [st.tree for st in scc_states if st.id in mapper.group_map and st.tree] if not trees: continue fresh = all(id not in result.manager.rechecked_modules for id in scc.mod_ids) if fresh: load_scc_from_cache(trees, result, mapper, deser_ctx) else: scc_ir = compile_scc_to_ir(trees, result, mapper, compiler_options, errors) modules.update(scc_ir) return modules def compile_ir_to_c( groups: Groups, modules: ModuleIRs, result: BuildResult, mapper: Mapper, compiler_options: CompilerOptions, ) -> dict[str | None, list[tuple[str, str]]]: """Compile a collection of ModuleIRs to C source text. Returns a dictionary mapping group names to a list of (file name, file text) pairs. """ source_paths = { source.module: result.graph[source.module].xpath for sources, _ in groups for source in sources } names = NameGenerator( [[source.module for source in sources] for sources, _ in groups], separate=compiler_options.separate, ) # Generate C code for each compilation group. Each group will be # compiled into a separate extension module. ctext: dict[str | None, list[tuple[str, str]]] = {} for group_sources, group_name in groups: group_modules = { source.module: modules[source.module] for source in group_sources if source.module in modules } if not group_modules: ctext[group_name] = [] continue generator = GroupGenerator( group_modules, source_paths, group_name, mapper.group_map, names, compiler_options ) ctext[group_name] = generator.generate_c_for_modules() return ctext def get_ir_cache_name(id: str, path: str, options: Options) -> str: meta_path, _, _ = get_cache_names(id, path, options) # Mypyc uses JSON cache even with --fixed-format-cache (for now). return meta_path.replace(".meta.json", ".ir.json").replace(".meta.ff", ".ir.json") def get_state_ir_cache_name(state: State) -> str: return get_ir_cache_name(state.id, state.xpath, state.options) def write_cache( modules: ModuleIRs, result: BuildResult, group_map: dict[str, str | None], ctext: dict[str | None, list[tuple[str, str]]], ) -> None: """Write out the cache information for modules. Each module has the following cache information written (which is in addition to the cache information written by mypy itself): * A serialized version of its mypyc IR, minus the bodies of functions. This allows code that depends on it to use these serialized data structures when compiling against it instead of needing to recompile it. (Compiling against a module requires access to both its mypy and mypyc data structures.) * The hash of the mypy metadata cache file for the module. This is used to ensure that the mypyc cache and the mypy cache are in sync and refer to the same version of the code. This is particularly important if mypyc crashes/errors/is stopped after mypy has written its cache but before mypyc has. * The hashes of all the source file outputs for the group the module is in. This is so that the module will be recompiled if the source outputs are missing. """ hashes = {} for name, files in ctext.items(): hashes[name] = {file: compute_hash(data) for file, data in files} # Write out cache data for id, module in modules.items(): st = result.graph[id] meta_path, _, _ = get_cache_names(id, st.xpath, result.manager.options) # If the metadata isn't there, skip writing the cache. try: meta_data = result.manager.metastore.read(meta_path) except OSError: continue newpath = get_state_ir_cache_name(st) ir_data = { "ir": module.serialize(), "meta_hash": hash_digest(meta_data), "src_hashes": hashes[group_map[id]], } result.manager.metastore.write(newpath, json_dumps(ir_data)) result.manager.metastore.commit() def load_scc_from_cache( scc: list[MypyFile], result: BuildResult, mapper: Mapper, ctx: DeserMaps ) -> ModuleIRs: """Load IR for an SCC of modules from the cache. Arguments and return are as compile_scc_to_ir. """ cache_data = { k.fullname: json.loads( result.manager.metastore.read(get_state_ir_cache_name(result.graph[k.fullname])) )["ir"] for k in scc } modules = deserialize_modules(cache_data, ctx) load_type_map(mapper, scc, ctx) return modules def collect_source_dependencies(modules: dict[str, ModuleIR]) -> set[SourceDep]: """Collect all SourceDep dependencies from all modules.""" source_deps: set[SourceDep] = set() for module in modules.values(): for dep in module.dependencies: if isinstance(dep, SourceDep): source_deps.add(dep) return source_deps def compile_modules_to_c( result: BuildResult, compiler_options: CompilerOptions, errors: Errors, groups: Groups ) -> tuple[ModuleIRs, list[FileContents], Mapper]: """Compile Python module(s) to the source of Python C extension modules. This generates the source code for the "shared library" module for each group. The shim modules are generated in mypyc.build. Each shared library module provides, for each module in its group, a PyCapsule containing an initialization function. Additionally, it provides a capsule containing an export table of pointers to all the group's functions and static variables. Arguments: result: The BuildResult from the mypy front-end compiler_options: The compilation options errors: Where to report any errors encountered groups: The groups that we are compiling. See documentation of Groups type above. Returns the IR of the modules and a list containing the generated files for each group. """ # Construct a map from modules to what group they belong to group_map = {source.module: lib_name for group, lib_name in groups for source in group} mapper = Mapper(group_map) # Sometimes when we call back into mypy, there might be errors. # We don't want to crash when that happens. result.manager.errors.set_file( "", module=None, scope=None, options=result.manager.options ) modules = compile_modules_to_ir(result, mapper, compiler_options, errors) if errors.num_errors > 0: return {}, [], Mapper({}) ctext = compile_ir_to_c(groups, modules, result, mapper, compiler_options) write_cache(modules, result, group_map, ctext) return modules, [ctext[name] for _, name in groups], mapper def generate_function_declaration(fn: FuncIR, emitter: Emitter) -> None: emitter.context.declarations[emitter.native_function_name(fn.decl)] = HeaderDeclaration( f"{native_function_header(fn.decl, emitter)};", needs_export=True ) if fn.name != TOP_LEVEL_NAME and not fn.internal: if is_fastcall_supported(fn, emitter.capi_version): emitter.context.declarations[PREFIX + fn.cname(emitter.names)] = HeaderDeclaration( f"{wrapper_function_header(fn, emitter.names)};" ) else: emitter.context.declarations[PREFIX + fn.cname(emitter.names)] = HeaderDeclaration( f"{legacy_wrapper_function_header(fn, emitter.names)};" ) def pointerize(decl: str, name: str) -> str: """Given a C decl and its name, modify it to be a declaration to a pointer.""" # This doesn't work in general but does work for all our types... if "(" in decl: # Function pointer. Stick an * in front of the name and wrap it in parens. return decl.replace(name, f"(*{name})") else: # Non-function pointer. Just stick an * in front of the name. return decl.replace(name, f"*{name}") def group_dir(group_name: str) -> str: """Given a group name, return the relative directory path for it.""" return os.sep.join(group_name.split(".")[:-1]) class GroupGenerator: def __init__( self, modules: dict[str, ModuleIR], source_paths: dict[str, str], group_name: str | None, group_map: dict[str, str | None], names: NameGenerator, compiler_options: CompilerOptions, ) -> None: """Generator for C source for a compilation group. The code for a compilation group contains an internal and an external .h file, and then one .c if not in multi_file mode or one .c file per module if in multi_file mode. Arguments: modules: (name, ir) pairs for each module in the group source_paths: Map from module names to source file paths group_name: The name of the group (or None if this is single-module compilation) group_map: A map of modules to their group names names: The name generator for the compilation compiler_options: Mypyc specific options, including multi_file mode """ self.modules = modules self.source_paths = source_paths self.context = EmitterContext( names, compiler_options.strict_traceback_checks, group_name, group_map ) self.names = names # Initializations of globals to simple values that we can't # do statically because the windows loader is bad. self.simple_inits: list[tuple[str, str]] = [] self.group_name = group_name self.use_shared_lib = group_name is not None self.compiler_options = compiler_options self.multi_file = compiler_options.multi_file # Multi-phase init is needed to enable free-threading. In the future we'll # probably want to enable it always, but we'll wait until it's stable. self.multi_phase_init = IS_FREE_THREADED @property def group_suffix(self) -> str: return "_" + exported_name(self.group_name) if self.group_name else "" @property def short_group_suffix(self) -> str: return "_" + exported_name(self.group_name.split(".")[-1]) if self.group_name else "" def generate_c_for_modules(self) -> list[tuple[str, str]]: file_contents = [] multi_file = self.use_shared_lib and self.multi_file # Collect all literal refs in IR. for module in self.modules.values(): for fn in module.functions: collect_literals(fn, self.context.literals) base_emitter = Emitter(self.context) # Optionally just include the runtime library c files to # reduce the number of compiler invocations needed if self.compiler_options.include_runtime_files: for name in RUNTIME_C_FILES: base_emitter.emit_line(f'#include "{name}"') # Include conditional source files source_deps = collect_source_dependencies(self.modules) for source_dep in sorted(source_deps, key=lambda d: d.path): base_emitter.emit_line(f'#include "{source_dep.path}"') base_emitter.emit_line(f'#include "__native{self.short_group_suffix}.h"') base_emitter.emit_line(f'#include "__native_internal{self.short_group_suffix}.h"') emitter = base_emitter self.generate_literal_tables() for module_name, module in self.modules.items(): if multi_file: emitter = Emitter(self.context, filepath=self.source_paths[module_name]) emitter.emit_line(f'#include "__native{self.short_group_suffix}.h"') emitter.emit_line(f'#include "__native_internal{self.short_group_suffix}.h"') self.declare_module(module_name, emitter) self.declare_internal_globals(module_name, emitter) self.declare_imports(module.imports, emitter) for cl in module.classes: if cl.is_ext_class: generate_class(cl, module_name, emitter) # Generate Python extension module definitions and module initialization functions. self.generate_module_def(emitter, module_name, module) for fn in module.functions: emitter.emit_line() generate_native_function(fn, emitter, self.source_paths[module_name], module_name) if fn.name != TOP_LEVEL_NAME and not fn.internal: emitter.emit_line() if is_fastcall_supported(fn, emitter.capi_version): generate_wrapper_function( fn, emitter, self.source_paths[module_name], module_name ) else: generate_legacy_wrapper_function( fn, emitter, self.source_paths[module_name], module_name ) if multi_file: name = f"__native_{exported_name(module_name)}.c" file_contents.append((name, "".join(emitter.fragments))) # The external header file contains type declarations while # the internal contains declarations of functions and objects # (which are shared between shared libraries via dynamic # exports tables and not accessed directly.) ext_declarations = Emitter(self.context) ext_declarations.emit_line(f"#ifndef MYPYC_NATIVE{self.group_suffix}_H") ext_declarations.emit_line(f"#define MYPYC_NATIVE{self.group_suffix}_H") ext_declarations.emit_line("#include ") ext_declarations.emit_line("#include ") if self.compiler_options.depends_on_librt_internal: ext_declarations.emit_line("#include ") if any(LIBRT_BASE64 in mod.dependencies for mod in self.modules.values()): ext_declarations.emit_line("#include ") if any(LIBRT_STRINGS in mod.dependencies for mod in self.modules.values()): ext_declarations.emit_line("#include ") if any(LIBRT_TIME in mod.dependencies for mod in self.modules.values()): ext_declarations.emit_line("#include