Skip to content

Commit f549980

Browse files
authored
Migrate to Rust 2024, and fix clippy warnings (from newer nightlies). (#21)
A bit late on this because my default `nightly` ended up falling behind Rust-GPU's own toolchain, but I'm guessing landing any other PR will need this first anyway (due to CI).
2 parents c7a9f61 + 55ee3c8 commit f549980

File tree

10 files changed

+113
-119
lines changed

10 files changed

+113
-119
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ repository = "https://github.com/rust-gpu/spirt"
55
homepage = "https://github.com/rust-gpu/spirt"
66
version = "0.4.0"
77
authors = ["SPIR-T developers", "Embark <[email protected]>"]
8-
edition = "2021"
8+
edition = "2024"
99
license = "MIT OR Apache-2.0"
1010
readme = "README.md"
1111
# FIXME(eddyb) should this point to the version built from `git`?

src/cfg.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl ControlFlowGraph {
8282
pub fn rev_post_order(
8383
&self,
8484
func_def_body: &FuncDefBody,
85-
) -> impl DoubleEndedIterator<Item = Region> {
85+
) -> impl DoubleEndedIterator<Item = Region> + use<> {
8686
let mut post_order = SmallVec::<[_; 8]>::new();
8787
self.traverse_whole_func(
8888
func_def_body,
@@ -1871,14 +1871,14 @@ impl<'a> Structurizer<'a> {
18711871
// but instead make all `Region`s entirely hermetic wrt inputs.
18721872
#[allow(clippy::manual_flatten)]
18731873
for case in cases {
1874-
if let Ok(ClaimedRegion { structured_body, structured_body_inputs, .. }) = case {
1875-
if !structured_body_inputs.is_empty() {
1876-
self.region_input_rewrites.insert(
1877-
structured_body,
1878-
RegionInputRewrites::ReplaceWith(structured_body_inputs),
1879-
);
1880-
self.func_def_body.at_mut(structured_body).def().inputs.clear();
1881-
}
1874+
if let Ok(ClaimedRegion { structured_body, structured_body_inputs, .. }) = case
1875+
&& !structured_body_inputs.is_empty()
1876+
{
1877+
self.region_input_rewrites.insert(
1878+
structured_body,
1879+
RegionInputRewrites::ReplaceWith(structured_body_inputs),
1880+
);
1881+
self.func_def_body.at_mut(structured_body).def().inputs.clear();
18821882
}
18831883
}
18841884

src/cfgssa.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ mod data {
564564
self.contains(k).then(|| self.entry(k).remove().unwrap())
565565
}
566566

567-
pub fn keys(&self) -> impl Iterator<Item = K> {
567+
pub fn keys(&self) -> impl Iterator<Item = K> + use<K, V, VS> {
568568
let mut i = 0;
569569
let mut remaining = self.occupied;
570570
iter::from_fn(move || {

src/passes/link.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ impl Visitor<'_> for LiveExportCollector<'_> {
9595
match *import {
9696
Import::LinkName(name) => {
9797
let export_key = ExportKey::LinkName(name);
98-
if let Some(&exportee) = self.module.exports.get(&export_key) {
99-
if self.live_exports.insert(export_key) {
100-
exportee.inner_visit_with(self);
101-
}
98+
if let Some(&exportee) = self.module.exports.get(&export_key)
99+
&& self.live_exports.insert(export_key)
100+
{
101+
exportee.inner_visit_with(self);
102102
}
103103
}
104104
}
@@ -198,12 +198,11 @@ impl Visitor<'_> for ImportResolutionCollector<'_> {
198198
// FIXME(eddyb) if the export is missing (or the wrong kind), it will
199199
// simply not get remapped - perhaps some kind of diagnostic is in
200200
// order? (maybe an entire pass infrastructure that can report errors)
201-
if let DeclDef::Imported(Import::LinkName(name)) = gv_decl.def {
202-
if let Some(&Exportee::GlobalVar(def_gv)) =
201+
if let DeclDef::Imported(Import::LinkName(name)) = gv_decl.def
202+
&& let Some(&Exportee::GlobalVar(def_gv)) =
203203
self.module.exports.get(&ExportKey::LinkName(name))
204-
{
205-
self.resolved_global_vars.insert(gv, def_gv);
206-
}
204+
{
205+
self.resolved_global_vars.insert(gv, def_gv);
207206
}
208207
}
209208
}
@@ -215,12 +214,11 @@ impl Visitor<'_> for ImportResolutionCollector<'_> {
215214
// FIXME(eddyb) if the export is missing (or the wrong kind), it will
216215
// simply not get remapped - perhaps some kind of diagnostic is in
217216
// order? (maybe an entire pass infrastructure that can report errors)
218-
if let DeclDef::Imported(Import::LinkName(name)) = func_decl.def {
219-
if let Some(&Exportee::Func(def_func)) =
217+
if let DeclDef::Imported(Import::LinkName(name)) = func_decl.def
218+
&& let Some(&Exportee::Func(def_func)) =
220219
self.module.exports.get(&ExportKey::LinkName(name))
221-
{
222-
self.resolved_funcs.insert(func, def_func);
223-
}
220+
{
221+
self.resolved_funcs.insert(func, def_func);
224222
}
225223
}
226224
}

src/print/mod.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -630,13 +630,13 @@ impl<'a> Visitor<'a> for Plan<'a> {
630630
}
631631

632632
fn visit_func_decl(&mut self, func_decl: &'a FuncDecl) {
633-
if let DeclDef::Present(func_def_body) = &func_decl.def {
634-
if let Some(cfg) = &func_def_body.unstructured_cfg {
635-
for region in cfg.rev_post_order(func_def_body) {
636-
if let Some(control_inst) = cfg.control_inst_on_exit_from.get(region) {
637-
for &target in &control_inst.targets {
638-
*self.use_counts.entry(Use::RegionLabel(target)).or_default() += 1;
639-
}
633+
if let DeclDef::Present(func_def_body) = &func_decl.def
634+
&& let Some(cfg) = &func_def_body.unstructured_cfg
635+
{
636+
for region in cfg.rev_post_order(func_def_body) {
637+
if let Some(control_inst) = cfg.control_inst_on_exit_from.get(region) {
638+
for &target in &control_inst.targets {
639+
*self.use_counts.entry(Use::RegionLabel(target)).or_default() += 1;
640640
}
641641
}
642642
}
@@ -1952,7 +1952,7 @@ impl AttrsAndDef {
19521952
let mut maybe_def_end_anchor = pretty::Fragment::default();
19531953
let mut name = name.into();
19541954
if let [
1955-
pretty::Node::Anchor { is_def: ref mut original_anchor_is_def @ true, anchor, text: _ },
1955+
pretty::Node::Anchor { is_def: original_anchor_is_def @ true, anchor, text: _ },
19561956
..,
19571957
] = &mut name.nodes[..]
19581958
{
@@ -4089,12 +4089,12 @@ impl Print for FuncAt<'_, DataInst> {
40894089
}
40904090
ConstKind::SpvInst { spv_inst_and_const_inputs } => {
40914091
let (spv_inst, _const_inputs) = &**spv_inst_and_const_inputs;
4092-
if spv_inst.opcode == wk.OpConstant {
4093-
if let [spv::Imm::Short(_, x)] = spv_inst.imms[..] {
4094-
// HACK(eddyb) only allow unambiguously positive values.
4095-
if i32::try_from(x).and_then(u32::try_from) == Ok(x) {
4096-
return Some(PseudoImm::U32(x));
4097-
}
4092+
if spv_inst.opcode == wk.OpConstant
4093+
&& let [spv::Imm::Short(_, x)] = spv_inst.imms[..]
4094+
{
4095+
// HACK(eddyb) only allow unambiguously positive values.
4096+
if i32::try_from(x).and_then(u32::try_from) == Ok(x) {
4097+
return Some(PseudoImm::U32(x));
40984098
}
40994099
}
41004100
}

src/qptr/analyze.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,10 @@ impl MemTypeLayout {
533533
// "Fast accept" based on type alone (expected as recursion base case).
534534
if let QPtrMemUsageKind::StrictlyTyped(usage_type)
535535
| QPtrMemUsageKind::DirectAccess(usage_type) = usage.kind
536+
&& usage_offset == 0
537+
&& self.original_type == usage_type
536538
{
537-
if usage_offset == 0 && self.original_type == usage_type {
538-
return true;
539-
}
539+
return true;
540540
}
541541

542542
{
@@ -608,7 +608,7 @@ impl MemTypeLayout {
608608
let usage_fixed_len = usage
609609
.max_size
610610
.map(|size| {
611-
if size % usage_stride.get() != 0 {
611+
if !size.is_multiple_of(usage_stride.get()) {
612612
// FIXME(eddyb) maybe this should be propagated up,
613613
// as a sign that `usage` is malformed?
614614
return Err(());
@@ -924,11 +924,11 @@ impl<'a> InferUsage<'a> {
924924
));
925925
}
926926
};
927-
if data_inst_def.output_type.is_some_and(is_qptr) {
928-
if let Some(usage) = output_usage {
929-
usage_or_err_attrs_to_attach
930-
.push((Value::DataInstOutput(data_inst), usage));
931-
}
927+
if data_inst_def.output_type.is_some_and(is_qptr)
928+
&& let Some(usage) = output_usage
929+
{
930+
usage_or_err_attrs_to_attach
931+
.push((Value::DataInstOutput(data_inst), usage));
932932
}
933933
}
934934

src/qptr/lift.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl<'a> LiftToSpvPtrs<'a> {
245245
let fixed_len = usage
246246
.max_size
247247
.map(|size| {
248-
if size % stride.get() != 0 {
248+
if !size.is_multiple_of(stride.get()) {
249249
return Err(LiftError(Diag::bug([format!(
250250
"DynOffsetBase: size ({size}) not a multiple of stride ({stride})"
251251
)
@@ -430,13 +430,13 @@ impl LiftToSpvPtrInstsInFunc<'_> {
430430
// FIXME(eddyb) maybe all this data should be packaged up together in a
431431
// type with fields like those of `DeferredPtrNoop` (or even more).
432432
let type_of_val_as_spv_ptr_with_layout = |v: Value| {
433-
if let Value::DataInstOutput(v_data_inst) = v {
434-
if let Some(ptr_noop) = self.deferred_ptr_noops.get(&v_data_inst) {
435-
return Ok((
436-
ptr_noop.output_pointer_addr_space,
437-
ptr_noop.output_pointee_layout.clone(),
438-
));
439-
}
433+
if let Value::DataInstOutput(v_data_inst) = v
434+
&& let Some(ptr_noop) = self.deferred_ptr_noops.get(&v_data_inst)
435+
{
436+
return Ok((
437+
ptr_noop.output_pointer_addr_space,
438+
ptr_noop.output_pointee_layout.clone(),
439+
));
440440
}
441441

442442
let (addr_space, pointee_type) =
@@ -685,17 +685,15 @@ impl LiftToSpvPtrInstsInFunc<'_> {
685685
loop {
686686
if let Components::Elements { stride: layout_stride, elem, fixed_len } =
687687
&layout.components
688+
&& layout_stride == stride
689+
&& Ok(index_bounds.clone())
690+
== fixed_len
691+
.map(|len| i32::try_from(len.get()).map(|len| 0..len))
692+
.transpose()
688693
{
689-
if layout_stride == stride
690-
&& Ok(index_bounds.clone())
691-
== fixed_len
692-
.map(|len| i32::try_from(len.get()).map(|len| 0..len))
693-
.transpose()
694-
{
695-
access_chain_inputs.push(data_inst_def.inputs[1]);
696-
layout = elem.clone();
697-
break;
698-
}
694+
access_chain_inputs.push(data_inst_def.inputs[1]);
695+
layout = elem.clone();
696+
break;
699697
}
700698

701699
// FIXME(eddyb) deduplicate with `maybe_adjust_pointer_for_access`.
@@ -1094,12 +1092,12 @@ impl Transformer for LiftToSpvPtrInstsInFunc<'_> {
10941092
if let DataInstKind::QPtr(_) = data_inst_def.kind {
10951093
lifted =
10961094
Err(LiftError(Diag::bug(["unimplemented qptr instruction".into()])));
1097-
} else if let Some(ty) = data_inst_def.output_type {
1098-
if matches!(self.lifter.cx[ty].kind, TypeKind::QPtr) {
1099-
lifted = Err(LiftError(Diag::bug([
1100-
"unimplemented qptr-producing instruction".into(),
1101-
])));
1102-
}
1095+
} else if let Some(ty) = data_inst_def.output_type
1096+
&& matches!(self.lifter.cx[ty].kind, TypeKind::QPtr)
1097+
{
1098+
lifted = Err(LiftError(Diag::bug([
1099+
"unimplemented qptr-producing instruction".into(),
1100+
])));
11031101
}
11041102
}
11051103
match lifted {

src/qptr/lower.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -625,16 +625,16 @@ impl LowerFromSpvPtrInstsInFunc<'_> {
625625
);
626626
}
627627
}
628-
if let Some(output_type) = data_inst_def.output_type {
629-
if let Some((addr_space, pointee)) = self.lowerer.as_spv_ptr_type(output_type) {
630-
old_and_new_attrs.get_or_insert_with(get_old_attrs).attrs.insert(
631-
QPtrAttr::FromSpvPtrOutput {
632-
addr_space: OrdAssertEq(addr_space),
633-
pointee: OrdAssertEq(pointee),
634-
}
635-
.into(),
636-
);
637-
}
628+
if let Some(output_type) = data_inst_def.output_type
629+
&& let Some((addr_space, pointee)) = self.lowerer.as_spv_ptr_type(output_type)
630+
{
631+
old_and_new_attrs.get_or_insert_with(get_old_attrs).attrs.insert(
632+
QPtrAttr::FromSpvPtrOutput {
633+
addr_space: OrdAssertEq(addr_space),
634+
pointee: OrdAssertEq(pointee),
635+
}
636+
.into(),
637+
);
638638
}
639639

640640
if let Some(LowerError(e)) = extra_error {

src/spv/lower.rs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -799,12 +799,12 @@ impl Module {
799799

800800
Seq::Function
801801
};
802-
if let Some(prev_seq) = seq {
803-
if prev_seq > next_seq {
804-
return Err(invalid(&format!(
805-
"out of order: {next_seq:?} instructions must precede {prev_seq:?} instructions"
806-
)));
807-
}
802+
if let Some(prev_seq) = seq
803+
&& prev_seq > next_seq
804+
{
805+
return Err(invalid(&format!(
806+
"out of order: {next_seq:?} instructions must precede {prev_seq:?} instructions"
807+
)));
808808
}
809809
seq = Some(next_seq);
810810

@@ -978,26 +978,26 @@ impl Module {
978978
local_id_defs.insert(id, local_id_def);
979979
}
980980

981-
if let Some(def_map) = &mut cfgssa_def_map {
982-
if let DeclDef::Present(func_def_body) = &func_decl.def {
983-
let current_block = match block_details.last() {
984-
Some((&current_block, _)) => current_block,
985-
// HACK(eddyb) ensure e.g. `OpFunctionParameter`
986-
// are treated like `OpPhi`s of the entry block.
987-
None => func_def_body.body,
988-
};
981+
if let Some(def_map) = &mut cfgssa_def_map
982+
&& let DeclDef::Present(func_def_body) = &func_decl.def
983+
{
984+
let current_block = match block_details.last() {
985+
Some((&current_block, _)) => current_block,
986+
// HACK(eddyb) ensure e.g. `OpFunctionParameter`
987+
// are treated like `OpPhi`s of the entry block.
988+
None => func_def_body.body,
989+
};
989990

990-
if opcode == wk.OpLabel {
991-
// HACK(eddyb) the entry block was already added.
992-
if current_block != func_def_body.body {
993-
def_map.add_block(current_block);
994-
}
995-
continue;
991+
if opcode == wk.OpLabel {
992+
// HACK(eddyb) the entry block was already added.
993+
if current_block != func_def_body.body {
994+
def_map.add_block(current_block);
996995
}
996+
continue;
997+
}
997998

998-
if let Some(id) = result_id {
999-
def_map.add_def(current_block, id, result_type.unwrap());
1000-
}
999+
if let Some(id) = result_id {
1000+
def_map.add_def(current_block, id, result_type.unwrap());
10011001
}
10021002
}
10031003
}
@@ -1643,14 +1643,12 @@ impl Module {
16431643
}
16441644

16451645
// Sanity-check the entry block.
1646-
if let Some(func_def_body) = func_def_body {
1647-
if block_details[&func_def_body.body].phi_count > 0 {
1648-
// FIXME(remove) embed IDs in errors by moving them to the
1649-
// `let invalid = |...| ...;` closure that wraps insts.
1650-
return Err(invalid(&format!(
1651-
"in %{func_id}, the entry block contains `OpPhi`s"
1652-
)));
1653-
}
1646+
if let Some(func_def_body) = func_def_body
1647+
&& block_details[&func_def_body.body].phi_count > 0
1648+
{
1649+
// FIXME(remove) embed IDs in errors by moving them to the
1650+
// `let invalid = |...| ...;` closure that wraps insts.
1651+
return Err(invalid(&format!("in %{func_id}, the entry block contains `OpPhi`s")));
16541652
}
16551653
}
16561654

src/spv/read.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ impl InstParser<'_> {
213213
self.inst.result_id = def.has_result_id.then(&mut id).transpose()?;
214214
}
215215

216-
if let Some(type_id) = self.inst.result_type_id {
217-
if !self.known_ids.contains_key(&type_id) {
218-
// FIXME(eddyb) also check that the ID is a valid type.
219-
return Err(Error::UnknownResultTypeId(type_id));
220-
}
216+
if let Some(type_id) = self.inst.result_type_id
217+
&& !self.known_ids.contains_key(&type_id)
218+
{
219+
// FIXME(eddyb) also check that the ID is a valid type.
220+
return Err(Error::UnknownResultTypeId(type_id));
221221
}
222222

223223
for (mode, kind) in def.all_operands() {
@@ -266,7 +266,7 @@ impl ModuleParser {
266266
pub fn read_from_spv_bytes(spv_bytes: Vec<u8>) -> io::Result<Self> {
267267
let spv_spec = spec::Spec::get();
268268

269-
if spv_bytes.len() % 4 != 0 {
269+
if !spv_bytes.len().is_multiple_of(4) {
270270
return Err(invalid("not a multiple of 4 bytes"));
271271
}
272272
// May need to mutate the bytes (to normalize endianness) later below.

0 commit comments

Comments
 (0)