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 10.2 KB
Newer Older
1
2
3
extern crate mu;
extern crate log;
extern crate simple_logger;
4
extern crate libloading;
5
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
17
use std::sync::RwLock;
use std::collections::HashMap;
18

19
20
21
22
#[test]
fn test_ir_liveness_fac() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
qinsoon's avatar
qinsoon committed
23
    let vm = Arc::new(factorial());
24
25
26
27
28
29
    
    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()),
30
            Box::new(backend::inst_sel::InstructionSelection::new()),
qinsoon's avatar
qinsoon committed
31
    ]), vm.clone());
32
    
qinsoon's avatar
qinsoon committed
33
    let func_id = vm.id_of("fac");
qinsoon's avatar
qinsoon committed
34
    let funcs = vm.funcs().read().unwrap();
35
    let func = funcs.get(&func_id).unwrap().read().unwrap();
qinsoon's avatar
qinsoon committed
36
    let func_vers = vm.func_vers().read().unwrap();
qinsoon's avatar
qinsoon committed
37
    let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();
38
    
qinsoon's avatar
qinsoon committed
39
    compiler.compile(&mut func_ver);
40
    
qinsoon's avatar
qinsoon committed
41
    let cf_lock = vm.compiled_funcs().read().unwrap();
42
    let cf = cf_lock.get(&func_ver.id()).unwrap().read().unwrap();
43
44
45
    
    // block 0
    
qinsoon's avatar
qinsoon committed
46
    let block_0_livein = cf.mc().get_ir_block_livein("blk_0").unwrap();
qinsoon's avatar
qinsoon committed
47
    let blk_0_n_3 = vm.id_of("blk_0_n_3");;
48
49
    assert!(vec_utils::is_identical_to_str_ignore_order(block_0_livein, vec![blk_0_n_3]));
    
qinsoon's avatar
qinsoon committed
50
    let block_0_liveout = cf.mc().get_ir_block_liveout("blk_0").unwrap();
51
52
53
54
    assert!(vec_utils::is_identical_to_str_ignore_order(block_0_liveout, vec![blk_0_n_3]));
    
    // block 1
    
qinsoon's avatar
qinsoon committed
55
    let block_1_livein = cf.mc().get_ir_block_livein("blk_1").unwrap();
qinsoon's avatar
qinsoon committed
56
    let blk_1_n_3 = vm.id_of("blk_1_n_3");
57
58
    assert!(vec_utils::is_identical_to_str_ignore_order(block_1_livein, vec![blk_1_n_3]));
    
qinsoon's avatar
qinsoon committed
59
    let block_1_liveout = cf.mc().get_ir_block_liveout("blk_1").unwrap();
qinsoon's avatar
qinsoon committed
60
    let blk_1_v52 = vm.id_of("blk_1_v52");
qinsoon's avatar
qinsoon committed
61
62
    trace!("lhs: {:?}", block_1_liveout);
    trace!("rhs: {:?}", vec![blk_1_v52]);
63
64
65
66
    assert!(vec_utils::is_identical_to_str_ignore_order(block_1_liveout, vec![blk_1_v52]));
    
    // block 2
    
qinsoon's avatar
qinsoon committed
67
    let block_2_livein = cf.mc().get_ir_block_livein("blk_2").unwrap();
qinsoon's avatar
qinsoon committed
68
    let blk_2_v53 = vm.id_of("blk_2_v53");
69
70
    assert!(vec_utils::is_identical_to_str_ignore_order(block_2_livein, vec![blk_2_v53]));
    
qinsoon's avatar
qinsoon committed
71
    let block_2_liveout = cf.mc().get_ir_block_liveout("blk_2").unwrap();
72
73
    let expect : Vec<MuID> = vec![];
    assert!(vec_utils::is_identical_to_str_ignore_order(block_2_liveout, expect));
qinsoon's avatar
qinsoon committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
}

#[test]
fn test_spill1() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
    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);
    }
}

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
126
    let blk_entry_spill1_funcref = func_ver.new_constant(const_func_spill1.clone());
qinsoon's avatar
qinsoon committed
127
128
129
130
    // 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
131
    vm.set_name(blk_entry_t2.as_entity(), Mu("blk_entry_t2"));
qinsoon's avatar
qinsoon committed
132
    let blk_entry_t3 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
133
    vm.set_name(blk_entry_t3.as_entity(), Mu("blk_entry_t3"));
qinsoon's avatar
qinsoon committed
134
    let blk_entry_t4 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
135
    vm.set_name(blk_entry_t4.as_entity(), Mu("blk_entry_t4"));
qinsoon's avatar
qinsoon committed
136
    let blk_entry_t5 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
137
    vm.set_name(blk_entry_t5.as_entity(), Mu("blk_entry_t5"));
qinsoon's avatar
qinsoon committed
138
    let blk_entry_t6 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
139
    vm.set_name(blk_entry_t6.as_entity(), Mu("blk_entry_t6"));
qinsoon's avatar
qinsoon committed
140
    let blk_entry_t7 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
141
    vm.set_name(blk_entry_t7.as_entity(), Mu("blk_entry_t7"));
qinsoon's avatar
qinsoon committed
142
    let blk_entry_t8 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
143
    vm.set_name(blk_entry_t8.as_entity(), Mu("blk_entry_t8"));
qinsoon's avatar
qinsoon committed
144
    let blk_entry_t9 = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
145
    vm.set_name(blk_entry_t9.as_entity(), Mu("blk_entry_t9"));
qinsoon's avatar
qinsoon committed
146
    let blk_entry_t10= func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
qinsoon's avatar
qinsoon committed
147
    vm.set_name(blk_entry_t10.as_entity(), Mu("blk_entry_t10"));
qinsoon's avatar
qinsoon committed
148
    
149
150
    // %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
151
152
    let blk_entry_call = func_ver.new_inst(Instruction{
        hdr: MuEntityHeader::unnamed(vm.next_id()),
153
        value: Some(vec![blk_entry_x.clone_value()]),
qinsoon's avatar
qinsoon committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
        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
180
181
    let blk_entry_add0 = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
182
183
184
185
186
187
188
189
        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
190
191
    let blk_entry_add1 = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
192
193
194
195
196
197
198
199
        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
200
201
    let blk_entry_add2 = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
202
203
204
205
206
207
208
209
        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
210
211
    let blk_entry_add3 = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
212
213
214
215
216
217
        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
218
219
    let blk_entry_ret = func_ver.new_inst(Instruction{
        hdr: MuEntityHeader::unnamed(vm.next_id()),
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
        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: {
            let mut blocks = HashMap::new();
            blocks.insert(blk_entry.id(), blk_entry);
            blocks
        }
    });
    
    vm.define_func_version(func_ver);
    
    vm
262
}