This repository was archived by the owner on Oct 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathentry.rs
More file actions
265 lines (259 loc) · 9.59 KB
/
entry.rs
File metadata and controls
265 lines (259 loc) · 9.59 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
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
use super::CodegenCx;
use crate::abi::ConvSpirvType;
use crate::builder_spirv::SpirvValue;
use crate::spirv_type::SpirvType;
use crate::symbols::{parse_attrs, Entry, SpirvAttribute};
use rspirv::dr::Operand;
use rspirv::spirv::{Decoration, ExecutionModel, FunctionControl, StorageClass, Word};
use rustc_hir as hir;
use rustc_middle::ty::layout::TyAndLayout;
use rustc_middle::ty::{Instance, Ty};
use rustc_span::Span;
use rustc_target::abi::call::{FnAbi, PassMode};
use std::collections::HashMap;
impl<'tcx> CodegenCx<'tcx> {
// Entry points declare their "interface" (all uniforms, inputs, outputs, etc.) as parameters.
// spir-v uses globals to declare the interface. So, we need to generate a lil stub for the
// "real" main that collects all those global variables and calls the user-defined main
// function.
pub fn entry_stub(
&self,
instance: &Instance<'_>,
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
entry_func: SpirvValue,
name: String,
entry: Entry,
) {
let local_id = match instance.def_id().as_local() {
Some(id) => id,
None => {
self.tcx
.sess
.err(&format!("Cannot declare {} as an entry point", name));
return;
}
};
let fn_hir_id = self.tcx.hir().local_def_id_to_hir_id(local_id);
let body = self.tcx.hir().body(self.tcx.hir().body_owned_by(fn_hir_id));
for (abi, arg) in fn_abi.args.iter().zip(body.params) {
if let PassMode::Direct(_) = abi.mode {
} else {
self.tcx.sess.span_err(
arg.span,
&format!("PassMode {:?} invalid for entry point parameter", abi.mode),
)
}
}
if let PassMode::Ignore = fn_abi.ret.mode {
} else {
self.tcx.sess.span_err(
self.tcx.hir().span(fn_hir_id),
&format!(
"PassMode {:?} invalid for entry point return type",
fn_abi.ret.mode
),
)
}
let execution_model = entry.execution_model;
let fn_id = if execution_model == ExecutionModel::Kernel {
self.kernel_entry_stub(entry_func, name, execution_model)
} else {
self.shader_entry_stub(
self.tcx.def_span(instance.def_id()),
entry_func,
fn_abi,
body.params,
name,
execution_model,
)
};
let mut emit = self.emit_global();
entry
.execution_modes
.iter()
.for_each(|(execution_mode, execution_mode_extra)| {
emit.execution_mode(fn_id, *execution_mode, execution_mode_extra);
});
}
fn shader_entry_stub(
&self,
span: Span,
entry_func: SpirvValue,
entry_fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
hir_params: &[hir::Param<'tcx>],
name: String,
execution_model: ExecutionModel,
) -> Word {
let void = SpirvType::Void.def(span, self);
let fn_void_void = SpirvType::Function {
return_type: void,
arguments: vec![],
}
.def(span, self);
let entry_func_return_type = match self.lookup_type(entry_func.ty) {
SpirvType::Function {
return_type,
arguments: _,
} => return_type,
other => self.tcx.sess.fatal(&format!(
"Invalid entry_stub type: {}",
other.debug(entry_func.ty, self)
)),
};
let mut decoration_locations = HashMap::new();
// Create OpVariables before OpFunction so they're global instead of local vars.
let arguments = entry_fn_abi
.args
.iter()
.zip(hir_params)
.map(|(entry_fn_arg, hir_param)| {
self.declare_parameter(entry_fn_arg.layout, hir_param, &mut decoration_locations)
})
.collect::<Vec<_>>();
let mut emit = self.emit_global();
let fn_id = emit
.begin_function(void, None, FunctionControl::NONE, fn_void_void)
.unwrap();
emit.begin_block(None).unwrap();
emit.function_call(
entry_func_return_type,
None,
entry_func.def_cx(self),
arguments.iter().map(|&(a, _)| a),
)
.unwrap();
emit.ret().unwrap();
emit.end_function().unwrap();
let interface: Vec<_> = if emit.version().unwrap() > (1, 3) {
// SPIR-V >= v1.4 includes all OpVariables in the interface.
arguments.into_iter().map(|(a, _)| a).collect()
} else {
// SPIR-V <= v1.3 only includes Input and Output in the interface.
arguments
.into_iter()
.filter(|&(_, s)| s == StorageClass::Input || s == StorageClass::Output)
.map(|(a, _)| a)
.collect()
};
emit.entry_point(execution_model, fn_id, name, interface);
fn_id
}
fn declare_parameter(
&self,
layout: TyAndLayout<'tcx>,
hir_param: &hir::Param<'tcx>,
decoration_locations: &mut HashMap<StorageClass, u32>,
) -> (Word, StorageClass) {
let storage_class = crate::abi::get_storage_class(self, layout).unwrap_or_else(|| {
self.tcx.sess.span_fatal(
hir_param.span,
&format!("invalid entry param type `{}`", layout.ty),
);
});
let mut has_location = matches!(
storage_class,
StorageClass::Input | StorageClass::Output | StorageClass::UniformConstant
);
// Note: this *declares* the variable too.
let spirv_type = layout.spirv_type(hir_param.span, self);
let variable = self
.emit_global()
.variable(spirv_type, None, storage_class, None);
if let hir::PatKind::Binding(_, _, ident, _) = &hir_param.pat.kind {
self.emit_global().name(variable, ident.to_string());
}
for attr in parse_attrs(self, self.tcx.hir().attrs(hir_param.hir_id)) {
match attr {
SpirvAttribute::Builtin(builtin) => {
self.emit_global().decorate(
variable,
Decoration::BuiltIn,
std::iter::once(Operand::BuiltIn(builtin)),
);
has_location = false;
}
SpirvAttribute::DescriptorSet(index) => {
self.emit_global().decorate(
variable,
Decoration::DescriptorSet,
std::iter::once(Operand::LiteralInt32(index)),
);
has_location = false;
}
SpirvAttribute::Binding(index) => {
self.emit_global().decorate(
variable,
Decoration::Binding,
std::iter::once(Operand::LiteralInt32(index)),
);
has_location = false;
}
SpirvAttribute::Flat => {
self.emit_global()
.decorate(variable, Decoration::Flat, std::iter::empty());
}
_ => {}
}
}
// Assign locations from left to right, incrementing each storage class
// individually.
// TODO: Is this right for UniformConstant? Do they share locations with
// input/outpus?
if has_location {
let location = decoration_locations
.entry(storage_class)
.or_insert_with(|| 0);
self.emit_global().decorate(
variable,
Decoration::Location,
std::iter::once(Operand::LiteralInt32(*location)),
);
*location += 1;
}
(variable, storage_class)
}
// Kernel mode takes its interface as function parameters(??)
// OpEntryPoints cannot be OpLinkage, so write out a stub to call through.
fn kernel_entry_stub(
&self,
entry_func: SpirvValue,
name: String,
execution_model: ExecutionModel,
) -> Word {
let (entry_func_return, entry_func_args) = match self.lookup_type(entry_func.ty) {
SpirvType::Function {
return_type,
arguments,
} => (return_type, arguments),
other => self.tcx.sess.fatal(&format!(
"Invalid kernel_entry_stub type: {}",
other.debug(entry_func.ty, self)
)),
};
let mut emit = self.emit_global();
let fn_id = emit
.begin_function(
entry_func_return,
None,
FunctionControl::NONE,
entry_func.ty,
)
.unwrap();
let arguments = entry_func_args
.iter()
.map(|&ty| emit.function_parameter(ty).unwrap())
.collect::<Vec<_>>();
emit.begin_block(None).unwrap();
let call_result = emit
.function_call(entry_func_return, None, entry_func.def_cx(self), arguments)
.unwrap();
if self.lookup_type(entry_func_return) == SpirvType::Void {
emit.ret().unwrap();
} else {
emit.ret_value(call_result).unwrap();
}
emit.end_function().unwrap();
emit.entry_point(execution_model, fn_id, name, &[]);
fn_id
}
}