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
176
177
178
179
180
181
182
183
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
210
211
212
213
214
215
216
217
218
219
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
use std::collections::{BTreeMap, BTreeSet};
use super::*;
/*
PTX represents dynamically allocated shared local memory as
.extern .shared .b32 shared_mem[];
In SPIRV/OpenCL world this is expressed as an additional argument to the kernel
And in AMD compilation
This pass looks for all uses of .extern .shared and converts them to
an additional method argument
The question is how this artificial argument should be expressed. There are
several options:
* Straight conversion:
.shared .b32 shared_mem[]
* Introduce .param_shared statespace:
.param_shared .b32 shared_mem
or
.param_shared .b32 shared_mem[]
* Introduce .shared_ptr <SCALAR> type:
.param .shared_ptr .b32 shared_mem
* Reuse .ptr hint:
.param .u64 .ptr shared_mem
This is the most tempting, but also the most nonsensical, .ptr is just a
hint, which has no semantical meaning (and the output of our
transformation has a semantical meaning - we emit additional
"OpFunctionParameter ..." with type "OpTypePointer Workgroup ...")
*/
pub(super) fn run<'input>(
module: Vec<Directive<'input>>,
kernels_methods_call_map: &MethodsCallMap<'input>,
new_id: &mut impl FnMut() -> SpirvWord,
) -> Result<Vec<Directive<'input>>, TranslateError> {
let mut globals_shared = HashMap::new();
for dir in module.iter() {
match dir {
Directive::Variable(
_,
ast::Variable {
state_space: ast::StateSpace::Shared,
name,
v_type,
..
},
) => {
globals_shared.insert(*name, v_type.clone());
}
_ => {}
}
}
if globals_shared.len() == 0 {
return Ok(module);
}
let mut methods_to_directly_used_shared_globals = HashMap::<_, HashSet<SpirvWord>>::new();
let module = module
.into_iter()
.map(|directive| match directive {
Directive::Method(Function {
func_decl,
globals,
body: Some(statements),
import_as,
tuning,
linkage,
}) => {
let call_key = (*func_decl).borrow().name;
let statements = statements
.into_iter()
.map(|statement| {
statement.visit_map(
&mut |id, _: Option<(&ast::Type, ast::StateSpace)>, _, _| {
if let Some(_) = globals_shared.get(&id) {
methods_to_directly_used_shared_globals
.entry(call_key)
.or_insert_with(HashSet::new)
.insert(id);
}
Ok::<_, TranslateError>(id)
},
)
})
.collect::<Result<Vec<_>, _>>()?;
Ok::<_, TranslateError>(Directive::Method(Function {
func_decl,
globals,
body: Some(statements),
import_as,
tuning,
linkage,
}))
}
directive => Ok(directive),
})
.collect::<Result<Vec<_>, _>>()?;
// If there's a chain `kernel` -> `fn1` -> `fn2`, where only `fn2` uses extern shared,
// make sure it gets propagated to `fn1` and `kernel`
let methods_to_indirectly_used_shared_globals = resolve_indirect_uses_of_globals_shared(
methods_to_directly_used_shared_globals,
kernels_methods_call_map,
);
// now visit every method declaration and inject those additional arguments
let mut directives = Vec::with_capacity(module.len());
for directive in module.into_iter() {
match directive {
Directive::Method(Function {
func_decl,
globals,
body: Some(statements),
import_as,
tuning,
linkage,
}) => {
let statements = {
let func_decl_ref = &mut (*func_decl).borrow_mut();
let method_name = func_decl_ref.name;
insert_arguments_remap_statements(
new_id,
kernels_methods_call_map,
&globals_shared,
&methods_to_indirectly_used_shared_globals,
method_name,
&mut directives,
func_decl_ref,
statements,
)?
};
directives.push(Directive::Method(Function {
func_decl,
globals,
body: Some(statements),
import_as,
tuning,
linkage,
}));
}
directive => directives.push(directive),
}
}
Ok(directives)
}
// We need to compute two kinds of information:
// * If it's a kernel -> size of .shared globals in use (direct or indirect)
// * If it's a function -> does it use .shared global (directly or indirectly)
fn resolve_indirect_uses_of_globals_shared<'input>(
methods_use_of_globals_shared: HashMap<ast::MethodName<'input, SpirvWord>, HashSet<SpirvWord>>,
kernels_methods_call_map: &MethodsCallMap<'input>,
) -> HashMap<ast::MethodName<'input, SpirvWord>, BTreeSet<SpirvWord>> {
let mut result = HashMap::new();
for (method, callees) in kernels_methods_call_map.methods() {
let mut indirect_globals = methods_use_of_globals_shared
.get(&method)
.into_iter()
.flatten()
.copied()
.collect::<BTreeSet<_>>();
for &callee in callees {
indirect_globals.extend(
methods_use_of_globals_shared
.get(&ast::MethodName::Func(callee))
.into_iter()
.flatten()
.copied(),
);
}
result.insert(method, indirect_globals);
}
result
}
fn insert_arguments_remap_statements<'input>(
new_id: &mut impl FnMut() -> SpirvWord,
kernels_methods_call_map: &MethodsCallMap<'input>,
globals_shared: &HashMap<SpirvWord, ast::Type>,
methods_to_indirectly_used_shared_globals: &HashMap<
ast::MethodName<'input, SpirvWord>,
BTreeSet<SpirvWord>,
>,
method_name: ast::MethodName<SpirvWord>,
result: &mut Vec<Directive>,
func_decl_ref: &mut std::cell::RefMut<ast::MethodDeclaration<SpirvWord>>,
statements: Vec<Statement<ast::Instruction<SpirvWord>, SpirvWord>>,
) -> Result<Vec<Statement<ast::Instruction<SpirvWord>, SpirvWord>>, TranslateError> {
let remapped_globals_in_method =
if let Some(method_globals) = methods_to_indirectly_used_shared_globals.get(&method_name) {
match method_name {
ast::MethodName::Func(..) => {
let remapped_globals = method_globals
.iter()
.map(|global| {
(
*global,
(
new_id(),
globals_shared
.get(&global)
.unwrap_or_else(|| todo!())
.clone(),
),
)
})
.collect::<BTreeMap<_, _>>();
for (_, (new_shared_global_id, shared_global_type)) in remapped_globals.iter() {
func_decl_ref.input_arguments.push(ast::Variable {
align: None,
v_type: shared_global_type.clone(),
state_space: ast::StateSpace::Shared,
name: *new_shared_global_id,
array_init: Vec::new(),
});
}
remapped_globals
}
ast::MethodName::Kernel(..) => method_globals
.iter()
.map(|global| {
(
*global,
(
*global,
globals_shared
.get(&global)
.unwrap_or_else(|| todo!())
.clone(),
),
)
})
.collect::<BTreeMap<_, _>>(),
}
} else {
return Ok(statements);
};
replace_uses_of_shared_memory(
new_id,
methods_to_indirectly_used_shared_globals,
statements,
remapped_globals_in_method,
)
}
fn replace_uses_of_shared_memory<'input>(
new_id: &mut impl FnMut() -> SpirvWord,
methods_to_indirectly_used_shared_globals: &HashMap<
ast::MethodName<'input, SpirvWord>,
BTreeSet<SpirvWord>,
>,
statements: Vec<ExpandedStatement>,
remapped_globals_in_method: BTreeMap<SpirvWord, (SpirvWord, ast::Type)>,
) -> Result<Vec<ExpandedStatement>, TranslateError> {
let mut result = Vec::with_capacity(statements.len());
for statement in statements {
match statement {
Statement::Instruction(ast::Instruction::Call {
mut data,
mut arguments,
}) => {
// We can safely skip checking call arguments,
// because there's simply no way to pass shared ptr
// without converting it to .b64 first
if let Some(shared_globals_used_by_callee) =
methods_to_indirectly_used_shared_globals
.get(&ast::MethodName::Func(arguments.func))
{
for &shared_global_used_by_callee in shared_globals_used_by_callee {
let (remapped_shared_id, type_) = remapped_globals_in_method
.get(&shared_global_used_by_callee)
.unwrap_or_else(|| todo!());
data.input_arguments
.push((type_.clone(), ast::StateSpace::Shared));
arguments.input_arguments.push(*remapped_shared_id);
}
}
result.push(Statement::Instruction(ast::Instruction::Call {
data,
arguments,
}))
}
statement => {
let new_statement =
statement.visit_map(&mut |id,
_: Option<(&ast::Type, ast::StateSpace)>,
_,
_| {
Ok::<_, TranslateError>(
if let Some((remapped_shared_id, _)) =
remapped_globals_in_method.get(&id)
{
*remapped_shared_id
} else {
id
},
)
})?;
result.push(new_statement);
}
}
}
Ok(result)
}
|