Skip to content

Commit febd253

Browse files
khyperiaeddyb
authored andcommitted
Bypass rspirv O(n^2) deduplication
1 parent 7974817 commit febd253

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

crates/rustc_codegen_spirv/src/spirv_type.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,14 @@ impl SpirvType {
9797
if let Some(cached) = cx.type_cache.get(&self) {
9898
return cached;
9999
}
100+
let id = Some(cx.emit_global().id());
100101
let result = match self {
101-
Self::Void => cx.emit_global().type_void(),
102-
Self::Bool => cx.emit_global().type_bool(),
102+
Self::Void => cx.emit_global().type_void_id(id),
103+
Self::Bool => cx.emit_global().type_bool_id(id),
103104
Self::Integer(width, signedness) => {
104-
let result = cx
105-
.emit_global()
106-
.type_int(width, if signedness { 1 } else { 0 });
105+
let result =
106+
cx.emit_global()
107+
.type_int_id(id, width, if signedness { 1 } else { 0 });
107108
match width {
108109
8 if !cx.builder.has_capability(Capability::Int8) => cx
109110
.zombie_even_in_user_code(result, def_span, "u8 without OpCapability Int8"),
@@ -130,7 +131,7 @@ impl SpirvType {
130131
result
131132
}
132133
Self::Float(width) => {
133-
let result = cx.emit_global().type_float(width);
134+
let result = cx.emit_global().type_float_id(id, width);
134135
match width {
135136
64 if !cx.builder.has_capability(Capability::Float64) => cx
136137
.zombie_even_in_user_code(
@@ -156,9 +157,7 @@ impl SpirvType {
156157
ref field_names,
157158
} => {
158159
let mut emit = cx.emit_global();
159-
// Ensure a unique struct is emitted each time, due to possibly having different OpMemberDecorates
160-
let id = emit.id();
161-
let result = emit.type_struct_id(Some(id), field_types.iter().cloned());
160+
let result = emit.type_struct_id(id, field_types.iter().cloned());
162161
// The struct size is only used in our own sizeof_in_bits() (used in e.g. ArrayStride decoration)
163162
if !cx.target.is_kernel() {
164163
// TODO: kernel mode can't do this??
@@ -181,18 +180,19 @@ impl SpirvType {
181180
result
182181
}
183182
Self::Opaque { ref name } => cx.emit_global().type_opaque(name),
184-
Self::Vector { element, count } => cx.emit_global().type_vector(element, count),
183+
Self::Vector { element, count } => cx.emit_global().type_vector_id(id, element, count),
185184
Self::Array { element, count } => {
186185
// ArrayStride decoration wants in *bytes*
187186
let element_size = cx
188187
.lookup_type(element)
189188
.sizeof(cx)
190189
.expect("Element of sized array must be sized")
191190
.bytes();
192-
let result = cx.emit_global().type_array(element, count.def_cx(cx));
191+
let mut emit = cx.emit_global();
192+
let result = emit.type_array_id(id, element, count.def_cx(cx));
193193
if !cx.target.is_kernel() {
194194
// TODO: kernel mode can't do this??
195-
cx.emit_global().decorate(
195+
emit.decorate(
196196
result,
197197
Decoration::ArrayStride,
198198
iter::once(Operand::LiteralInt32(element_size as u32)),
@@ -201,14 +201,15 @@ impl SpirvType {
201201
result
202202
}
203203
Self::RuntimeArray { element } => {
204-
let result = cx.emit_global().type_runtime_array(element);
204+
let mut emit = cx.emit_global();
205+
let result = emit.type_runtime_array_id(id, element);
205206
// ArrayStride decoration wants in *bytes*
206207
let element_size = cx
207208
.lookup_type(element)
208209
.sizeof(cx)
209210
.expect("Element of sized array must be sized")
210211
.bytes();
211-
cx.emit_global().decorate(
212+
emit.decorate(
212213
result,
213214
Decoration::ArrayStride,
214215
iter::once(Operand::LiteralInt32(element_size as u32)),
@@ -224,7 +225,7 @@ impl SpirvType {
224225
// storage classes inferred from `OpVariable`s.
225226
let result = cx
226227
.emit_global()
227-
.type_pointer(None, StorageClass::Generic, pointee);
228+
.type_pointer(id, StorageClass::Generic, pointee);
228229
// no pointers to functions
229230
if let Self::Function { .. } = cx.lookup_type(pointee) {
230231
cx.zombie_even_in_user_code(
@@ -240,7 +241,7 @@ impl SpirvType {
240241
ref arguments,
241242
} => cx
242243
.emit_global()
243-
.type_function(return_type, arguments.iter().cloned()),
244+
.type_function_id(id, return_type, arguments.iter().cloned()),
244245
Self::Image {
245246
sampled_type,
246247
dim,
@@ -250,7 +251,8 @@ impl SpirvType {
250251
sampled,
251252
image_format,
252253
access_qualifier,
253-
} => cx.emit_global().type_image(
254+
} => cx.emit_global().type_image_id(
255+
id,
254256
sampled_type,
255257
dim,
256258
depth,
@@ -260,15 +262,18 @@ impl SpirvType {
260262
image_format,
261263
access_qualifier,
262264
),
263-
Self::Sampler => cx.emit_global().type_sampler(),
264-
Self::AccelerationStructureKhr => cx.emit_global().type_acceleration_structure_khr(),
265-
Self::RayQueryKhr => cx.emit_global().type_ray_query_khr(),
266-
Self::SampledImage { image_type } => cx.emit_global().type_sampled_image(image_type),
265+
Self::Sampler => cx.emit_global().type_sampler_id(id),
266+
Self::AccelerationStructureKhr => {
267+
cx.emit_global().type_acceleration_structure_khr_id(id)
268+
}
269+
Self::RayQueryKhr => cx.emit_global().type_ray_query_khr_id(id),
270+
Self::SampledImage { image_type } => {
271+
cx.emit_global().type_sampled_image_id(id, image_type)
272+
}
267273

268274
Self::InterfaceBlock { inner_type } => {
269275
let mut emit = cx.emit_global();
270-
let id = emit.id();
271-
let result = emit.type_struct_id(Some(id), iter::once(inner_type));
276+
let result = emit.type_struct_id(id, iter::once(inner_type));
272277
emit.decorate(result, Decoration::Block, iter::empty());
273278
emit.member_decorate(
274279
result,

0 commit comments

Comments
 (0)