Skip to content

lpython_bit_length() fails for negative numbers in release mode #1836

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
ubaidsk opened this issue May 18, 2023 · 3 comments
Open

lpython_bit_length() fails for negative numbers in release mode #1836

ubaidsk opened this issue May 18, 2023 · 3 comments

Comments

@ubaidsk
Copy link
Collaborator

ubaidsk commented May 18, 2023

On latest main, compiling using the following commands on Mac M1

./build0.sh
cmake -DWITH_FMT=yes -DCMAKE_CXX_FLAGS_RELEASE="-Wall -Wextra -O3 -funroll-loops -DNDEBUG" -DWITH_LLVM=yes .
cmake --build . -j16

I am experiencing the following:

$ python integration_tests/test_bit_length.py               
7
3
7
7
$ lpython integration_tests/test_bit_length.py               
AssertionError

Originally posted by @Shaikh-Ubaid in #1835 (comment)

@certik
Copy link
Contributor

certik commented May 18, 2023

Awesome, I wonder if it has something to do with compiling in optimized mode, which is what the Conda package uses.

@ubaidsk
Copy link
Collaborator Author

ubaidsk commented May 20, 2023

I would like to know if this issue is a general case or if it is something experienced specifically my environment setup. Dr. Ondrej, please, could you share if you are able to reproduce this?

@Vipul-Cariappa
Copy link
Contributor

Vipul-Cariappa commented Jul 8, 2024

bit_lenght does not work properly in interactive mode:

>>> (12).bit_length()
4
>>> u8(12).bit_length()
>>> i8(12).bit_length()
>>> x: i32 = i8(12).bit_length()
>>> x
0

Looking at the LLVM IR; Looks like no IR was generated:

>>> (12).bit_length()
LLVM IR:
; ModuleID = 'LFortran'
source_filename = "LFortran"

define i32 @__main__global_stmts_1__() {
.entry:
  %__main__global_stmts_1__1 = alloca i32, align 4
  store i32 4, i32* %__main__global_stmts_1__1, align 4
  br label %return

return:                                           ; preds = %.entry
  %0 = load i32, i32* %__main__global_stmts_1__1, align 4
  ret i32 %0
}

Return type: i32
Result:
4
>>> (i8(12)).bit_length()
LLVM IR:
; ModuleID = 'LFortran'
source_filename = "LFortran"

Return type: none
Result:
(nothing to execute)

And, no ASR is being generate

>>> (12).bit_length()
ASR:
(TranslationUnit
    (SymbolTable
        1
        {
            __main__:
                (Module
                    (SymbolTable
                        2
                        {
                            __main__global_stmts_1__:
                                (Function
                                    (SymbolTable
                                        3
                                        {
                                            __main__global_stmts_1__1:
                                                (Variable
                                                    3
                                                    __main__global_stmts_1__1
                                                    []
                                                    ReturnVar
                                                    ()
                                                    ()
                                                    Default
                                                    (Integer 4)
                                                    ()
                                                    BindC
                                                    Public
                                                    Required
                                                    .false.
                                                )
                                        })
                                    __main__global_stmts_1__
                                    (FunctionType
                                        []
                                        (Integer 4)
                                        BindC
                                        Implementation
                                        ()
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    []
                                    []
                                    [(Assignment
                                        (Var 3 __main__global_stmts_1__1)
                                        (IntegerConstant 4 (Integer 4))
                                        ()
                                    )]
                                    (Var 3 __main__global_stmts_1__1)
                                    Public
                                    .false.
                                    .false.
                                    ()
                                )
                        })
                    __main__
                    []
                    .false.
                    .false.
                )
        })
    []
)
Return type: i32
Result:
4
>>> (i8(12)).bit_length()
ASR:
(TranslationUnit
    (SymbolTable
        1
        {
            __main__:
                (Module
                    (SymbolTable
                        2
                        {
                            
                        })
                    __main__
                    []
                    .false.
                    .false.
                )
        })
    []
)
Return type: none
Result:
(nothing to execute)

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

No branches or pull requests

3 participants