Skip to content

[mlir][arith] arith.floordivsi -(2^n -1), -1 generates wrong code with UB #83079

Closed
@pingshiyu

Description

@pingshiyu

The code below tries to calculate: arith.floordivsi (1 - 2^63), -1, which should give a well defined result equalling 2^63 - 1 (as far as I can tell from the documentation). But instead generates code that crashes when compiled with:

mlir-opt --arith-expand --test-lower-to-llvm out.mlir | mlir-cpu-runner -e main --shared-libs ~/lib/mlir_c_runner --entry-point-result void

"builtin.module"() ({
    ^bb0(): 
        "func.func"() ({
            ^bb0(): 
                "func.call"() {callee=@func0} : () -> ()
                "func.return"() : () -> ()
            }) {sym_name="main", function_type=() -> ()} : () -> ()
        "func.func"() ({
            ^bb0(): 
                %nMaxInt, %n1 = "func.call"() {callee=@func1} : () -> (i64, i64)
                vector.print %nMaxInt : i64 // -(2^n - 1)
                vector.print %n1 : i64      // -1

                %BUG = "arith.floordivsi"(%nMaxInt, %n1) : (i64, i64) -> (i64) 
                vector.print %BUG : i64 // // should == 2^n - 1, but crashes here
                "func.return"() : () -> ()
            }) {sym_name="func0", function_type=() -> ()} : () -> ()
        "func.func"() ({
            ^bb0(): 
                %nMaxInt = arith.constant -9223372036854775807 : i64
                %n1 = arith.constant -1 : i64
                "func.return"(%nMaxInt, %n1) : (i64, i64) -> ()
            }) {sym_name="func1", function_type=() -> (i64, i64)} : () -> ()
    }) : () -> ()

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions