forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmir_wasm_simd.rs
More file actions
175 lines (152 loc) · 5.11 KB
/
Copy pathmir_wasm_simd.rs
File metadata and controls
175 lines (152 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use crate::ast::BinOp;
use crate::jit::simd_analyzer::SimdElemType;
use crate::mir::{self, BlockId, LocalId, RValue, Stmt, Terminator};
use crate::type_metadata::KnownType;
#[derive(Debug, Clone)]
pub struct VecLoop {
pub header: BlockId,
pub body: BlockId,
pub exit: BlockId,
pub induction: LocalId,
pub array_a: LocalId,
pub array_b: LocalId,
pub array_c: LocalId,
pub op: BinOp,
pub lane_type: SimdElemType,
}
pub fn find_vectorizable_loops(func: &mir::Function) -> Vec<VecLoop> {
let mut loops = Vec::new();
// We are looking for:
// Header block:
// Branch { cond, then_(body), else_(exit) }
// where cond is from an Assign { cond, RValue::Binary(Lt, induction, ArrayLen(arr)) }
// Wait, let's just find headers that branch to a body.
for (header_id_usize, header_block) in func.blocks.iter().enumerate() {
let header_id = BlockId(header_id_usize as u32);
let Terminator::Branch {
cond: _,
then_,
else_,
} = header_block.terminator
else {
continue;
};
let body = then_;
let exit = else_;
let body_block = &func.blocks[body.0 as usize];
// Ensure body ends with Jump(header)
if body_block.terminator != Terminator::Jump(header_id) {
continue;
}
// We look for statements in body block:
// Assign { ai, ArrayLoad { a, i } }
// Assign { bi, ArrayLoad { b, i } }
// Assign { ci, Binary(op, ai, bi) }
// ArrayStore { c, i, ci }
// Assign { i, Binary(Add, i, const 1) }
// Note: order might be slightly different. We need to match the data flow.
let mut loads = std::collections::HashMap::new();
let mut stores = Vec::new();
let mut binaries = Vec::new();
let mut has_other_control_flow = false;
for stmt in &body_block.stmts {
match stmt {
Stmt::Assign {
dst,
op: RValue::ArrayLoad { arr, idx },
} => {
loads.insert(*dst, (*arr, *idx));
}
Stmt::Assign {
dst,
op: RValue::Binary(op, left, right),
} => {
binaries.push((*dst, *op, *left, *right));
}
Stmt::ArrayStore { arr, idx, src } => {
stores.push((*arr, *idx, *src));
}
Stmt::EnterHandle { .. }
| Stmt::PopHandler
| Stmt::Emit { .. }
| Stmt::StateSet { .. }
| Stmt::StoreFieldNamed { .. } => {
has_other_control_flow = true;
}
Stmt::Assign {
op: RValue::Call { .. },
..
} => {
has_other_control_flow = true;
}
_ => {}
}
}
if has_other_control_flow || stores.is_empty() || loads.is_empty() {
continue;
}
// Find the store: ArrayStore { c, i, ci }
// Only one store supported for now
if stores.len() != 1 {
continue;
}
let (array_c, idx_c, src_c) = stores[0];
// The induction variable is idx_c.
let induction = idx_c;
// Check if there is an increment for induction: i = i + 1
let mut has_increment = false;
for (dst, op, left, right) in &binaries {
if *dst == induction && *op == BinOp::Add {
if *left == induction || *right == induction {
has_increment = true;
break;
}
}
}
if !has_increment {
continue;
}
// Find the binary op that produces src_c
let mut found_binop = None;
for (dst, op, left, right) in &binaries {
if *dst == src_c {
found_binop = Some((*op, *left, *right));
break;
}
}
let Some((op, left, right)) = found_binop else {
continue;
};
// Left and right must come from array loads using the same induction variable
let Some(&(array_a, idx_a)) = loads.get(&left) else {
continue;
};
let Some(&(array_b, idx_b)) = loads.get(&right) else {
continue;
};
if idx_a != induction || idx_b != induction {
continue;
}
// Distinct destination array (no loop-carried dependency)
if array_c == array_a || array_c == array_b {
continue;
}
// Check element type
let lane_type = match func.type_metadata.get_type(left.0 as usize) {
KnownType::Float => SimdElemType::Float64,
_ => SimdElemType::Int64,
};
loops.push(VecLoop {
header: header_id,
body,
exit,
induction,
array_a,
array_b,
array_c,
op,
lane_type,
});
}
loops
}