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

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

use std::sync::Arc;
qinsoon's avatar
qinsoon committed
16
use std::sync::RwLock;
17

qinsoon's avatar
[wip]    
qinsoon committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
}

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

#[test]
qinsoon's avatar
qinsoon committed
92
#[allow(unused_variables)]
qinsoon's avatar
qinsoon committed
93
fn test_spill1() {
qinsoon's avatar
qinsoon committed
94
    VM::start_logging_trace();
qinsoon's avatar
qinsoon committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    
    let vm = Arc::new(create_spill1());
    
    let compiler = Compiler::new(CompilerPolicy::default(), vm.clone());
    
    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
109
110
111

    backend::emit_context(&vm);

qinsoon's avatar
qinsoon committed
112
    let dylib = aot::link_dylib(vec![Mu("spill1")], "libspill1.dylib", &vm);
qinsoon's avatar
qinsoon committed
113
114
115

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

        // we cannot call this (it doesnt return)
qinsoon's avatar
qinsoon committed
122
    }
qinsoon's avatar
qinsoon committed
123
124
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
}

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
156
    let blk_entry_spill1_funcref = func_ver.new_constant(const_func_spill1.clone());
qinsoon's avatar
qinsoon committed
157
158
159
160
    // 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
161
    vm.set_name(blk_entry_t2.as_entity(), Mu("blk_entry_t2"));
qinsoon's avatar
qinsoon committed
162
    let blk_entry_t3 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
163
    vm.set_name(blk_entry_t3.as_entity(), Mu("blk_entry_t3"));
qinsoon's avatar
qinsoon committed
164
    let blk_entry_t4 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
165
    vm.set_name(blk_entry_t4.as_entity(), Mu("blk_entry_t4"));
qinsoon's avatar
qinsoon committed
166
    let blk_entry_t5 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
167
    vm.set_name(blk_entry_t5.as_entity(), Mu("blk_entry_t5"));
qinsoon's avatar
qinsoon committed
168
    let blk_entry_t6 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
169
    vm.set_name(blk_entry_t6.as_entity(), Mu("blk_entry_t6"));
qinsoon's avatar
qinsoon committed
170
    let blk_entry_t7 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
171
    vm.set_name(blk_entry_t7.as_entity(), Mu("blk_entry_t7"));
qinsoon's avatar
qinsoon committed
172
    let blk_entry_t8 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
173
    vm.set_name(blk_entry_t8.as_entity(), Mu("blk_entry_t8"));
qinsoon's avatar
qinsoon committed
174
    let blk_entry_t9 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
175
    vm.set_name(blk_entry_t9.as_entity(), Mu("blk_entry_t9"));
qinsoon's avatar
qinsoon committed
176
    let blk_entry_t10= func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
177
    vm.set_name(blk_entry_t10.as_entity(), Mu("blk_entry_t10"));
qinsoon's avatar
qinsoon committed
178
    
179
180
    // %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
181
182
    let blk_entry_call = func_ver.new_inst(Instruction{
        hdr: MuEntityHeader::unnamed(vm.next_id()),
183
        value: Some(vec![blk_entry_x.clone_value()]),
qinsoon's avatar
qinsoon committed
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
        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
210
211
    let blk_entry_add0 = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
212
213
214
215
216
217
218
219
        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
220
221
    let blk_entry_add1 = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
222
223
224
225
226
227
228
229
        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
230
231
    let blk_entry_add2 = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
232
233
234
235
236
237
238
239
        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
240
241
    let blk_entry_add3 = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
242
243
244
245
246
247
        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
248
249
    let blk_entry_ret = func_ver.new_inst(Instruction{
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
250
251
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
282
        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
    });
    
    func_ver.define(FunctionContent {
        entry: blk_entry.id(),
        blocks: {
283
            let mut blocks = LinkedHashMap::new();
qinsoon's avatar
qinsoon committed
284
285
286
287
288
289
290
            blocks.insert(blk_entry.id(), blk_entry);
            blocks
        }
    });
    
    vm.define_func_version(func_ver);
    
qinsoon's avatar
qinsoon committed
291
292
293
294
295
    vm
}

#[test]
fn test_simple_spill() {
qinsoon's avatar
qinsoon committed
296
    VM::start_logging_trace();
qinsoon's avatar
qinsoon committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313

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

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

    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);

qinsoon's avatar
qinsoon committed
314
    let dylib = aot::link_dylib(vec![Mu("simple_spill")], "libsimple_spill.dylib", &vm);
qinsoon's avatar
qinsoon committed
315
316
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
572

    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
    });

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

    vm.define_func_version(func_ver);

583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
    vm
}

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

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

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

    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
607
        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)");
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
    }
}

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
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
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
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
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
    vm
}

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

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

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

    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());

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

    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);

    let dylib = aot::link_dylib(vec![Mu("coalesce_branch2_moves")], "libcoalesce_branch2_moves.dylib", &vm);

    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
847
    vm
848
}