Stream: cranelift

Topic: Confusing error when finalizing function


view this post on Zulip Chase Wilson (Feb 20 2022 at 21:26):

I've got this function which looks correct to me

function u0:0(i64, i64, i64) -> i8 system_v {
    sig0 = (i64) -> i16 windows_fastcall
    sig1 = (i64, i8) -> b8 windows_fastcall
    sig2 = (i64) -> b8 windows_fastcall
    fn0 = u0:0 sig0
    fn1 = u0:1 sig1
    fn2 = u0:2 sig2

block0(v0: i64, v1: i64, v2: i64):
    jump block1

block1:
    v3 = call fn0(v0)
    v4 = ishl_imm v3, 8
    v5 = ireduce.i8 v4
    brnz v5, block4
    jump block5

block5:
    v6 = ireduce.i8 v3
    v7 = bitcast.i8 v6
    v8 = iconst.i8 0
    v9 = icmp eq v7, v8
    v10 = bitcast.i64 v6
    store notrap aligned v10, v1
    v11 = call fn0(v0)
    v12 = ishl_imm v11, 8
    v13 = ireduce.i8 v12
    brnz v13, block4
    jump block6

block6:
    v14 = ireduce.i8 v11
    v15 = bitcast.i64 v14
    store notrap aligned v15, v1+1
    brnz.b1 v9, block3
    jump block2

block2:
    v16 = iconst.i64 0
    v17 = iadd.i64 v1, v16
    v18 = load.i8 v17
    v19 = iconst.i64 1
    v20 = iadd.i64 v1, v19
    v21 = load.i8 v20
    v22 = iadd v18, v21
    v23 = bitcast.i64 v22
    store notrap aligned v23, v1+1
    v24 = iconst.i64 0
    store notrap aligned v24, v1
    jump block3

block3:
    v25 = iconst.i64 1
    v26 = iadd.i64 v1, v25
    v27 = load.i8 v26
    v28 = bitcast.i8 v27
    v29 = call fn1(v0, v28)
    brnz v29, block4
    jump block7

block7:
    v30 = iconst.i8 0
    return v30

block4 cold:
    v31 = call fn2(v0)
    v32 = iconst.i8 101
    return v32
}

but cranelift panics on it somewhere within lowering

thread 'main' panicked at 'assertion failed: !is_bool_ty(src_ty)', C:\Users\Chase\.cargo\registry\src\github.com-1ecc6299db9ec823\cranelift-codegen-0.81.1\src\isa\x64\lower.rs:5352:29
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/777bb86bcdbc568be7cff6eeeaaf81a89b4aa50b\/library\std\src\panicking.rs:577
   1: core::panicking::panic_fmt
             at /rustc/777bb86bcdbc568be7cff6eeeaaf81a89b4aa50b\/library\core\src\panicking.rs:110
   2: core::panicking::panic
             at /rustc/777bb86bcdbc568be7cff6eeeaaf81a89b4aa50b\/library\core\src\panicking.rs:48
   3: cranelift_codegen::isa::x64::lower::impl$0::lower_branch_group<cranelift_codegen::machinst::lower::Lower<enum$<cranelift_codegen::isa::x64::lower::isle::generated_code::MInst> > >
             at C:\Users\Chase\.cargo\registry\src\github.com-1ecc6299db9ec823\cranelift-codegen-0.81.1\src\isa\x64\lower.rs:5352
   4: cranelift_codegen::machinst::lower::Lower<enum$<cranelift_codegen::isa::x64::lower::isle::generated_code::MInst> >::lower_clif_branches<enum$<cranelift_codegen::isa::x64::lower::isle::generated_code::MInst>,cranelift_codegen::isa::x64::X64Backend>
             at C:\Users\Chase\.cargo\registry\src\github.com-1ecc6299db9ec823\cranelift-codegen-0.81.1\src\machinst\lower.rs:899
   5: cranelift_codegen::machinst::lower::Lower<enum$<cranelift_codegen::isa::x64::lower::isle::generated_code::MInst> >::lower<enum$<cranelift_codegen::isa::x64::lower::isle::generated_code::MInst>,cranelift_codegen::isa::x64::X64Backend>
             at C:\Users\Chase\.cargo\registry\src\github.com-1ecc6299db9ec823\cranelift-codegen-0.81.1\src\machinst\lower.rs:974
   6: cranelift_codegen::machinst::compile::compile<cranelift_codegen::isa::x64::X64Backend>
             at C:\Users\Chase\.cargo\registry\src\github.com-1ecc6299db9ec823\cranelift-codegen-0.81.1\src\machinst\compile.rs:31
   7: cranelift_codegen::isa::x64::X64Backend::compile_vcode
             at C:\Users\Chase\.cargo\registry\src\github.com-1ecc6299db9ec823\cranelift-codegen-0.81.1\src\isa\x64\mod.rs:53
   8: cranelift_codegen::isa::x64::impl$1::compile_function
             at C:\Users\Chase\.cargo\registry\src\github.com-1ecc6299db9ec823\cranelift-codegen-0.81.1\src\isa\x64\mod.rs:64
   9: cranelift_codegen::context::Context::compile
             at C:\Users\Chase\.cargo\registry\src\github.com-1ecc6299db9ec823\cranelift-codegen-0.81.1\src\context.rs:165
  10: cranelift_jit::backend::impl$2::define_function
             at C:\Users\Chase\.cargo\registry\src\github.com-1ecc6299db9ec823\cranelift-jit-0.81.1\src\backend.rs:665
  11: cranial_coitus::jit::Jit::compile
             at .\src\jit\mod.rs:501
  12: cranial_coitus::debug
             at .\src\main.rs:522
  13: cranial_coitus::main
             at .\src\main.rs:69
  14: core::ops::function::FnOnce::call_once<enum$<core::result::Result<tuple$<>,anyhow::Error>, 1, 18446744073709551615, Err> (*)(),tuple$<> >
             at /rustc/777bb86bcdbc568be7cff6eeeaaf81a89b4aa50b\library\core\src\ops\function.rs:227
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

looking at the code it points to, I think someone may have forgotten a check for bool types other than b1 or something

// src\isa\x64\lower.rs:5352:29

// See case for `Opcode::Select` above re: testing the
// boolean input.
let test_input = if src_ty == types::B1 {
    // test src, 1
    RegMemImm::imm(1)
} else {
    assert!(!is_bool_ty(src_ty));
    // test src, src
    RegMemImm::reg(src)
};

It seems like any bool type (which in my case I think are b8s) should be able to get the simple test against one if I'm not mistaken

view this post on Zulip Chase Wilson (Feb 20 2022 at 21:40):

It looks like changing the external function signatures from fn(..) -> b8 to fn(..) -> b1 fixes it


Last updated: Dec 23 2024 at 12:05 UTC