Skip to content

Commit 0e70ca7

Browse files
jbower-fbmeta-codesync[bot]
authored andcommitted
Use _PyDict_GetKeysVersionForCurrentState to get version_tag from dictionaries
Summary: The `ma_version_tag` field no longer exists. We also now have a type for the tag value which switches between 32 or 64 bits depending on Python version. Reviewed By: DinoV Differential Revision: D83861595 fbshipit-source-id: c6da67156441d201dfa8c25fb79aa252498c723c
1 parent f2d1bbd commit 0e70ca7

4 files changed

Lines changed: 39 additions & 15 deletions

File tree

cinderx/Common/dict.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
#include "cinderx/python.h"
66

7+
#if PY_VERSION_HEX >= 0x030C0000
8+
// This needs to come before borrowed.h
9+
#include "pycore_dict.h"
10+
#endif
11+
12+
#include "cinderx/UpstreamBorrow/borrowed.h"
13+
714
#include <stdbool.h>
815

916
#ifdef __cplusplus
@@ -85,6 +92,18 @@ static inline uint32_t dictGetKeysVersion(
8592
}
8693
#endif
8794

95+
#if PY_VERSION_HEX >= 0x030E0000
96+
typedef uint32_t ci_dict_version_tag_t;
97+
static inline ci_dict_version_tag_t Ci_DictVersionTag(PyDictObject* dict) {
98+
return _PyDict_GetKeysVersionForCurrentState(_PyInterpreterState_GET(), dict);
99+
}
100+
#else
101+
typedef uint64_t ci_dict_version_tag_t;
102+
static inline ci_dict_version_tag_t Ci_DictVersionTag(PyDictObject* dict) {
103+
return dict->ma_version_tag;
104+
}
105+
#endif
106+
88107
#ifdef __cplusplus
89108
} // extern "C"
90109
#endif

cinderx/Jit/inline_cache.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,23 @@ PyObject* __attribute__((noinline)) raise_attribute_error(
106106
return nullptr;
107107
}
108108

109-
uint64_t getModuleVersion(BorrowedRef<PyModuleObject> mod) {
110-
#if PY_VERSION_HEX < 0x030E0000 // TASK(T229234686)
109+
ci_dict_version_tag_t getModuleVersion(BorrowedRef<PyModuleObject> mod) {
111110
if (mod->md_dict) {
112111
BorrowedRef<PyDictObject> md_dict = mod->md_dict;
113-
return md_dict->ma_version_tag;
112+
return Ci_DictVersionTag(md_dict.get());
114113
}
115-
#endif
116114
return 0;
117115
}
118116

119-
uint64_t getModuleVersion(BorrowedRef<Ci_StrictModuleObject> mod) {
120-
#if PY_VERSION_HEX < 0x030E0000 // TASK(T229234686)
117+
ci_dict_version_tag_t getModuleVersion(BorrowedRef<Ci_StrictModuleObject> mod) {
121118
if (mod->globals) {
122119
BorrowedRef<PyDictObject> globals = mod->globals;
123-
return globals->ma_version_tag;
120+
return Ci_DictVersionTag(globals.get());
124121
}
125-
#endif
126122
return 0;
127123
}
128124

129-
uint64_t getModuleVersion(BorrowedRef<> obj) {
125+
ci_dict_version_tag_t getModuleVersion(BorrowedRef<> obj) {
130126
if (PyModule_Check(obj)) {
131127
BorrowedRef<PyModuleObject> mod{obj};
132128
return getModuleVersion(mod);
@@ -1380,7 +1376,7 @@ LoadMethodResult LoadModuleMethodCache::lookup(
13801376
BorrowedRef<> obj,
13811377
BorrowedRef<> name) {
13821378
if (module_obj_ == obj && value_ != nullptr) {
1383-
uint64_t version = 0;
1379+
ci_dict_version_tag_t version = 0;
13841380
if (PyModule_Check(obj)) {
13851381
BorrowedRef<PyModuleObject> mod{obj};
13861382
version = getModuleVersion(mod);
@@ -1430,7 +1426,7 @@ LoadModuleMethodCache::lookupSlowPath(BorrowedRef<> obj, BorrowedRef<> name) {
14301426
void LoadModuleMethodCache::fill(
14311427
BorrowedRef<> obj,
14321428
BorrowedRef<> value,
1433-
uint64_t version) {
1429+
ci_dict_version_tag_t version) {
14341430
module_obj_ = obj;
14351431
value_ = value;
14361432
module_version_ = version;

cinderx/Jit/inline_cache.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "cinderx/python.h"
66

7+
#include "cinderx/Common/dict.h"
78
#include "cinderx/Common/ref.h"
89
#include "cinderx/Common/util.h"
910
#include "cinderx/Jit/config.h"
@@ -330,11 +331,12 @@ class LoadModuleAttrCache {
330331

331332
private:
332333
PyObject* lookupSlowPath(BorrowedRef<> obj, BorrowedRef<> name);
333-
void fill(BorrowedRef<> obj, BorrowedRef<> value, uint64_t version);
334+
void
335+
fill(BorrowedRef<> obj, BorrowedRef<> value, ci_dict_version_tag_t version);
334336

335337
// This corresponds to module __dict__'s version which allows us
336338
// to correctly invalidate the cache whenever the dictionary changes.
337-
uint64_t version_{0};
339+
ci_dict_version_tag_t version_{0};
338340
BorrowedRef<> module_;
339341
BorrowedRef<> value_;
340342
};
@@ -351,11 +353,12 @@ class LoadModuleMethodCache {
351353

352354
private:
353355
LoadMethodResult lookupSlowPath(BorrowedRef<> obj, BorrowedRef<> name);
354-
void fill(BorrowedRef<> obj, BorrowedRef<> value, uint64_t version);
356+
void
357+
fill(BorrowedRef<> obj, BorrowedRef<> value, ci_dict_version_tag_t version);
355358

356359
// This corresponds to module __dict__'s version which allows us
357360
// to correctly invalidate the cache whenever the dictionary changes.
358-
uint64_t module_version_{0};
361+
ci_dict_version_tag_t module_version_{0};
359362
BorrowedRef<> module_obj_;
360363
BorrowedRef<> value_;
361364
};

cinderx/UpstreamBorrow/borrowed.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ PyObject* Ci_Builtin_Next_Core(PyObject* it, PyObject* def);
240240
void Cix_gen_dealloc_with_custom_free(PyObject* obj);
241241
#endif
242242

243+
#if PY_VERSION_HEX >= 0x030E0000
244+
uint32_t _CiDict_GetKeysVersionForCurrentState(
245+
PyInterpreterState* interp,
246+
PyDictObject* dict);
247+
#endif
248+
243249
#ifdef ENABLE_PEP523_HOOK
244250
extern _PyFrameEvalFunction Ci_EvalFrameFunc;
245251
#endif

0 commit comments

Comments
 (0)