-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathshared_memories.rs
More file actions
74 lines (66 loc) · 1.99 KB
/
shared_memories.rs
File metadata and controls
74 lines (66 loc) · 1.99 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
use cubecl_core::ir::Type;
use cubecl_opt::SharedLiveness;
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum SharedMemory {
Array {
id: u32,
ty: Type,
// Length includes unroll_factor; vectorization is in ty.size()
length: u32,
offset: u32,
},
Value {
id: u32,
ty: Type,
offset: u32,
},
}
impl SharedMemory {
pub fn id(&self) -> u32 {
match self {
SharedMemory::Array { id, .. } => *id,
SharedMemory::Value { id, .. } => *id,
}
}
pub fn offset(&self) -> u32 {
match self {
SharedMemory::Array { offset, .. } => *offset,
SharedMemory::Value { offset, .. } => *offset,
}
}
pub fn size(&self) -> u32 {
match self {
SharedMemory::Array { ty, length, .. } => *length * ty.size() as u32,
SharedMemory::Value { ty, .. } => ty.size() as u32,
}
}
}
#[derive(Default)]
pub struct SharedMemories(pub Vec<SharedMemory>);
impl SharedMemories {
/// Build from the [SharedLiveness] analysis so non-overlapping lifetimes can reuse memory.
pub fn from_liveness(shared_liveness: &SharedLiveness) -> Self {
let mut memories: Vec<SharedMemory> = shared_liveness
.allocations
.values()
.map(|alloc| match alloc.smem {
cubecl_opt::SharedMemory::Array { id, length, ty, .. } => SharedMemory::Array {
id,
ty,
length,
offset: alloc.offset,
},
cubecl_opt::SharedMemory::Value { id, ty, .. } => SharedMemory::Value {
id,
ty,
offset: alloc.offset,
},
})
.collect();
memories.sort_by_key(|m| m.id());
Self(memories)
}
pub fn size(&self) -> Option<u64> {
self.0.iter().map(|m| (m.offset() + m.size()) as u64).max()
}
}