← 输入框|github.com|刷新
Skip to content

Const i32 expressions do not support integer div/mod operations #2349

Closed

Description

@dylon

I have the following source:

# path: /var/tmp/lp/qux.py
from lpython import Const, i32
foo: Const[i32] = 4
bar: Const[i32] = foo // 2

running it through lpython breaks with the following error:

$ ~/Workspace/lpython/src/bin/lpython /var/tmp/lp/qux.py
semantic error: Arguments do not match for any generic procedure, _lpython_floordiv
 --> /var/tmp/lp/qux.py:4:19
  |
4 | bar: Const[i32] = foo // 2
  |                   ^^^^^^^^


Note: Please report unclear or confusing messages as bugs at
https://github.com/lcompilers/lpython/issues.

This should be supported, especially since lpython does not complain about multiplication:

# path: /var/tmp/lp/qux.py
from lpython import Const, i32
foo: Const[i32] = 4
bar: Const[i32] = foo * 2

def main() -> None:
    print(foo)
    print(bar)

main()
$ ~/Workspace/lpython/src/bin/lpython /var/tmp/lp/qux.py
4
8

The following does work, but is ugly:

# path: /var/tmp/lp/qux.py
from lpython import Const, i32
foo: Const[i32] = 4
bar: Const[i32] = i32(foo / 2)

def main() -> None:
    print(foo)
    print(bar)

main()
$ ~/Workspace/lpython/src/bin/lpython /var/tmp/lp/qux.py
4
2

Note that mod (%) also does not work:

$ cat /var/tmp/lp/qux.py
# path: /var/tmp/lp/qux.py
from lpython import Const, i32
foo: Const[i32] = 4
bar: Const[i32] = foo % 2
$ ~/Workspace/lpython/src/bin/lpython /var/tmp/lp/qux.py
semantic error: Arguments do not match for any generic procedure, _mod
 --> /var/tmp/lp/qux.py:4:19
  |
4 | bar: Const[i32] = foo % 2
  |                   ^^^^^^^


Note: Please report unclear or confusing messages as bugs at
https://github.com/lcompilers/lpython/issues.

Activity

  1. Thirumalai-Shaktivel commented on Sep 29, 2023

    @Thirumalai-Shaktivel
    Collaborator

    I think, the Const[i32] is not handled in _lpython_floordiv and _mod

  2. abhi-glitchhg commented on Sep 29, 2023

    @abhi-glitchhg
  3. Thirumalai-Shaktivel commented on Sep 29, 2023

    @Thirumalai-Shaktivel
    Collaborator

    Sure, go ahead and submit a PR!

  4. AnandRaj-7 commented on Sep 29, 2023

    @AnandRaj-7

    If i am right then this is becasue in C and C++ comments start with "//" so it is unable to recognize it as a floor division operator and thinks of it as a comment.

  5. certik commented on Sep 29, 2023

    @certik
    Contributor

    No, the error comes from LPython not being able to find an implementation for this case. Rather, we should be routing this operation via an explicit ASR node, or via the IntrinsicFunction node.

  6. ubaidsk commented on Oct 2, 2023

    @ubaidsk
    Collaborator

    I think we can implement floordiv operation similar to the mod operation using IntrinsicFunction node as in lfortran/lfortran#2519.

  7. abhi-glitchhg commented on Oct 2, 2023

    @abhi-glitchhg

    I was thinking of overloading the _lpython_floordiv function with inputs dtypes const[i32]. is this approach not good/preferred?

  8. certik commented on Oct 2, 2023

    @certik
    Contributor

    You can try it, and if the patch is simple, we can merge it as a short-term workaround. Floor belongs into ASR itself as IntrinsicFunction, so it's always better to invest into the long-term solution.

  9. certik commented on Oct 8, 2023

    @certik
    Contributor

    The following now works:

    $ cat qux.py 
    from lpython import Const, i32
    foo: Const[i32] = 4
    bar: Const[i32] = foo // 2
    print(foo, bar)
    $ lpython qux.py
    4 2

    Thanks @Shaikh-Ubaid !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions