To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

test_controlflow.rs 27.7 KB
Newer Older
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
1
// Copyright 2017 The Australian National University
2
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
3
4
5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
6
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
9
10
11
12
13
14
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

qinsoon's avatar
qinsoon committed
15
16
extern crate libloading;

qinsoon's avatar
qinsoon committed
17
18
19
20
21
use mu::ast::types::*;
use mu::ast::ir::*;
use mu::ast::inst::*;
use mu::ast::op::*;
use mu::vm::*;
qinsoon's avatar
qinsoon committed
22
use mu::linkutils;
23
use mu::utils::LinkedHashMap;
qinsoon's avatar
qinsoon committed
24
25
26

#[test]
fn test_switch() {
qinsoon's avatar
qinsoon committed
27
    let lib = linkutils::aot::compile_fnc("switch", &switch);
qinsoon's avatar
qinsoon committed
28
29

    unsafe {
30
31
        let switch: libloading::Symbol<unsafe extern "C" fn(u64) -> u64> =
            lib.get(b"switch").unwrap();
qinsoon's avatar
qinsoon committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

        let res = switch(0);
        println!("switch(0) = {}", res);
        assert!(res == 0);

        let res = switch(1);
        println!("switch(1) = {}", res);
        assert!(res == 1);

        let res = switch(2);
        println!("switch(2) = {}", res);
        assert!(res == 2);

        let res = switch(3);
        println!("switch(3) = {}", res);
        assert!(res == 99);
    }
}

fn switch() -> VM {
    let vm = VM::new();

54
    typedef!    ((vm) int64 = mu_int(64));
qinsoon's avatar
qinsoon committed
55

56
57
58
59
60
61
62
63
    constdef!   ((vm) <int64> int64_0  = Constant::Int(0));
    constdef!   ((vm) <int64> int64_1  = Constant::Int(1));
    constdef!   ((vm) <int64> int64_2  = Constant::Int(2));
    constdef!   ((vm) <int64> int64_99 = Constant::Int(99));

    funcsig!    ((vm) switch_sig = (int64) -> (int64));
    funcdecl!   ((vm) <switch_sig> switch);
    funcdef!    ((vm) <switch_sig> switch VERSION switch_v1);
qinsoon's avatar
qinsoon committed
64

65
66
67
    // %entry(<@int64> %a):
    block!      ((vm, switch_v1) blk_entry);
    ssa!        ((vm, switch_v1) <int64> a);
qinsoon's avatar
qinsoon committed
68
69

    // SWITCH %a %blk_default (0 -> %blk_ret0, 1 -> %blk_ret1, 2 -> %blk_ret2)
70
71
72
73
    block!      ((vm, switch_v1) blk_ret0);
    block!      ((vm, switch_v1) blk_ret1);
    block!      ((vm, switch_v1) blk_ret2);
    block!      ((vm, switch_v1) blk_default);
qinsoon's avatar
qinsoon committed
74

75
76
77
    consta!     ((vm, switch_v1) int64_0_local = int64_0);
    consta!     ((vm, switch_v1) int64_1_local = int64_1);
    consta!     ((vm, switch_v1) int64_2_local = int64_2);
qinsoon's avatar
qinsoon committed
78

79
    let blk_entry_switch = switch_v1.new_inst(Instruction {
qinsoon's avatar
qinsoon committed
80
81
        hdr: MuEntityHeader::unnamed(vm.next_id()),
        value: None,
82
        ops: vec![
83
84
85
86
            a.clone(), // 0
            int64_0_local.clone(), // 1
            int64_1_local.clone(), // 2
            int64_2_local.clone(), // 3
87
        ],
qinsoon's avatar
qinsoon committed
88
89
90
        v: Instruction_::Switch {
            cond: 0,
            default: Destination {
91
                target: blk_default.id(),
qinsoon's avatar
qinsoon committed
92
93
94
                args: vec![]
            },
            branches: vec![
95
96
97
                (1, Destination{target: blk_ret0.id(), args: vec![]}),
                (2, Destination{target: blk_ret1.id(), args: vec![]}),
                (3, Destination{target: blk_ret2.id(), args: vec![]})
qinsoon's avatar
qinsoon committed
98
99
100
101
            ]
        }
    });

102
103
    define_block!((vm, switch_v1) blk_entry(a) {
        blk_entry_switch
qinsoon's avatar
qinsoon committed
104
105
106
    });

    // blk_default
107
108
109
110
    consta!     ((vm, switch_v1) int64_99_local = int64_99);
    inst!       ((vm, switch_v1) blk_default_ret:
        RET (int64_99_local)
    );
qinsoon's avatar
qinsoon committed
111

112
113
    define_block!((vm, switch_v1) blk_default() {
        blk_default_ret
qinsoon's avatar
qinsoon committed
114
115
116
    });

    // blk_ret0
117
118
119
    inst!       ((vm, switch_v1) blk_ret0_ret:
        RET (int64_0_local)
    );
qinsoon's avatar
qinsoon committed
120

121
122
    define_block!((vm, switch_v1) blk_ret0() {
        blk_ret0_ret
qinsoon's avatar
qinsoon committed
123
124
125
    });

    // blk_ret1
126
127
128
    inst!       ((vm, switch_v1) blk_ret1_ret:
        RET (int64_1_local)
    );
qinsoon's avatar
qinsoon committed
129

130
131
    define_block!((vm, switch_v1) blk_ret1() {
        blk_ret1_ret
qinsoon's avatar
qinsoon committed
132
133
134
    });

    // blk_ret2
135
136
137
    inst!       ((vm, switch_v1) blk_ret2_ret:
        RET (int64_2_local)
    );
qinsoon's avatar
qinsoon committed
138

139
140
    define_block!((vm, switch_v1) blk_ret2() {
        blk_ret2_ret
qinsoon's avatar
qinsoon committed
141
142
    });

143
144
145
146
147
148
    define_func_ver!((vm) switch_v1 (entry: blk_entry) {
        blk_entry,
        blk_default,
        blk_ret0,
        blk_ret1,
        blk_ret2
qinsoon's avatar
qinsoon committed
149
150
    });

qinsoon's avatar
qinsoon committed
151
152
153
154
155
    vm
}

#[test]
fn test_select_eq_zero() {
qinsoon's avatar
qinsoon committed
156
    let lib = linkutils::aot::compile_fnc("select_eq_zero", &select_eq_zero);
qinsoon's avatar
qinsoon committed
157
158

    unsafe {
159
160
        let select_eq_zero: libloading::Symbol<unsafe extern "C" fn(u64) -> u64> =
            lib.get(b"select_eq_zero").unwrap();
qinsoon's avatar
qinsoon committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

        let res = select_eq_zero(0);
        println!("select_eq_zero(0) = {}", res);
        assert!(res == 1);

        let res = select_eq_zero(1);
        println!("select_eq_zero(1) = {}", res);
        assert!(res == 0);
    }
}

fn select_eq_zero() -> VM {
    let vm = VM::new();

    typedef! ((vm) int64 = mu_int(64));
    typedef! ((vm) int1  = mu_int(1));
    constdef!((vm) <int64> int64_0 = Constant::Int(0));
    constdef!((vm) <int64> int64_1 = Constant::Int(1));

    funcsig! ((vm) sig = (int64) -> (int64));
    funcdecl!((vm) <sig> select_eq_zero);
    funcdef! ((vm) <sig> select_eq_zero VERSION select_v1);

    // blk entry
    block! ((vm, select_v1) blk_entry);
    ssa!   ((vm, select_v1) <int64> blk_entry_n);

    ssa!   ((vm, select_v1) <int1> blk_entry_cond);
    consta!((vm, select_v1) int64_0_local = int64_0);
    consta!((vm, select_v1) int64_1_local = int64_1);
    inst!  ((vm, select_v1) blk_entry_inst_cmp:
        blk_entry_cond = CMPOP (CmpOp::EQ) blk_entry_n int64_0_local
    );

    ssa!   ((vm, select_v1) <int64> blk_entry_ret);
    inst!  ((vm, select_v1) blk_entry_inst_select:
        blk_entry_ret = SELECT blk_entry_cond int64_1_local int64_0_local
    );

    inst!  ((vm, select_v1) blk_entry_inst_ret:
        RET (blk_entry_ret)
    );

    define_block!   ((vm, select_v1) blk_entry(blk_entry_n){
        blk_entry_inst_cmp, blk_entry_inst_select, blk_entry_inst_ret
    });

    define_func_ver!((vm) select_v1 (entry: blk_entry) {blk_entry});

    vm
}

qinsoon's avatar
qinsoon committed
213
214
#[test]
fn test_select_u8_eq_zero() {
qinsoon's avatar
qinsoon committed
215
    let lib = linkutils::aot::compile_fnc("select_u8_eq_zero", &select_u8_eq_zero);
qinsoon's avatar
qinsoon committed
216
217

    unsafe {
218
219
        let select_eq_zero: libloading::Symbol<unsafe extern "C" fn(u8) -> u8> =
            lib.get(b"select_u8_eq_zero").unwrap();
qinsoon's avatar
qinsoon committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

        let res = select_eq_zero(0);
        println!("select_u8_eq_zero(0) = {}", res);
        assert!(res == 1);

        let res = select_eq_zero(1);
        println!("select_u8_eq_zero(1) = {}", res);
        assert!(res == 0);
    }
}

fn select_u8_eq_zero() -> VM {
    let vm = VM::new();

    typedef! ((vm) int8 = mu_int(8));
    typedef! ((vm) int1  = mu_int(1));
    constdef!((vm) <int8> int8_0 = Constant::Int(0));
    constdef!((vm) <int8> int8_1 = Constant::Int(1));

    funcsig! ((vm) sig = (int8) -> (int8));
    funcdecl!((vm) <sig> select_u8_eq_zero);
    funcdef! ((vm) <sig> select_u8_eq_zero VERSION select_u8_eq_zero_v1);

    // blk entry
    block! ((vm, select_u8_eq_zero_v1) blk_entry);
    ssa!   ((vm, select_u8_eq_zero_v1) <int8> blk_entry_n);

    ssa!   ((vm, select_u8_eq_zero_v1) <int1> blk_entry_cond);
    consta!((vm, select_u8_eq_zero_v1) int8_0_local = int8_0);
    consta!((vm, select_u8_eq_zero_v1) int8_1_local = int8_1);
    inst!  ((vm, select_u8_eq_zero_v1) blk_entry_inst_cmp:
        blk_entry_cond = CMPOP (CmpOp::EQ) blk_entry_n int8_0_local
    );

    ssa!   ((vm, select_u8_eq_zero_v1) <int8> blk_entry_ret);
    inst!  ((vm, select_u8_eq_zero_v1) blk_entry_inst_select:
        blk_entry_ret = SELECT blk_entry_cond int8_1_local int8_0_local
    );

    inst!  ((vm, select_u8_eq_zero_v1) blk_entry_inst_ret:
        RET (blk_entry_ret)
    );

    define_block!   ((vm, select_u8_eq_zero_v1) blk_entry(blk_entry_n){
        blk_entry_inst_cmp, blk_entry_inst_select, blk_entry_inst_ret
    });

    define_func_ver!((vm) select_u8_eq_zero_v1 (entry: blk_entry) {blk_entry});

    vm
}

qinsoon's avatar
qinsoon committed
272
273
#[test]
fn test_select_sge_zero() {
qinsoon's avatar
qinsoon committed
274
    let lib = linkutils::aot::compile_fnc("select_sge_zero", &select_sge_zero);
qinsoon's avatar
qinsoon committed
275
276

    unsafe {
277
278
        let select_sge_zero: libloading::Symbol<unsafe extern "C" fn(i64) -> u64> =
            lib.get(b"select_sge_zero").unwrap();
qinsoon's avatar
qinsoon committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331

        let res = select_sge_zero(0);
        println!("select_sge_zero(0) = {}", res);
        assert!(res == 1);

        let res = select_sge_zero(1);
        println!("select_sge_zero(1) = {}", res);
        assert!(res == 1);

        let res = select_sge_zero(-1);
        println!("select_sge_zero(-1) = {}", res);
        assert!(res == 0);
    }
}

fn select_sge_zero() -> VM {
    let vm = VM::new();

    typedef! ((vm) int64 = mu_int(64));
    typedef! ((vm) int1  = mu_int(1));
    constdef!((vm) <int64> int64_0 = Constant::Int(0));
    constdef!((vm) <int64> int64_1 = Constant::Int(1));

    funcsig! ((vm) sig = (int64) -> (int64));
    funcdecl!((vm) <sig> select_sge_zero);
    funcdef! ((vm) <sig> select_sge_zero VERSION select_v1);

    // blk entry
    block! ((vm, select_v1) blk_entry);
    ssa!   ((vm, select_v1) <int64> blk_entry_n);

    ssa!   ((vm, select_v1) <int1> blk_entry_cond);
    consta!((vm, select_v1) int64_0_local = int64_0);
    consta!((vm, select_v1) int64_1_local = int64_1);
    inst!  ((vm, select_v1) blk_entry_inst_cmp:
        blk_entry_cond = CMPOP (CmpOp::SGE) blk_entry_n int64_0_local
    );

    ssa!   ((vm, select_v1) <int64> blk_entry_ret);
    inst!  ((vm, select_v1) blk_entry_inst_select:
        blk_entry_ret = SELECT blk_entry_cond int64_1_local int64_0_local
    );

    inst!  ((vm, select_v1) blk_entry_inst_ret:
        RET (blk_entry_ret)
    );

    define_block!   ((vm, select_v1) blk_entry(blk_entry_n){
        blk_entry_inst_cmp, blk_entry_inst_select, blk_entry_inst_ret
    });

    define_func_ver!((vm) select_v1 (entry: blk_entry) {blk_entry});

332
333
334
335
336
    vm
}

#[test]
fn test_sgt_value() {
qinsoon's avatar
qinsoon committed
337
    let lib = linkutils::aot::compile_fnc("sgt_value", &sgt_value);
338
339

    unsafe {
340
341
        let sgt_value: libloading::Symbol<unsafe extern "C" fn(i64, i64) -> u8> =
            lib.get(b"sgt_value").unwrap();
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

        let res = sgt_value(255, 0);
        println!("sgt_value(255, 0) = {}", res);
        assert!(res == 1);

        let res = sgt_value(255, 255);
        println!("sgt_value(255, 255) = {}", res);
        assert!(res == 0);

        let res = sgt_value(0, 255);
        println!("sgt_value(0, 255) = {}", res);
        assert!(res == 0);
    }
}

fn sgt_value() -> VM {
    let vm = VM::new();

    typedef! ((vm) int64 = mu_int(64));
    typedef! ((vm) int1  = mu_int(1));
    constdef!((vm) <int64> int64_0 = Constant::Int(0));
    constdef!((vm) <int64> int64_1 = Constant::Int(1));

    funcsig! ((vm) sig = (int64, int64) -> (int1));
    funcdecl!((vm) <sig> sgt_value);
    funcdef! ((vm) <sig> sgt_value VERSION sgt_value_v1);

    // blk entry
    block! ((vm, sgt_value_v1) blk_entry);
    ssa!   ((vm, sgt_value_v1) <int64> blk_entry_op1);
    ssa!   ((vm, sgt_value_v1) <int64> blk_entry_op2);

    ssa!   ((vm, sgt_value_v1) <int1> blk_entry_cond);
    inst!  ((vm, sgt_value_v1) blk_entry_inst_cmp:
        blk_entry_cond = CMPOP (CmpOp::SGT) blk_entry_op1 blk_entry_op2
    );

    inst!  ((vm, sgt_value_v1) blk_entry_inst_ret:
        RET (blk_entry_cond)
    );

    define_block!   ((vm, sgt_value_v1) blk_entry(blk_entry_op1, blk_entry_op2){
        blk_entry_inst_cmp, blk_entry_inst_ret
    });

    define_func_ver!((vm) sgt_value_v1 (entry: blk_entry) {blk_entry});

    vm
}

#[test]
fn test_sgt_u8_value() {
qinsoon's avatar
qinsoon committed
394
    let lib = linkutils::aot::compile_fnc("sgt_u8_value", &sgt_u8_value);
395
396

    unsafe {
397
398
        let sgt_u8_value: libloading::Symbol<unsafe extern "C" fn(i8, i8) -> u8> =
            lib.get(b"sgt_u8_value").unwrap();
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457

        let res = sgt_u8_value(-1, 0);
        println!("sgt_u8_value(-1, 0) = {}", res);
        assert!(res == 0);

        let res = sgt_u8_value(0, -1);
        println!("sgt_u8_value(0, -1) = {}", res);
        assert!(res == 1);

        let res = sgt_u8_value(2, 1);
        println!("sgt_u8_value(2, 1) = {}", res);
        assert!(res == 1);

        let res = sgt_u8_value(1, 2);
        println!("sgt_u8_value(1, 2) = {}", res);
        assert!(res == 0);

        let res = sgt_u8_value(-2, -1);
        println!("sgt_u8_value(-2, -1) = {}", res);
        assert!(res == 0);

        let res = sgt_u8_value(-1, -2);
        println!("sgt_u8_value(-1, -2) = {}", res);
        assert!(res == 1);
    }
}

fn sgt_u8_value() -> VM {
    let vm = VM::new();

    typedef! ((vm) int8  = mu_int(8));
    typedef! ((vm) int1  = mu_int(1));
    constdef!((vm) <int8> int8_0 = Constant::Int(0));
    constdef!((vm) <int8> int8_1 = Constant::Int(1));

    funcsig! ((vm) sig = (int8, int8) -> (int1));
    funcdecl!((vm) <sig> sgt_u8_value);
    funcdef! ((vm) <sig> sgt_u8_value VERSION sgt_u8_value_v1);

    // blk entry
    block! ((vm, sgt_u8_value_v1) blk_entry);
    ssa!   ((vm, sgt_u8_value_v1) <int8> blk_entry_op1);
    ssa!   ((vm, sgt_u8_value_v1) <int8> blk_entry_op2);

    ssa!   ((vm, sgt_u8_value_v1) <int1> blk_entry_cond);
    inst!  ((vm, sgt_u8_value_v1) blk_entry_inst_cmp:
        blk_entry_cond = CMPOP (CmpOp::SGT) blk_entry_op1 blk_entry_op2
    );

    inst!  ((vm, sgt_u8_value_v1) blk_entry_inst_ret:
        RET (blk_entry_cond)
    );

    define_block!   ((vm, sgt_u8_value_v1) blk_entry(blk_entry_op1, blk_entry_op2){
        blk_entry_inst_cmp, blk_entry_inst_ret
    });

    define_func_ver!((vm) sgt_u8_value_v1 (entry: blk_entry) {blk_entry});

qinsoon's avatar
qinsoon committed
458
459
460
461
462
    vm
}

#[test]
fn test_sgt_i32_branch() {
qinsoon's avatar
qinsoon committed
463
    let lib = linkutils::aot::compile_fnc("sgt_i32_branch", &sgt_i32_branch);
qinsoon's avatar
qinsoon committed
464
465

    unsafe {
466
467
        let sgt_i32: libloading::Symbol<unsafe extern "C" fn(i32, i32) -> u32> =
            lib.get(b"sgt_i32_branch").unwrap();
qinsoon's avatar
qinsoon committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574

        let res = sgt_i32(-1, 0);
        println!("sgt_i32(-1, 0) = {}", res);
        assert!(res == 0);

        let res = sgt_i32(0, -1);
        println!("sgt_i32(0, -1) = {}", res);
        assert!(res == 1);

        let res = sgt_i32(-1, -1);
        println!("sgt_i32(-1, -1) = {}", res);
        assert!(res == 0);

        let res = sgt_i32(2, 1);
        println!("sgt_i32(2, 1) = {}", res);
        assert!(res == 1);

        let res = sgt_i32(1, 2);
        println!("sgt_i32(1, 2) = {}", res);
        assert!(res == 0);

        let res = sgt_i32(2, 2);
        println!("sgt_i32(2, 2) = {}", res);
        assert!(res == 0);

        let res = sgt_i32(-2, -1);
        println!("sgt_i32(-2, -1) = {}", res);
        assert!(res == 0);

        let res = sgt_i32(-1, -2);
        println!("sgt_i32(-1, -2) = {}", res);
        assert!(res == 1);

        let res = sgt_i32(0, 0);
        println!("sgt_i32(0, 0) = {}", res);
        assert!(res == 0);
    }
}

fn sgt_i32_branch() -> VM {
    let vm = VM::new();

    typedef! ((vm) int32 = mu_int(32));
    typedef! ((vm) int1  = mu_int(1));

    constdef!((vm) <int32> int32_0 = Constant::Int(0));
    constdef!((vm) <int32> int32_1 = Constant::Int(1));

    funcsig! ((vm) sig = (int32, int32) -> (int32));
    funcdecl!((vm) <sig> sgt_i32_branch);
    funcdef! ((vm) <sig> sgt_i32_branch VERSION sgt_i32_branch_v1);

    // blk entry
    block!   ((vm, sgt_i32_branch_v1) blk_entry);
    ssa!     ((vm, sgt_i32_branch_v1) <int32> blk_entry_a);
    ssa!     ((vm, sgt_i32_branch_v1) <int32> blk_entry_b);

    ssa!     ((vm, sgt_i32_branch_v1) <int1> blk_entry_cond);
    inst!    ((vm, sgt_i32_branch_v1) blk_entry_cmp:
        blk_entry_cond = CMPOP (CmpOp::SGT) blk_entry_a blk_entry_b
    );

    block!   ((vm, sgt_i32_branch_v1) blk_ret1);
    consta!  ((vm, sgt_i32_branch_v1) int32_1_local = int32_1);
    block!   ((vm, sgt_i32_branch_v1) blk_ret0);
    consta!  ((vm, sgt_i32_branch_v1) int32_0_local = int32_0);

    inst!   ((vm, sgt_i32_branch_v1) blk_entry_branch:
        BRANCH2 (blk_entry_cond, int32_1_local, int32_0_local)
            IF (OP 0)
            THEN blk_ret1 (vec![1]) WITH 0.6f32,
            ELSE blk_ret0 (vec![2])
    );

    define_block! ((vm, sgt_i32_branch_v1) blk_entry(blk_entry_a, blk_entry_b){
        blk_entry_cmp, blk_entry_branch
    });

    // blk_ret1
    ssa!    ((vm, sgt_i32_branch_v1) <int32> blk_ret1_res);
    inst!   ((vm, sgt_i32_branch_v1) blk_ret1_inst:
        RET (blk_ret1_res)
    );

    define_block! ((vm, sgt_i32_branch_v1) blk_ret1(blk_ret1_res){
        blk_ret1_inst
    });

    // blk_ret0
    ssa!    ((vm, sgt_i32_branch_v1) <int32> blk_ret0_res);
    inst!   ((vm, sgt_i32_branch_v1) blk_ret0_inst:
        RET (blk_ret0_res)
    );

    define_block! ((vm, sgt_i32_branch_v1) blk_ret0(blk_ret0_res){
        blk_ret0_inst
    });

    define_func_ver!((vm) sgt_i32_branch_v1 (entry: blk_entry) {
        blk_entry, blk_ret1, blk_ret0
    });

    vm
}

#[test]
fn test_sge_i32_branch() {
qinsoon's avatar
qinsoon committed
575
    let lib = linkutils::aot::compile_fnc("sge_i32_branch", &sge_i32_branch);
qinsoon's avatar
qinsoon committed
576
577

    unsafe {
578
579
        let sge_i32: libloading::Symbol<unsafe extern "C" fn(i32, i32) -> u32> =
            lib.get(b"sge_i32_branch").unwrap();
qinsoon's avatar
qinsoon committed
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681

        let res = sge_i32(-1, 0);
        println!("sge_i32(-1, 0) = {}", res);
        assert!(res == 0);

        let res = sge_i32(0, -1);
        println!("sge_i32(0, -1) = {}", res);
        assert!(res == 1);

        let res = sge_i32(-1, -1);
        println!("sge_i32(-1, -1) = {}", res);
        assert!(res == 1);

        let res = sge_i32(2, 1);
        println!("sge_i32(2, 1) = {}", res);
        assert!(res == 1);

        let res = sge_i32(1, 2);
        println!("sge_i32(1, 2) = {}", res);
        assert!(res == 0);

        let res = sge_i32(2, 2);
        println!("sge_i32(2, 2) = {}", res);
        assert!(res == 1);

        let res = sge_i32(-2, -1);
        println!("sge_i32(-2, -1) = {}", res);
        assert!(res == 0);

        let res = sge_i32(-1, -2);
        println!("sge_i32(-1, -2) = {}", res);
        assert!(res == 1);

        let res = sge_i32(0, 0);
        println!("sge_i32(0, 0) = {}", res);
        assert!(res == 1);
    }
}

fn sge_i32_branch() -> VM {
    let vm = VM::new();

    typedef! ((vm) int32 = mu_int(32));
    typedef! ((vm) int1  = mu_int(1));

    constdef!((vm) <int32> int32_0 = Constant::Int(0));
    constdef!((vm) <int32> int32_1 = Constant::Int(1));

    funcsig! ((vm) sig = (int32, int32) -> (int32));
    funcdecl!((vm) <sig> sge_i32_branch);
    funcdef! ((vm) <sig> sge_i32_branch VERSION sge_i32_branch_v1);

    // blk entry
    block!   ((vm, sge_i32_branch_v1) blk_entry);
    ssa!     ((vm, sge_i32_branch_v1) <int32> blk_entry_a);
    ssa!     ((vm, sge_i32_branch_v1) <int32> blk_entry_b);

    ssa!     ((vm, sge_i32_branch_v1) <int1> blk_entry_cond);
    inst!    ((vm, sge_i32_branch_v1) blk_entry_cmp:
        blk_entry_cond = CMPOP (CmpOp::SGE) blk_entry_a blk_entry_b
    );

    block!   ((vm, sge_i32_branch_v1) blk_ret1);
    consta!  ((vm, sge_i32_branch_v1) int32_1_local = int32_1);
    block!   ((vm, sge_i32_branch_v1) blk_ret0);
    consta!  ((vm, sge_i32_branch_v1) int32_0_local = int32_0);

    inst!   ((vm, sge_i32_branch_v1) blk_entry_branch:
        BRANCH2 (blk_entry_cond, int32_1_local, int32_0_local)
            IF (OP 0)
            THEN blk_ret1 (vec![1]) WITH 0.6f32,
            ELSE blk_ret0 (vec![2])
    );

    define_block! ((vm, sge_i32_branch_v1) blk_entry(blk_entry_a, blk_entry_b){
        blk_entry_cmp, blk_entry_branch
    });

    // blk_ret1
    ssa!    ((vm, sge_i32_branch_v1) <int32> blk_ret1_res);
    inst!   ((vm, sge_i32_branch_v1) blk_ret1_inst:
        RET (blk_ret1_res)
    );

    define_block! ((vm, sge_i32_branch_v1) blk_ret1(blk_ret1_res){
        blk_ret1_inst
    });

    // blk_ret0
    ssa!    ((vm, sge_i32_branch_v1) <int32> blk_ret0_res);
    inst!   ((vm, sge_i32_branch_v1) blk_ret0_inst:
        RET (blk_ret0_res)
    );

    define_block! ((vm, sge_i32_branch_v1) blk_ret0(blk_ret0_res){
        blk_ret0_inst
    });

    define_func_ver!((vm) sge_i32_branch_v1 (entry: blk_entry) {
        blk_entry, blk_ret1, blk_ret0
    });

qinsoon's avatar
qinsoon committed
682
    vm
683
}
684
685
686

#[test]
fn test_branch2_eq_50p_1() {
qinsoon's avatar
qinsoon committed
687
    let lib = linkutils::aot::compile_fnc("branch2_eq_50p_1", &branch2_eq_50p_1);
688
689

    unsafe {
690
691
        let branch2_eq_50p: libloading::Symbol<unsafe extern "C" fn(u8) -> (u64)> =
            lib.get(b"branch2_eq_50p_1").unwrap();
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772

        let res = branch2_eq_50p(1);
        println!("branch2_eq_50p(1) = {}", res);
        assert!(res == 1);

        let res = branch2_eq_50p(0);
        println!("branch2_eq_50p(0) = {}", res);
        assert!(res == 0);
    }
}

fn branch2_eq_50p_1() -> VM {
    let vm = VM::new();

    typedef!    ((vm) int1  = mu_int(1));
    typedef!    ((vm) int8  = mu_int(8));
    typedef!    ((vm) int64 = mu_int(64));

    constdef!   ((vm) <int8>  int8_1  = Constant::Int(1));
    constdef!   ((vm) <int64> int64_0 = Constant::Int(0));
    constdef!   ((vm) <int64> int64_1 = Constant::Int(1));

    funcsig!    ((vm) sig = (int8) -> (int64));
    funcdecl!   ((vm) <sig> branch2_eq_50p_1);
    funcdef!    ((vm) <sig> branch2_eq_50p_1 VERSION branch2_eq_50p_1_v1);

    // blk entry
    block!      ((vm, branch2_eq_50p_1_v1) blk_entry);
    ssa!        ((vm, branch2_eq_50p_1_v1) <int8> cond);

    block!      ((vm, branch2_eq_50p_1_v1) blk_true);
    block!      ((vm, branch2_eq_50p_1_v1) blk_false);

    consta!     ((vm, branch2_eq_50p_1_v1) int8_1_local = int8_1);
    ssa!        ((vm, branch2_eq_50p_1_v1) <int1> cmp_res);
    inst!       ((vm, branch2_eq_50p_1_v1) blk_entry_cmp:
        cmp_res = CMPOP (CmpOp::EQ) cond int8_1_local
    );

    inst!       ((vm, branch2_eq_50p_1_v1) blk_entry_branch2:
        BRANCH2 (cmp_res)
            IF (OP 0)
            THEN blk_true  (vec![]) WITH 0.5f32,
            ELSE blk_false (vec![])
    );

    define_block!((vm, branch2_eq_50p_1_v1) blk_entry(cond) {
        blk_entry_cmp,
        blk_entry_branch2
    });

    // blk_true
    consta!     ((vm, branch2_eq_50p_1_v1) int64_1_local = int64_1);
    consta!     ((vm, branch2_eq_50p_1_v1) int64_0_local = int64_0);

    inst!       ((vm, branch2_eq_50p_1_v1) blk_true_ret:
        RET (int64_1_local)
    );

    define_block!((vm, branch2_eq_50p_1_v1) blk_true() {
        blk_true_ret
    });

    // blk_false
    inst!       ((vm, branch2_eq_50p_1_v1) blk_false_ret:
        RET (int64_0_local)
    );

    define_block!((vm, branch2_eq_50p_1_v1) blk_false() {
        blk_false_ret
    });

    define_func_ver!((vm) branch2_eq_50p_1_v1 (entry: blk_entry) {
        blk_entry, blk_true, blk_false
    });

    vm
}

#[test]
fn test_branch2_eq_50p_2() {
qinsoon's avatar
qinsoon committed
773
    let lib = linkutils::aot::compile_fnc("branch2_eq_50p_2", &branch2_eq_50p_2);
774
775

    unsafe {
776
777
        let branch2_eq_50p: libloading::Symbol<unsafe extern "C" fn(u8) -> (u64)> =
            lib.get(b"branch2_eq_50p_2").unwrap();
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858

        let res = branch2_eq_50p(1);
        println!("branch2_eq_50p(1) = {}", res);
        assert!(res == 1);

        let res = branch2_eq_50p(0);
        println!("branch2_eq_50p(0) = {}", res);
        assert!(res == 0);
    }
}

fn branch2_eq_50p_2() -> VM {
    let vm = VM::new();

    typedef!    ((vm) int1  = mu_int(1));
    typedef!    ((vm) int8  = mu_int(8));
    typedef!    ((vm) int64 = mu_int(64));

    constdef!   ((vm) <int8>  int8_1  = Constant::Int(1));
    constdef!   ((vm) <int64> int64_0 = Constant::Int(0));
    constdef!   ((vm) <int64> int64_1 = Constant::Int(1));

    funcsig!    ((vm) sig = (int8) -> (int64));
    funcdecl!   ((vm) <sig> branch2_eq_50p_2);
    funcdef!    ((vm) <sig> branch2_eq_50p_2 VERSION branch2_eq_50p_2_v1);

    // blk entry
    block!      ((vm, branch2_eq_50p_2_v1) blk_entry);
    ssa!        ((vm, branch2_eq_50p_2_v1) <int8> cond);

    block!      ((vm, branch2_eq_50p_2_v1) blk_true);
    block!      ((vm, branch2_eq_50p_2_v1) blk_false);

    consta!     ((vm, branch2_eq_50p_2_v1) int8_1_local = int8_1);
    ssa!        ((vm, branch2_eq_50p_2_v1) <int1> cmp_res);
    inst!       ((vm, branch2_eq_50p_2_v1) blk_entry_cmp:
        cmp_res = CMPOP (CmpOp::EQ) cond int8_1_local
    );

    inst!       ((vm, branch2_eq_50p_2_v1) blk_entry_branch2:
        BRANCH2 (cmp_res)
            IF (OP 0)
            THEN blk_true  (vec![]) WITH 0.5f32,
            ELSE blk_false (vec![])
    );

    define_block!((vm, branch2_eq_50p_2_v1) blk_entry(cond) {
        blk_entry_cmp,
        blk_entry_branch2
    });

    // blk_true
    consta!     ((vm, branch2_eq_50p_2_v1) int64_1_local = int64_1);
    consta!     ((vm, branch2_eq_50p_2_v1) int64_0_local = int64_0);

    inst!       ((vm, branch2_eq_50p_2_v1) blk_true_ret:
        RET (int64_1_local)
    );

    define_block!((vm, branch2_eq_50p_2_v1) blk_true() {
        blk_true_ret
    });

    // blk_false
    inst!       ((vm, branch2_eq_50p_2_v1) blk_false_ret:
        RET (int64_0_local)
    );

    define_block!((vm, branch2_eq_50p_2_v1) blk_false() {
        blk_false_ret
    });

    define_func_ver!((vm) branch2_eq_50p_2_v1 (entry: blk_entry) {
        blk_entry, blk_false, blk_true
    });

    vm
}

#[test]
fn test_branch2_high_prob_branch_cannot_fallthrough() {
qinsoon's avatar
qinsoon committed
859
    let lib = linkutils::aot::compile_fnc("branch2", &branch2_high_prob_branch_cannot_fallthrough);
860
861

    unsafe {
862
863
        let branch2: libloading::Symbol<unsafe extern "C" fn(u8) -> (u64)> =
            lib.get(b"branch2").unwrap();
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965

        let res = branch2(1);
        println!("branch2(1) = {}", res);
        assert!(res == 1);

        let res = branch2(0);
        println!("branch2(0) = {}", res);
        assert!(res == 0);
    }
}

fn branch2_high_prob_branch_cannot_fallthrough() -> VM {
    let vm = VM::new();

    typedef!    ((vm) int1  = mu_int(1));
    typedef!    ((vm) int8  = mu_int(8));
    typedef!    ((vm) int64 = mu_int(64));

    constdef!   ((vm) <int8>  int8_1  = Constant::Int(1));
    constdef!   ((vm) <int64> int64_0 = Constant::Int(0));
    constdef!   ((vm) <int64> int64_1 = Constant::Int(1));

    funcsig!    ((vm) sig = (int8) -> (int64));
    funcdecl!   ((vm) <sig> branch2);
    funcdef!    ((vm) <sig> branch2 VERSION branch2_v1);

    // blk entry
    block!      ((vm, branch2_v1) blk_entry);
    ssa!        ((vm, branch2_v1) <int8> blk_entry_arg);

    consta!     ((vm, branch2_v1) int64_0_local = int64_0);
    consta!     ((vm, branch2_v1) int64_1_local = int64_1);

    // cmp1_res = EQ 0 1
    ssa!        ((vm, branch2_v1) <int1> cmp1_res);
    inst!       ((vm, branch2_v1) blk_entry_cmp:
        cmp1_res = CMPOP (CmpOp::EQ) int64_0_local int64_1_local
    );

    // branch2 cmp1_res blk_true blk_check(blk_entry_arg)
    block!      ((vm, branch2_v1) blk_true);
    block!      ((vm, branch2_v1) blk_check);
    inst!       ((vm, branch2_v1) blk_entry_branch2:
        BRANCH2 (cmp1_res, blk_entry_arg)
            IF (OP 0)
            THEN blk_true  (vec![]) WITH 0.6f32,
            ELSE blk_check (vec![1])
    );

    define_block!((vm, branch2_v1) blk_entry (blk_entry_arg) {
        blk_entry_cmp, blk_entry_branch2
    });

    // blk_check
    ssa!        ((vm, branch2_v1) <int8> blk_check_arg);

    // cmp2_res = EQ blk_check_arg 1
    ssa!        ((vm, branch2_v1) <int1> cmp2_res);
    consta!     ((vm, branch2_v1) int8_1_local = int8_1);
    inst!       ((vm, branch2_v1) blk_check_cmp:
        cmp2_res = CMPOP (CmpOp::EQ) blk_check_arg int8_1_local
    );

    // branch2 cmp2_res blk_true blk_false
    block!      ((vm, branch2_v1) blk_false);
    inst!       ((vm, branch2_v1) blk_check_branch2:
        BRANCH2 (cmp2_res)
            IF (OP 0)
            THEN blk_true  (vec![]) WITH 0.6f32,
            ELSE blk_false (vec![])
    );

    define_block!((vm, branch2_v1) blk_check (blk_check_arg) {
        blk_check_cmp, blk_check_branch2
    });

    // blk_true
    consta!     ((vm, branch2_v1) int64_1_local = int64_1);
    consta!     ((vm, branch2_v1) int64_0_local = int64_0);

    inst!       ((vm, branch2_v1) blk_true_ret:
        RET (int64_1_local)
    );

    define_block!((vm, branch2_v1) blk_true() {
        blk_true_ret
    });

    // blk_false
    inst!       ((vm, branch2_v1) blk_false_ret:
        RET (int64_0_local)
    );

    define_block!((vm, branch2_v1) blk_false() {
        blk_false_ret
    });

    define_func_ver!((vm) branch2_v1 (entry: blk_entry) {
        blk_entry, blk_check, blk_true, blk_false
    });

    vm
966
}