specialize.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  1. #include "Python.h"
  2. #include "pycore_code.h"
  3. #include "pycore_dict.h"
  4. #include "pycore_function.h" // _PyFunction_GetVersionForCurrentState()
  5. #include "pycore_global_strings.h" // _Py_ID()
  6. #include "pycore_long.h"
  7. #include "pycore_moduleobject.h"
  8. #include "pycore_object.h"
  9. #include "pycore_opcode.h" // _PyOpcode_Caches
  10. #include "structmember.h" // struct PyMemberDef, T_OFFSET_EX
  11. #include "pycore_descrobject.h"
  12. #include <stdlib.h> // rand()
  13. /* For guidance on adding or extending families of instructions see
  14. * ./adaptive.md
  15. */
  16. #ifdef Py_STATS
  17. PyStats _py_stats_struct = { 0 };
  18. PyStats *_py_stats = NULL;
  19. #define ADD_STAT_TO_DICT(res, field) \
  20. do { \
  21. PyObject *val = PyLong_FromUnsignedLongLong(stats->field); \
  22. if (val == NULL) { \
  23. Py_DECREF(res); \
  24. return NULL; \
  25. } \
  26. if (PyDict_SetItemString(res, #field, val) == -1) { \
  27. Py_DECREF(res); \
  28. Py_DECREF(val); \
  29. return NULL; \
  30. } \
  31. Py_DECREF(val); \
  32. } while(0);
  33. static PyObject*
  34. stats_to_dict(SpecializationStats *stats)
  35. {
  36. PyObject *res = PyDict_New();
  37. if (res == NULL) {
  38. return NULL;
  39. }
  40. ADD_STAT_TO_DICT(res, success);
  41. ADD_STAT_TO_DICT(res, failure);
  42. ADD_STAT_TO_DICT(res, hit);
  43. ADD_STAT_TO_DICT(res, deferred);
  44. ADD_STAT_TO_DICT(res, miss);
  45. ADD_STAT_TO_DICT(res, deopt);
  46. PyObject *failure_kinds = PyTuple_New(SPECIALIZATION_FAILURE_KINDS);
  47. if (failure_kinds == NULL) {
  48. Py_DECREF(res);
  49. return NULL;
  50. }
  51. for (int i = 0; i < SPECIALIZATION_FAILURE_KINDS; i++) {
  52. PyObject *stat = PyLong_FromUnsignedLongLong(stats->failure_kinds[i]);
  53. if (stat == NULL) {
  54. Py_DECREF(res);
  55. Py_DECREF(failure_kinds);
  56. return NULL;
  57. }
  58. PyTuple_SET_ITEM(failure_kinds, i, stat);
  59. }
  60. if (PyDict_SetItemString(res, "failure_kinds", failure_kinds)) {
  61. Py_DECREF(res);
  62. Py_DECREF(failure_kinds);
  63. return NULL;
  64. }
  65. Py_DECREF(failure_kinds);
  66. return res;
  67. }
  68. #undef ADD_STAT_TO_DICT
  69. static int
  70. add_stat_dict(
  71. PyObject *res,
  72. int opcode,
  73. const char *name) {
  74. SpecializationStats *stats = &_py_stats_struct.opcode_stats[opcode].specialization;
  75. PyObject *d = stats_to_dict(stats);
  76. if (d == NULL) {
  77. return -1;
  78. }
  79. int err = PyDict_SetItemString(res, name, d);
  80. Py_DECREF(d);
  81. return err;
  82. }
  83. #ifdef Py_STATS
  84. PyObject*
  85. _Py_GetSpecializationStats(void) {
  86. PyObject *stats = PyDict_New();
  87. if (stats == NULL) {
  88. return NULL;
  89. }
  90. int err = 0;
  91. err += add_stat_dict(stats, LOAD_SUPER_ATTR, "load_super_attr");
  92. err += add_stat_dict(stats, LOAD_ATTR, "load_attr");
  93. err += add_stat_dict(stats, LOAD_GLOBAL, "load_global");
  94. err += add_stat_dict(stats, BINARY_SUBSCR, "binary_subscr");
  95. err += add_stat_dict(stats, STORE_SUBSCR, "store_subscr");
  96. err += add_stat_dict(stats, STORE_ATTR, "store_attr");
  97. err += add_stat_dict(stats, CALL, "call");
  98. err += add_stat_dict(stats, BINARY_OP, "binary_op");
  99. err += add_stat_dict(stats, COMPARE_OP, "compare_op");
  100. err += add_stat_dict(stats, UNPACK_SEQUENCE, "unpack_sequence");
  101. err += add_stat_dict(stats, FOR_ITER, "for_iter");
  102. if (err < 0) {
  103. Py_DECREF(stats);
  104. return NULL;
  105. }
  106. return stats;
  107. }
  108. #endif
  109. #define PRINT_STAT(i, field) \
  110. if (stats[i].field) { \
  111. fprintf(out, " opcode[%d]." #field " : %" PRIu64 "\n", i, stats[i].field); \
  112. }
  113. static void
  114. print_spec_stats(FILE *out, OpcodeStats *stats)
  115. {
  116. /* Mark some opcodes as specializable for stats,
  117. * even though we don't specialize them yet. */
  118. fprintf(out, "opcode[%d].specializable : 1\n", BINARY_SLICE);
  119. fprintf(out, "opcode[%d].specializable : 1\n", COMPARE_OP);
  120. fprintf(out, "opcode[%d].specializable : 1\n", STORE_SLICE);
  121. fprintf(out, "opcode[%d].specializable : 1\n", SEND);
  122. for (int i = 0; i < 256; i++) {
  123. if (_PyOpcode_Caches[i]) {
  124. fprintf(out, "opcode[%d].specializable : 1\n", i);
  125. }
  126. PRINT_STAT(i, specialization.success);
  127. PRINT_STAT(i, specialization.failure);
  128. PRINT_STAT(i, specialization.hit);
  129. PRINT_STAT(i, specialization.deferred);
  130. PRINT_STAT(i, specialization.miss);
  131. PRINT_STAT(i, specialization.deopt);
  132. PRINT_STAT(i, execution_count);
  133. for (int j = 0; j < SPECIALIZATION_FAILURE_KINDS; j++) {
  134. uint64_t val = stats[i].specialization.failure_kinds[j];
  135. if (val) {
  136. fprintf(out, " opcode[%d].specialization.failure_kinds[%d] : %"
  137. PRIu64 "\n", i, j, val);
  138. }
  139. }
  140. for (int j = 0; j < 256; j++) {
  141. if (stats[i].pair_count[j]) {
  142. fprintf(out, "opcode[%d].pair_count[%d] : %" PRIu64 "\n",
  143. i, j, stats[i].pair_count[j]);
  144. }
  145. }
  146. }
  147. }
  148. #undef PRINT_STAT
  149. static void
  150. print_call_stats(FILE *out, CallStats *stats)
  151. {
  152. fprintf(out, "Calls to PyEval_EvalDefault: %" PRIu64 "\n", stats->pyeval_calls);
  153. fprintf(out, "Calls to Python functions inlined: %" PRIu64 "\n", stats->inlined_py_calls);
  154. fprintf(out, "Frames pushed: %" PRIu64 "\n", stats->frames_pushed);
  155. fprintf(out, "Frame objects created: %" PRIu64 "\n", stats->frame_objects_created);
  156. for (int i = 0; i < EVAL_CALL_KINDS; i++) {
  157. fprintf(out, "Calls via PyEval_EvalFrame[%d] : %" PRIu64 "\n", i, stats->eval_calls[i]);
  158. }
  159. }
  160. static void
  161. print_object_stats(FILE *out, ObjectStats *stats)
  162. {
  163. fprintf(out, "Object allocations from freelist: %" PRIu64 "\n", stats->from_freelist);
  164. fprintf(out, "Object frees to freelist: %" PRIu64 "\n", stats->to_freelist);
  165. fprintf(out, "Object allocations: %" PRIu64 "\n", stats->allocations);
  166. fprintf(out, "Object allocations to 512 bytes: %" PRIu64 "\n", stats->allocations512);
  167. fprintf(out, "Object allocations to 4 kbytes: %" PRIu64 "\n", stats->allocations4k);
  168. fprintf(out, "Object allocations over 4 kbytes: %" PRIu64 "\n", stats->allocations_big);
  169. fprintf(out, "Object frees: %" PRIu64 "\n", stats->frees);
  170. fprintf(out, "Object new values: %" PRIu64 "\n", stats->new_values);
  171. fprintf(out, "Object interpreter increfs: %" PRIu64 "\n", stats->interpreter_increfs);
  172. fprintf(out, "Object interpreter decrefs: %" PRIu64 "\n", stats->interpreter_decrefs);
  173. fprintf(out, "Object increfs: %" PRIu64 "\n", stats->increfs);
  174. fprintf(out, "Object decrefs: %" PRIu64 "\n", stats->decrefs);
  175. fprintf(out, "Object materialize dict (on request): %" PRIu64 "\n", stats->dict_materialized_on_request);
  176. fprintf(out, "Object materialize dict (new key): %" PRIu64 "\n", stats->dict_materialized_new_key);
  177. fprintf(out, "Object materialize dict (too big): %" PRIu64 "\n", stats->dict_materialized_too_big);
  178. fprintf(out, "Object materialize dict (str subclass): %" PRIu64 "\n", stats->dict_materialized_str_subclass);
  179. fprintf(out, "Object method cache hits: %" PRIu64 "\n", stats->type_cache_hits);
  180. fprintf(out, "Object method cache misses: %" PRIu64 "\n", stats->type_cache_misses);
  181. fprintf(out, "Object method cache collisions: %" PRIu64 "\n", stats->type_cache_collisions);
  182. fprintf(out, "Object method cache dunder hits: %" PRIu64 "\n", stats->type_cache_dunder_hits);
  183. fprintf(out, "Object method cache dunder misses: %" PRIu64 "\n", stats->type_cache_dunder_misses);
  184. }
  185. static void
  186. print_stats(FILE *out, PyStats *stats) {
  187. print_spec_stats(out, stats->opcode_stats);
  188. print_call_stats(out, &stats->call_stats);
  189. print_object_stats(out, &stats->object_stats);
  190. }
  191. void
  192. _Py_StatsClear(void)
  193. {
  194. _py_stats_struct = (PyStats) { 0 };
  195. }
  196. void
  197. _Py_PrintSpecializationStats(int to_file)
  198. {
  199. FILE *out = stderr;
  200. if (to_file) {
  201. /* Write to a file instead of stderr. */
  202. # ifdef MS_WINDOWS
  203. const char *dirname = "c:\\temp\\py_stats\\";
  204. # else
  205. const char *dirname = "/tmp/py_stats/";
  206. # endif
  207. /* Use random 160 bit number as file name,
  208. * to avoid both accidental collisions and
  209. * symlink attacks. */
  210. unsigned char rand[20];
  211. char hex_name[41];
  212. _PyOS_URandomNonblock(rand, 20);
  213. for (int i = 0; i < 20; i++) {
  214. hex_name[2*i] = "0123456789abcdef"[rand[i]&15];
  215. hex_name[2*i+1] = "0123456789abcdef"[(rand[i]>>4)&15];
  216. }
  217. hex_name[40] = '\0';
  218. char buf[64];
  219. assert(strlen(dirname) + 40 + strlen(".txt") < 64);
  220. sprintf(buf, "%s%s.txt", dirname, hex_name);
  221. FILE *fout = fopen(buf, "w");
  222. if (fout) {
  223. out = fout;
  224. }
  225. }
  226. else {
  227. fprintf(out, "Specialization stats:\n");
  228. }
  229. print_stats(out, &_py_stats_struct);
  230. if (out != stderr) {
  231. fclose(out);
  232. }
  233. }
  234. #ifdef Py_STATS
  235. #define SPECIALIZATION_FAIL(opcode, kind) \
  236. do { \
  237. if (_py_stats) { \
  238. _py_stats->opcode_stats[opcode].specialization.failure_kinds[kind]++; \
  239. } \
  240. } while (0)
  241. #endif
  242. #endif
  243. #ifndef SPECIALIZATION_FAIL
  244. #define SPECIALIZATION_FAIL(opcode, kind) ((void)0)
  245. #endif
  246. // Initialize warmup counters and insert superinstructions. This cannot fail.
  247. void
  248. _PyCode_Quicken(PyCodeObject *code)
  249. {
  250. #if ENABLE_SPECIALIZATION
  251. int opcode = 0;
  252. _Py_CODEUNIT *instructions = _PyCode_CODE(code);
  253. for (int i = 0; i < Py_SIZE(code); i++) {
  254. int previous_opcode = opcode;
  255. opcode = _Py_GetBaseOpcode(code, i);
  256. assert(opcode < MIN_INSTRUMENTED_OPCODE);
  257. int caches = _PyOpcode_Caches[opcode];
  258. if (caches) {
  259. instructions[i + 1].cache = adaptive_counter_warmup();
  260. i += caches;
  261. continue;
  262. }
  263. switch (previous_opcode << 8 | opcode) {
  264. case LOAD_CONST << 8 | LOAD_FAST:
  265. instructions[i - 1].op.code = LOAD_CONST__LOAD_FAST;
  266. break;
  267. case LOAD_FAST << 8 | LOAD_CONST:
  268. instructions[i - 1].op.code = LOAD_FAST__LOAD_CONST;
  269. break;
  270. case LOAD_FAST << 8 | LOAD_FAST:
  271. instructions[i - 1].op.code = LOAD_FAST__LOAD_FAST;
  272. break;
  273. case STORE_FAST << 8 | LOAD_FAST:
  274. instructions[i - 1].op.code = STORE_FAST__LOAD_FAST;
  275. break;
  276. case STORE_FAST << 8 | STORE_FAST:
  277. instructions[i - 1].op.code = STORE_FAST__STORE_FAST;
  278. break;
  279. }
  280. }
  281. #endif /* ENABLE_SPECIALIZATION */
  282. }
  283. #define SIMPLE_FUNCTION 0
  284. /* Common */
  285. #define SPEC_FAIL_OTHER 0
  286. #define SPEC_FAIL_NO_DICT 1
  287. #define SPEC_FAIL_OVERRIDDEN 2
  288. #define SPEC_FAIL_OUT_OF_VERSIONS 3
  289. #define SPEC_FAIL_OUT_OF_RANGE 4
  290. #define SPEC_FAIL_EXPECTED_ERROR 5
  291. #define SPEC_FAIL_WRONG_NUMBER_ARGUMENTS 6
  292. #define SPEC_FAIL_CODE_COMPLEX_PARAMETERS 7
  293. #define SPEC_FAIL_CODE_NOT_OPTIMIZED 8
  294. #define SPEC_FAIL_LOAD_GLOBAL_NON_DICT 17
  295. #define SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT 18
  296. /* Super */
  297. #define SPEC_FAIL_SUPER_BAD_CLASS 9
  298. #define SPEC_FAIL_SUPER_SHADOWED 10
  299. /* Attributes */
  300. #define SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR 9
  301. #define SPEC_FAIL_ATTR_NON_OVERRIDING_DESCRIPTOR 10
  302. #define SPEC_FAIL_ATTR_NOT_DESCRIPTOR 11
  303. #define SPEC_FAIL_ATTR_METHOD 12
  304. #define SPEC_FAIL_ATTR_MUTABLE_CLASS 13
  305. #define SPEC_FAIL_ATTR_PROPERTY 14
  306. #define SPEC_FAIL_ATTR_NON_OBJECT_SLOT 15
  307. #define SPEC_FAIL_ATTR_READ_ONLY 16
  308. #define SPEC_FAIL_ATTR_AUDITED_SLOT 17
  309. #define SPEC_FAIL_ATTR_NOT_MANAGED_DICT 18
  310. #define SPEC_FAIL_ATTR_NON_STRING_OR_SPLIT 19
  311. #define SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND 20
  312. #define SPEC_FAIL_ATTR_SHADOWED 21
  313. #define SPEC_FAIL_ATTR_BUILTIN_CLASS_METHOD 22
  314. #define SPEC_FAIL_ATTR_CLASS_METHOD_OBJ 23
  315. #define SPEC_FAIL_ATTR_OBJECT_SLOT 24
  316. #define SPEC_FAIL_ATTR_HAS_MANAGED_DICT 25
  317. #define SPEC_FAIL_ATTR_INSTANCE_ATTRIBUTE 26
  318. #define SPEC_FAIL_ATTR_METACLASS_ATTRIBUTE 27
  319. #define SPEC_FAIL_ATTR_PROPERTY_NOT_PY_FUNCTION 28
  320. #define SPEC_FAIL_ATTR_NOT_IN_KEYS 29
  321. #define SPEC_FAIL_ATTR_NOT_IN_DICT 30
  322. #define SPEC_FAIL_ATTR_CLASS_ATTR_SIMPLE 31
  323. #define SPEC_FAIL_ATTR_CLASS_ATTR_DESCRIPTOR 32
  324. #define SPEC_FAIL_ATTR_BUILTIN_CLASS_METHOD_OBJ 33
  325. /* Binary subscr and store subscr */
  326. #define SPEC_FAIL_SUBSCR_ARRAY_INT 9
  327. #define SPEC_FAIL_SUBSCR_ARRAY_SLICE 10
  328. #define SPEC_FAIL_SUBSCR_LIST_SLICE 11
  329. #define SPEC_FAIL_SUBSCR_TUPLE_SLICE 12
  330. #define SPEC_FAIL_SUBSCR_STRING_INT 13
  331. #define SPEC_FAIL_SUBSCR_STRING_SLICE 14
  332. #define SPEC_FAIL_SUBSCR_BUFFER_INT 15
  333. #define SPEC_FAIL_SUBSCR_BUFFER_SLICE 16
  334. #define SPEC_FAIL_SUBSCR_SEQUENCE_INT 17
  335. /* Store subscr */
  336. #define SPEC_FAIL_SUBSCR_BYTEARRAY_INT 18
  337. #define SPEC_FAIL_SUBSCR_BYTEARRAY_SLICE 19
  338. #define SPEC_FAIL_SUBSCR_PY_SIMPLE 20
  339. #define SPEC_FAIL_SUBSCR_PY_OTHER 21
  340. #define SPEC_FAIL_SUBSCR_DICT_SUBCLASS_NO_OVERRIDE 22
  341. #define SPEC_FAIL_SUBSCR_NOT_HEAP_TYPE 23
  342. /* Binary op */
  343. #define SPEC_FAIL_BINARY_OP_ADD_DIFFERENT_TYPES 9
  344. #define SPEC_FAIL_BINARY_OP_ADD_OTHER 10
  345. #define SPEC_FAIL_BINARY_OP_AND_DIFFERENT_TYPES 11
  346. #define SPEC_FAIL_BINARY_OP_AND_INT 12
  347. #define SPEC_FAIL_BINARY_OP_AND_OTHER 13
  348. #define SPEC_FAIL_BINARY_OP_FLOOR_DIVIDE 14
  349. #define SPEC_FAIL_BINARY_OP_LSHIFT 15
  350. #define SPEC_FAIL_BINARY_OP_MATRIX_MULTIPLY 16
  351. #define SPEC_FAIL_BINARY_OP_MULTIPLY_DIFFERENT_TYPES 17
  352. #define SPEC_FAIL_BINARY_OP_MULTIPLY_OTHER 18
  353. #define SPEC_FAIL_BINARY_OP_OR 19
  354. #define SPEC_FAIL_BINARY_OP_POWER 20
  355. #define SPEC_FAIL_BINARY_OP_REMAINDER 21
  356. #define SPEC_FAIL_BINARY_OP_RSHIFT 22
  357. #define SPEC_FAIL_BINARY_OP_SUBTRACT_DIFFERENT_TYPES 23
  358. #define SPEC_FAIL_BINARY_OP_SUBTRACT_OTHER 24
  359. #define SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_DIFFERENT_TYPES 25
  360. #define SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_FLOAT 26
  361. #define SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_OTHER 27
  362. #define SPEC_FAIL_BINARY_OP_XOR 28
  363. /* Calls */
  364. #define SPEC_FAIL_CALL_INSTANCE_METHOD 11
  365. #define SPEC_FAIL_CALL_CMETHOD 12
  366. #define SPEC_FAIL_CALL_CFUNC_VARARGS 13
  367. #define SPEC_FAIL_CALL_CFUNC_VARARGS_KEYWORDS 14
  368. #define SPEC_FAIL_CALL_CFUNC_NOARGS 15
  369. #define SPEC_FAIL_CALL_CFUNC_METHOD_FASTCALL_KEYWORDS 16
  370. #define SPEC_FAIL_CALL_METH_DESCR_VARARGS 17
  371. #define SPEC_FAIL_CALL_METH_DESCR_VARARGS_KEYWORDS 18
  372. #define SPEC_FAIL_CALL_METH_DESCR_METHOD_FASTCALL_KEYWORDS 19
  373. #define SPEC_FAIL_CALL_BAD_CALL_FLAGS 20
  374. #define SPEC_FAIL_CALL_PYTHON_CLASS 21
  375. #define SPEC_FAIL_CALL_PEP_523 22
  376. #define SPEC_FAIL_CALL_BOUND_METHOD 23
  377. #define SPEC_FAIL_CALL_STR 24
  378. #define SPEC_FAIL_CALL_CLASS_NO_VECTORCALL 25
  379. #define SPEC_FAIL_CALL_CLASS_MUTABLE 26
  380. #define SPEC_FAIL_CALL_KWNAMES 27
  381. #define SPEC_FAIL_CALL_METHOD_WRAPPER 28
  382. #define SPEC_FAIL_CALL_OPERATOR_WRAPPER 29
  383. /* COMPARE_OP */
  384. #define SPEC_FAIL_COMPARE_OP_DIFFERENT_TYPES 12
  385. #define SPEC_FAIL_COMPARE_OP_STRING 13
  386. #define SPEC_FAIL_COMPARE_OP_BIG_INT 14
  387. #define SPEC_FAIL_COMPARE_OP_BYTES 15
  388. #define SPEC_FAIL_COMPARE_OP_TUPLE 16
  389. #define SPEC_FAIL_COMPARE_OP_LIST 17
  390. #define SPEC_FAIL_COMPARE_OP_SET 18
  391. #define SPEC_FAIL_COMPARE_OP_BOOL 19
  392. #define SPEC_FAIL_COMPARE_OP_BASEOBJECT 20
  393. #define SPEC_FAIL_COMPARE_OP_FLOAT_LONG 21
  394. #define SPEC_FAIL_COMPARE_OP_LONG_FLOAT 22
  395. /* FOR_ITER and SEND */
  396. #define SPEC_FAIL_ITER_GENERATOR 10
  397. #define SPEC_FAIL_ITER_COROUTINE 11
  398. #define SPEC_FAIL_ITER_ASYNC_GENERATOR 12
  399. #define SPEC_FAIL_ITER_LIST 13
  400. #define SPEC_FAIL_ITER_TUPLE 14
  401. #define SPEC_FAIL_ITER_SET 15
  402. #define SPEC_FAIL_ITER_STRING 16
  403. #define SPEC_FAIL_ITER_BYTES 17
  404. #define SPEC_FAIL_ITER_RANGE 18
  405. #define SPEC_FAIL_ITER_ITERTOOLS 19
  406. #define SPEC_FAIL_ITER_DICT_KEYS 20
  407. #define SPEC_FAIL_ITER_DICT_ITEMS 21
  408. #define SPEC_FAIL_ITER_DICT_VALUES 22
  409. #define SPEC_FAIL_ITER_ENUMERATE 23
  410. #define SPEC_FAIL_ITER_MAP 24
  411. #define SPEC_FAIL_ITER_ZIP 25
  412. #define SPEC_FAIL_ITER_SEQ_ITER 26
  413. #define SPEC_FAIL_ITER_REVERSED_LIST 27
  414. #define SPEC_FAIL_ITER_CALLABLE 28
  415. #define SPEC_FAIL_ITER_ASCII_STRING 29
  416. #define SPEC_FAIL_ITER_ASYNC_GENERATOR_SEND 30
  417. // UNPACK_SEQUENCE
  418. #define SPEC_FAIL_UNPACK_SEQUENCE_ITERATOR 9
  419. #define SPEC_FAIL_UNPACK_SEQUENCE_SEQUENCE 10
  420. static int function_kind(PyCodeObject *code);
  421. static bool function_check_args(PyObject *o, int expected_argcount, int opcode);
  422. static uint32_t function_get_version(PyObject *o, int opcode);
  423. static uint32_t type_get_version(PyTypeObject *t, int opcode);
  424. static int
  425. specialize_module_load_attr(
  426. PyObject *owner, _Py_CODEUNIT *instr, PyObject *name
  427. ) {
  428. _PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
  429. PyModuleObject *m = (PyModuleObject *)owner;
  430. assert((owner->ob_type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
  431. PyDictObject *dict = (PyDictObject *)m->md_dict;
  432. if (dict == NULL) {
  433. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_NO_DICT);
  434. return -1;
  435. }
  436. if (dict->ma_keys->dk_kind != DICT_KEYS_UNICODE) {
  437. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_NON_STRING_OR_SPLIT);
  438. return -1;
  439. }
  440. Py_ssize_t index = _PyDict_LookupIndex(dict, &_Py_ID(__getattr__));
  441. assert(index != DKIX_ERROR);
  442. if (index != DKIX_EMPTY) {
  443. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND);
  444. return -1;
  445. }
  446. index = _PyDict_LookupIndex(dict, name);
  447. assert (index != DKIX_ERROR);
  448. if (index != (uint16_t)index) {
  449. SPECIALIZATION_FAIL(LOAD_ATTR,
  450. index == DKIX_EMPTY ?
  451. SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND :
  452. SPEC_FAIL_OUT_OF_RANGE);
  453. return -1;
  454. }
  455. uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(
  456. _PyInterpreterState_GET(), dict->ma_keys);
  457. if (keys_version == 0) {
  458. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_VERSIONS);
  459. return -1;
  460. }
  461. write_u32(cache->version, keys_version);
  462. cache->index = (uint16_t)index;
  463. instr->op.code = LOAD_ATTR_MODULE;
  464. return 0;
  465. }
  466. /* Attribute specialization */
  467. void
  468. _Py_Specialize_LoadSuperAttr(PyObject *global_super, PyObject *cls, _Py_CODEUNIT *instr, int load_method) {
  469. assert(ENABLE_SPECIALIZATION);
  470. assert(_PyOpcode_Caches[LOAD_SUPER_ATTR] == INLINE_CACHE_ENTRIES_LOAD_SUPER_ATTR);
  471. _PySuperAttrCache *cache = (_PySuperAttrCache *)(instr + 1);
  472. if (global_super != (PyObject *)&PySuper_Type) {
  473. SPECIALIZATION_FAIL(LOAD_SUPER_ATTR, SPEC_FAIL_SUPER_SHADOWED);
  474. goto fail;
  475. }
  476. if (!PyType_Check(cls)) {
  477. SPECIALIZATION_FAIL(LOAD_SUPER_ATTR, SPEC_FAIL_SUPER_BAD_CLASS);
  478. goto fail;
  479. }
  480. instr->op.code = load_method ? LOAD_SUPER_ATTR_METHOD : LOAD_SUPER_ATTR_ATTR;
  481. goto success;
  482. fail:
  483. STAT_INC(LOAD_SUPER_ATTR, failure);
  484. assert(!PyErr_Occurred());
  485. instr->op.code = LOAD_SUPER_ATTR;
  486. cache->counter = adaptive_counter_backoff(cache->counter);
  487. return;
  488. success:
  489. STAT_INC(LOAD_SUPER_ATTR, success);
  490. assert(!PyErr_Occurred());
  491. cache->counter = adaptive_counter_cooldown();
  492. }
  493. typedef enum {
  494. OVERRIDING, /* Is an overriding descriptor, and will remain so. */
  495. METHOD, /* Attribute has Py_TPFLAGS_METHOD_DESCRIPTOR set */
  496. PROPERTY, /* Is a property */
  497. OBJECT_SLOT, /* Is an object slot descriptor */
  498. OTHER_SLOT, /* Is a slot descriptor of another type */
  499. NON_OVERRIDING, /* Is another non-overriding descriptor, and is an instance of an immutable class*/
  500. BUILTIN_CLASSMETHOD, /* Builtin methods with METH_CLASS */
  501. PYTHON_CLASSMETHOD, /* Python classmethod(func) object */
  502. NON_DESCRIPTOR, /* Is not a descriptor, and is an instance of an immutable class */
  503. MUTABLE, /* Instance of a mutable class; might, or might not, be a descriptor */
  504. ABSENT, /* Attribute is not present on the class */
  505. DUNDER_CLASS, /* __class__ attribute */
  506. GETSET_OVERRIDDEN, /* __getattribute__ or __setattr__ has been overridden */
  507. GETATTRIBUTE_IS_PYTHON_FUNCTION /* Descriptor requires calling a Python __getattribute__ */
  508. } DescriptorClassification;
  509. static DescriptorClassification
  510. analyze_descriptor(PyTypeObject *type, PyObject *name, PyObject **descr, int store)
  511. {
  512. bool has_getattr = false;
  513. if (store) {
  514. if (type->tp_setattro != PyObject_GenericSetAttr) {
  515. *descr = NULL;
  516. return GETSET_OVERRIDDEN;
  517. }
  518. }
  519. else {
  520. getattrofunc getattro_slot = type->tp_getattro;
  521. if (getattro_slot == PyObject_GenericGetAttr) {
  522. /* Normal attribute lookup; */
  523. has_getattr = false;
  524. }
  525. else if (getattro_slot == _Py_slot_tp_getattr_hook ||
  526. getattro_slot == _Py_slot_tp_getattro) {
  527. /* One or both of __getattribute__ or __getattr__ may have been
  528. overridden See typeobject.c for why these functions are special. */
  529. PyObject *getattribute = _PyType_Lookup(type,
  530. &_Py_ID(__getattribute__));
  531. PyInterpreterState *interp = _PyInterpreterState_GET();
  532. bool has_custom_getattribute = getattribute != NULL &&
  533. getattribute != interp->callable_cache.object__getattribute__;
  534. has_getattr = _PyType_Lookup(type, &_Py_ID(__getattr__)) != NULL;
  535. if (has_custom_getattribute) {
  536. if (getattro_slot == _Py_slot_tp_getattro &&
  537. !has_getattr &&
  538. Py_IS_TYPE(getattribute, &PyFunction_Type)) {
  539. *descr = getattribute;
  540. return GETATTRIBUTE_IS_PYTHON_FUNCTION;
  541. }
  542. /* Potentially both __getattr__ and __getattribute__ are set.
  543. Too complicated */
  544. *descr = NULL;
  545. return GETSET_OVERRIDDEN;
  546. }
  547. /* Potentially has __getattr__ but no custom __getattribute__.
  548. Fall through to usual descriptor analysis.
  549. Usual attribute lookup should only be allowed at runtime
  550. if we can guarantee that there is no way an exception can be
  551. raised. This means some specializations, e.g. specializing
  552. for property() isn't safe.
  553. */
  554. }
  555. else {
  556. *descr = NULL;
  557. return GETSET_OVERRIDDEN;
  558. }
  559. }
  560. PyObject *descriptor = _PyType_Lookup(type, name);
  561. *descr = descriptor;
  562. if (descriptor == NULL) {
  563. return ABSENT;
  564. }
  565. PyTypeObject *desc_cls = Py_TYPE(descriptor);
  566. if (!(desc_cls->tp_flags & Py_TPFLAGS_IMMUTABLETYPE)) {
  567. return MUTABLE;
  568. }
  569. if (desc_cls->tp_descr_set) {
  570. if (desc_cls == &PyMemberDescr_Type) {
  571. PyMemberDescrObject *member = (PyMemberDescrObject *)descriptor;
  572. struct PyMemberDef *dmem = member->d_member;
  573. if (dmem->type == T_OBJECT_EX) {
  574. return OBJECT_SLOT;
  575. }
  576. return OTHER_SLOT;
  577. }
  578. if (desc_cls == &PyProperty_Type) {
  579. /* We can't detect at runtime whether an attribute exists
  580. with property. So that means we may have to call
  581. __getattr__. */
  582. return has_getattr ? GETSET_OVERRIDDEN : PROPERTY;
  583. }
  584. if (PyUnicode_CompareWithASCIIString(name, "__class__") == 0) {
  585. if (descriptor == _PyType_Lookup(&PyBaseObject_Type, name)) {
  586. return DUNDER_CLASS;
  587. }
  588. }
  589. if (store) {
  590. return OVERRIDING;
  591. }
  592. }
  593. if (desc_cls->tp_descr_get) {
  594. if (desc_cls->tp_flags & Py_TPFLAGS_METHOD_DESCRIPTOR) {
  595. return METHOD;
  596. }
  597. if (Py_IS_TYPE(descriptor, &PyClassMethodDescr_Type)) {
  598. return BUILTIN_CLASSMETHOD;
  599. }
  600. if (Py_IS_TYPE(descriptor, &PyClassMethod_Type)) {
  601. return PYTHON_CLASSMETHOD;
  602. }
  603. return NON_OVERRIDING;
  604. }
  605. return NON_DESCRIPTOR;
  606. }
  607. static int
  608. specialize_dict_access(
  609. PyObject *owner, _Py_CODEUNIT *instr, PyTypeObject *type,
  610. DescriptorClassification kind, PyObject *name,
  611. int base_op, int values_op, int hint_op)
  612. {
  613. assert(kind == NON_OVERRIDING || kind == NON_DESCRIPTOR || kind == ABSENT ||
  614. kind == BUILTIN_CLASSMETHOD || kind == PYTHON_CLASSMETHOD);
  615. // No descriptor, or non overriding.
  616. if ((type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) {
  617. SPECIALIZATION_FAIL(base_op, SPEC_FAIL_ATTR_NOT_MANAGED_DICT);
  618. return 0;
  619. }
  620. _PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
  621. PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(owner);
  622. if (_PyDictOrValues_IsValues(dorv)) {
  623. // Virtual dictionary
  624. PyDictKeysObject *keys = ((PyHeapTypeObject *)type)->ht_cached_keys;
  625. assert(PyUnicode_CheckExact(name));
  626. Py_ssize_t index = _PyDictKeys_StringLookup(keys, name);
  627. assert (index != DKIX_ERROR);
  628. if (index != (uint16_t)index) {
  629. SPECIALIZATION_FAIL(base_op,
  630. index == DKIX_EMPTY ?
  631. SPEC_FAIL_ATTR_NOT_IN_KEYS :
  632. SPEC_FAIL_OUT_OF_RANGE);
  633. return 0;
  634. }
  635. write_u32(cache->version, type->tp_version_tag);
  636. cache->index = (uint16_t)index;
  637. instr->op.code = values_op;
  638. }
  639. else {
  640. PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv);
  641. if (dict == NULL || !PyDict_CheckExact(dict)) {
  642. SPECIALIZATION_FAIL(base_op, SPEC_FAIL_NO_DICT);
  643. return 0;
  644. }
  645. // We found an instance with a __dict__.
  646. Py_ssize_t index =
  647. _PyDict_LookupIndex(dict, name);
  648. if (index != (uint16_t)index) {
  649. SPECIALIZATION_FAIL(base_op,
  650. index == DKIX_EMPTY ?
  651. SPEC_FAIL_ATTR_NOT_IN_DICT :
  652. SPEC_FAIL_OUT_OF_RANGE);
  653. return 0;
  654. }
  655. cache->index = (uint16_t)index;
  656. write_u32(cache->version, type->tp_version_tag);
  657. instr->op.code = hint_op;
  658. }
  659. return 1;
  660. }
  661. static int specialize_attr_loadmethod(PyObject* owner, _Py_CODEUNIT* instr, PyObject* name,
  662. PyObject* descr, DescriptorClassification kind);
  663. static int specialize_class_load_attr(PyObject* owner, _Py_CODEUNIT* instr, PyObject* name);
  664. void
  665. _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name)
  666. {
  667. assert(ENABLE_SPECIALIZATION);
  668. assert(_PyOpcode_Caches[LOAD_ATTR] == INLINE_CACHE_ENTRIES_LOAD_ATTR);
  669. _PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
  670. PyTypeObject *type = Py_TYPE(owner);
  671. if (!_PyType_IsReady(type)) {
  672. // We *might* not really need this check, but we inherited it from
  673. // PyObject_GenericGetAttr and friends... and this way we still do the
  674. // right thing if someone forgets to call PyType_Ready(type):
  675. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OTHER);
  676. goto fail;
  677. }
  678. if (PyModule_CheckExact(owner)) {
  679. if (specialize_module_load_attr(owner, instr, name))
  680. {
  681. goto fail;
  682. }
  683. goto success;
  684. }
  685. if (PyType_Check(owner)) {
  686. if (specialize_class_load_attr(owner, instr, name)) {
  687. goto fail;
  688. }
  689. goto success;
  690. }
  691. PyObject *descr = NULL;
  692. DescriptorClassification kind = analyze_descriptor(type, name, &descr, 0);
  693. assert(descr != NULL || kind == ABSENT || kind == GETSET_OVERRIDDEN);
  694. if (type_get_version(type, LOAD_ATTR) == 0) {
  695. goto fail;
  696. }
  697. switch(kind) {
  698. case OVERRIDING:
  699. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR);
  700. goto fail;
  701. case METHOD:
  702. {
  703. int oparg = instr->op.arg;
  704. if (oparg & 1) {
  705. if (specialize_attr_loadmethod(owner, instr, name, descr, kind)) {
  706. goto success;
  707. }
  708. }
  709. else {
  710. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_METHOD);
  711. }
  712. goto fail;
  713. }
  714. case PROPERTY:
  715. {
  716. _PyLoadMethodCache *lm_cache = (_PyLoadMethodCache *)(instr + 1);
  717. assert(Py_TYPE(descr) == &PyProperty_Type);
  718. PyObject *fget = ((_PyPropertyObject *)descr)->prop_get;
  719. if (fget == NULL) {
  720. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_EXPECTED_ERROR);
  721. goto fail;
  722. }
  723. if (!Py_IS_TYPE(fget, &PyFunction_Type)) {
  724. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_PROPERTY_NOT_PY_FUNCTION);
  725. goto fail;
  726. }
  727. if (!function_check_args(fget, 1, LOAD_ATTR)) {
  728. goto fail;
  729. }
  730. uint32_t version = function_get_version(fget, LOAD_ATTR);
  731. if (version == 0) {
  732. goto fail;
  733. }
  734. if (_PyInterpreterState_GET()->eval_frame) {
  735. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OTHER);
  736. goto fail;
  737. }
  738. write_u32(lm_cache->keys_version, version);
  739. assert(type->tp_version_tag != 0);
  740. write_u32(lm_cache->type_version, type->tp_version_tag);
  741. /* borrowed */
  742. write_obj(lm_cache->descr, fget);
  743. instr->op.code = LOAD_ATTR_PROPERTY;
  744. goto success;
  745. }
  746. case OBJECT_SLOT:
  747. {
  748. PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
  749. struct PyMemberDef *dmem = member->d_member;
  750. Py_ssize_t offset = dmem->offset;
  751. if (!PyObject_TypeCheck(owner, member->d_common.d_type)) {
  752. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_EXPECTED_ERROR);
  753. goto fail;
  754. }
  755. if (dmem->flags & PY_AUDIT_READ) {
  756. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_AUDITED_SLOT);
  757. goto fail;
  758. }
  759. if (offset != (uint16_t)offset) {
  760. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_RANGE);
  761. goto fail;
  762. }
  763. assert(dmem->type == T_OBJECT_EX);
  764. assert(offset > 0);
  765. cache->index = (uint16_t)offset;
  766. write_u32(cache->version, type->tp_version_tag);
  767. instr->op.code = LOAD_ATTR_SLOT;
  768. goto success;
  769. }
  770. case DUNDER_CLASS:
  771. {
  772. Py_ssize_t offset = offsetof(PyObject, ob_type);
  773. assert(offset == (uint16_t)offset);
  774. cache->index = (uint16_t)offset;
  775. write_u32(cache->version, type->tp_version_tag);
  776. instr->op.code = LOAD_ATTR_SLOT;
  777. goto success;
  778. }
  779. case OTHER_SLOT:
  780. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_NON_OBJECT_SLOT);
  781. goto fail;
  782. case MUTABLE:
  783. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_MUTABLE_CLASS);
  784. goto fail;
  785. case GETSET_OVERRIDDEN:
  786. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OVERRIDDEN);
  787. goto fail;
  788. case GETATTRIBUTE_IS_PYTHON_FUNCTION:
  789. {
  790. assert(type->tp_getattro == _Py_slot_tp_getattro);
  791. assert(Py_IS_TYPE(descr, &PyFunction_Type));
  792. _PyLoadMethodCache *lm_cache = (_PyLoadMethodCache *)(instr + 1);
  793. if (!function_check_args(descr, 2, LOAD_ATTR)) {
  794. goto fail;
  795. }
  796. uint32_t version = function_get_version(descr, LOAD_ATTR);
  797. if (version == 0) {
  798. goto fail;
  799. }
  800. if (_PyInterpreterState_GET()->eval_frame) {
  801. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OTHER);
  802. goto fail;
  803. }
  804. write_u32(lm_cache->keys_version, version);
  805. /* borrowed */
  806. write_obj(lm_cache->descr, descr);
  807. write_u32(lm_cache->type_version, type->tp_version_tag);
  808. instr->op.code = LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN;
  809. goto success;
  810. }
  811. case BUILTIN_CLASSMETHOD:
  812. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_BUILTIN_CLASS_METHOD_OBJ);
  813. goto fail;
  814. case PYTHON_CLASSMETHOD:
  815. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_CLASS_METHOD_OBJ);
  816. goto fail;
  817. case NON_OVERRIDING:
  818. SPECIALIZATION_FAIL(LOAD_ATTR,
  819. (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) ?
  820. SPEC_FAIL_ATTR_CLASS_ATTR_DESCRIPTOR :
  821. SPEC_FAIL_ATTR_NOT_MANAGED_DICT);
  822. goto fail;
  823. case NON_DESCRIPTOR:
  824. SPECIALIZATION_FAIL(LOAD_ATTR,
  825. (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) ?
  826. SPEC_FAIL_ATTR_CLASS_ATTR_SIMPLE :
  827. SPEC_FAIL_ATTR_NOT_MANAGED_DICT);
  828. goto fail;
  829. case ABSENT:
  830. if (specialize_dict_access(owner, instr, type, kind, name, LOAD_ATTR,
  831. LOAD_ATTR_INSTANCE_VALUE, LOAD_ATTR_WITH_HINT))
  832. {
  833. goto success;
  834. }
  835. }
  836. fail:
  837. STAT_INC(LOAD_ATTR, failure);
  838. assert(!PyErr_Occurred());
  839. instr->op.code = LOAD_ATTR;
  840. cache->counter = adaptive_counter_backoff(cache->counter);
  841. return;
  842. success:
  843. STAT_INC(LOAD_ATTR, success);
  844. assert(!PyErr_Occurred());
  845. cache->counter = adaptive_counter_cooldown();
  846. }
  847. void
  848. _Py_Specialize_StoreAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name)
  849. {
  850. assert(ENABLE_SPECIALIZATION);
  851. assert(_PyOpcode_Caches[STORE_ATTR] == INLINE_CACHE_ENTRIES_STORE_ATTR);
  852. _PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
  853. PyTypeObject *type = Py_TYPE(owner);
  854. if (!_PyType_IsReady(type)) {
  855. // We *might* not really need this check, but we inherited it from
  856. // PyObject_GenericSetAttr and friends... and this way we still do the
  857. // right thing if someone forgets to call PyType_Ready(type):
  858. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_OTHER);
  859. goto fail;
  860. }
  861. if (PyModule_CheckExact(owner)) {
  862. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_OVERRIDDEN);
  863. goto fail;
  864. }
  865. PyObject *descr;
  866. DescriptorClassification kind = analyze_descriptor(type, name, &descr, 1);
  867. if (type_get_version(type, STORE_ATTR) == 0) {
  868. goto fail;
  869. }
  870. switch(kind) {
  871. case OVERRIDING:
  872. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR);
  873. goto fail;
  874. case METHOD:
  875. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_METHOD);
  876. goto fail;
  877. case PROPERTY:
  878. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_PROPERTY);
  879. goto fail;
  880. case OBJECT_SLOT:
  881. {
  882. PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
  883. struct PyMemberDef *dmem = member->d_member;
  884. Py_ssize_t offset = dmem->offset;
  885. if (!PyObject_TypeCheck(owner, member->d_common.d_type)) {
  886. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_EXPECTED_ERROR);
  887. goto fail;
  888. }
  889. if (dmem->flags & READONLY) {
  890. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_READ_ONLY);
  891. goto fail;
  892. }
  893. if (offset != (uint16_t)offset) {
  894. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_OUT_OF_RANGE);
  895. goto fail;
  896. }
  897. assert(dmem->type == T_OBJECT_EX);
  898. assert(offset > 0);
  899. cache->index = (uint16_t)offset;
  900. write_u32(cache->version, type->tp_version_tag);
  901. instr->op.code = STORE_ATTR_SLOT;
  902. goto success;
  903. }
  904. case DUNDER_CLASS:
  905. case OTHER_SLOT:
  906. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_NON_OBJECT_SLOT);
  907. goto fail;
  908. case MUTABLE:
  909. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_MUTABLE_CLASS);
  910. goto fail;
  911. case GETATTRIBUTE_IS_PYTHON_FUNCTION:
  912. case GETSET_OVERRIDDEN:
  913. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_OVERRIDDEN);
  914. goto fail;
  915. case BUILTIN_CLASSMETHOD:
  916. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_BUILTIN_CLASS_METHOD_OBJ);
  917. goto fail;
  918. case PYTHON_CLASSMETHOD:
  919. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_CLASS_METHOD_OBJ);
  920. goto fail;
  921. case NON_OVERRIDING:
  922. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_CLASS_ATTR_DESCRIPTOR);
  923. goto fail;
  924. case NON_DESCRIPTOR:
  925. SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_CLASS_ATTR_SIMPLE);
  926. goto fail;
  927. case ABSENT:
  928. if (specialize_dict_access(owner, instr, type, kind, name, STORE_ATTR,
  929. STORE_ATTR_INSTANCE_VALUE, STORE_ATTR_WITH_HINT))
  930. {
  931. goto success;
  932. }
  933. }
  934. fail:
  935. STAT_INC(STORE_ATTR, failure);
  936. assert(!PyErr_Occurred());
  937. instr->op.code = STORE_ATTR;
  938. cache->counter = adaptive_counter_backoff(cache->counter);
  939. return;
  940. success:
  941. STAT_INC(STORE_ATTR, success);
  942. assert(!PyErr_Occurred());
  943. cache->counter = adaptive_counter_cooldown();
  944. }
  945. #ifdef Py_STATS
  946. static int
  947. load_attr_fail_kind(DescriptorClassification kind)
  948. {
  949. switch (kind) {
  950. case OVERRIDING:
  951. return SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR;
  952. case METHOD:
  953. return SPEC_FAIL_ATTR_METHOD;
  954. case PROPERTY:
  955. return SPEC_FAIL_ATTR_PROPERTY;
  956. case OBJECT_SLOT:
  957. return SPEC_FAIL_ATTR_OBJECT_SLOT;
  958. case OTHER_SLOT:
  959. return SPEC_FAIL_ATTR_NON_OBJECT_SLOT;
  960. case DUNDER_CLASS:
  961. return SPEC_FAIL_OTHER;
  962. case MUTABLE:
  963. return SPEC_FAIL_ATTR_MUTABLE_CLASS;
  964. case GETSET_OVERRIDDEN:
  965. case GETATTRIBUTE_IS_PYTHON_FUNCTION:
  966. return SPEC_FAIL_OVERRIDDEN;
  967. case BUILTIN_CLASSMETHOD:
  968. return SPEC_FAIL_ATTR_BUILTIN_CLASS_METHOD;
  969. case PYTHON_CLASSMETHOD:
  970. return SPEC_FAIL_ATTR_CLASS_METHOD_OBJ;
  971. case NON_OVERRIDING:
  972. return SPEC_FAIL_ATTR_NON_OVERRIDING_DESCRIPTOR;
  973. case NON_DESCRIPTOR:
  974. return SPEC_FAIL_ATTR_NOT_DESCRIPTOR;
  975. case ABSENT:
  976. return SPEC_FAIL_ATTR_INSTANCE_ATTRIBUTE;
  977. }
  978. Py_UNREACHABLE();
  979. }
  980. #endif
  981. static int
  982. specialize_class_load_attr(PyObject *owner, _Py_CODEUNIT *instr,
  983. PyObject *name)
  984. {
  985. _PyLoadMethodCache *cache = (_PyLoadMethodCache *)(instr + 1);
  986. if (!PyType_CheckExact(owner) || _PyType_Lookup(Py_TYPE(owner), name)) {
  987. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_METACLASS_ATTRIBUTE);
  988. return -1;
  989. }
  990. PyObject *descr = NULL;
  991. DescriptorClassification kind = 0;
  992. kind = analyze_descriptor((PyTypeObject *)owner, name, &descr, 0);
  993. if (type_get_version((PyTypeObject *)owner, LOAD_ATTR) == 0) {
  994. return -1;
  995. }
  996. switch (kind) {
  997. case METHOD:
  998. case NON_DESCRIPTOR:
  999. write_u32(cache->type_version, ((PyTypeObject *)owner)->tp_version_tag);
  1000. write_obj(cache->descr, descr);
  1001. instr->op.code = LOAD_ATTR_CLASS;
  1002. return 0;
  1003. #ifdef Py_STATS
  1004. case ABSENT:
  1005. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_EXPECTED_ERROR);
  1006. return -1;
  1007. #endif
  1008. default:
  1009. SPECIALIZATION_FAIL(LOAD_ATTR, load_attr_fail_kind(kind));
  1010. return -1;
  1011. }
  1012. }
  1013. // Please collect stats carefully before and after modifying. A subtle change
  1014. // can cause a significant drop in cache hits. A possible test is
  1015. // python.exe -m test_typing test_re test_dis test_zlib.
  1016. static int
  1017. specialize_attr_loadmethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name,
  1018. PyObject *descr, DescriptorClassification kind)
  1019. {
  1020. _PyLoadMethodCache *cache = (_PyLoadMethodCache *)(instr + 1);
  1021. PyTypeObject *owner_cls = Py_TYPE(owner);
  1022. assert(kind == METHOD && descr != NULL);
  1023. if (owner_cls->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
  1024. PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(owner);
  1025. PyDictKeysObject *keys = ((PyHeapTypeObject *)owner_cls)->ht_cached_keys;
  1026. if (!_PyDictOrValues_IsValues(dorv)) {
  1027. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_HAS_MANAGED_DICT);
  1028. return 0;
  1029. }
  1030. Py_ssize_t index = _PyDictKeys_StringLookup(keys, name);
  1031. if (index != DKIX_EMPTY) {
  1032. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_SHADOWED);
  1033. return 0;
  1034. }
  1035. uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(
  1036. _PyInterpreterState_GET(), keys);
  1037. if (keys_version == 0) {
  1038. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_VERSIONS);
  1039. return 0;
  1040. }
  1041. write_u32(cache->keys_version, keys_version);
  1042. instr->op.code = LOAD_ATTR_METHOD_WITH_VALUES;
  1043. }
  1044. else {
  1045. Py_ssize_t dictoffset = owner_cls->tp_dictoffset;
  1046. if (dictoffset < 0 || dictoffset > INT16_MAX) {
  1047. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_RANGE);
  1048. return 0;
  1049. }
  1050. if (dictoffset == 0) {
  1051. instr->op.code = LOAD_ATTR_METHOD_NO_DICT;
  1052. }
  1053. else {
  1054. PyObject *dict = *(PyObject **) ((char *)owner + dictoffset);
  1055. if (dict) {
  1056. SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_NOT_MANAGED_DICT);
  1057. return 0;
  1058. }
  1059. assert(owner_cls->tp_dictoffset > 0);
  1060. assert(owner_cls->tp_dictoffset <= INT16_MAX);
  1061. instr->op.code = LOAD_ATTR_METHOD_LAZY_DICT;
  1062. }
  1063. }
  1064. /* `descr` is borrowed. This is safe for methods (even inherited ones from
  1065. * super classes!) as long as tp_version_tag is validated for two main reasons:
  1066. *
  1067. * 1. The class will always hold a reference to the method so it will
  1068. * usually not be GC-ed. Should it be deleted in Python, e.g.
  1069. * `del obj.meth`, tp_version_tag will be invalidated, because of reason 2.
  1070. *
  1071. * 2. The pre-existing type method cache (MCACHE) uses the same principles
  1072. * of caching a borrowed descriptor. The MCACHE infrastructure does all the
  1073. * heavy lifting for us. E.g. it invalidates tp_version_tag on any MRO
  1074. * modification, on any type object change along said MRO, etc. (see
  1075. * PyType_Modified usages in typeobject.c). The MCACHE has been
  1076. * working since Python 2.6 and it's battle-tested.
  1077. */
  1078. write_u32(cache->type_version, owner_cls->tp_version_tag);
  1079. write_obj(cache->descr, descr);
  1080. return 1;
  1081. }
  1082. void
  1083. _Py_Specialize_LoadGlobal(
  1084. PyObject *globals, PyObject *builtins,
  1085. _Py_CODEUNIT *instr, PyObject *name)
  1086. {
  1087. assert(ENABLE_SPECIALIZATION);
  1088. assert(_PyOpcode_Caches[LOAD_GLOBAL] == INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
  1089. /* Use inline cache */
  1090. _PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)(instr + 1);
  1091. assert(PyUnicode_CheckExact(name));
  1092. if (!PyDict_CheckExact(globals)) {
  1093. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_DICT);
  1094. goto fail;
  1095. }
  1096. PyDictKeysObject * globals_keys = ((PyDictObject *)globals)->ma_keys;
  1097. if (!DK_IS_UNICODE(globals_keys)) {
  1098. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT);
  1099. goto fail;
  1100. }
  1101. Py_ssize_t index = _PyDictKeys_StringLookup(globals_keys, name);
  1102. if (index == DKIX_ERROR) {
  1103. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_EXPECTED_ERROR);
  1104. goto fail;
  1105. }
  1106. PyInterpreterState *interp = _PyInterpreterState_GET();
  1107. if (index != DKIX_EMPTY) {
  1108. if (index != (uint16_t)index) {
  1109. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
  1110. goto fail;
  1111. }
  1112. uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(
  1113. interp, globals_keys);
  1114. if (keys_version == 0) {
  1115. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
  1116. goto fail;
  1117. }
  1118. if (keys_version != (uint16_t)keys_version) {
  1119. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
  1120. goto fail;
  1121. }
  1122. cache->index = (uint16_t)index;
  1123. cache->module_keys_version = (uint16_t)keys_version;
  1124. instr->op.code = LOAD_GLOBAL_MODULE;
  1125. goto success;
  1126. }
  1127. if (!PyDict_CheckExact(builtins)) {
  1128. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_DICT);
  1129. goto fail;
  1130. }
  1131. PyDictKeysObject * builtin_keys = ((PyDictObject *)builtins)->ma_keys;
  1132. if (!DK_IS_UNICODE(builtin_keys)) {
  1133. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT);
  1134. goto fail;
  1135. }
  1136. index = _PyDictKeys_StringLookup(builtin_keys, name);
  1137. if (index == DKIX_ERROR) {
  1138. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_EXPECTED_ERROR);
  1139. goto fail;
  1140. }
  1141. if (index != (uint16_t)index) {
  1142. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
  1143. goto fail;
  1144. }
  1145. uint32_t globals_version = _PyDictKeys_GetVersionForCurrentState(
  1146. interp, globals_keys);
  1147. if (globals_version == 0) {
  1148. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
  1149. goto fail;
  1150. }
  1151. if (globals_version != (uint16_t)globals_version) {
  1152. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
  1153. goto fail;
  1154. }
  1155. uint32_t builtins_version = _PyDictKeys_GetVersionForCurrentState(
  1156. interp, builtin_keys);
  1157. if (builtins_version == 0) {
  1158. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
  1159. goto fail;
  1160. }
  1161. if (builtins_version > UINT16_MAX) {
  1162. SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
  1163. goto fail;
  1164. }
  1165. cache->index = (uint16_t)index;
  1166. cache->module_keys_version = (uint16_t)globals_version;
  1167. cache->builtin_keys_version = (uint16_t)builtins_version;
  1168. instr->op.code = LOAD_GLOBAL_BUILTIN;
  1169. goto success;
  1170. fail:
  1171. STAT_INC(LOAD_GLOBAL, failure);
  1172. assert(!PyErr_Occurred());
  1173. instr->op.code = LOAD_GLOBAL;
  1174. cache->counter = adaptive_counter_backoff(cache->counter);
  1175. return;
  1176. success:
  1177. STAT_INC(LOAD_GLOBAL, success);
  1178. assert(!PyErr_Occurred());
  1179. cache->counter = adaptive_counter_cooldown();
  1180. }
  1181. #ifdef Py_STATS
  1182. static int
  1183. binary_subscr_fail_kind(PyTypeObject *container_type, PyObject *sub)
  1184. {
  1185. if (container_type == &PyUnicode_Type) {
  1186. if (PyLong_CheckExact(sub)) {
  1187. return SPEC_FAIL_SUBSCR_STRING_INT;
  1188. }
  1189. if (PySlice_Check(sub)) {
  1190. return SPEC_FAIL_SUBSCR_STRING_SLICE;
  1191. }
  1192. return SPEC_FAIL_OTHER;
  1193. }
  1194. else if (strcmp(container_type->tp_name, "array.array") == 0) {
  1195. if (PyLong_CheckExact(sub)) {
  1196. return SPEC_FAIL_SUBSCR_ARRAY_INT;
  1197. }
  1198. if (PySlice_Check(sub)) {
  1199. return SPEC_FAIL_SUBSCR_ARRAY_SLICE;
  1200. }
  1201. return SPEC_FAIL_OTHER;
  1202. }
  1203. else if (container_type->tp_as_buffer) {
  1204. if (PyLong_CheckExact(sub)) {
  1205. return SPEC_FAIL_SUBSCR_BUFFER_INT;
  1206. }
  1207. if (PySlice_Check(sub)) {
  1208. return SPEC_FAIL_SUBSCR_BUFFER_SLICE;
  1209. }
  1210. return SPEC_FAIL_OTHER;
  1211. }
  1212. else if (container_type->tp_as_sequence) {
  1213. if (PyLong_CheckExact(sub) && container_type->tp_as_sequence->sq_item) {
  1214. return SPEC_FAIL_SUBSCR_SEQUENCE_INT;
  1215. }
  1216. }
  1217. return SPEC_FAIL_OTHER;
  1218. }
  1219. #endif
  1220. static int
  1221. function_kind(PyCodeObject *code) {
  1222. int flags = code->co_flags;
  1223. if ((flags & (CO_VARKEYWORDS | CO_VARARGS)) || code->co_kwonlyargcount) {
  1224. return SPEC_FAIL_CODE_COMPLEX_PARAMETERS;
  1225. }
  1226. if ((flags & CO_OPTIMIZED) == 0) {
  1227. return SPEC_FAIL_CODE_NOT_OPTIMIZED;
  1228. }
  1229. return SIMPLE_FUNCTION;
  1230. }
  1231. /* Returning false indicates a failure. */
  1232. static bool
  1233. function_check_args(PyObject *o, int expected_argcount, int opcode)
  1234. {
  1235. assert(Py_IS_TYPE(o, &PyFunction_Type));
  1236. PyFunctionObject *func = (PyFunctionObject *)o;
  1237. PyCodeObject *fcode = (PyCodeObject *)func->func_code;
  1238. int kind = function_kind(fcode);
  1239. if (kind != SIMPLE_FUNCTION) {
  1240. SPECIALIZATION_FAIL(opcode, kind);
  1241. return false;
  1242. }
  1243. if (fcode->co_argcount != expected_argcount) {
  1244. SPECIALIZATION_FAIL(opcode, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
  1245. return false;
  1246. }
  1247. return true;
  1248. }
  1249. /* Returning 0 indicates a failure. */
  1250. static uint32_t
  1251. function_get_version(PyObject *o, int opcode)
  1252. {
  1253. assert(Py_IS_TYPE(o, &PyFunction_Type));
  1254. PyFunctionObject *func = (PyFunctionObject *)o;
  1255. uint32_t version = _PyFunction_GetVersionForCurrentState(func);
  1256. if (version == 0) {
  1257. SPECIALIZATION_FAIL(opcode, SPEC_FAIL_OUT_OF_VERSIONS);
  1258. return 0;
  1259. }
  1260. return version;
  1261. }
  1262. /* Returning 0 indicates a failure. */
  1263. static uint32_t
  1264. type_get_version(PyTypeObject *t, int opcode)
  1265. {
  1266. uint32_t version = t->tp_version_tag;
  1267. if (version == 0) {
  1268. SPECIALIZATION_FAIL(opcode, SPEC_FAIL_OUT_OF_VERSIONS);
  1269. return 0;
  1270. }
  1271. return version;
  1272. }
  1273. void
  1274. _Py_Specialize_BinarySubscr(
  1275. PyObject *container, PyObject *sub, _Py_CODEUNIT *instr)
  1276. {
  1277. assert(ENABLE_SPECIALIZATION);
  1278. assert(_PyOpcode_Caches[BINARY_SUBSCR] ==
  1279. INLINE_CACHE_ENTRIES_BINARY_SUBSCR);
  1280. _PyBinarySubscrCache *cache = (_PyBinarySubscrCache *)(instr + 1);
  1281. PyTypeObject *container_type = Py_TYPE(container);
  1282. if (container_type == &PyList_Type) {
  1283. if (PyLong_CheckExact(sub)) {
  1284. if (_PyLong_IsNonNegativeCompact((PyLongObject *)sub)) {
  1285. instr->op.code = BINARY_SUBSCR_LIST_INT;
  1286. goto success;
  1287. }
  1288. SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_RANGE);
  1289. goto fail;
  1290. }
  1291. SPECIALIZATION_FAIL(BINARY_SUBSCR,
  1292. PySlice_Check(sub) ? SPEC_FAIL_SUBSCR_LIST_SLICE : SPEC_FAIL_OTHER);
  1293. goto fail;
  1294. }
  1295. if (container_type == &PyTuple_Type) {
  1296. if (PyLong_CheckExact(sub)) {
  1297. if (_PyLong_IsNonNegativeCompact((PyLongObject *)sub)) {
  1298. instr->op.code = BINARY_SUBSCR_TUPLE_INT;
  1299. goto success;
  1300. }
  1301. SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_RANGE);
  1302. goto fail;
  1303. }
  1304. SPECIALIZATION_FAIL(BINARY_SUBSCR,
  1305. PySlice_Check(sub) ? SPEC_FAIL_SUBSCR_TUPLE_SLICE : SPEC_FAIL_OTHER);
  1306. goto fail;
  1307. }
  1308. if (container_type == &PyDict_Type) {
  1309. instr->op.code = BINARY_SUBSCR_DICT;
  1310. goto success;
  1311. }
  1312. PyTypeObject *cls = Py_TYPE(container);
  1313. PyObject *descriptor = _PyType_Lookup(cls, &_Py_ID(__getitem__));
  1314. if (descriptor && Py_TYPE(descriptor) == &PyFunction_Type) {
  1315. if (!(container_type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
  1316. SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_SUBSCR_NOT_HEAP_TYPE);
  1317. goto fail;
  1318. }
  1319. PyFunctionObject *func = (PyFunctionObject *)descriptor;
  1320. PyCodeObject *fcode = (PyCodeObject *)func->func_code;
  1321. int kind = function_kind(fcode);
  1322. if (kind != SIMPLE_FUNCTION) {
  1323. SPECIALIZATION_FAIL(BINARY_SUBSCR, kind);
  1324. goto fail;
  1325. }
  1326. if (fcode->co_argcount != 2) {
  1327. SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
  1328. goto fail;
  1329. }
  1330. uint32_t version = _PyFunction_GetVersionForCurrentState(func);
  1331. if (version == 0) {
  1332. SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_VERSIONS);
  1333. goto fail;
  1334. }
  1335. if (_PyInterpreterState_GET()->eval_frame) {
  1336. SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OTHER);
  1337. goto fail;
  1338. }
  1339. PyHeapTypeObject *ht = (PyHeapTypeObject *)container_type;
  1340. // This pointer is invalidated by PyType_Modified (see the comment on
  1341. // struct _specialization_cache):
  1342. ht->_spec_cache.getitem = descriptor;
  1343. ht->_spec_cache.getitem_version = version;
  1344. instr->op.code = BINARY_SUBSCR_GETITEM;
  1345. goto success;
  1346. }
  1347. SPECIALIZATION_FAIL(BINARY_SUBSCR,
  1348. binary_subscr_fail_kind(container_type, sub));
  1349. fail:
  1350. STAT_INC(BINARY_SUBSCR, failure);
  1351. assert(!PyErr_Occurred());
  1352. instr->op.code = BINARY_SUBSCR;
  1353. cache->counter = adaptive_counter_backoff(cache->counter);
  1354. return;
  1355. success:
  1356. STAT_INC(BINARY_SUBSCR, success);
  1357. assert(!PyErr_Occurred());
  1358. cache->counter = adaptive_counter_cooldown();
  1359. }
  1360. void
  1361. _Py_Specialize_StoreSubscr(PyObject *container, PyObject *sub, _Py_CODEUNIT *instr)
  1362. {
  1363. assert(ENABLE_SPECIALIZATION);
  1364. _PyStoreSubscrCache *cache = (_PyStoreSubscrCache *)(instr + 1);
  1365. PyTypeObject *container_type = Py_TYPE(container);
  1366. if (container_type == &PyList_Type) {
  1367. if (PyLong_CheckExact(sub)) {
  1368. if (_PyLong_IsNonNegativeCompact((PyLongObject *)sub)
  1369. && ((PyLongObject *)sub)->long_value.ob_digit[0] < (size_t)PyList_GET_SIZE(container))
  1370. {
  1371. instr->op.code = STORE_SUBSCR_LIST_INT;
  1372. goto success;
  1373. }
  1374. else {
  1375. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_OUT_OF_RANGE);
  1376. goto fail;
  1377. }
  1378. }
  1379. else if (PySlice_Check(sub)) {
  1380. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_SUBSCR_LIST_SLICE);
  1381. goto fail;
  1382. }
  1383. else {
  1384. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_OTHER);
  1385. goto fail;
  1386. }
  1387. }
  1388. if (container_type == &PyDict_Type) {
  1389. instr->op.code = STORE_SUBSCR_DICT;
  1390. goto success;
  1391. }
  1392. #ifdef Py_STATS
  1393. PyMappingMethods *as_mapping = container_type->tp_as_mapping;
  1394. if (as_mapping && (as_mapping->mp_ass_subscript
  1395. == PyDict_Type.tp_as_mapping->mp_ass_subscript)) {
  1396. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_SUBSCR_DICT_SUBCLASS_NO_OVERRIDE);
  1397. goto fail;
  1398. }
  1399. if (PyObject_CheckBuffer(container)) {
  1400. if (PyLong_CheckExact(sub) && (!_PyLong_IsNonNegativeCompact((PyLongObject *)sub))) {
  1401. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_OUT_OF_RANGE);
  1402. }
  1403. else if (strcmp(container_type->tp_name, "array.array") == 0) {
  1404. if (PyLong_CheckExact(sub)) {
  1405. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_SUBSCR_ARRAY_INT);
  1406. }
  1407. else if (PySlice_Check(sub)) {
  1408. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_SUBSCR_ARRAY_SLICE);
  1409. }
  1410. else {
  1411. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_OTHER);
  1412. }
  1413. }
  1414. else if (PyByteArray_CheckExact(container)) {
  1415. if (PyLong_CheckExact(sub)) {
  1416. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_SUBSCR_BYTEARRAY_INT);
  1417. }
  1418. else if (PySlice_Check(sub)) {
  1419. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_SUBSCR_BYTEARRAY_SLICE);
  1420. }
  1421. else {
  1422. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_OTHER);
  1423. }
  1424. }
  1425. else {
  1426. if (PyLong_CheckExact(sub)) {
  1427. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_SUBSCR_BUFFER_INT);
  1428. }
  1429. else if (PySlice_Check(sub)) {
  1430. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_SUBSCR_BUFFER_SLICE);
  1431. }
  1432. else {
  1433. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_OTHER);
  1434. }
  1435. }
  1436. goto fail;
  1437. }
  1438. PyObject *descriptor = _PyType_Lookup(container_type, &_Py_ID(__setitem__));
  1439. if (descriptor && Py_TYPE(descriptor) == &PyFunction_Type) {
  1440. PyFunctionObject *func = (PyFunctionObject *)descriptor;
  1441. PyCodeObject *code = (PyCodeObject *)func->func_code;
  1442. int kind = function_kind(code);
  1443. if (kind == SIMPLE_FUNCTION) {
  1444. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_SUBSCR_PY_SIMPLE);
  1445. }
  1446. else {
  1447. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_SUBSCR_PY_OTHER);
  1448. }
  1449. goto fail;
  1450. }
  1451. #endif
  1452. SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_OTHER);
  1453. fail:
  1454. STAT_INC(STORE_SUBSCR, failure);
  1455. assert(!PyErr_Occurred());
  1456. instr->op.code = STORE_SUBSCR;
  1457. cache->counter = adaptive_counter_backoff(cache->counter);
  1458. return;
  1459. success:
  1460. STAT_INC(STORE_SUBSCR, success);
  1461. assert(!PyErr_Occurred());
  1462. cache->counter = adaptive_counter_cooldown();
  1463. }
  1464. static int
  1465. specialize_class_call(PyObject *callable, _Py_CODEUNIT *instr, int nargs,
  1466. PyObject *kwnames)
  1467. {
  1468. PyTypeObject *tp = _PyType_CAST(callable);
  1469. if (tp->tp_new == PyBaseObject_Type.tp_new) {
  1470. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_PYTHON_CLASS);
  1471. return -1;
  1472. }
  1473. if (tp->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) {
  1474. int oparg = instr->op.arg;
  1475. if (nargs == 1 && kwnames == NULL && oparg == 1) {
  1476. if (tp == &PyUnicode_Type) {
  1477. instr->op.code = CALL_NO_KW_STR_1;
  1478. return 0;
  1479. }
  1480. else if (tp == &PyType_Type) {
  1481. instr->op.code = CALL_NO_KW_TYPE_1;
  1482. return 0;
  1483. }
  1484. else if (tp == &PyTuple_Type) {
  1485. instr->op.code = CALL_NO_KW_TUPLE_1;
  1486. return 0;
  1487. }
  1488. }
  1489. if (tp->tp_vectorcall != NULL) {
  1490. instr->op.code = CALL_BUILTIN_CLASS;
  1491. return 0;
  1492. }
  1493. SPECIALIZATION_FAIL(CALL, tp == &PyUnicode_Type ?
  1494. SPEC_FAIL_CALL_STR : SPEC_FAIL_CALL_CLASS_NO_VECTORCALL);
  1495. return -1;
  1496. }
  1497. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_CLASS_MUTABLE);
  1498. return -1;
  1499. }
  1500. #ifdef Py_STATS
  1501. static int
  1502. builtin_call_fail_kind(int ml_flags)
  1503. {
  1504. switch (ml_flags & (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O |
  1505. METH_KEYWORDS | METH_METHOD)) {
  1506. case METH_VARARGS:
  1507. return SPEC_FAIL_CALL_CFUNC_VARARGS;
  1508. case METH_VARARGS | METH_KEYWORDS:
  1509. return SPEC_FAIL_CALL_CFUNC_VARARGS_KEYWORDS;
  1510. case METH_NOARGS:
  1511. return SPEC_FAIL_CALL_CFUNC_NOARGS;
  1512. case METH_METHOD | METH_FASTCALL | METH_KEYWORDS:
  1513. return SPEC_FAIL_CALL_CFUNC_METHOD_FASTCALL_KEYWORDS;
  1514. /* These cases should be optimized, but return "other" just in case */
  1515. case METH_O:
  1516. case METH_FASTCALL:
  1517. case METH_FASTCALL | METH_KEYWORDS:
  1518. return SPEC_FAIL_OTHER;
  1519. default:
  1520. return SPEC_FAIL_CALL_BAD_CALL_FLAGS;
  1521. }
  1522. }
  1523. static int
  1524. meth_descr_call_fail_kind(int ml_flags)
  1525. {
  1526. switch (ml_flags & (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O |
  1527. METH_KEYWORDS | METH_METHOD)) {
  1528. case METH_VARARGS:
  1529. return SPEC_FAIL_CALL_METH_DESCR_VARARGS;
  1530. case METH_VARARGS | METH_KEYWORDS:
  1531. return SPEC_FAIL_CALL_METH_DESCR_VARARGS_KEYWORDS;
  1532. case METH_METHOD | METH_FASTCALL | METH_KEYWORDS:
  1533. return SPEC_FAIL_CALL_METH_DESCR_METHOD_FASTCALL_KEYWORDS;
  1534. /* These cases should be optimized, but return "other" just in case */
  1535. case METH_NOARGS:
  1536. case METH_O:
  1537. case METH_FASTCALL:
  1538. case METH_FASTCALL | METH_KEYWORDS:
  1539. return SPEC_FAIL_OTHER;
  1540. default:
  1541. return SPEC_FAIL_CALL_BAD_CALL_FLAGS;
  1542. }
  1543. }
  1544. #endif
  1545. static int
  1546. specialize_method_descriptor(PyMethodDescrObject *descr, _Py_CODEUNIT *instr,
  1547. int nargs, PyObject *kwnames)
  1548. {
  1549. if (kwnames) {
  1550. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_KWNAMES);
  1551. return -1;
  1552. }
  1553. switch (descr->d_method->ml_flags &
  1554. (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O |
  1555. METH_KEYWORDS | METH_METHOD)) {
  1556. case METH_NOARGS: {
  1557. if (nargs != 1) {
  1558. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
  1559. return -1;
  1560. }
  1561. instr->op.code = CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS;
  1562. return 0;
  1563. }
  1564. case METH_O: {
  1565. if (nargs != 2) {
  1566. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
  1567. return -1;
  1568. }
  1569. PyInterpreterState *interp = _PyInterpreterState_GET();
  1570. PyObject *list_append = interp->callable_cache.list_append;
  1571. _Py_CODEUNIT next = instr[INLINE_CACHE_ENTRIES_CALL + 1];
  1572. bool pop = (next.op.code == POP_TOP);
  1573. int oparg = instr->op.arg;
  1574. if ((PyObject *)descr == list_append && oparg == 1 && pop) {
  1575. instr->op.code = CALL_NO_KW_LIST_APPEND;
  1576. return 0;
  1577. }
  1578. instr->op.code = CALL_NO_KW_METHOD_DESCRIPTOR_O;
  1579. return 0;
  1580. }
  1581. case METH_FASTCALL: {
  1582. instr->op.code = CALL_NO_KW_METHOD_DESCRIPTOR_FAST;
  1583. return 0;
  1584. }
  1585. case METH_FASTCALL | METH_KEYWORDS: {
  1586. instr->op.code = CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS;
  1587. return 0;
  1588. }
  1589. }
  1590. SPECIALIZATION_FAIL(CALL, meth_descr_call_fail_kind(descr->d_method->ml_flags));
  1591. return -1;
  1592. }
  1593. static int
  1594. specialize_py_call(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs,
  1595. PyObject *kwnames, bool bound_method)
  1596. {
  1597. _PyCallCache *cache = (_PyCallCache *)(instr + 1);
  1598. PyCodeObject *code = (PyCodeObject *)func->func_code;
  1599. int kind = function_kind(code);
  1600. /* Don't specialize if PEP 523 is active */
  1601. if (_PyInterpreterState_GET()->eval_frame) {
  1602. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_PEP_523);
  1603. return -1;
  1604. }
  1605. if (kwnames) {
  1606. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_KWNAMES);
  1607. return -1;
  1608. }
  1609. if (kind != SIMPLE_FUNCTION) {
  1610. SPECIALIZATION_FAIL(CALL, kind);
  1611. return -1;
  1612. }
  1613. int argcount = code->co_argcount;
  1614. int defcount = func->func_defaults == NULL ? 0 : (int)PyTuple_GET_SIZE(func->func_defaults);
  1615. int min_args = argcount-defcount;
  1616. // GH-105840: min_args is negative when somebody sets too many __defaults__!
  1617. if (min_args < 0 || nargs > argcount || nargs < min_args) {
  1618. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
  1619. return -1;
  1620. }
  1621. assert(nargs <= argcount && nargs >= min_args);
  1622. assert(min_args >= 0 && defcount >= 0);
  1623. assert(defcount == 0 || func->func_defaults != NULL);
  1624. int version = _PyFunction_GetVersionForCurrentState(func);
  1625. if (version == 0) {
  1626. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_OUT_OF_VERSIONS);
  1627. return -1;
  1628. }
  1629. write_u32(cache->func_version, version);
  1630. if (argcount == nargs) {
  1631. instr->op.code = bound_method ? CALL_BOUND_METHOD_EXACT_ARGS : CALL_PY_EXACT_ARGS;
  1632. }
  1633. else if (bound_method) {
  1634. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_BOUND_METHOD);
  1635. return -1;
  1636. }
  1637. else {
  1638. instr->op.code = CALL_PY_WITH_DEFAULTS;
  1639. }
  1640. return 0;
  1641. }
  1642. static int
  1643. specialize_c_call(PyObject *callable, _Py_CODEUNIT *instr, int nargs,
  1644. PyObject *kwnames)
  1645. {
  1646. if (PyCFunction_GET_FUNCTION(callable) == NULL) {
  1647. return 1;
  1648. }
  1649. switch (PyCFunction_GET_FLAGS(callable) &
  1650. (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O |
  1651. METH_KEYWORDS | METH_METHOD)) {
  1652. case METH_O: {
  1653. if (kwnames) {
  1654. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_KWNAMES);
  1655. return -1;
  1656. }
  1657. if (nargs != 1) {
  1658. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
  1659. return 1;
  1660. }
  1661. /* len(o) */
  1662. PyInterpreterState *interp = _PyInterpreterState_GET();
  1663. if (callable == interp->callable_cache.len) {
  1664. instr->op.code = CALL_NO_KW_LEN;
  1665. return 0;
  1666. }
  1667. instr->op.code = CALL_NO_KW_BUILTIN_O;
  1668. return 0;
  1669. }
  1670. case METH_FASTCALL: {
  1671. if (kwnames) {
  1672. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_KWNAMES);
  1673. return -1;
  1674. }
  1675. if (nargs == 2) {
  1676. /* isinstance(o1, o2) */
  1677. PyInterpreterState *interp = _PyInterpreterState_GET();
  1678. if (callable == interp->callable_cache.isinstance) {
  1679. instr->op.code = CALL_NO_KW_ISINSTANCE;
  1680. return 0;
  1681. }
  1682. }
  1683. instr->op.code = CALL_NO_KW_BUILTIN_FAST;
  1684. return 0;
  1685. }
  1686. case METH_FASTCALL | METH_KEYWORDS: {
  1687. instr->op.code = CALL_BUILTIN_FAST_WITH_KEYWORDS;
  1688. return 0;
  1689. }
  1690. default:
  1691. SPECIALIZATION_FAIL(CALL,
  1692. builtin_call_fail_kind(PyCFunction_GET_FLAGS(callable)));
  1693. return 1;
  1694. }
  1695. }
  1696. #ifdef Py_STATS
  1697. static int
  1698. call_fail_kind(PyObject *callable)
  1699. {
  1700. assert(!PyCFunction_CheckExact(callable));
  1701. assert(!PyFunction_Check(callable));
  1702. assert(!PyType_Check(callable));
  1703. assert(!Py_IS_TYPE(callable, &PyMethodDescr_Type));
  1704. assert(!PyMethod_Check(callable));
  1705. if (PyInstanceMethod_Check(callable)) {
  1706. return SPEC_FAIL_CALL_INSTANCE_METHOD;
  1707. }
  1708. // builtin method
  1709. else if (PyCMethod_Check(callable)) {
  1710. return SPEC_FAIL_CALL_CMETHOD;
  1711. }
  1712. else if (Py_TYPE(callable) == &PyWrapperDescr_Type) {
  1713. return SPEC_FAIL_CALL_OPERATOR_WRAPPER;
  1714. }
  1715. else if (Py_TYPE(callable) == &_PyMethodWrapper_Type) {
  1716. return SPEC_FAIL_CALL_METHOD_WRAPPER;
  1717. }
  1718. return SPEC_FAIL_OTHER;
  1719. }
  1720. #endif
  1721. /* TODO:
  1722. - Specialize calling classes.
  1723. */
  1724. void
  1725. _Py_Specialize_Call(PyObject *callable, _Py_CODEUNIT *instr, int nargs,
  1726. PyObject *kwnames)
  1727. {
  1728. assert(ENABLE_SPECIALIZATION);
  1729. assert(_PyOpcode_Caches[CALL] == INLINE_CACHE_ENTRIES_CALL);
  1730. assert(_Py_OPCODE(*instr) != INSTRUMENTED_CALL);
  1731. _PyCallCache *cache = (_PyCallCache *)(instr + 1);
  1732. int fail;
  1733. if (PyCFunction_CheckExact(callable)) {
  1734. fail = specialize_c_call(callable, instr, nargs, kwnames);
  1735. }
  1736. else if (PyFunction_Check(callable)) {
  1737. fail = specialize_py_call((PyFunctionObject *)callable, instr, nargs,
  1738. kwnames, false);
  1739. }
  1740. else if (PyType_Check(callable)) {
  1741. fail = specialize_class_call(callable, instr, nargs, kwnames);
  1742. }
  1743. else if (Py_IS_TYPE(callable, &PyMethodDescr_Type)) {
  1744. fail = specialize_method_descriptor((PyMethodDescrObject *)callable,
  1745. instr, nargs, kwnames);
  1746. }
  1747. else if (PyMethod_Check(callable)) {
  1748. PyObject *func = ((PyMethodObject *)callable)->im_func;
  1749. if (PyFunction_Check(func)) {
  1750. fail = specialize_py_call((PyFunctionObject *)func,
  1751. instr, nargs+1, kwnames, true);
  1752. } else {
  1753. SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_BOUND_METHOD);
  1754. fail = -1;
  1755. }
  1756. }
  1757. else {
  1758. SPECIALIZATION_FAIL(CALL, call_fail_kind(callable));
  1759. fail = -1;
  1760. }
  1761. if (fail) {
  1762. STAT_INC(CALL, failure);
  1763. assert(!PyErr_Occurred());
  1764. instr->op.code = CALL;
  1765. cache->counter = adaptive_counter_backoff(cache->counter);
  1766. }
  1767. else {
  1768. STAT_INC(CALL, success);
  1769. assert(!PyErr_Occurred());
  1770. cache->counter = adaptive_counter_cooldown();
  1771. }
  1772. }
  1773. #ifdef Py_STATS
  1774. static int
  1775. binary_op_fail_kind(int oparg, PyObject *lhs, PyObject *rhs)
  1776. {
  1777. switch (oparg) {
  1778. case NB_ADD:
  1779. case NB_INPLACE_ADD:
  1780. if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
  1781. return SPEC_FAIL_BINARY_OP_ADD_DIFFERENT_TYPES;
  1782. }
  1783. return SPEC_FAIL_BINARY_OP_ADD_OTHER;
  1784. case NB_AND:
  1785. case NB_INPLACE_AND:
  1786. if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
  1787. return SPEC_FAIL_BINARY_OP_AND_DIFFERENT_TYPES;
  1788. }
  1789. if (PyLong_CheckExact(lhs)) {
  1790. return SPEC_FAIL_BINARY_OP_AND_INT;
  1791. }
  1792. return SPEC_FAIL_BINARY_OP_AND_OTHER;
  1793. case NB_FLOOR_DIVIDE:
  1794. case NB_INPLACE_FLOOR_DIVIDE:
  1795. return SPEC_FAIL_BINARY_OP_FLOOR_DIVIDE;
  1796. case NB_LSHIFT:
  1797. case NB_INPLACE_LSHIFT:
  1798. return SPEC_FAIL_BINARY_OP_LSHIFT;
  1799. case NB_MATRIX_MULTIPLY:
  1800. case NB_INPLACE_MATRIX_MULTIPLY:
  1801. return SPEC_FAIL_BINARY_OP_MATRIX_MULTIPLY;
  1802. case NB_MULTIPLY:
  1803. case NB_INPLACE_MULTIPLY:
  1804. if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
  1805. return SPEC_FAIL_BINARY_OP_MULTIPLY_DIFFERENT_TYPES;
  1806. }
  1807. return SPEC_FAIL_BINARY_OP_MULTIPLY_OTHER;
  1808. case NB_OR:
  1809. case NB_INPLACE_OR:
  1810. return SPEC_FAIL_BINARY_OP_OR;
  1811. case NB_POWER:
  1812. case NB_INPLACE_POWER:
  1813. return SPEC_FAIL_BINARY_OP_POWER;
  1814. case NB_REMAINDER:
  1815. case NB_INPLACE_REMAINDER:
  1816. return SPEC_FAIL_BINARY_OP_REMAINDER;
  1817. case NB_RSHIFT:
  1818. case NB_INPLACE_RSHIFT:
  1819. return SPEC_FAIL_BINARY_OP_RSHIFT;
  1820. case NB_SUBTRACT:
  1821. case NB_INPLACE_SUBTRACT:
  1822. if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
  1823. return SPEC_FAIL_BINARY_OP_SUBTRACT_DIFFERENT_TYPES;
  1824. }
  1825. return SPEC_FAIL_BINARY_OP_SUBTRACT_OTHER;
  1826. case NB_TRUE_DIVIDE:
  1827. case NB_INPLACE_TRUE_DIVIDE:
  1828. if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
  1829. return SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_DIFFERENT_TYPES;
  1830. }
  1831. if (PyFloat_CheckExact(lhs)) {
  1832. return SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_FLOAT;
  1833. }
  1834. return SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_OTHER;
  1835. case NB_XOR:
  1836. case NB_INPLACE_XOR:
  1837. return SPEC_FAIL_BINARY_OP_XOR;
  1838. }
  1839. Py_UNREACHABLE();
  1840. }
  1841. #endif
  1842. void
  1843. _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
  1844. int oparg, PyObject **locals)
  1845. {
  1846. assert(ENABLE_SPECIALIZATION);
  1847. assert(_PyOpcode_Caches[BINARY_OP] == INLINE_CACHE_ENTRIES_BINARY_OP);
  1848. _PyBinaryOpCache *cache = (_PyBinaryOpCache *)(instr + 1);
  1849. switch (oparg) {
  1850. case NB_ADD:
  1851. case NB_INPLACE_ADD:
  1852. if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
  1853. break;
  1854. }
  1855. if (PyUnicode_CheckExact(lhs)) {
  1856. _Py_CODEUNIT next = instr[INLINE_CACHE_ENTRIES_BINARY_OP + 1];
  1857. bool to_store = (next.op.code == STORE_FAST ||
  1858. next.op.code == STORE_FAST__LOAD_FAST);
  1859. if (to_store && locals[next.op.arg] == lhs) {
  1860. instr->op.code = BINARY_OP_INPLACE_ADD_UNICODE;
  1861. goto success;
  1862. }
  1863. instr->op.code = BINARY_OP_ADD_UNICODE;
  1864. goto success;
  1865. }
  1866. if (PyLong_CheckExact(lhs)) {
  1867. instr->op.code = BINARY_OP_ADD_INT;
  1868. goto success;
  1869. }
  1870. if (PyFloat_CheckExact(lhs)) {
  1871. instr->op.code = BINARY_OP_ADD_FLOAT;
  1872. goto success;
  1873. }
  1874. break;
  1875. case NB_MULTIPLY:
  1876. case NB_INPLACE_MULTIPLY:
  1877. if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
  1878. break;
  1879. }
  1880. if (PyLong_CheckExact(lhs)) {
  1881. instr->op.code = BINARY_OP_MULTIPLY_INT;
  1882. goto success;
  1883. }
  1884. if (PyFloat_CheckExact(lhs)) {
  1885. instr->op.code = BINARY_OP_MULTIPLY_FLOAT;
  1886. goto success;
  1887. }
  1888. break;
  1889. case NB_SUBTRACT:
  1890. case NB_INPLACE_SUBTRACT:
  1891. if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
  1892. break;
  1893. }
  1894. if (PyLong_CheckExact(lhs)) {
  1895. instr->op.code = BINARY_OP_SUBTRACT_INT;
  1896. goto success;
  1897. }
  1898. if (PyFloat_CheckExact(lhs)) {
  1899. instr->op.code = BINARY_OP_SUBTRACT_FLOAT;
  1900. goto success;
  1901. }
  1902. break;
  1903. }
  1904. SPECIALIZATION_FAIL(BINARY_OP, binary_op_fail_kind(oparg, lhs, rhs));
  1905. STAT_INC(BINARY_OP, failure);
  1906. instr->op.code = BINARY_OP;
  1907. cache->counter = adaptive_counter_backoff(cache->counter);
  1908. return;
  1909. success:
  1910. STAT_INC(BINARY_OP, success);
  1911. cache->counter = adaptive_counter_cooldown();
  1912. }
  1913. #ifdef Py_STATS
  1914. static int
  1915. compare_op_fail_kind(PyObject *lhs, PyObject *rhs)
  1916. {
  1917. if (Py_TYPE(lhs) != Py_TYPE(rhs)) {
  1918. if (PyFloat_CheckExact(lhs) && PyLong_CheckExact(rhs)) {
  1919. return SPEC_FAIL_COMPARE_OP_FLOAT_LONG;
  1920. }
  1921. if (PyLong_CheckExact(lhs) && PyFloat_CheckExact(rhs)) {
  1922. return SPEC_FAIL_COMPARE_OP_LONG_FLOAT;
  1923. }
  1924. return SPEC_FAIL_COMPARE_OP_DIFFERENT_TYPES;
  1925. }
  1926. if (PyBytes_CheckExact(lhs)) {
  1927. return SPEC_FAIL_COMPARE_OP_BYTES;
  1928. }
  1929. if (PyTuple_CheckExact(lhs)) {
  1930. return SPEC_FAIL_COMPARE_OP_TUPLE;
  1931. }
  1932. if (PyList_CheckExact(lhs)) {
  1933. return SPEC_FAIL_COMPARE_OP_LIST;
  1934. }
  1935. if (PySet_CheckExact(lhs) || PyFrozenSet_CheckExact(lhs)) {
  1936. return SPEC_FAIL_COMPARE_OP_SET;
  1937. }
  1938. if (PyBool_Check(lhs)) {
  1939. return SPEC_FAIL_COMPARE_OP_BOOL;
  1940. }
  1941. if (Py_TYPE(lhs)->tp_richcompare == PyBaseObject_Type.tp_richcompare) {
  1942. return SPEC_FAIL_COMPARE_OP_BASEOBJECT;
  1943. }
  1944. return SPEC_FAIL_OTHER;
  1945. }
  1946. #endif
  1947. void
  1948. _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
  1949. int oparg)
  1950. {
  1951. assert(ENABLE_SPECIALIZATION);
  1952. assert(_PyOpcode_Caches[COMPARE_OP] == INLINE_CACHE_ENTRIES_COMPARE_OP);
  1953. _PyCompareOpCache *cache = (_PyCompareOpCache *)(instr + 1);
  1954. if (Py_TYPE(lhs) != Py_TYPE(rhs)) {
  1955. SPECIALIZATION_FAIL(COMPARE_OP, compare_op_fail_kind(lhs, rhs));
  1956. goto failure;
  1957. }
  1958. if (PyFloat_CheckExact(lhs)) {
  1959. instr->op.code = COMPARE_OP_FLOAT;
  1960. goto success;
  1961. }
  1962. if (PyLong_CheckExact(lhs)) {
  1963. if (_PyLong_IsCompact((PyLongObject *)lhs) && _PyLong_IsCompact((PyLongObject *)rhs)) {
  1964. instr->op.code = COMPARE_OP_INT;
  1965. goto success;
  1966. }
  1967. else {
  1968. SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_OP_BIG_INT);
  1969. goto failure;
  1970. }
  1971. }
  1972. if (PyUnicode_CheckExact(lhs)) {
  1973. int cmp = oparg >> 4;
  1974. if (cmp != Py_EQ && cmp != Py_NE) {
  1975. SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_OP_STRING);
  1976. goto failure;
  1977. }
  1978. else {
  1979. instr->op.code = COMPARE_OP_STR;
  1980. goto success;
  1981. }
  1982. }
  1983. SPECIALIZATION_FAIL(COMPARE_OP, compare_op_fail_kind(lhs, rhs));
  1984. failure:
  1985. STAT_INC(COMPARE_OP, failure);
  1986. instr->op.code = COMPARE_OP;
  1987. cache->counter = adaptive_counter_backoff(cache->counter);
  1988. return;
  1989. success:
  1990. STAT_INC(COMPARE_OP, success);
  1991. cache->counter = adaptive_counter_cooldown();
  1992. }
  1993. #ifdef Py_STATS
  1994. static int
  1995. unpack_sequence_fail_kind(PyObject *seq)
  1996. {
  1997. if (PySequence_Check(seq)) {
  1998. return SPEC_FAIL_UNPACK_SEQUENCE_SEQUENCE;
  1999. }
  2000. if (PyIter_Check(seq)) {
  2001. return SPEC_FAIL_UNPACK_SEQUENCE_ITERATOR;
  2002. }
  2003. return SPEC_FAIL_OTHER;
  2004. }
  2005. #endif
  2006. void
  2007. _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr, int oparg)
  2008. {
  2009. assert(ENABLE_SPECIALIZATION);
  2010. assert(_PyOpcode_Caches[UNPACK_SEQUENCE] ==
  2011. INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
  2012. _PyUnpackSequenceCache *cache = (_PyUnpackSequenceCache *)(instr + 1);
  2013. if (PyTuple_CheckExact(seq)) {
  2014. if (PyTuple_GET_SIZE(seq) != oparg) {
  2015. SPECIALIZATION_FAIL(UNPACK_SEQUENCE, SPEC_FAIL_EXPECTED_ERROR);
  2016. goto failure;
  2017. }
  2018. if (PyTuple_GET_SIZE(seq) == 2) {
  2019. instr->op.code = UNPACK_SEQUENCE_TWO_TUPLE;
  2020. goto success;
  2021. }
  2022. instr->op.code = UNPACK_SEQUENCE_TUPLE;
  2023. goto success;
  2024. }
  2025. if (PyList_CheckExact(seq)) {
  2026. if (PyList_GET_SIZE(seq) != oparg) {
  2027. SPECIALIZATION_FAIL(UNPACK_SEQUENCE, SPEC_FAIL_EXPECTED_ERROR);
  2028. goto failure;
  2029. }
  2030. instr->op.code = UNPACK_SEQUENCE_LIST;
  2031. goto success;
  2032. }
  2033. SPECIALIZATION_FAIL(UNPACK_SEQUENCE, unpack_sequence_fail_kind(seq));
  2034. failure:
  2035. STAT_INC(UNPACK_SEQUENCE, failure);
  2036. instr->op.code = UNPACK_SEQUENCE;
  2037. cache->counter = adaptive_counter_backoff(cache->counter);
  2038. return;
  2039. success:
  2040. STAT_INC(UNPACK_SEQUENCE, success);
  2041. cache->counter = adaptive_counter_cooldown();
  2042. }
  2043. #ifdef Py_STATS
  2044. int
  2045. _PySpecialization_ClassifyIterator(PyObject *iter)
  2046. {
  2047. if (PyGen_CheckExact(iter)) {
  2048. return SPEC_FAIL_ITER_GENERATOR;
  2049. }
  2050. if (PyCoro_CheckExact(iter)) {
  2051. return SPEC_FAIL_ITER_COROUTINE;
  2052. }
  2053. if (PyAsyncGen_CheckExact(iter)) {
  2054. return SPEC_FAIL_ITER_ASYNC_GENERATOR;
  2055. }
  2056. if (PyAsyncGenASend_CheckExact(iter)) {
  2057. return SPEC_FAIL_ITER_ASYNC_GENERATOR_SEND;
  2058. }
  2059. PyTypeObject *t = Py_TYPE(iter);
  2060. if (t == &PyListIter_Type) {
  2061. return SPEC_FAIL_ITER_LIST;
  2062. }
  2063. if (t == &PyTupleIter_Type) {
  2064. return SPEC_FAIL_ITER_TUPLE;
  2065. }
  2066. if (t == &PyDictIterKey_Type) {
  2067. return SPEC_FAIL_ITER_DICT_KEYS;
  2068. }
  2069. if (t == &PyDictIterValue_Type) {
  2070. return SPEC_FAIL_ITER_DICT_VALUES;
  2071. }
  2072. if (t == &PyDictIterItem_Type) {
  2073. return SPEC_FAIL_ITER_DICT_ITEMS;
  2074. }
  2075. if (t == &PySetIter_Type) {
  2076. return SPEC_FAIL_ITER_SET;
  2077. }
  2078. if (t == &PyUnicodeIter_Type) {
  2079. return SPEC_FAIL_ITER_STRING;
  2080. }
  2081. if (t == &PyBytesIter_Type) {
  2082. return SPEC_FAIL_ITER_BYTES;
  2083. }
  2084. if (t == &PyRangeIter_Type) {
  2085. return SPEC_FAIL_ITER_RANGE;
  2086. }
  2087. if (t == &PyEnum_Type) {
  2088. return SPEC_FAIL_ITER_ENUMERATE;
  2089. }
  2090. if (t == &PyMap_Type) {
  2091. return SPEC_FAIL_ITER_MAP;
  2092. }
  2093. if (t == &PyZip_Type) {
  2094. return SPEC_FAIL_ITER_ZIP;
  2095. }
  2096. if (t == &PySeqIter_Type) {
  2097. return SPEC_FAIL_ITER_SEQ_ITER;
  2098. }
  2099. if (t == &PyListRevIter_Type) {
  2100. return SPEC_FAIL_ITER_REVERSED_LIST;
  2101. }
  2102. if (t == &_PyUnicodeASCIIIter_Type) {
  2103. return SPEC_FAIL_ITER_ASCII_STRING;
  2104. }
  2105. const char *name = t->tp_name;
  2106. if (strncmp(name, "itertools", 9) == 0) {
  2107. return SPEC_FAIL_ITER_ITERTOOLS;
  2108. }
  2109. if (strncmp(name, "callable_iterator", 17) == 0) {
  2110. return SPEC_FAIL_ITER_CALLABLE;
  2111. }
  2112. return SPEC_FAIL_OTHER;
  2113. }
  2114. #endif
  2115. void
  2116. _Py_Specialize_ForIter(PyObject *iter, _Py_CODEUNIT *instr, int oparg)
  2117. {
  2118. assert(ENABLE_SPECIALIZATION);
  2119. assert(_PyOpcode_Caches[FOR_ITER] == INLINE_CACHE_ENTRIES_FOR_ITER);
  2120. _PyForIterCache *cache = (_PyForIterCache *)(instr + 1);
  2121. PyTypeObject *tp = Py_TYPE(iter);
  2122. if (tp == &PyListIter_Type) {
  2123. instr->op.code = FOR_ITER_LIST;
  2124. goto success;
  2125. }
  2126. else if (tp == &PyTupleIter_Type) {
  2127. instr->op.code = FOR_ITER_TUPLE;
  2128. goto success;
  2129. }
  2130. else if (tp == &PyRangeIter_Type) {
  2131. instr->op.code = FOR_ITER_RANGE;
  2132. goto success;
  2133. }
  2134. else if (tp == &PyGen_Type && oparg <= SHRT_MAX) {
  2135. assert(instr[oparg + INLINE_CACHE_ENTRIES_FOR_ITER + 1].op.code == END_FOR ||
  2136. instr[oparg + INLINE_CACHE_ENTRIES_FOR_ITER + 1].op.code == INSTRUMENTED_END_FOR
  2137. );
  2138. if (_PyInterpreterState_GET()->eval_frame) {
  2139. SPECIALIZATION_FAIL(FOR_ITER, SPEC_FAIL_OTHER);
  2140. goto failure;
  2141. }
  2142. instr->op.code = FOR_ITER_GEN;
  2143. goto success;
  2144. }
  2145. SPECIALIZATION_FAIL(FOR_ITER,
  2146. _PySpecialization_ClassifyIterator(iter));
  2147. failure:
  2148. STAT_INC(FOR_ITER, failure);
  2149. instr->op.code = FOR_ITER;
  2150. cache->counter = adaptive_counter_backoff(cache->counter);
  2151. return;
  2152. success:
  2153. STAT_INC(FOR_ITER, success);
  2154. cache->counter = adaptive_counter_cooldown();
  2155. }
  2156. void
  2157. _Py_Specialize_Send(PyObject *receiver, _Py_CODEUNIT *instr)
  2158. {
  2159. assert(ENABLE_SPECIALIZATION);
  2160. assert(_PyOpcode_Caches[SEND] == INLINE_CACHE_ENTRIES_SEND);
  2161. _PySendCache *cache = (_PySendCache *)(instr + 1);
  2162. PyTypeObject *tp = Py_TYPE(receiver);
  2163. if (tp == &PyGen_Type || tp == &PyCoro_Type) {
  2164. if (_PyInterpreterState_GET()->eval_frame) {
  2165. SPECIALIZATION_FAIL(SEND, SPEC_FAIL_OTHER);
  2166. goto failure;
  2167. }
  2168. instr->op.code = SEND_GEN;
  2169. goto success;
  2170. }
  2171. SPECIALIZATION_FAIL(SEND,
  2172. _PySpecialization_ClassifyIterator(receiver));
  2173. failure:
  2174. STAT_INC(SEND, failure);
  2175. instr->op.code = SEND;
  2176. cache->counter = adaptive_counter_backoff(cache->counter);
  2177. return;
  2178. success:
  2179. STAT_INC(SEND, success);
  2180. cache->counter = adaptive_counter_cooldown();
  2181. }