|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include "cinderx/Jit/hir/hir.h" |
| 6 | + |
| 7 | +namespace jit::hir { |
| 8 | + |
| 9 | +class Function { |
| 10 | + public: |
| 11 | + using InlineFailureStats = |
| 12 | + UnorderedMap<InlineFailureType, UnorderedSet<std::string>>; |
| 13 | + Function(); |
| 14 | + ~Function(); |
| 15 | + |
| 16 | + ThreadedRef<PyCodeObject> code; |
| 17 | + ThreadedRef<PyDictObject> builtins; |
| 18 | + ThreadedRef<PyDictObject> globals; |
| 19 | + |
| 20 | + // for primitive args only, null if there are none |
| 21 | + ThreadedRef<_PyTypedArgsInfo> prim_args_info; |
| 22 | + |
| 23 | + // Fully-qualified name of the function |
| 24 | + std::string fullname; |
| 25 | + |
| 26 | + // Does this function need its PyFunctionObject* at runtime? |
| 27 | + // (This is always the case in 3.12 as it is used to quickly access the |
| 28 | + // _PyInterpreterFrame) |
| 29 | + bool uses_runtime_func{ |
| 30 | +#if PY_VERSION_HEX < 0x030C0000 |
| 31 | + false |
| 32 | +#else |
| 33 | + true |
| 34 | +#endif |
| 35 | + }; |
| 36 | + |
| 37 | + // Does this function have primitive args? |
| 38 | + bool has_primitive_args{false}; |
| 39 | + |
| 40 | + // is the first argument a primitive? |
| 41 | + bool has_primitive_first_arg{false}; |
| 42 | + |
| 43 | + struct InlineFunctionStats { |
| 44 | + int num_inlined_functions{0}; |
| 45 | + // map of {inline_failure_type -> function_names} |
| 46 | + InlineFailureStats failure_stats; |
| 47 | + } inline_function_stats; |
| 48 | + |
| 49 | + // vector of {locals_idx, type, optional} |
| 50 | + // in argument order, may have gaps for unchecked args |
| 51 | + std::vector<TypedArgument> typed_args; |
| 52 | + |
| 53 | + // Return type |
| 54 | + Type return_type{TObject}; |
| 55 | + |
| 56 | + FrameMode frameMode{FrameMode::kNormal}; |
| 57 | + |
| 58 | + CFG cfg; |
| 59 | + |
| 60 | + Environment env; |
| 61 | + |
| 62 | + // All the code patchers pointing to patch points in this function. |
| 63 | + // |
| 64 | + // These will be moved over to the CompiledFunction after compilation is |
| 65 | + // complete. |
| 66 | + std::vector<std::unique_ptr<CodePatcher>> code_patchers; |
| 67 | + |
| 68 | + // Optional property used to track time taken for individual compilation |
| 69 | + // phases |
| 70 | + std::unique_ptr<CompilationPhaseTimer> compilation_phase_timer; |
| 71 | + |
| 72 | + // Return the total number of arguments (positional + kwonly + varargs + |
| 73 | + // varkeywords) |
| 74 | + int numArgs() const; |
| 75 | + |
| 76 | + // Return the number of locals + cellvars + freevars |
| 77 | + Py_ssize_t numVars() const; |
| 78 | + |
| 79 | + // Set code and a number of other members that are derived from it. |
| 80 | + void setCode(BorrowedRef<PyCodeObject> code); |
| 81 | + |
| 82 | + // Count the number of instructions that match the predicate |
| 83 | + std::size_t CountInstrs(InstrPredicate pred) const; |
| 84 | + |
| 85 | + // Does this function return a primitive type? |
| 86 | + bool returnsPrimitive() const; |
| 87 | + |
| 88 | + // Does this function return a primitive double? |
| 89 | + bool returnsPrimitiveDouble() const; |
| 90 | + |
| 91 | + void setCompilationPhaseTimer(std::unique_ptr<CompilationPhaseTimer> cpt); |
| 92 | + |
| 93 | + bool canDeopt() const; |
| 94 | + |
| 95 | + template <typename T, typename... Args> |
| 96 | + T* allocateCodePatcher(Args&&... args) { |
| 97 | + code_patchers.emplace_back( |
| 98 | + std::make_unique<T>(std::forward<Args>(args)...)); |
| 99 | + return static_cast<T*>(code_patchers.back().get()); |
| 100 | + } |
| 101 | + |
| 102 | + // Get the code object for the given instruction. Handles inlined functions |
| 103 | + // but assumes that inlined functions have a dominating FrameState from |
| 104 | + // BeginInlinedFunction to use. If we start optimizing that out for inlined |
| 105 | + // functions that cannot deopt, we will have to do something different. |
| 106 | + // |
| 107 | + // The instruction must be part of this function. |
| 108 | + BorrowedRef<PyCodeObject> codeFor(const Instr& instr) const; |
| 109 | + |
| 110 | + private: |
| 111 | + DISALLOW_COPY_AND_ASSIGN(Function); |
| 112 | +}; |
| 113 | + |
| 114 | +using OpcodeCounts = std::array<int, kNumOpcodes>; |
| 115 | +OpcodeCounts count_opcodes(const Function& func); |
| 116 | + |
| 117 | +} // namespace jit::hir |
0 commit comments