GitLab will be upgraded on 30 Jan 2023 from 2.00 pm (AEDT) to 3.00 pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

test_regalloc.rs 38.8 KB
Newer Older
1
extern crate mu;
2
extern crate libloading;
3

4
use mu::testutil;
5
use mu::testutil::aot;
6
use mu::utils::LinkedHashMap;
7
use test_compiler::test_call::gen_ccall_exit;
8
9
use test_ir::test_ir::factorial;
use self::mu::compiler::*;
10
11
use self::mu::utils::vec_utils;
use self::mu::ast::ir::*;
qinsoon's avatar
qinsoon committed
12
13
14
15
use self::mu::ast::types::*;
use self::mu::ast::inst::*;
use self::mu::ast::op::*;
use self::mu::vm::VM;
16
17

use std::sync::Arc;
qinsoon's avatar
qinsoon committed
18
use std::sync::RwLock;
19

qinsoon's avatar
[wip]    
qinsoon committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
fn get_number_of_moves(fv_id: MuID, vm: &VM) -> usize {
    let cfs = vm.compiled_funcs().read().unwrap();
    let cf  = cfs.get(&fv_id).unwrap().read().unwrap();

    let mut n_mov_insts = 0;

    let mc = cf.mc();
    for i in 0..mc.number_of_insts() {
        if mc.is_move(i) {
            n_mov_insts += 1;
        }
    }

    n_mov_insts
}

36
37
#[test]
fn test_ir_liveness_fac() {
qinsoon's avatar
qinsoon committed
38
    VM::start_logging_trace();
39
    
qinsoon's avatar
qinsoon committed
40
    let vm = Arc::new(factorial());
41
42
43
44
45
46
    
    let compiler = Compiler::new(CompilerPolicy::new(vec![
            Box::new(passes::DefUse::new()),
            Box::new(passes::TreeGen::new()),
            Box::new(passes::ControlFlowAnalysis::new()),
            Box::new(passes::TraceGen::new()),
47
            Box::new(backend::inst_sel::InstructionSelection::new()),
qinsoon's avatar
qinsoon committed
48
    ]), &vm);
49
    
qinsoon's avatar
qinsoon committed
50
    let func_id = vm.id_of("fac");
qinsoon's avatar
qinsoon committed
51
    let funcs = vm.funcs().read().unwrap();
52
    let func = funcs.get(&func_id).unwrap().read().unwrap();
qinsoon's avatar
qinsoon committed
53
    let func_vers = vm.func_vers().read().unwrap();
qinsoon's avatar
qinsoon committed
54
    let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();
55
    
qinsoon's avatar
qinsoon committed
56
    compiler.compile(&mut func_ver);
57
    
qinsoon's avatar
qinsoon committed
58
    let cf_lock = vm.compiled_funcs().read().unwrap();
59
    let cf = cf_lock.get(&func_ver.id()).unwrap().read().unwrap();
60
61
62
    
    // block 0
    
qinsoon's avatar
qinsoon committed
63
    let block_0_livein = cf.mc().get_ir_block_livein("blk_0").unwrap();
qinsoon's avatar
qinsoon committed
64
    let blk_0_n_3 = vm.id_of("blk_0_n_3");;
65
66
    assert!(vec_utils::is_identical_to_str_ignore_order(block_0_livein, vec![blk_0_n_3]));
    
qinsoon's avatar
qinsoon committed
67
    let block_0_liveout = cf.mc().get_ir_block_liveout("blk_0").unwrap();
68
69
70
71
    assert!(vec_utils::is_identical_to_str_ignore_order(block_0_liveout, vec![blk_0_n_3]));
    
    // block 1
    
qinsoon's avatar
qinsoon committed
72
    let block_1_livein = cf.mc().get_ir_block_livein("blk_1").unwrap();
qinsoon's avatar
qinsoon committed
73
    let blk_1_n_3 = vm.id_of("blk_1_n_3");
74
75
    assert!(vec_utils::is_identical_to_str_ignore_order(block_1_livein, vec![blk_1_n_3]));
    
qinsoon's avatar
qinsoon committed
76
    let block_1_liveout = cf.mc().get_ir_block_liveout("blk_1").unwrap();
qinsoon's avatar
qinsoon committed
77
    let blk_1_v52 = vm.id_of("blk_1_v52");
qinsoon's avatar
qinsoon committed
78
79
    trace!("lhs: {:?}", block_1_liveout);
    trace!("rhs: {:?}", vec![blk_1_v52]);
80
81
82
83
    assert!(vec_utils::is_identical_to_str_ignore_order(block_1_liveout, vec![blk_1_v52]));
    
    // block 2
    
qinsoon's avatar
qinsoon committed
84
    let block_2_livein = cf.mc().get_ir_block_livein("blk_2").unwrap();
qinsoon's avatar
qinsoon committed
85
    let blk_2_v53 = vm.id_of("blk_2_v53");
86
87
    assert!(vec_utils::is_identical_to_str_ignore_order(block_2_livein, vec![blk_2_v53]));
    
qinsoon's avatar
qinsoon committed
88
    let block_2_liveout = cf.mc().get_ir_block_liveout("blk_2").unwrap();
89
90
    let expect : Vec<MuID> = vec![];
    assert!(vec_utils::is_identical_to_str_ignore_order(block_2_liveout, expect));
qinsoon's avatar
qinsoon committed
91
92
93
}

#[test]
qinsoon's avatar
qinsoon committed
94
#[allow(unused_variables)]
qinsoon's avatar
qinsoon committed
95
fn test_spill1() {
qinsoon's avatar
qinsoon committed
96
    VM::start_logging_trace();
qinsoon's avatar
qinsoon committed
97
98
99
    
    let vm = Arc::new(create_spill1());
    
qinsoon's avatar
qinsoon committed
100
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
qinsoon's avatar
qinsoon committed
101
102
103
104
105
106
107
108
109
110
    
    let func_id = vm.id_of("spill1");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
        let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();
        
        compiler.compile(&mut func_ver);
    }
qinsoon's avatar
qinsoon committed
111
112
113

    backend::emit_context(&vm);

114
    let dylib = aot::link_dylib(vec![Mu("spill1")], &testutil::get_dylib_name("spill1"), &vm);
qinsoon's avatar
qinsoon committed
115
116
117

    let lib = libloading::Library::new(dylib.as_os_str()).unwrap();
    unsafe {
qinsoon's avatar
[wip]    
qinsoon committed
118
        let spill1 : libloading::Symbol<unsafe extern fn() -> u64> = match lib.get(b"spill1") {
qinsoon's avatar
qinsoon committed
119
120
121
            Ok(symbol) => symbol,
            Err(e) => panic!("cannot find symbol spill1 in dylib: {:?}", e)
        };
122
123

        // we cannot call this (it doesnt return)
qinsoon's avatar
qinsoon committed
124
    }
qinsoon's avatar
qinsoon committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
}

fn create_spill1() -> VM {
    let vm = VM::new();
    
    // .typedef @int_64 = int<64>
    let type_def_int64 = vm.declare_type(vm.next_id(), MuType_::int(64));
    vm.set_name(type_def_int64.as_entity(), "int_64".to_string());
    
    // .funcsig @spill1_sig = (@int_64 x 10) -> (@int_64)
    let spill1_sig = vm.declare_func_sig(vm.next_id(), vec![type_def_int64.clone()], vec![type_def_int64.clone(); 10]);
    vm.set_name(spill1_sig.as_entity(), Mu("spill1_sig"));
    
    // .typedef @funcref_spill1 = funcref<@spill1_sig>
    let type_def_funcref_spill1 = vm.declare_type(vm.next_id(), MuType_::funcref(spill1_sig.clone()));
    vm.set_name(type_def_funcref_spill1.as_entity(), Mu("funcref_spill1"));
    
    // .funcdecl @spill1 <@spill1_sig>
    let func = MuFunction::new(vm.next_id(), spill1_sig.clone());
    vm.set_name(func.as_entity(), Mu("spill1"));
    let func_id = func.id();
    vm.declare_func(func);
    
    // .funcdef @spill1 VERSION @spill1_v1 <@spill1_sig>
    let const_func_spill1 = vm.declare_const(vm.next_id(), type_def_funcref_spill1, Constant::FuncRef(func_id));    
    let mut func_ver = MuFunctionVersion::new(vm.next_id(), func_id, spill1_sig.clone());
    vm.set_name(func_ver.as_entity(), Mu("spill1_v1"));
    
    // %entry(<@int_64> %t1, t2, ... t10):
    let mut blk_entry = Block::new(vm.next_id());
    vm.set_name(blk_entry.as_entity(), Mu("entry"));
    
    // callee
qinsoon's avatar
qinsoon committed
158
    let blk_entry_spill1_funcref = func_ver.new_constant(const_func_spill1.clone());
qinsoon's avatar
qinsoon committed
159
160
161
162
    // args
    let blk_entry_t1 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_entry_t1.as_entity(), Mu("blk_entry_t1"));
    let blk_entry_t2 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
163
    vm.set_name(blk_entry_t2.as_entity(), Mu("blk_entry_t2"));
qinsoon's avatar
qinsoon committed
164
    let blk_entry_t3 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
165
    vm.set_name(blk_entry_t3.as_entity(), Mu("blk_entry_t3"));
qinsoon's avatar
qinsoon committed
166
    let blk_entry_t4 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
167
    vm.set_name(blk_entry_t4.as_entity(), Mu("blk_entry_t4"));
qinsoon's avatar
qinsoon committed
168
    let blk_entry_t5 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
169
    vm.set_name(blk_entry_t5.as_entity(), Mu("blk_entry_t5"));
qinsoon's avatar
qinsoon committed
170
    let blk_entry_t6 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
171
    vm.set_name(blk_entry_t6.as_entity(), Mu("blk_entry_t6"));
qinsoon's avatar
qinsoon committed
172
    let blk_entry_t7 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
173
    vm.set_name(blk_entry_t7.as_entity(), Mu("blk_entry_t7"));
qinsoon's avatar
qinsoon committed
174
    let blk_entry_t8 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
175
    vm.set_name(blk_entry_t8.as_entity(), Mu("blk_entry_t8"));
qinsoon's avatar
qinsoon committed
176
    let blk_entry_t9 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
177
    vm.set_name(blk_entry_t9.as_entity(), Mu("blk_entry_t9"));
qinsoon's avatar
qinsoon committed
178
    let blk_entry_t10= func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
179
    vm.set_name(blk_entry_t10.as_entity(), Mu("blk_entry_t10"));
qinsoon's avatar
qinsoon committed
180
    
181
182
    // %x = CALL spill1(%t1, %t2, ... t10)
    let blk_entry_x = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
183
184
    let blk_entry_call = func_ver.new_inst(Instruction{
        hdr: MuEntityHeader::unnamed(vm.next_id()),
185
        value: Some(vec![blk_entry_x.clone_value()]),
qinsoon's avatar
qinsoon committed
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
        ops: RwLock::new(vec![
                blk_entry_spill1_funcref,
                blk_entry_t1.clone(),
                blk_entry_t2.clone(),
                blk_entry_t3.clone(),
                blk_entry_t4.clone(),
                blk_entry_t5.clone(),
                blk_entry_t6.clone(),
                blk_entry_t7.clone(),
                blk_entry_t8.clone(),
                blk_entry_t9.clone(),
                blk_entry_t10.clone()
            ]),
        v: Instruction_::ExprCall {
            data: CallData {
                func: 0,
                args: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                convention: CallConvention::Mu
            },
            is_abort: false
        }
    });
    
    // %res0 = ADD %t1 %t2
    let blk_entry_res0 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_entry_res0.as_entity(), Mu("blk_entry_res0"));
qinsoon's avatar
qinsoon committed
212
213
    let blk_entry_add0 = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
214
215
216
217
218
219
220
221
        value: Some(vec![blk_entry_res0.clone_value()]),
        ops: RwLock::new(vec![blk_entry_t1.clone(), blk_entry_t2.clone()]),
        v: Instruction_::BinOp(BinOp::Add, 0, 1)
    });
    
    // %res1 = ADD %res0 %t3
    let blk_entry_res1 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_entry_res1.as_entity(), Mu("blk_entry_res1"));
qinsoon's avatar
qinsoon committed
222
223
    let blk_entry_add1 = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
224
225
226
227
228
229
230
231
        value: Some(vec![blk_entry_res1.clone_value()]),
        ops: RwLock::new(vec![blk_entry_res0.clone(), blk_entry_t3.clone()]),
        v: Instruction_::BinOp(BinOp::Add, 0, 1)
    });
    
    // %res2 = ADD %res1 %t4
    let blk_entry_res2 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_entry_res2.as_entity(), Mu("blk_entry_res2"));
qinsoon's avatar
qinsoon committed
232
233
    let blk_entry_add2 = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
234
235
236
237
238
239
240
241
        value: Some(vec![blk_entry_res2.clone_value()]),
        ops: RwLock::new(vec![blk_entry_res1.clone(), blk_entry_t4.clone()]),
        v: Instruction_::BinOp(BinOp::Add, 0, 1)
    });
    
    // %res3 = ADD %res2 %t5
    let blk_entry_res3 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_entry_res3.as_entity(), Mu("blk_entry_res3"));
qinsoon's avatar
qinsoon committed
242
243
    let blk_entry_add3 = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
244
245
246
247
248
249
        value: Some(vec![blk_entry_res3.clone_value()]),
        ops: RwLock::new(vec![blk_entry_res2.clone(), blk_entry_t5.clone()]),
        v: Instruction_::BinOp(BinOp::Add, 0, 1)
    });
    
    // RET %res3
qinsoon's avatar
qinsoon committed
250
251
    let blk_entry_ret = func_ver.new_inst(Instruction{
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
        value: None,
        ops: RwLock::new(vec![blk_entry_res3.clone()]),
        v: Instruction_::Return(vec![0])
    });
    
    blk_entry.content = Some(BlockContent{
        args: vec![
            blk_entry_t1.clone_value(),
            blk_entry_t2.clone_value(),
            blk_entry_t3.clone_value(),
            blk_entry_t4.clone_value(),
            blk_entry_t5.clone_value(),
            blk_entry_t6.clone_value(),
            blk_entry_t7.clone_value(),
            blk_entry_t8.clone_value(),
            blk_entry_t9.clone_value(),
            blk_entry_t10.clone_value()
        ],
        exn_arg: None,
        body: vec![
            blk_entry_call,
            blk_entry_add0,
            blk_entry_add1,
            blk_entry_add2,
            blk_entry_add3,
            blk_entry_ret
        ],
        keepalives: None
    });
    
282
283
284
    func_ver.define(FunctionContent::new(
        blk_entry.id(),
        {
285
            let mut blocks = LinkedHashMap::new();
qinsoon's avatar
qinsoon committed
286
287
288
            blocks.insert(blk_entry.id(), blk_entry);
            blocks
        }
289
    ));
qinsoon's avatar
qinsoon committed
290
291
292
    
    vm.define_func_version(func_ver);
    
qinsoon's avatar
qinsoon committed
293
294
295
296
297
    vm
}

#[test]
fn test_simple_spill() {
qinsoon's avatar
qinsoon committed
298
    VM::start_logging_trace();
qinsoon's avatar
qinsoon committed
299
300
301

    let vm = Arc::new(create_simple_spill());

qinsoon's avatar
qinsoon committed
302
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
qinsoon's avatar
qinsoon committed
303
304
305
306
307
308
309
310
311
312
313
314
315

    let func_id = vm.id_of("simple_spill");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
        let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

        compiler.compile(&mut func_ver);
    }

    backend::emit_context(&vm);

316
    let dylib = aot::link_dylib(vec![Mu("simple_spill")], &testutil::get_dylib_name("simple_spill"), &vm);
qinsoon's avatar
qinsoon committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
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
394
395
396
397
398
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
458
459
460
461
462
463
464
465
466
467
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

    let lib = libloading::Library::new(dylib.as_os_str()).unwrap();
    unsafe {
        let simple_spill : libloading::Symbol<unsafe extern fn() -> u64> = match lib.get(b"simple_spill") {
            Ok(symbol) => symbol,
            Err(e) => panic!("cannot find symbol simple_spill in dylib: {:?}", e)
        };

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

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

    // .typedef @int_64 = int<64>
    let type_def_int64 = vm.declare_type(vm.next_id(), MuType_::int(64));
    vm.set_name(type_def_int64.as_entity(), Mu("int_64"));

    // .const @int_64_1 <@int_64> = 1
    let const_def_int64_1 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(1));
    vm.set_name(const_def_int64_1.as_entity(), "int64_1".to_string());

    // .funcsig @simple_spill_sig = () -> (@int_64)
    let simple_spill_sig = vm.declare_func_sig(vm.next_id(), vec![type_def_int64.clone()], vec![]);
    vm.set_name(simple_spill_sig.as_entity(), Mu("simple_spill_sig"));

    // .funcdecl @simple_spill <@simple_spill_sig>
    let func = MuFunction::new(vm.next_id(), simple_spill_sig.clone());
    vm.set_name(func.as_entity(), Mu("simple_spill"));
    let func_id = func.id();
    vm.declare_func(func);

    // .funcdef @simple_spill VERSION @simple_spill_v1 <@simple_spill_sig>
    let mut func_ver = MuFunctionVersion::new(vm.next_id(), func_id, simple_spill_sig.clone());
    vm.set_name(func_ver.as_entity(), Mu("simple_spill_v1"));

    // %entry():
    let mut blk_entry = Block::new(vm.next_id());
    vm.set_name(blk_entry.as_entity(), Mu("entry"));

    // BRANCH %start(1, 1, 1, 1, ..., 1) // 14 constant ONE
    let const_int64_1 = func_ver.new_constant(const_def_int64_1.clone());
    let blk_start_id = vm.next_id();
    let blk_entry_branch = func_ver.new_inst(Instruction{
        hdr: MuEntityHeader::unnamed(vm.next_id()),
        value: None,
        ops: RwLock::new(vec![const_int64_1.clone(); 14]),
        v: Instruction_::Branch1(Destination{
            target: blk_start_id,
            args: vec![
                DestArg::Normal(0),
                DestArg::Normal(1),
                DestArg::Normal(2),
                DestArg::Normal(3),
                DestArg::Normal(4),
                DestArg::Normal(5),
                DestArg::Normal(6),
                DestArg::Normal(7),
                DestArg::Normal(8),
                DestArg::Normal(9),
                DestArg::Normal(10),
                DestArg::Normal(11),
                DestArg::Normal(12),
                DestArg::Normal(13),
            ]
        })
    });

    blk_entry.content = Some(BlockContent {
        args: vec![],
        exn_arg: None,
        body: vec![blk_entry_branch],
        keepalives: None
    });

    // %start(%t1, %t2, ..., %t14):
    let mut blk_start = Block::new(blk_start_id);
    vm.set_name(blk_start.as_entity(), Mu("start"));

    // args
    let blk_start_t1 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t1.as_entity(), Mu("blk_start_t1"));
    let blk_start_t2 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t2.as_entity(), Mu("blk_start_t2"));
    let blk_start_t3 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t3.as_entity(), Mu("blk_start_t3"));
    let blk_start_t4 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t4.as_entity(), Mu("blk_start_t4"));
    let blk_start_t5 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t5.as_entity(), Mu("blk_start_t5"));
    let blk_start_t6 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t6.as_entity(), Mu("blk_start_t6"));
    let blk_start_t7 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t7.as_entity(), Mu("blk_start_t7"));
    let blk_start_t8 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t8.as_entity(), Mu("blk_start_t8"));
    let blk_start_t9 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t9.as_entity(), Mu("blk_start_t9"));
    let blk_start_t10= func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t10.as_entity(), Mu("blk_start_t10"));
    let blk_start_t11= func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t11.as_entity(), Mu("blk_start_t11"));
    let blk_start_t12= func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t12.as_entity(), Mu("blk_start_t12"));
    let blk_start_t13= func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t13.as_entity(), Mu("blk_start_t13"));
    let blk_start_t14= func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_t14.as_entity(), Mu("blk_start_t14"));

    // %res = ADD %t1 %t2
    let blk_start_res = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_start_res.as_entity(), Mu("blk_start_res"));
    let blk_start_add = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
        value: Some(vec![blk_start_res.clone_value()]),
        ops: RwLock::new(vec![blk_start_t1.clone(), blk_start_t2.clone()]),
        v: Instruction_::BinOp(BinOp::Add, 0, 1)
    });

    // BRANCH %ret (%res, %t1, %t2, ..., %t14)
    let blk_ret_id = vm.next_id();
    let blk_start_branch = func_ver.new_inst(Instruction{
        hdr: MuEntityHeader::unnamed(vm.next_id()),
        value: None,
        ops: RwLock::new(vec![
            blk_start_res.clone(),
            blk_start_t1.clone(),
            blk_start_t2.clone(),
            blk_start_t3.clone(),
            blk_start_t4.clone(),
            blk_start_t5.clone(),
            blk_start_t6.clone(),
            blk_start_t7.clone(),
            blk_start_t8.clone(),
            blk_start_t9.clone(),
            blk_start_t10.clone(),
            blk_start_t11.clone(),
            blk_start_t12.clone(),
            blk_start_t13.clone(),
            blk_start_t14.clone(),
        ]),
        v: Instruction_::Branch1(Destination{
            target: blk_ret_id,
            args: vec![
                DestArg::Normal(0),
                DestArg::Normal(1),
                DestArg::Normal(2),
                DestArg::Normal(3),
                DestArg::Normal(4),
                DestArg::Normal(5),
                DestArg::Normal(6),
                DestArg::Normal(7),
                DestArg::Normal(8),
                DestArg::Normal(9),
                DestArg::Normal(10),
                DestArg::Normal(11),
                DestArg::Normal(12),
                DestArg::Normal(13),
                DestArg::Normal(14),
            ]
        })
    });

    blk_start.content = Some(BlockContent {
        args: vec![
            blk_start_t1.clone_value(),
            blk_start_t2.clone_value(),
            blk_start_t3.clone_value(),
            blk_start_t4.clone_value(),
            blk_start_t5.clone_value(),
            blk_start_t6.clone_value(),
            blk_start_t7.clone_value(),
            blk_start_t8.clone_value(),
            blk_start_t9.clone_value(),
            blk_start_t10.clone_value(),
            blk_start_t11.clone_value(),
            blk_start_t12.clone_value(),
            blk_start_t13.clone_value(),
            blk_start_t14.clone_value(),
        ],
        exn_arg: None,
        body: vec![blk_start_add, blk_start_branch],
        keepalives: None
    });

    // %ret(%res, %t1, %t2, ... %t14):
    let mut blk_ret = Block::new(blk_ret_id);
    vm.set_name(blk_ret.as_entity(), Mu("ret"));
    
    // args
    let blk_ret_res = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_res.as_entity(), Mu("blk_ret_res"));
    let blk_ret_t1 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t1.as_entity(), Mu("blk_ret_t1"));
    let blk_ret_t2 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t2.as_entity(), Mu("blk_ret_t2"));
    let blk_ret_t3 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t3.as_entity(), Mu("blk_ret_t3"));
    let blk_ret_t4 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t4.as_entity(), Mu("blk_ret_t4"));
    let blk_ret_t5 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t5.as_entity(), Mu("blk_ret_t5"));
    let blk_ret_t6 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t6.as_entity(), Mu("blk_ret_t6"));
    let blk_ret_t7 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t7.as_entity(), Mu("blk_ret_t7"));
    let blk_ret_t8 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t8.as_entity(), Mu("blk_ret_t8"));
    let blk_ret_t9 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t9.as_entity(), Mu("blk_ret_t9"));
    let blk_ret_t10= func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t10.as_entity(), Mu("blk_ret_t10"));
    let blk_ret_t11= func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t11.as_entity(), Mu("blk_ret_t11"));
    let blk_ret_t12= func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t12.as_entity(), Mu("blk_ret_t12"));
    let blk_ret_t13= func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t13.as_entity(), Mu("blk_ret_t13"));
    let blk_ret_t14= func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_ret_t14.as_entity(), Mu("blk_ret_t14"));

    // RET %res
    let blk_ret_ret = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
        value: None,
        ops: RwLock::new(vec![blk_ret_res.clone()]),
        v: Instruction_::Return(vec![0])
    });

    blk_ret.content = Some(BlockContent {
        args: vec![
            blk_ret_res.clone_value(),
            blk_ret_t1.clone_value(),
            blk_ret_t2.clone_value(),
            blk_ret_t3.clone_value(),
            blk_ret_t4.clone_value(),
            blk_ret_t5.clone_value(),
            blk_ret_t6.clone_value(),
            blk_ret_t7.clone_value(),
            blk_ret_t8.clone_value(),
            blk_ret_t9.clone_value(),
            blk_ret_t10.clone_value(),
            blk_ret_t11.clone_value(),
            blk_ret_t12.clone_value(),
            blk_ret_t13.clone_value(),
            blk_ret_t14.clone_value(),
        ],
        exn_arg: None,
        body: vec![blk_ret_ret],
        keepalives: None
    });

572
573
574
    func_ver.define(FunctionContent::new(
        blk_entry.id(),
        {
575
            let mut blocks = LinkedHashMap::new();
qinsoon's avatar
qinsoon committed
576
577
578
579
580
            blocks.insert(blk_entry.id(), blk_entry);
            blocks.insert(blk_start.id(), blk_start);
            blocks.insert(blk_ret.id(), blk_ret);
            blocks
        }
581
    ));
qinsoon's avatar
qinsoon committed
582
583
584

    vm.define_func_version(func_ver);

585
586
587
588
589
590
591
592
593
594
    vm
}

#[test]
#[cfg(target_arch = "x86_64")]
fn test_coalesce_branch_moves() {
    VM::start_logging_trace();

    let vm = Arc::new(coalesce_branch_moves());

qinsoon's avatar
qinsoon committed
595
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
596
597
598
599
600
601
602
603
604
605
606
607
608

    let func_id = vm.id_of("coalesce_branch_moves");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
        let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

        compiler.compile(&mut func_ver);

        // check
        let fv_id = func_ver.id();

qinsoon's avatar
[wip]    
qinsoon committed
609
        assert!(get_number_of_moves(fv_id, &vm) == 1, "The function should not yield any mov instructions other than mov %rsp->%rbp (some possible coalescing failed)");
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
    }
}

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

    typedef! ((vm) int64 = mu_int(64));

    funcsig! ((vm) sig = (int64, int64, int64, int64) -> ());
    funcdecl!((vm) <sig> coalesce_branch_moves);
    funcdef! ((vm) <sig> coalesce_branch_moves VERSION coalesce_branch_moves_v1);

    // blk entry
    block!   ((vm, coalesce_branch_moves_v1) blk_entry);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> arg0);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> arg1);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> arg2);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> arg3);

    block!   ((vm, coalesce_branch_moves_v1) blk1);
    inst!    ((vm, coalesce_branch_moves_v1) blk_entry_branch:
        BRANCH blk1 (arg0, arg1, arg2, arg3)
    );

    define_block!((vm, coalesce_branch_moves_v1) blk_entry (arg0, arg1, arg2, arg3) {blk_entry_branch});

    ssa!     ((vm, coalesce_branch_moves_v1) <int64> blk1_arg0);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> blk1_arg1);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> blk1_arg2);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> blk1_arg3);

    inst!    ((vm, coalesce_branch_moves_v1) blk1_ret:
        RET
    );

    define_block!((vm, coalesce_branch_moves_v1) blk1 (blk1_arg0, blk1_arg1, blk1_arg2, blk1_arg3) {
        blk1_ret
    });

    define_func_ver!((vm) coalesce_branch_moves_v1 (entry: blk_entry){
        blk_entry, blk1
    });

qinsoon's avatar
[wip]    
qinsoon committed
653
654
655
656
657
658
659
660
661
662
    vm
}

#[test]
#[cfg(target_arch = "x86_64")]
fn test_coalesce_args() {
    VM::start_logging_trace();

    let vm = Arc::new(coalesce_args());

qinsoon's avatar
qinsoon committed
663
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
qinsoon's avatar
[wip]    
qinsoon committed
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
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

    let func_id = vm.id_of("coalesce_args");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
        let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

        compiler.compile(&mut func_ver);

        // check
        let fv_id = func_ver.id();

        assert!(get_number_of_moves(fv_id, &vm) == 1, "The function should not yield any mov instructions other than mov %rsp->%rbp (some possible coalescing failed)");
    }
}

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

    typedef!    ((vm) int64 = mu_int(64));

    funcsig!    ((vm) sig = (int64, int64, int64, int64) -> ());
    funcdecl!   ((vm) <sig> coalesce_args);
    funcdef!    ((vm) <sig> coalesce_args VERSION coalesce_args_v1);

    typedef!    ((vm) funcref_to_sig = mu_funcref(sig));
    constdef!   ((vm) <funcref_to_sig> funcref = Constant::FuncRef(coalesce_args));

    // blk entry
    block!      ((vm, coalesce_args_v1) blk_entry);
    ssa!        ((vm, coalesce_args_v1) <int64> arg0);
    ssa!        ((vm, coalesce_args_v1) <int64> arg1);
    ssa!        ((vm, coalesce_args_v1) <int64> arg2);
    ssa!        ((vm, coalesce_args_v1) <int64> arg3);

    consta!     ((vm, coalesce_args_v1) funcref_local = funcref);
    inst!       ((vm, coalesce_args_v1) blk_entry_call:
        EXPRCALL (CallConvention::Mu, is_abort: false) funcref_local (arg0, arg1, arg2, arg3)
    );

    inst!       ((vm, coalesce_args_v1) blk_entry_ret:
        RET
    );

    define_block!   ((vm, coalesce_args_v1) blk_entry(arg0, arg1, arg2, arg3) {blk_entry_call, blk_entry_ret});

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

    vm
}

#[test]
#[cfg(target_arch = "x86_64")]
fn test_coalesce_branch2_moves() {
    VM::start_logging_trace();

    let vm = Arc::new(coalesce_branch2_moves());

qinsoon's avatar
qinsoon committed
723
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
qinsoon's avatar
[wip]    
qinsoon committed
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741

    let func_id = vm.id_of("coalesce_branch2_moves");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
        let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

        compiler.compile(&mut func_ver);

        // check
        let fv_id = func_ver.id();

        assert!(get_number_of_moves(fv_id, &vm) <= 3, "too many moves (some possible coalescing failed)");
    }

    backend::emit_context(&vm);

742
    let dylib = aot::link_dylib(vec![Mu("coalesce_branch2_moves")], &testutil::get_dylib_name("coalesce_branch2_moves"), &vm);
qinsoon's avatar
[wip]    
qinsoon committed
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
773
774
775
776
777
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

    let lib = libloading::Library::new(dylib.as_os_str()).unwrap();
    unsafe {
        let coalesce_branch2_moves : libloading::Symbol<unsafe extern fn(u64, u64, u64, u64, u64, u64) -> u64> = match lib.get(b"coalesce_branch2_moves") {
            Ok(symbol) => symbol,
            Err(e) => panic!("cannot find symbol coalesce_branch2_moves in dylib: {:?}", e)
        };

        let res = coalesce_branch2_moves(1, 1, 10, 10, 0, 0);
        println!("if 0 == 0 then return 1 + 1 else return 10 + 10");
        println!("coalesce_branch2_moves(1, 1, 10, 10, 0, 0) = {}", res);
        assert!(res == 2);

        let res = coalesce_branch2_moves(1, 1, 10, 10, 1, 0);
        println!("if 1 == 0 then return 1 + 1 else return 10 + 10");
        println!("coalesce_branch2_moves(1, 1, 10, 10, 1, 0) = {}", res);
        assert!(res == 20);
    }
}

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

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

    funcsig! ((vm) sig = (int64, int64, int64, int64) -> ());
    funcdecl!((vm) <sig> coalesce_branch2_moves);
    funcdef! ((vm) <sig> coalesce_branch2_moves VERSION coalesce_branch2_moves_v1);

    // blk entry
    block!   ((vm, coalesce_branch2_moves_v1) blk_entry);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> arg0);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> arg1);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> arg2);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> arg3);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> arg4);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> arg5);

    ssa!     ((vm, coalesce_branch2_moves_v1) <int1> cond);
    inst!    ((vm, coalesce_branch2_moves_v1) blk_entry_cmp:
        cond = CMPOP (CmpOp::EQ) arg4 arg5
    );

    block!   ((vm, coalesce_branch2_moves_v1) blk_add01);
    block!   ((vm, coalesce_branch2_moves_v1) blk_add23);
    block!   ((vm, coalesce_branch2_moves_v1) blk_ret);
    inst!    ((vm, coalesce_branch2_moves_v1) blk_entry_branch2:
        BRANCH2 (cond, arg0, arg1, arg2, arg3)
            IF (OP 0)
            THEN blk_add01 (vec![1, 2]) WITH 0.6f32,
            ELSE blk_add23 (vec![3, 4])
    );

    define_block!((vm, coalesce_branch2_moves_v1) blk_entry (arg0, arg1, arg2, arg3, arg4, arg5) {
        blk_entry_cmp, blk_entry_branch2
    });

    // blk_add01
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> blk_add01_arg0);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> blk_add01_arg1);

    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> res01);
    inst!    ((vm, coalesce_branch2_moves_v1) blk_add01_add:
        res01 = BINOP (BinOp::Add) blk_add01_arg0 blk_add01_arg1
    );

    inst!    ((vm, coalesce_branch2_moves_v1) blk_add01_branch:
        BRANCH blk_ret (res01)
    );

    define_block!((vm, coalesce_branch2_moves_v1) blk_add01 (blk_add01_arg0, blk_add01_arg1) {
        blk_add01_add, blk_add01_branch
    });

    // blk_add23
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> blk_add23_arg2);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> blk_add23_arg3);

    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> res23);
    inst!    ((vm, coalesce_branch2_moves_v1) blk_add23_add:
        res23 = BINOP (BinOp::Add) blk_add23_arg2 blk_add23_arg3
    );

    inst!    ((vm, coalesce_branch2_moves_v1) blk_add23_branch:
        BRANCH blk_ret (res23)
    );

    define_block!((vm, coalesce_branch2_moves_v1) blk_add23 (blk_add23_arg2, blk_add23_arg3) {
        blk_add23_add, blk_add23_branch
    });

    // blk_ret
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> res);
    inst!    ((vm, coalesce_branch2_moves_v1) blk_ret_ret:
        RET (res)
    );

    define_block!((vm, coalesce_branch2_moves_v1) blk_ret (res) {
        blk_ret_ret
    });

    define_func_ver!((vm) coalesce_branch2_moves_v1 (entry: blk_entry){
        blk_entry, blk_add01, blk_add23, blk_ret
    });

qinsoon's avatar
qinsoon committed
849
    vm
850
}
851
852
853
854
855
856
857
858
859
860
861
862
863
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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058

#[test]
fn test_preserve_caller_saved() {
    VM::start_logging_trace();
    let vm = Arc::new(preserve_caller_saved());

    let compiler = Compiler::new(CompilerPolicy::default(), &vm);

    let func_foo = vm.id_of("foo");
    let func_preserve_caller_saved = vm.id_of("preserve_caller_saved");
    {
        let funcs = vm.funcs().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();

        {
            let func = funcs.get(&func_foo).unwrap().read().unwrap();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            compiler.compile(&mut func_ver);
        }
        {
            let func = funcs.get(&func_preserve_caller_saved).unwrap().read().unwrap();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            compiler.compile(&mut func_ver);
        }
    }

    vm.make_primordial_thread(func_preserve_caller_saved, true, vec![]);
    backend::emit_context(&vm);

    let executable = aot::link_primordial(vec![Mu("foo"), Mu("preserve_caller_saved")], "test_preserve_caller_saved", &vm);
    let output = aot::execute_nocheck(executable);

    // add from 0 to 9
    assert!(output.status.code().is_some());
    assert_eq!(output.status.code().unwrap(), 45);
}

fn preserve_caller_saved() -> VM {
    let vm = VM::new_with_opts("init_mu --disable-inline");

    create_empty_func_foo(&vm);

    typedef!    ((vm) int64 = mu_int(64));

    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_3 = Constant::Int(3));
    constdef!   ((vm) <int64> int64_4 = Constant::Int(4));
    constdef!   ((vm) <int64> int64_5 = Constant::Int(5));
    constdef!   ((vm) <int64> int64_6 = Constant::Int(6));
    constdef!   ((vm) <int64> int64_7 = Constant::Int(7));
    constdef!   ((vm) <int64> int64_8 = Constant::Int(8));
    constdef!   ((vm) <int64> int64_9 = Constant::Int(9));

    funcsig!    ((vm) sig = () -> ());
    funcdecl!   ((vm) <sig> preserve_caller_saved);
    funcdef!    ((vm) <sig> preserve_caller_saved VERSION preserve_caller_saved_v1);

    // blk entry
    block!      ((vm, preserve_caller_saved_v1) blk_entry);
    block!      ((vm, preserve_caller_saved_v1) blk_main);

    consta!   ((vm, preserve_caller_saved_v1)  int64_0_local = int64_0);
    consta!   ((vm, preserve_caller_saved_v1)  int64_1_local = int64_1);
    consta!   ((vm, preserve_caller_saved_v1)  int64_2_local = int64_2);
    consta!   ((vm, preserve_caller_saved_v1)  int64_3_local = int64_3);
    consta!   ((vm, preserve_caller_saved_v1)  int64_4_local = int64_4);
    consta!   ((vm, preserve_caller_saved_v1)  int64_5_local = int64_5);
    consta!   ((vm, preserve_caller_saved_v1)  int64_6_local = int64_6);
    consta!   ((vm, preserve_caller_saved_v1)  int64_7_local = int64_7);
    consta!   ((vm, preserve_caller_saved_v1)  int64_8_local = int64_8);
    consta!   ((vm, preserve_caller_saved_v1)  int64_9_local = int64_9);
    inst!       ((vm, preserve_caller_saved_v1) blk_entry_branch:
        BRANCH blk_main (
             int64_0_local,
             int64_1_local,
             int64_2_local,
             int64_3_local,
             int64_4_local,
             int64_5_local,
             int64_6_local,
             int64_7_local,
             int64_8_local,
             int64_9_local
        )
    );

    define_block!   ((vm, preserve_caller_saved_v1) blk_entry() {
        blk_entry_branch
    });

    // blk main
    ssa!    ((vm, preserve_caller_saved_v1) <int64> v0);
    ssa!    ((vm, preserve_caller_saved_v1) <int64> v1);
    ssa!    ((vm, preserve_caller_saved_v1) <int64> v2);
    ssa!    ((vm, preserve_caller_saved_v1) <int64> v3);
    ssa!    ((vm, preserve_caller_saved_v1) <int64> v4);
    ssa!    ((vm, preserve_caller_saved_v1) <int64> v5);
    ssa!    ((vm, preserve_caller_saved_v1) <int64> v6);
    ssa!    ((vm, preserve_caller_saved_v1) <int64> v7);
    ssa!    ((vm, preserve_caller_saved_v1) <int64> v8);
    ssa!    ((vm, preserve_caller_saved_v1) <int64> v9);

    let foo_sig = vm.get_func_sig(vm.id_of("foo_sig"));
    let foo_id  = vm.id_of("foo");
    typedef!    ((vm) type_funcref_foo = mu_funcref(foo_sig));
    constdef!   ((vm) <type_funcref_foo> const_funcref_foo = Constant::FuncRef(foo_id));

    consta!     ((vm, preserve_caller_saved_v1) const_funcref_foo_local = const_funcref_foo);
    inst!       ((vm, preserve_caller_saved_v1) blk_main_call:
        EXPRCALL (CallConvention::Mu, is_abort: false) const_funcref_foo_local ()
    );

    ssa!        ((vm, preserve_caller_saved_v1) <int64> res1);
    inst!       ((vm, preserve_caller_saved_v1) blk_main_add1:
        res1 = BINOP (BinOp::Add) v0 v1
    );

    ssa!        ((vm, preserve_caller_saved_v1) <int64> res2);
    inst!       ((vm, preserve_caller_saved_v1) blk_main_add2:
        res2 = BINOP (BinOp::Add) res1 v2
    );

    ssa!        ((vm, preserve_caller_saved_v1) <int64> res3);
    inst!       ((vm, preserve_caller_saved_v1) blk_main_add3:
        res3 = BINOP (BinOp::Add) res2 v3
    );

    ssa!        ((vm, preserve_caller_saved_v1) <int64> res4);
    inst!       ((vm, preserve_caller_saved_v1) blk_main_add4:
        res4 = BINOP (BinOp::Add) res3 v4
    );

    ssa!        ((vm, preserve_caller_saved_v1) <int64> res5);
    inst!       ((vm, preserve_caller_saved_v1) blk_main_add5:
        res5 = BINOP (BinOp::Add) res4 v5
    );

    ssa!        ((vm, preserve_caller_saved_v1) <int64> res6);
    inst!       ((vm, preserve_caller_saved_v1) blk_main_add6:
        res6 = BINOP (BinOp::Add) res5 v6
    );

    ssa!        ((vm, preserve_caller_saved_v1) <int64> res7);
    inst!       ((vm, preserve_caller_saved_v1) blk_main_add7:
        res7 = BINOP (BinOp::Add) res6 v7
    );

    ssa!        ((vm, preserve_caller_saved_v1) <int64> res8);
    inst!       ((vm, preserve_caller_saved_v1) blk_main_add8:
        res8 = BINOP (BinOp::Add) res7 v8
    );

    ssa!        ((vm, preserve_caller_saved_v1) <int64> res9);
    inst!       ((vm, preserve_caller_saved_v1) blk_main_add9:
        res9 = BINOP (BinOp::Add) res8 v9
    );

    let blk_main_exit = gen_ccall_exit(res9.clone(), &mut preserve_caller_saved_v1, &vm);

    inst!       ((vm, preserve_caller_saved_v1) blk_main_ret:
        RET
    );

    define_block!   ((vm, preserve_caller_saved_v1) blk_main(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) {
        blk_main_call,

        blk_main_add1,
        blk_main_add2,
        blk_main_add3,
        blk_main_add4,
        blk_main_add5,
        blk_main_add6,
        blk_main_add7,
        blk_main_add8,
        blk_main_add9,

        blk_main_exit,
        blk_main_ret
    });

    define_func_ver!((vm) preserve_caller_saved_v1 (entry: blk_entry) {
        blk_entry,
        blk_main
    });

    vm
}

fn create_empty_func_foo(vm: &VM) {
    funcsig!    ((vm) foo_sig = () -> ());
    funcdecl!   ((vm) <foo_sig> foo);
    funcdef!    ((vm) <foo_sig> foo VERSION foo_v1);

    block!      ((vm, foo_v1) blk_entry);
    inst!       ((vm, foo_v1) blk_entry_ret:
        RET
    );

    define_block!   ((vm, foo_v1) blk_entry() {
        blk_entry_ret
    });

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