Skip to content

Commit 830f313

Browse files
alexmalyshevfacebook-github-bot
authored andcommitted
Deduplicate Preloader's checked arg types maps
Summary: No need to have both of them, we can keep the one with the strong references and compute hir::Type objects out of it whenever we want. Reviewed By: mpage Differential Revision: D78560496 fbshipit-source-id: b2d97148dc1083872caa9b3ecc09db79bff2681c
1 parent e3dbfbb commit 830f313

2 files changed

Lines changed: 24 additions & 22 deletions

File tree

Jit/hir/preload.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ const NativeTarget& Preloader::invokeNativeTarget(BorrowedRef<> target) const {
253253
}
254254

255255
Type Preloader::checkArgType(long local_idx) const {
256-
return map_get(check_arg_types_, local_idx, TObject);
256+
auto it = check_arg_types_.find(local_idx);
257+
return it != check_arg_types_.end() ? it->second.toHir() : TObject;
257258
}
258259

259260
PyObject** Preloader::getGlobalCache(BorrowedRef<> name_obj) const {
@@ -290,7 +291,7 @@ std::unique_ptr<Function> Preloader::makeFunction() const {
290291
irfunc->return_type = return_type_;
291292
irfunc->has_primitive_args = has_primitive_args_;
292293
irfunc->has_primitive_first_arg = has_primitive_first_arg_;
293-
for (auto& [local, preloaded_type] : check_arg_pytypes_) {
294+
for (auto& [local, preloaded_type] : check_arg_types_) {
294295
irfunc->typed_args.emplace_back(
295296
local,
296297
preloaded_type.type,
@@ -493,8 +494,7 @@ bool Preloader::preloadStatic() {
493494
preloaded_type.type != reinterpret_cast<PyTypeObject*>(&PyObject_Type),
494495
"shouldn't generate type checks for object");
495496
Type type = preloaded_type.toHir();
496-
check_arg_types_.emplace(local, type);
497-
check_arg_pytypes_.emplace(local, std::move(preloaded_type));
497+
check_arg_types_.emplace(local, std::move(preloaded_type));
498498
if (type <= TPrimitive) {
499499
has_primitive_args_ = true;
500500
if (local == 0) {

Jit/hir/preload.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,7 @@ class Preloader {
185185
return has_primitive_args_;
186186
}
187187

188-
std::unique_ptr<InvokeTarget> resolve_target_descr(
189-
BorrowedRef<> descr,
190-
int opcode);
191-
192188
private:
193-
BorrowedRef<> constArg(BytecodeInstruction& bc_instr) const;
194-
PyObject** getGlobalCache(BorrowedRef<> name) const;
195-
bool canCacheGlobals() const;
196-
bool preload();
197-
198-
// Preload information only relevant to Static Python functions.
199-
bool preloadStatic();
200-
201-
// Check if a code object is for the top-level code in a module.
202-
bool isModuleCodeObject() const;
203-
204189
explicit Preloader(
205190
BorrowedRef<PyCodeObject> code,
206191
BorrowedRef<PyDictObject> builtins,
@@ -215,6 +200,21 @@ class Preloader {
215200
JIT_CHECK(PyCode_Check(code_), "Expected PyCodeObject");
216201
}
217202

203+
BorrowedRef<> constArg(BytecodeInstruction& bc_instr) const;
204+
PyObject** getGlobalCache(BorrowedRef<> name) const;
205+
bool canCacheGlobals() const;
206+
bool preload();
207+
208+
// Preload information only relevant to Static Python functions.
209+
bool preloadStatic();
210+
211+
// Check if a code object is for the top-level code in a module.
212+
bool isModuleCodeObject() const;
213+
214+
std::unique_ptr<InvokeTarget> resolve_target_descr(
215+
BorrowedRef<> descr,
216+
int opcode);
217+
218218
Ref<PyCodeObject> code_;
219219
Ref<PyDictObject> builtins_;
220220
Ref<PyDictObject> globals_;
@@ -227,9 +227,11 @@ class Preloader {
227227
InvokeTargetMap func_targets_;
228228
InvokeTargetMap meth_targets_;
229229
std::unordered_map<PyObject*, std::unique_ptr<NativeTarget>> native_targets_;
230-
// keyed by locals index
231-
std::unordered_map<long, Type> check_arg_types_;
232-
std::map<long, OwnedType> check_arg_pytypes_;
230+
231+
// Maps locals (by their index) to their type. This type must be checked at
232+
// the start of the function.
233+
std::map<long, OwnedType> check_arg_types_;
234+
233235
// keyed by name index, names borrowed from code object
234236
GlobalNamesMap global_names_;
235237
Type return_type_{TObject};

0 commit comments

Comments
 (0)