Skip to content
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

[BUG] Cython master crashes analysing division of int by float #6183

Open
oscarbenjamin opened this issue May 5, 2024 · 3 comments · May be fixed by #6195
Open

[BUG] Cython master crashes analysing division of int by float #6183

oscarbenjamin opened this issue May 5, 2024 · 3 comments · May be fixed by #6195

Comments

@oscarbenjamin
Copy link

Describe the bug

I have bisected this problem to 0adbcde

I just tried to build python-flint using Cython master but it crashes with

$ meson compile -C build
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /home/oscar/.pyenv/shims/ninja -C /home/oscar/current/active/python-flint/build
ninja: Entering directory `/home/oscar/current/active/python-flint/build'
[1/99] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/types/fmpz.pyx
FAILED: src/flint/types/fmpz.cpython-311-x86_64-linux-gnu.so.p/src/flint/types/fmpz.pyx.c 
cython -M --fast-fail -3 -X embedsignature=True -X emit_code_comments=True -X linetrace=True /home/oscar/current/active/python-flint/src/flint/types/fmpz.pyx -o src/flint/types/fmpz.cpython-311-x86_64-linux-gnu.so.p/src/flint/types/fmpz.pyx.c

Error compiling Cython file:
------------------------------------------------------------
...
cdef inline long prec_to_dps(n):
    return max(1, int(round(int(n)/3.3219280948873626)-1))
                                  ^
------------------------------------------------------------

/home/oscar/current/active/python-flint/src/flint/utils/conversion.pxd:2:34: Compiler crash in AnalyseExpressionsTransform

ModuleNode.body = StatListNode(conversion.pxd:1:0)
StatListNode.stats[0] = CFuncDefNode(conversion.pxd:1:5,
    args = [...]/1,
    inline_in_pxd = True,
    modifiers = [...]/1,
    outer_attrs = [...]/2,
    visibility = 'private')
File 'Nodes.py', line 390, in analyse_expressions: StatListNode(conversion.pxd:2:4,
    is_terminator = True)
File 'Nodes.py', line 6807, in analyse_expressions: ReturnStatNode(conversion.pxd:2:4,
    is_terminator = True)
File 'UtilNodes.py', line 282, in analyse_types: EvalWithTempExprNode(conversion.pxd:2:21,
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 6080, in analyse_types: SimpleCallNode(conversion.pxd:2:21,
    analysed = True,
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 8447, in analyse_types: TupleNode(conversion.pxd:2:21,
    is_sequence_constructor = 1,
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 11594, in analyse_types: SubNode(conversion.pxd:2:54,
    infix = True,
    operator = '-',
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 6080, in analyse_types: SimpleCallNode(conversion.pxd:2:27,
    analysed = True,
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 8447, in analyse_types: TupleNode(conversion.pxd:2:27,
    is_sequence_constructor = 1,
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 11596, in analyse_types: DivNode(conversion.pxd:2:34,
    ctruedivision = True,
    infix = True,
    operator = '/',
    result_is_used = True,
    truedivision = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 12180, in analyse_operation: DivNode(conversion.pxd:2:34,
    ctruedivision = True,
    infix = True,
    operator = '/',
    result_is_used = True,
    truedivision = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 11609, in analyse_operation: DivNode(conversion.pxd:2:34,
    ctruedivision = True,
    infix = True,
    operator = '/',
    result_is_used = True,
    truedivision = True,
    use_managed_ref = True)

Compiler crash traceback from this point on:
  File "/home/oscar/.pyenv/versions/3.11.3/envs/python-flint-3.11/lib/python3.11/site-packages/Cython/Compiler/ExprNodes.py", line 11609, in analyse_operation
    assert self.type.is_pyobject
AssertionError: 
[2/99] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/pyflint.pyx
FAILED: src/flint/pyflint.cpython-311-x86_64-linux-gnu.so.p/src/flint/pyflint.pyx.c 
cython -M --fast-fail -3 -X embedsignature=True -X emit_code_comments=True -X linetrace=True /home/oscar/current/active/python-flint/src/flint/pyflint.pyx -o src/flint/pyflint.cpython-311-x86_64-linux-gnu.so.p/src/flint/pyflint.pyx.c

Error compiling Cython file:
------------------------------------------------------------
...
cdef inline long prec_to_dps(n):
    return max(1, int(round(int(n)/3.3219280948873626)-1))
                                  ^
------------------------------------------------------------

/home/oscar/current/active/python-flint/src/flint/utils/conversion.pxd:2:34: Compiler crash in AnalyseExpressionsTransform

Code to reproduce the behaviour:

I haven't tried distilling this down to a standalone reproducer but the code being complained about is:

cdef inline long prec_to_dps(n):
    return max(1, int(round(int(n)/3.3219280948873626)-1))

Expected behaviour

This all builds fine with Cython 3.0.10 so I expected it to build fine.

OS

Linux

Python version

3.12

Cython version

master

Additional context

No response

@da-woods
Copy link
Contributor

da-woods commented May 5, 2024

I suspect that this is a duplicate of #6004 but not absolutely sure

@oscarbenjamin
Copy link
Author

Here is a simple reproducer:

$ cat tmp.pyx 
cdef inline long prec_to_dps(n):
    return max(1, int(round(int(n)/3.3219280948873626)-1))
$ cython tmp.pyx 
/home/oscar/.pyenv/versions/3.12.0/envs/sympy-3.12.git/lib/python3.12/site-packages/Cython/Compiler/Main.py:373: FutureWarning: Cython directive 'language_level' not set, using '3' (Py3). This has changed from earlier releases! File: /home/oscar/current/active/sympy/tmp.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)

Error compiling Cython file:
------------------------------------------------------------
...
cdef inline long prec_to_dps(n):
    return max(1, int(round(int(n)/3.3219280948873626)-1))
                                  ^
------------------------------------------------------------

tmp.pyx:2:34: Compiler crash in AnalyseExpressionsTransform

ModuleNode.body = StatListNode(tmp.pyx:1:5)
StatListNode.stats[0] = CFuncDefNode(tmp.pyx:1:5,
    args = [...]/1,
    modifiers = [...]/1,
    outer_attrs = [...]/2,
    visibility = 'private')
File 'Nodes.py', line 390, in analyse_expressions: StatListNode(tmp.pyx:2:4,
    is_terminator = True)
File 'Nodes.py', line 6858, in analyse_expressions: ReturnStatNode(tmp.pyx:2:4,
    is_terminator = True)
File 'UtilNodes.py', line 282, in analyse_types: EvalWithTempExprNode(tmp.pyx:2:21,
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 6155, in analyse_types: SimpleCallNode(tmp.pyx:2:21,
    analysed = True,
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 8661, in analyse_types: TupleNode(tmp.pyx:2:21,
    is_sequence_constructor = 1,
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 11831, in analyse_types: SubNode(tmp.pyx:2:54,
    infix = True,
    operator = '-',
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 6155, in analyse_types: SimpleCallNode(tmp.pyx:2:27,
    analysed = True,
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 8661, in analyse_types: TupleNode(tmp.pyx:2:27,
    is_sequence_constructor = 1,
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 11833, in analyse_types: DivNode(tmp.pyx:2:34,
    ctruedivision = True,
    infix = True,
    operator = '/',
    result_is_used = True,
    truedivision = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 12417, in analyse_operation: DivNode(tmp.pyx:2:34,
    ctruedivision = True,
    infix = True,
    operator = '/',
    result_is_used = True,
    truedivision = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 11846, in analyse_operation: DivNode(tmp.pyx:2:34,
    ctruedivision = True,
    infix = True,
    operator = '/',
    result_is_used = True,
    truedivision = True,
    use_managed_ref = True)

Compiler crash traceback from this point on:
  File "/home/oscar/.pyenv/versions/3.12.0/envs/sympy-3.12.git/lib/python3.12/site-packages/Cython/Compiler/ExprNodes.py", line 11846, in analyse_operation
    assert self.type.is_pyobject
AssertionError: 

@oscarbenjamin
Copy link
Author

In fact this is enough:

def prec_to_dps(n):
    return int(n) / 1.0

da-woods added a commit to da-woods/cython that referenced this issue May 12, 2024
This was broken due to changes to type inference. It infers the
type as a double, but "py_operations" for binops need to have
a Python result. Therefore I've separated into a two step:

* calculate Python result
* coerce to known C type.

In principle Optimize.c could take this back to 1 step. But
at the moment it doesn't.

Fixes cython#6183
Fixes cython#6004
@da-woods da-woods linked a pull request May 12, 2024 that will close this issue
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 a pull request may close this issue.

2 participants