Commit 930a379
authored
[mypyc] Add is_bool_or_bit_rprimitive (#19406)
Added a wrapper to check if a type is either a bool or bit primitive as
these two checks are often done together.
The wrapper should help in preventing suboptimal code generation if one
forgets to check for the bit primitive in cases when it can be trivially
expanded to bool. One such case was in translation of binary ops, which
is fixed in this PR.
Example code:
```
def f(a: float, b: float, c: float) -> bool:
return (a == b) & (a == c)
```
IR before:
```
def f(a, b, c):
a, b, c :: float
r0, r1 :: bit
r2 :: bool
r3 :: int
r4 :: bool
r5, r6 :: int
r7 :: object
r8, r9 :: bool
L0:
r0 = a == b
r1 = a == c
r2 = r0 << 1
r3 = extend r2: builtins.bool to builtins.int
r4 = r1 << 1
r5 = extend r4: builtins.bool to builtins.int
r6 = CPyTagged_And(r3, r5)
dec_ref r3 :: int
dec_ref r5 :: int
r7 = box(int, r6)
r8 = unbox(bool, r7)
dec_ref r7
if is_error(r8) goto L2 (error at f:2) else goto L1
L1:
return r8
L2:
r9 = <error> :: bool
return r9
```
IR after:
```
def f(a, b, c):
a, b, c :: float
r0, r1 :: bit
r2 :: bool
L0:
r0 = a == b
r1 = a == c
r2 = r0 & r1
return r2
```1 parent 10f95e6 commit 930a379
File tree
5 files changed
+78
-31
lines changed- mypyc
- codegen
- irbuild
- ir
- test-data
5 files changed
+78
-31
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
32 | | - | |
| 31 | + | |
33 | 32 | | |
34 | 33 | | |
35 | 34 | | |
| |||
615 | 614 | | |
616 | 615 | | |
617 | 616 | | |
618 | | - | |
619 | | - | |
| 617 | + | |
620 | 618 | | |
621 | 619 | | |
622 | 620 | | |
| |||
638 | 636 | | |
639 | 637 | | |
640 | 638 | | |
641 | | - | |
| 639 | + | |
642 | 640 | | |
643 | 641 | | |
644 | 642 | | |
| |||
889 | 887 | | |
890 | 888 | | |
891 | 889 | | |
892 | | - | |
| 890 | + | |
893 | 891 | | |
894 | 892 | | |
895 | 893 | | |
| |||
1015 | 1013 | | |
1016 | 1014 | | |
1017 | 1015 | | |
1018 | | - | |
| 1016 | + | |
1019 | 1017 | | |
1020 | 1018 | | |
1021 | 1019 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
46 | | - | |
| 45 | + | |
47 | 46 | | |
48 | 47 | | |
49 | 48 | | |
| |||
1089 | 1088 | | |
1090 | 1089 | | |
1091 | 1090 | | |
1092 | | - | |
1093 | | - | |
1094 | | - | |
1095 | | - | |
1096 | | - | |
| 1091 | + | |
1097 | 1092 | | |
1098 | 1093 | | |
1099 | 1094 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
582 | 582 | | |
583 | 583 | | |
584 | 584 | | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
585 | 589 | | |
586 | 590 | | |
587 | 591 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
96 | | - | |
97 | | - | |
| 96 | + | |
98 | 97 | | |
99 | 98 | | |
100 | 99 | | |
| |||
381 | 380 | | |
382 | 381 | | |
383 | 382 | | |
384 | | - | |
385 | | - | |
386 | | - | |
| 383 | + | |
387 | 384 | | |
388 | 385 | | |
389 | 386 | | |
390 | 387 | | |
391 | | - | |
392 | | - | |
393 | | - | |
| 388 | + | |
394 | 389 | | |
395 | 390 | | |
396 | 391 | | |
| |||
1341 | 1336 | | |
1342 | 1337 | | |
1343 | 1338 | | |
1344 | | - | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
1345 | 1344 | | |
1346 | 1345 | | |
1347 | 1346 | | |
| |||
1355 | 1354 | | |
1356 | 1355 | | |
1357 | 1356 | | |
1358 | | - | |
| 1357 | + | |
1359 | 1358 | | |
1360 | 1359 | | |
1361 | 1360 | | |
| |||
1367 | 1366 | | |
1368 | 1367 | | |
1369 | 1368 | | |
1370 | | - | |
| 1369 | + | |
1371 | 1370 | | |
1372 | 1371 | | |
1373 | 1372 | | |
| |||
1387 | 1386 | | |
1388 | 1387 | | |
1389 | 1388 | | |
1390 | | - | |
| 1389 | + | |
1391 | 1390 | | |
1392 | 1391 | | |
1393 | 1392 | | |
1394 | 1393 | | |
1395 | 1394 | | |
1396 | | - | |
| 1395 | + | |
1397 | 1396 | | |
1398 | 1397 | | |
1399 | 1398 | | |
| |||
1544 | 1543 | | |
1545 | 1544 | | |
1546 | 1545 | | |
1547 | | - | |
| 1546 | + | |
1548 | 1547 | | |
1549 | 1548 | | |
1550 | 1549 | | |
| |||
1563 | 1562 | | |
1564 | 1563 | | |
1565 | 1564 | | |
1566 | | - | |
| 1565 | + | |
1567 | 1566 | | |
1568 | 1567 | | |
1569 | 1568 | | |
| |||
1590 | 1589 | | |
1591 | 1590 | | |
1592 | 1591 | | |
1593 | | - | |
| 1592 | + | |
1594 | 1593 | | |
1595 | 1594 | | |
1596 | 1595 | | |
| |||
1748 | 1747 | | |
1749 | 1748 | | |
1750 | 1749 | | |
1751 | | - | |
| 1750 | + | |
1752 | 1751 | | |
1753 | 1752 | | |
1754 | 1753 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
422 | 422 | | |
423 | 423 | | |
424 | 424 | | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
0 commit comments