Skip to content

[mypyc] Add is_bool_or_bit_rprimitive #19406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

p-sawicki
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant