Skip to content

Commit 006718d

Browse files
committed
builder: rely on &mut self (instead of &self) on most methods.
1 parent e56cc4d commit 006718d

File tree

5 files changed

+42
-31
lines changed

5 files changed

+42
-31
lines changed

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ fn memset_fill_u64(b: u8) -> u64 {
127127
}
128128

129129
fn memset_dynamic_scalar(
130-
builder: &Builder<'_, '_>,
130+
builder: &mut Builder<'_, '_>,
131131
fill_var: Word,
132132
byte_width: usize,
133133
is_float: bool,
@@ -154,7 +154,7 @@ fn memset_dynamic_scalar(
154154

155155
impl<'a, 'tcx> Builder<'a, 'tcx> {
156156
#[instrument(level = "trace", skip(self))]
157-
fn ordering_to_semantics_def(&self, ordering: AtomicOrdering) -> SpirvValue {
157+
fn ordering_to_semantics_def(&mut self, ordering: AtomicOrdering) -> SpirvValue {
158158
let mut invalid_seq_cst = false;
159159
let semantics = match ordering {
160160
AtomicOrdering::Relaxed => MemorySemantics::NONE,
@@ -263,7 +263,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
263263
}
264264

265265
#[instrument(level = "trace", skip(self))]
266-
fn memset_dynamic_pattern(&self, ty: &SpirvType<'tcx>, fill_var: Word) -> Word {
266+
fn memset_dynamic_pattern(&mut self, ty: &SpirvType<'tcx>, fill_var: Word) -> Word {
267267
match *ty {
268268
SpirvType::Void => self.fatal("memset invalid on void pattern"),
269269
SpirvType::Bool => self.fatal("memset invalid on bool pattern"),
@@ -823,7 +823,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
823823
)
824824
)]
825825
fn emit_access_chain(
826-
&self,
826+
&mut self,
827827
result_type: <Self as BackendTypes>::Type,
828828
pointer: Word,
829829
ptr_base_index: Option<SpirvValue>,
@@ -3091,18 +3091,20 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
30913091
struct FormatArgsNotRecognized(String);
30923092

30933093
// HACK(eddyb) this is basically a `try` block.
3094-
let try_decode_and_remove_format_args = || {
3094+
let mut try_decode_and_remove_format_args = || {
30953095
let mut decoded_format_args = DecodedFormatArgs::default();
30963096

3097-
let const_u32_as_usize = |ct_id| match self.builder.lookup_const_by_id(ct_id)? {
3097+
// HACK(eddyb) work around mutable borrowing conflicts.
3098+
let cx = self.cx;
3099+
3100+
let const_u32_as_usize = |ct_id| match cx.builder.lookup_const_by_id(ct_id)? {
30983101
SpirvConst::Scalar(x) => Some(u32::try_from(x).ok()? as usize),
30993102
_ => None,
31003103
};
31013104
let const_slice_as_elem_ids = |ptr_id: Word, len: usize| {
3102-
if let SpirvConst::PtrTo { pointee } =
3103-
self.builder.lookup_const_by_id(ptr_id)?
3105+
if let SpirvConst::PtrTo { pointee } = cx.builder.lookup_const_by_id(ptr_id)?
31043106
&& let SpirvConst::Composite(elems) =
3105-
self.builder.lookup_const_by_id(pointee)?
3107+
cx.builder.lookup_const_by_id(pointee)?
31063108
&& elems.len() == len
31073109
{
31083110
return Some(elems);

crates/rustc_codegen_spirv/src/builder/ext_inst.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl ExtInst {
4141

4242
impl<'a, 'tcx> Builder<'a, 'tcx> {
4343
pub fn custom_inst(
44-
&self,
44+
&mut self,
4545
result_type: Word,
4646
inst: custom_insts::CustomInst<Operand>,
4747
) -> SpirvValue {
@@ -58,7 +58,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5858
.with_type(result_type)
5959
}
6060

61-
pub fn gl_op(&self, op: GLOp, result_type: Word, args: impl AsRef<[SpirvValue]>) -> SpirvValue {
61+
pub fn gl_op(
62+
&mut self,
63+
op: GLOp,
64+
result_type: Word,
65+
args: impl AsRef<[SpirvValue]>,
66+
) -> SpirvValue {
6267
let args = args.as_ref();
6368
let glsl = self.ext_inst.borrow_mut().import_glsl(self);
6469
self.emit()

crates/rustc_codegen_spirv/src/builder/intrinsics.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ impl<'a, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'tcx> {
381381
}
382382

383383
impl Builder<'_, '_> {
384-
pub fn count_ones(&self, arg: SpirvValue) -> SpirvValue {
384+
pub fn count_ones(&mut self, arg: SpirvValue) -> SpirvValue {
385385
let ty = arg.ty;
386386
match self.cx.lookup_type(ty) {
387387
SpirvType::Integer(bits, false) => {
@@ -426,7 +426,7 @@ impl Builder<'_, '_> {
426426
}
427427
}
428428

429-
pub fn bit_reverse(&self, arg: SpirvValue) -> SpirvValue {
429+
pub fn bit_reverse(&mut self, arg: SpirvValue) -> SpirvValue {
430430
let ty = arg.ty;
431431
match self.cx.lookup_type(ty) {
432432
SpirvType::Integer(bits, false) => {
@@ -489,7 +489,7 @@ impl Builder<'_, '_> {
489489
}
490490

491491
pub fn count_leading_trailing_zeros(
492-
&self,
492+
&mut self,
493493
arg: SpirvValue,
494494
trailing: bool,
495495
non_zero: bool,
@@ -501,9 +501,9 @@ impl Builder<'_, '_> {
501501
let u32 = SpirvType::Integer(32, false).def(self.span(), self);
502502

503503
let glsl = self.ext_inst.borrow_mut().import_glsl(self);
504-
let find_xsb = |arg, offset: i32| {
504+
let find_xsb = |this: &mut Self, arg, offset: i32| {
505505
if trailing {
506-
let lsb = self
506+
let lsb = this
507507
.emit()
508508
.ext_inst(
509509
u32,
@@ -516,12 +516,12 @@ impl Builder<'_, '_> {
516516
if offset == 0 {
517517
lsb
518518
} else {
519-
let const_offset = self.constant_i32(self.span(), offset).def(self);
520-
self.emit().i_add(u32, None, const_offset, lsb).unwrap()
519+
let const_offset = this.constant_i32(this.span(), offset).def(this);
520+
this.emit().i_add(u32, None, const_offset, lsb).unwrap()
521521
}
522522
} else {
523523
// rust is always unsigned, so FindUMsb
524-
let msb_bit = self
524+
let msb_bit = this
525525
.emit()
526526
.ext_inst(
527527
u32,
@@ -533,21 +533,21 @@ impl Builder<'_, '_> {
533533
.unwrap();
534534
// the glsl op returns the Msb bit, not the amount of leading zeros of this u32
535535
// leading zeros = 31 - Msb bit
536-
let const_offset = self.constant_i32(self.span(), 31 - offset).def(self);
537-
self.emit().i_sub(u32, None, const_offset, msb_bit).unwrap()
536+
let const_offset = this.constant_i32(this.span(), 31 - offset).def(this);
537+
this.emit().i_sub(u32, None, const_offset, msb_bit).unwrap()
538538
}
539539
};
540540

541541
let converted = match bits {
542542
8 | 16 => {
543543
let arg = self.emit().u_convert(u32, None, arg.def(self)).unwrap();
544544
if trailing {
545-
find_xsb(arg, 0)
545+
find_xsb(self, arg, 0)
546546
} else {
547-
find_xsb(arg, bits as i32 - 32)
547+
find_xsb(self, arg, bits as i32 - 32)
548548
}
549549
}
550-
32 => find_xsb(arg.def(self), 0),
550+
32 => find_xsb(self, arg.def(self), 0),
551551
64 => {
552552
let u32_0 = self.constant_int(u32, 0).def(self);
553553
let u32_32 = self.constant_u32(self.span(), 32).def(self);
@@ -562,16 +562,16 @@ impl Builder<'_, '_> {
562562

563563
if trailing {
564564
let use_lower = self.emit().i_equal(bool, None, lower, u32_0).unwrap();
565-
let lower_bits = find_xsb(lower, 32);
566-
let higher_bits = find_xsb(higher, 0);
565+
let lower_bits = find_xsb(self, lower, 32);
566+
let higher_bits = find_xsb(self, higher, 0);
567567
self.emit()
568568
.select(u32, None, use_lower, higher_bits, lower_bits)
569569
.unwrap()
570570
} else {
571571
let use_higher =
572572
self.emit().i_equal(bool, None, higher, u32_0).unwrap();
573-
let lower_bits = find_xsb(lower, 0);
574-
let higher_bits = find_xsb(higher, 32);
573+
let lower_bits = find_xsb(self, lower, 0);
574+
let higher_bits = find_xsb(self, higher, 32);
575575
self.emit()
576576
.select(u32, None, use_higher, lower_bits, higher_bits)
577577
.unwrap()

crates/rustc_codegen_spirv/src/builder/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ pub struct Builder<'a, 'tcx> {
4747

4848
impl<'a, 'tcx> Builder<'a, 'tcx> {
4949
/// See comment on `BuilderCursor`
50-
pub fn emit(&self) -> std::cell::RefMut<'_, rspirv::dr::Builder> {
51-
self.emit_with_cursor(self.cursor)
50+
//
51+
// FIXME(eddyb) take advantage of `&mut self` to avoid `RefCell` entirely
52+
// (sadly it requires making `&CodegeCx`'s types/consts more like SPIR-T,
53+
// and completely disjoint from mutably building functions).
54+
pub fn emit(&mut self) -> std::cell::RefMut<'a, rspirv::dr::Builder> {
55+
self.cx.emit_with_cursor(self.cursor)
5256
}
5357

5458
pub fn zombie(&self, word: Word, reason: &str) {

crates/rustc_codegen_spirv/src/builder/spirv_asm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ impl<'cx, 'tcx> Builder<'cx, 'tcx> {
538538
};
539539
let inst_class = inst_name
540540
.strip_prefix("Op")
541-
.and_then(|n| self.instruction_table.table.get(n));
541+
.and_then(|n| self.cx.instruction_table.table.get(n));
542542
let inst_class = if let Some(inst) = inst_class {
543543
inst
544544
} else {

0 commit comments

Comments
 (0)