lpython icon indicating copy to clipboard operation
lpython copied to clipboard

lpython_bit_length() fails for negative numbers in release mode

Open ubaidsk opened this issue 2 years ago • 3 comments

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 https://github.com/lcompilers/lpython/issues/1835#issuecomment-1553425715

ubaidsk avatar May 18 '23 19:05 ubaidsk

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

certik avatar May 18 '23 20:05 certik

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?

ubaidsk avatar May 20 '23 06:05 ubaidsk

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)

Vipul-Cariappa avatar Jul 08 '24 10:07 Vipul-Cariappa