wasmtime icon indicating copy to clipboard operation
wasmtime copied to clipboard

Add run tests for i128.icmp

Open bnjbvr opened this issue 7 years ago • 11 comments

It'd be nice to add run tests for the following condition codes:

  • [ ] eq with non-zero values
  • [ ] gt with non-zero values
  • [ ] ge with non-zero values

Start from https://github.com/CraneStation/cranelift/blob/master/filetests/isa/x86/icmp-i128.clif and add more tests in there!

bnjbvr avatar Oct 10 '19 09:10 bnjbvr

I'm interested in tackling this.

How should these tests be run? My initial thought would be to run this with cargo run compile --target=x86_64 filetests/isa/x86/icmp-i128.clif, but I'm not sure what we're checking against to determine that the test passed. I get filetests/isa/x86/icmp-i128.clif: 8: Unknown opcode: 'asdfasdfasgd' when I add nonsense to the test file, but I know there are more concerns than unknown opcodes.

ash-tamraz avatar Oct 18 '19 04:10 ash-tamraz

Thanks for looking into this! So you can use clif-util's test command: cargo run test ./filetests/isa/x86/icmp-i128.clif. Because of the metadata in this file (namely, the test run directive at the top), the filetests framework knows it has to parse, compile and run the test. Runtime support is very limited, but it is sufficient for our needs here.

Each test in this file is composed of a function that returns a single boolean value: by convention, the test passes if and only if the function returns true at runtime. A test directive ; run has to be put just after the function's body, as is done in this example file. So the first test consists of checking that when comparing twice the same i128 value (both low and high 64-bits parts being equal to 0), the comparison shall return true.

Does it make sense?

bnjbvr avatar Oct 18 '19 08:10 bnjbvr

Does it make sense?

Yes, your explanation is very helpful, thank you very much! I'll see if I can put together these other test cases.

ash-tamraz avatar Oct 19 '19 01:10 ash-tamraz

I'm not sure why I'm struggling with this so much. I can't seem to get the non-zero greater than test to work.

Here's what my file looks like:

test run
target x86_64 haswell

function %test_icmp_eq_i128() -> b1 {
ebb0:
    v11 = iconst.i64 0x0
    v12 = iconst.i64 0x0
    v1 = iconcat v11, v12
    v21 = iconst.i64 0x0
    v22 = iconst.i64 0x0
    v2 = iconcat v21, v22
    v10 = icmp.i128 eq v1, v2
    return v10
}

; run

function %test_icmp_imm_eq_i128() -> b1 {
ebb0:
    v11 = iconst.i64 0x0
    v12 = iconst.i64 0x0
    v1 = iconcat v11, v12
    v10 = icmp_imm.i128 eq v1, 0x0
    return v10
}

; run

function %test_icmp_ne_i128() -> b1 {
ebb0:
    v11 = iconst.i64 0x0
    v12 = iconst.i64 0x0
    v1 = iconcat v11, v12
    v21 = iconst.i64 0x0
    v22 = iconst.i64 0x1
    v2 = iconcat v21, v22
    v10 = icmp.i128 ne v1, v2
    return v10
}

; run

function %test_icmp_imm_ne_i128() -> b1 {
ebb0:
    v11 = iconst.i64 0x0
    v12 = iconst.i64 0x0
    v1 = iconcat v11, v12
    v10 = icmp_imm.i128 ne v1, 0x1
    return v10
}

; run

function %test_icmp_nz_eq_i128() -> b1 {
ebb0:
    v11 = iconst.i64 0x1
    v12 = iconst.i64 0x1
    v1 = iconcat v11, v12
    v21 = iconst.i64 0x1
    v22 = iconst.i64 0x1
    v2 = iconcat v21, v22
    v10 = icmp.i128 eq v1, v2
    return v10
}

; run

function %test_icmp_nz_gt_i128() -> b1 {
ebb0:
    v11 = iconst.i64 0x1
    v12 = iconst.i64 0x1
    v1 = iconcat v11, v12
    v21 = iconst.i64 0x2
    v22 = iconst.i64 0x2
    v2 = iconcat v21, v22
    v10 = icmp.i128 ugt v1, v2
    return v10
}

; run

Here is the output I get from running [~/Documents/projects/cranelift]$ RUST_LOG=cranelift_codegen::context=debug cargo run -- test -dvT filetests/isa/x86/icmp-i128.clif:

    Finished dev [unoptimized + debuginfo] target(s) in 0.05s
     Running `target/debug/clif-util test -dvT filetests/isa/x86/icmp-i128.clif`
 DEBUG cranelift_codegen::context > Compiling:
function %test_icmp_eq_i12() -> b1 system_v {
ebb0:
    v11 = iconst.i64 0
    v12 = iconst.i64 0
    v1 = iconcat v11, v12
    v21 = iconst.i64 0
    v22 = iconst.i64 0
    v2 = iconcat v21, v22
    v10 = icmp eq v1, v2
    return v10
}

 DEBUG cranelift_codegen::context > Legalized:
function %test_icmp_eq_i12() -> b1 [%rax] system_v {
                                ebb0:
[RexOp1pu_id#b8]                    v11 = iconst.i64 0
[RexOp1pu_id#b8]                    v12 = iconst.i64 0
[-]                                 v1 = iconcat v11, v12
[RexOp1pu_id#b8]                    v21 = iconst.i64 0
[RexOp1pu_id#b8]                    v22 = iconst.i64 0
[-]                                 v2 = iconcat v21, v22
[RexOp1icscc#8039]                  v23 = icmp eq v11, v21
[RexOp1icscc#8039]                  v24 = icmp eq v12, v22
[RexOp1rr#21]                       v10 = band v23, v24
[Op1ret#c3]                         return v10
}

 DEBUG cranelift_codegen::context > Compiled:
function %test_icmp_eq_i12(i64 fp [%rbp], i64 csr [%rbx]) -> b1 [%rax], i64 fp [%rbp], i64 csr [%rbx] system_v {
    ss0 = incoming_arg 24, offset -24

                                ebb0(v25: i64 [%rbp], v26: i64 [%rbx]):
[RexOp1pushq#50]                    x86_push v25
[RexOp1copysp#8089]                 copy_special %rsp -> %rbp
[RexOp1pushq#50]                    x86_push v26
[RexOp1adjustsp_ib#d083]            adjust_sp_down_imm 8
[RexOp1pu_id#b8,%rax]               v11 = iconst.i64 0
[RexOp1pu_id#b8,%rcx]               v12 = iconst.i64 0
[-,-]                               v1 = iconcat v11, v12
[RexOp1pu_id#b8,%rdx]               v21 = iconst.i64 0
[RexOp1pu_id#b8,%rbx]               v22 = iconst.i64 0
[-,-]                               v2 = iconcat v21, v22
[RexOp1icscc#8039,%rax]             v23 = icmp eq v11, v21
[RexOp1icscc#8039,%rcx]             v24 = icmp eq v12, v22
[RexOp1rr#21,%rax]                  v10 = band v23, v24
[RexOp1adjustsp_ib#8083]            adjust_sp_up_imm 8
[RexOp1popq#58,%rbx]                v28 = x86_pop.i64 
[RexOp1popq#58,%rbp]                v27 = x86_pop.i64 
[Op1ret#c3]                         return v10, v27, v28
}

 DEBUG cranelift_codegen::context > Compiling:
function %test_icmp_imm_eq() -> b1 system_v {
ebb0:
    v11 = iconst.i64 0
    v12 = iconst.i64 0
    v1 = iconcat v11, v12
    v10 = icmp_imm eq v1, 0
    return v10
}

 DEBUG cranelift_codegen::context > Legalized:
function %test_icmp_imm_eq() -> b1 [%rax] system_v {
                                ebb0:
[RexOp1pu_id#b8]                    v11 = iconst.i64 0
                                    v15 -> v11
[RexOp1pu_id#b8]                    v12 = iconst.i64 0
                                    v16 -> v12
[-]                                 v1 = iconcat v11, v12
[RexOp1pu_id#b8]                    v13 = iconst.i64 0
[RexOp1pu_id#b8]                    v14 = iconst.i64 0
[RexOp1icscc#8039]                  v17 = icmp eq v15, v13
[RexOp1icscc#8039]                  v18 = icmp eq v16, v14
[RexOp1rr#21]                       v10 = band v17, v18
[Op1ret#c3]                         return v10
}

 DEBUG cranelift_codegen::context > Compiled:
function %test_icmp_imm_eq(i64 fp [%rbp], i64 csr [%rbx]) -> b1 [%rax], i64 fp [%rbp], i64 csr [%rbx] system_v {
    ss0 = incoming_arg 24, offset -24

                                ebb0(v19: i64 [%rbp], v20: i64 [%rbx]):
[RexOp1pushq#50]                    x86_push v19
[RexOp1copysp#8089]                 copy_special %rsp -> %rbp
[RexOp1pushq#50]                    x86_push v20
[RexOp1adjustsp_ib#d083]            adjust_sp_down_imm 8
[RexOp1pu_id#b8,%rax]               v11 = iconst.i64 0
                                    v15 -> v11
[RexOp1pu_id#b8,%rcx]               v12 = iconst.i64 0
                                    v16 -> v12
[-,-]                               v1 = iconcat v11, v12
[RexOp1pu_id#b8,%rdx]               v13 = iconst.i64 0
[RexOp1pu_id#b8,%rbx]               v14 = iconst.i64 0
[RexOp1icscc#8039,%rax]             v17 = icmp eq v11, v13
[RexOp1icscc#8039,%rcx]             v18 = icmp eq v12, v14
[RexOp1rr#21,%rax]                  v10 = band v17, v18
[RexOp1adjustsp_ib#8083]            adjust_sp_up_imm 8
[RexOp1popq#58,%rbx]                v22 = x86_pop.i64 
[RexOp1popq#58,%rbp]                v21 = x86_pop.i64 
[Op1ret#c3]                         return v10, v21, v22
}

 DEBUG cranelift_codegen::context > Compiling:
function %test_icmp_ne_i12() -> b1 system_v {
ebb0:
    v11 = iconst.i64 0
    v12 = iconst.i64 0
    v1 = iconcat v11, v12
    v21 = iconst.i64 0
    v22 = iconst.i64 1
    v2 = iconcat v21, v22
    v10 = icmp ne v1, v2
    return v10
}

 DEBUG cranelift_codegen::context > Legalized:
function %test_icmp_ne_i12() -> b1 [%rax] system_v {
                                ebb0:
[RexOp1pu_id#b8]                    v11 = iconst.i64 0
[RexOp1pu_id#b8]                    v12 = iconst.i64 0
[-]                                 v1 = iconcat v11, v12
[RexOp1pu_id#b8]                    v21 = iconst.i64 0
[RexOp1pu_id#b8]                    v22 = iconst.i64 1
[-]                                 v2 = iconcat v21, v22
[RexOp1icscc#8039]                  v23 = icmp ne v11, v21
[RexOp1icscc#8039]                  v24 = icmp ne v12, v22
[RexOp1rr#09]                       v10 = bor v23, v24
[Op1ret#c3]                         return v10
}

 DEBUG cranelift_codegen::context > Compiled:
function %test_icmp_ne_i12(i64 fp [%rbp], i64 csr [%rbx]) -> b1 [%rax], i64 fp [%rbp], i64 csr [%rbx] system_v {
    ss0 = incoming_arg 24, offset -24

                                ebb0(v25: i64 [%rbp], v26: i64 [%rbx]):
[RexOp1pushq#50]                    x86_push v25
[RexOp1copysp#8089]                 copy_special %rsp -> %rbp
[RexOp1pushq#50]                    x86_push v26
[RexOp1adjustsp_ib#d083]            adjust_sp_down_imm 8
[RexOp1pu_id#b8,%rax]               v11 = iconst.i64 0
[RexOp1pu_id#b8,%rcx]               v12 = iconst.i64 0
[-,-]                               v1 = iconcat v11, v12
[RexOp1pu_id#b8,%rdx]               v21 = iconst.i64 0
[RexOp1pu_id#b8,%rbx]               v22 = iconst.i64 1
[-,-]                               v2 = iconcat v21, v22
[RexOp1icscc#8039,%rax]             v23 = icmp ne v11, v21
[RexOp1icscc#8039,%rcx]             v24 = icmp ne v12, v22
[RexOp1rr#09,%rax]                  v10 = bor v23, v24
[RexOp1adjustsp_ib#8083]            adjust_sp_up_imm 8
[RexOp1popq#58,%rbx]                v28 = x86_pop.i64 
[RexOp1popq#58,%rbp]                v27 = x86_pop.i64 
[Op1ret#c3]                         return v10, v27, v28
}

 DEBUG cranelift_codegen::context > Compiling:
function %test_icmp_imm_ne() -> b1 system_v {
ebb0:
    v11 = iconst.i64 0
    v12 = iconst.i64 0
    v1 = iconcat v11, v12
    v10 = icmp_imm ne v1, 1
    return v10
}

 DEBUG cranelift_codegen::context > Legalized:
function %test_icmp_imm_ne() -> b1 [%rax] system_v {
                                ebb0:
[RexOp1pu_id#b8]                    v11 = iconst.i64 0
                                    v15 -> v11
[RexOp1pu_id#b8]                    v12 = iconst.i64 0
                                    v16 -> v12
[-]                                 v1 = iconcat v11, v12
[RexOp1pu_id#b8]                    v13 = iconst.i64 0
[RexOp1pu_id#b8]                    v14 = iconst.i64 1
[RexOp1icscc#8039]                  v17 = icmp ne v15, v13
[RexOp1icscc#8039]                  v18 = icmp ne v16, v14
[RexOp1rr#09]                       v10 = bor v17, v18
[Op1ret#c3]                         return v10
}

 DEBUG cranelift_codegen::context > Compiled:
function %test_icmp_imm_ne(i64 fp [%rbp], i64 csr [%rbx]) -> b1 [%rax], i64 fp [%rbp], i64 csr [%rbx] system_v {
    ss0 = incoming_arg 24, offset -24

                                ebb0(v19: i64 [%rbp], v20: i64 [%rbx]):
[RexOp1pushq#50]                    x86_push v19
[RexOp1copysp#8089]                 copy_special %rsp -> %rbp
[RexOp1pushq#50]                    x86_push v20
[RexOp1adjustsp_ib#d083]            adjust_sp_down_imm 8
[RexOp1pu_id#b8,%rax]               v11 = iconst.i64 0
                                    v15 -> v11
[RexOp1pu_id#b8,%rcx]               v12 = iconst.i64 0
                                    v16 -> v12
[-,-]                               v1 = iconcat v11, v12
[RexOp1pu_id#b8,%rdx]               v13 = iconst.i64 0
[RexOp1pu_id#b8,%rbx]               v14 = iconst.i64 1
[RexOp1icscc#8039,%rax]             v17 = icmp ne v11, v13
[RexOp1icscc#8039,%rcx]             v18 = icmp ne v12, v14
[RexOp1rr#09,%rax]                  v10 = bor v17, v18
[RexOp1adjustsp_ib#8083]            adjust_sp_up_imm 8
[RexOp1popq#58,%rbx]                v22 = x86_pop.i64 
[RexOp1popq#58,%rbp]                v21 = x86_pop.i64 
[Op1ret#c3]                         return v10, v21, v22
}

 DEBUG cranelift_codegen::context > Compiling:
function %test_icmp_nz_eq_() -> b1 system_v {
ebb0:
    v11 = iconst.i64 1
    v12 = iconst.i64 1
    v1 = iconcat v11, v12
    v21 = iconst.i64 1
    v22 = iconst.i64 1
    v2 = iconcat v21, v22
    v10 = icmp eq v1, v2
    return v10
}

 DEBUG cranelift_codegen::context > Legalized:
function %test_icmp_nz_eq_() -> b1 [%rax] system_v {
                                ebb0:
[RexOp1pu_id#b8]                    v11 = iconst.i64 1
[RexOp1pu_id#b8]                    v12 = iconst.i64 1
[-]                                 v1 = iconcat v11, v12
[RexOp1pu_id#b8]                    v21 = iconst.i64 1
[RexOp1pu_id#b8]                    v22 = iconst.i64 1
[-]                                 v2 = iconcat v21, v22
[RexOp1icscc#8039]                  v23 = icmp eq v11, v21
[RexOp1icscc#8039]                  v24 = icmp eq v12, v22
[RexOp1rr#21]                       v10 = band v23, v24
[Op1ret#c3]                         return v10
}

 DEBUG cranelift_codegen::context > Compiled:
function %test_icmp_nz_eq_(i64 fp [%rbp], i64 csr [%rbx]) -> b1 [%rax], i64 fp [%rbp], i64 csr [%rbx] system_v {
    ss0 = incoming_arg 24, offset -24

                                ebb0(v25: i64 [%rbp], v26: i64 [%rbx]):
[RexOp1pushq#50]                    x86_push v25
[RexOp1copysp#8089]                 copy_special %rsp -> %rbp
[RexOp1pushq#50]                    x86_push v26
[RexOp1adjustsp_ib#d083]            adjust_sp_down_imm 8
[RexOp1pu_id#b8,%rax]               v11 = iconst.i64 1
[RexOp1pu_id#b8,%rcx]               v12 = iconst.i64 1
[-,-]                               v1 = iconcat v11, v12
[RexOp1pu_id#b8,%rdx]               v21 = iconst.i64 1
[RexOp1pu_id#b8,%rbx]               v22 = iconst.i64 1
[-,-]                               v2 = iconcat v21, v22
[RexOp1icscc#8039,%rax]             v23 = icmp eq v11, v21
[RexOp1icscc#8039,%rcx]             v24 = icmp eq v12, v22
[RexOp1rr#21,%rax]                  v10 = band v23, v24
[RexOp1adjustsp_ib#8083]            adjust_sp_up_imm 8
[RexOp1popq#58,%rbx]                v28 = x86_pop.i64 
[RexOp1popq#58,%rbp]                v27 = x86_pop.i64 
[Op1ret#c3]                         return v10, v27, v28
}

 DEBUG cranelift_codegen::context > Compiling:
function %test_icmp_nz_gt_() -> b1 system_v {
ebb0:
    v11 = iconst.i64 1
    v12 = iconst.i64 1
    v1 = iconcat v11, v12
    v21 = iconst.i64 2
    v22 = iconst.i64 2
    v2 = iconcat v21, v22
    v10 = icmp ugt v1, v2
    return v10
}

 DEBUG cranelift_codegen::context > Legalized:
function %test_icmp_nz_gt_() -> b1 [%rax] system_v {
                                ebb0:
[RexOp1pu_id#b8]                    v11 = iconst.i64 1
[RexOp1pu_id#b8]                    v12 = iconst.i64 1
[-]                                 v1 = iconcat v11, v12
[RexOp1pu_id#b8]                    v21 = iconst.i64 2
[RexOp1pu_id#b8]                    v22 = iconst.i64 2
[-]                                 v2 = iconcat v21, v22
[RexOp1icscc#8039]                  v23 = icmp ugt v12, v22
[RexOp1icscc#8039]                  v24 = icmp ult v12, v22
[RexOp1icscc#8039]                  v25 = icmp ugt v11, v21
[-]                                 v26 = bnot v24
[RexOp1rr#21]                       v27 = band v26, v25
[RexOp1rr#09]                       v10 = bor v23, v27
[Op1ret#c3]                         return v10
}

FAIL filetests/isa/x86/icmp-i128.clif: run(%test_icmp_nz_gt_):
Verifier errors:
- inst11: v26 is a real GPR value defined by a ghost instruction

======== ========  ==================================
   Total     Self  Pass
-------- --------  ----------------------------------
   0.019    0.001  Processing test file
   0.002    0.002  Parsing textual Cranelift IR
   0.007    0.005  Verify Cranelift IR
   0.000    0.000  Verify CSSA
   0.000    0.000  Verify live ranges
   0.000    0.000  Verify value locations
   0.001    0.001  Verify CPU flags
   0.015    0.005  Compilation passes
   0.000    0.000  Control flow graph
   0.000    0.000  Dominator tree
   0.000    0.000  Legalization
   0.000    0.000  Remove unreachable blocks
   0.005    0.000  Register allocation
   0.000    0.000  RA liveness analysis
   0.000    0.000  RA coalescing CSSA
   0.000    0.000  RA spilling
   0.000    0.000  RA reloading
   0.001    0.001  RA coloring
   0.000    0.000  Prologue/epilogue insertion
   0.000    0.000  Branch relaxation
   0.000    0.000  Binary machine code emission
======== ========  ==================================

1 tests
1 failure

Any ideas for what I'm doing incorrectly here?

ash-tamraz avatar Oct 26 '19 21:10 ash-tamraz

It seems like bnot.b1 is not yet implemented.

bjorn3 avatar Oct 26 '19 21:10 bjorn3

Does that mean I should wait to write tests for greater than and greater than or equal until that is implemented? Or is there a way to write tests for those conditions that doesn't require bnot.b1?

ash-tamraz avatar Oct 26 '19 21:10 ash-tamraz

Does that mean I should wait to write tests for greater than and greater than or equal until that is implemented?

Yes, or write a legalization for bint.b1 which expands to for example bint + bnot + icmp ne 0.

Or is there a way to write tests for those conditions that doesn't require bnot.b1?

No, without bnot.b1 icmp.i128 ugt wont work.

bjorn3 avatar Oct 26 '19 22:10 bjorn3

Does that mean I should wait to write tests for greater than and greater than or equal until that is implemented?

Yes, or write a legalization for bint.b1 which expands to for example bint + bnot + icmp ne 0.

I would be glad to work on that. Is that something I would open a separate issue for and assign to myself, or wrap into a PR for this via a separate commit?

Or is there a way to write tests for those conditions that doesn't require bnot.b1?

No, without bnot.b1 icmp.i128 ugt wont work.

Good to know, thanks :)

ash-tamraz avatar Oct 26 '19 22:10 ash-tamraz

It is part of https://github.com/CraneStation/cranelift/issues/1117. You could do both impl and test in one PR.

bjorn3 avatar Oct 27 '19 07:10 bjorn3

@ash-tamraz are you working on legalization for b1? If not I am interested in taking a look, how would I get started?

jyn514 avatar Nov 03 '19 02:11 jyn514

@ash-tamraz are you working on legalization for b1? If not I am interested in taking a look, how would I get started?

Yeah I'm working on that right now

ash-tamraz avatar Nov 03 '19 03:11 ash-tamraz