cranelift icon indicating copy to clipboard operation
cranelift copied to clipboard

ineg instruction on ssa values: register affinity never assigned?

Open jamesdelevoryas opened this issue 6 years ago • 4 comments

Thanks for opening a bug report on Cranelift! Please answer the questions below if they're relevant and delete this text before submitting.

  • My cranelift frontend is generating the following IL, and module.define_function(...) is failing with the "ghost value used by real instruction", which seems like maybe register allocation skipped over the ineg result??
function u0:0(i32, i64) -> i32 system_v {
    gv0 = symbol colocated u1:0
    sig0 = (i64, i64, i64, i64) -> i32 system_v
    fn0 = u0:1 sig0

block0(v0: i32, v1: i64):
    v2 = iconst.i32 1
    v3 = ineg v2
    return v3
}

error defining function "main": Compilation(
    Verifier(
        VerifierErrors(
            [
                VerifierError {
                    location: inst2,
                    context: None,
                    message: "v3 is a ghost value used by a real [Op1ret#c3] instruction",
                },
            ],
        ),
    ),
)
  • I guess I thought ineg could be used on scalar registers, but I only see simd filetests using it?
  • cranelift = "0.58.0"
  • This is on Mac OS, using rust 1.41.0. I'm generating object files using the cranelift-faerie integration

jamesdelevoryas avatar Feb 08 '20 23:02 jamesdelevoryas

Oh also, this isn't really a big deal, cause I guess you can always just construct an equivalent isub 0, v2 instruction or something

jamesdelevoryas avatar Feb 08 '20 23:02 jamesdelevoryas

Hi! Thanks for opening an issue. Indeed, that's what the legalization in cranelift-codegen/src/isa/x86/enc_tables.rs does in convert_ineg. It could and should be done for non-vector types too. This is a good bug to learn about manipulating Cranelift's IR, if somebody wants to chime in!

bnjbvr avatar Feb 10 '20 16:02 bnjbvr

Hey I really appreciate the quick response! I looked into making the change: unfortunately, when I added a case in convert_ineg for scalar integer types, it doesn't seem to actually run. It seems like this legalization function is only applied to SIMD types: is there something somewhere that specifies that? I was unable to find it. I mean, something outside of convert_ineg. I am aware the the current implementation of convert_ineg only applies the transformation on int vector types. But I'm wondering if there's something outside of convert_ineg that makes it only run on SIMD types.

jamesdelevoryas avatar Feb 11 '20 09:02 jamesdelevoryas

convert_neg is called from https://github.com/bytecodealliance/cranelift/blob/98c818c129979e98a3db150f8f9698f6451b7ef7/cranelift-codegen/meta/src/isa/x86/legalize.rs#L612. Adding copies of that line with widen and expand instead of narrow may work. Please also add a test for both i8/i16 (widen) and i32/i64 (expand).

bjorn3 avatar Feb 11 '20 09:02 bjorn3