Skip to content

Conversation

@p-sawicki
Copy link
Collaborator

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

@JukkaL JukkaL merged commit 930a379 into python:master Jul 9, 2025
13 checks passed
@p-sawicki p-sawicki deleted the is_bool_or_bit_rprimitive branch July 9, 2025 15:34
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.

2 participants