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

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

Open

Description

@ubaidsk

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)

Activity

  1. certik commented on May 18, 2023

    @certik
    Contributor

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

  2. ubaidsk commented on May 20, 2023

    @ubaidsk
    CollaboratorAuthor

    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?

  3. Vipul-Cariappa commented on Jul 8, 2024

    @Vipul-Cariappa
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions