diff --git a/src/native/mod.rs b/src/native/mod.rs index 8cf62f3bb..7591f0131 100644 --- a/src/native/mod.rs +++ b/src/native/mod.rs @@ -74,7 +74,7 @@ pub(crate) fn inline_direct_function_pointer_consumers( inline::inline_direct_function_pointer_consumers(san_ll, direct_functions) } -pub(crate) use owned_cfg::{owned_module_failure, OwnedModuleFailure}; +pub(crate) use owned_cfg::{owned_module_failure, owned_module_failures, OwnedModuleFailure}; pub(crate) use parse::{ parse_return_type as parse_llvm_return_type, parse_type_prefix as parse_llvm_type_prefix, }; diff --git a/src/native/owned_cfg.rs b/src/native/owned_cfg.rs index 81a8f815b..0aed161f9 100644 --- a/src/native/owned_cfg.rs +++ b/src/native/owned_cfg.rs @@ -5111,10 +5111,46 @@ fn owned_module_environment_error(module: &crate::spirv_module::Module) -> Resul Ok(()) } +/// The first owned-construction failure in `module`, or `None` when it satisfies the contract. +/// +/// This is the admission gate, so it stops at the first failure it finds. pub(crate) fn owned_module_failure( module: &crate::spirv_module::Module, ) -> Option { - let invalid = |error| Some(OwnedModuleFailure::Invalid(error)); + owned_module_failures(module, 1).into_iter().next() +} + +/// Up to `limit` owned-construction failures in `module`, at most one per independent check, in the +/// order the checks run. +/// +/// The gate only ever wants the first failure; a *diagnostic* reader wants all of them. A module +/// part-way through lowering routinely carries a transient violation -- a debug `OpName` still +/// pointing at an id a rewrite replaced, say -- that a later phase repairs, and reporting only that +/// one hides every structural violation underneath it for as long as it lasts, which is exactly when +/// a reader is trying to find the phase that introduced one. +/// +/// Section layout and the id-collection scan are prerequisites rather than independent checks: a +/// failure in either leaves the definition table incomplete, so every later check would be reading +/// nonsense. Those are still reported alone. +pub(crate) fn owned_module_failures( + module: &crate::spirv_module::Module, + limit: usize, +) -> Vec { + let mut failures: Vec = Vec::new(); + macro_rules! record { + ($failure:expr) => {{ + failures.push($failure); + if failures.len() >= limit { + return failures; + } + }}; + } + macro_rules! record_invalid { + ($error:expr) => { + record!(OwnedModuleFailure::Invalid($error)) + }; + } + let invalid = |error| vec![OwnedModuleFailure::Invalid(error)]; if let Some(error) = owned_module_layout_error(module) { return invalid(error); } @@ -5167,48 +5203,49 @@ pub(crate) fn owned_module_failure( value_types.insert(id, result_type); } } - for instruction in module.all_inst_iter() { - if let Some(id) = - referenced_ids(instruction).find(|id| *id == 0 || !definitions.contains_key(id)) - { - return invalid(format!( - "native emitter: owned {:?}{} references undefined id %{id}", - instruction.class.opcode, - instruction - .result_id - .map(|result| format!(" %{result}")) - .unwrap_or_default() - )); - } - if instruction.result_type.is_some_and(|result_type| { + if let Some(error) = module.all_inst_iter().find_map(|instruction| { + let id = + referenced_ids(instruction).find(|id| *id == 0 || !definitions.contains_key(id))?; + Some(format!( + "native emitter: owned {:?}{} references undefined id %{id}", + instruction.class.opcode, + instruction + .result_id + .map(|result| format!(" %{result}")) + .unwrap_or_default() + )) + }) { + record_invalid!(error); + } + if module.all_inst_iter().any(|instruction| { + instruction.result_type.is_some_and(|result_type| { !definitions .get(&result_type) .is_some_and(|definition| definition.class.opcode.is_type()) - }) { - return invalid( - "native emitter: owned instruction result type is not a type declaration" - .to_string(), - ); - } + }) + }) { + record_invalid!( + "native emitter: owned instruction result type is not a type declaration".to_string() + ); } if let Err(error) = owned_type_operand_error(module, &definitions) { - return invalid(error); + record_invalid!(error); } if let Some(error) = module .annotations .iter() .find_map(|instruction| owned_annotation_error(instruction, &definitions)) { - return invalid(error); + record_invalid!(error); } if let Some(error) = owned_block_layout_error(module, &definitions) { - return invalid(error); + record_invalid!(error); } if let Some(failure) = module .all_inst_iter() .find_map(|instruction| owned_access_chain_error(instruction, &definitions, &value_types)) { - return Some(if failure.raw_buffer_eligible { + record!(if failure.raw_buffer_eligible { OwnedModuleFailure::RawBufferConstruction(failure.error) } else { OwnedModuleFailure::TypeConstruction(failure.error) @@ -5218,7 +5255,7 @@ pub(crate) fn owned_module_failure( .all_inst_iter() .find_map(|instruction| owned_memory_type_error(instruction, &definitions, &value_types)) { - return Some(OwnedModuleFailure::TypeConstruction(error)); + record!(OwnedModuleFailure::TypeConstruction(error)); } if let Some(error) = module .types_global_values @@ -5236,17 +5273,17 @@ pub(crate) fn owned_module_failure( }) }) { - return Some(OwnedModuleFailure::TypeConstruction(error)); + record!(OwnedModuleFailure::TypeConstruction(error)); } if let Some(error) = module.all_inst_iter().find_map(|instruction| { owned_value_instruction_error(instruction, &definitions, &value_types).err() }) { - return invalid(error); + record_invalid!(error); } if let Some(error) = module.all_inst_iter().find_map(|instruction| { owned_composite_instruction_error(instruction, &definitions, &value_types).err() }) { - return invalid(error); + record_invalid!(error); } let logical = module.memory_model.as_ref().is_some_and(|instruction| { instruction.operands.first() @@ -5282,10 +5319,11 @@ pub(crate) fn owned_module_failure( }) .collect::>(); if roots.len() > 1 { - return Some(OwnedModuleFailure::RawBufferConstruction( + record!(OwnedModuleFailure::RawBufferConstruction( "native emitter: cross-root StorageBuffer pointer phi requires address-domain construction" .to_string(), )); + break; } } } @@ -5299,10 +5337,10 @@ pub(crate) fn owned_module_failure( variable_pointers_storage_buffer, ) }) { - return Some(OwnedModuleFailure::TypeConstruction(error)); + record!(OwnedModuleFailure::TypeConstruction(error)); } if let Err(error) = owned_module_environment_error(module) { - return invalid(error); + record_invalid!(error); } let mut local_owners = HashMap::new(); @@ -5330,9 +5368,10 @@ pub(crate) fn owned_module_failure( .is_some_and(|owner| *owner != function_index) }) }) { - return invalid( - "native emitter: owned function references an id from another function".to_string(), + record_invalid!( + "native emitter: owned function references an id from another function".to_string() ); + break; } } @@ -5342,18 +5381,18 @@ pub(crate) fn owned_module_failure( } None }) { - return invalid(error); + record_invalid!(error); } if let Some(error) = module.all_inst_iter().find_map(|instruction| { owned_function_call_contract_error(instruction, &definitions, &value_types).err() }) { - return invalid(error); + record_invalid!(error); } if let Err(error) = owned_module_linkage_error(module, &definitions) { - return invalid(error); + record_invalid!(error); } - module.functions.iter().find_map(|function| { + if let Some(failure) = module.functions.iter().find_map(|function| { if function.blocks.is_empty() { return None; } @@ -5361,7 +5400,10 @@ pub(crate) fn owned_module_failure( .and_then(|cfg| cfg.check(function, &definitions, &value_types)) .err() .map(OwnedModuleFailure::CfgConstruction) - }) + }) { + record!(failure); + } + failures } #[cfg(test)] diff --git a/src/passes/mod.rs b/src/passes/mod.rs index 3e435089f..d31a68f20 100644 --- a/src/passes/mod.rs +++ b/src/passes/mod.rs @@ -2233,7 +2233,7 @@ mod phase_contract_tests { fn a_staged_global_counts_as_declared() { assert_eq!( phase_contract_verdict(&ctx_with_staged_pointer_type()), - None + Vec::::new() ); } @@ -2243,34 +2243,91 @@ mod phase_contract_tests { fn a_genuinely_missing_global_is_reported_by_id() { let mut ctx = ctx_with_staged_pointer_type(); ctx.new_globals.clear(); - let verdict = phase_contract_verdict(&ctx).expect("the module references an undefined id"); + let verdicts = phase_contract_verdict(&ctx); assert!( - verdict.contains("references undefined id %2"), - "verdict must name the missing id, got: {verdict}" + verdicts + .iter() + .any(|verdict| verdict.contains("references undefined id %2")), + "verdict must name the missing id, got: {verdicts:?}" + ); + } + + /// Two independent violations at once: a debug name pointing at an id nothing defines, and a + /// variable whose result type is another variable rather than a type declaration. + /// + /// Lowering leaves dangling debug names behind constantly and repairs them later, so a trace + /// that stopped at the first failure would report only the name for every phase the name + /// survived -- and the reader would blame whichever phase happens to run after the repair. + #[test] + fn a_transient_debug_name_does_not_hide_the_violation_under_it() { + let mut ctx = ctx_with_staged_pointer_type(); + ctx.module.debug_names.push(Instruction::new( + Op::Name, + None, + None, + vec![ + Operand::IdRef(9), + Operand::LiteralString("erased".to_string()), + ], + )); + // Result type %3 is the Private variable declared above, not a type. + ctx.module.types_global_values.push(Instruction::new( + Op::Variable, + Some(3), + Some(4), + vec![Operand::StorageClass(StorageClass::Private)], + )); + let verdicts = phase_contract_verdict(&ctx); + assert!( + verdicts + .iter() + .any(|verdict| verdict.contains("references undefined id %9")), + "the dangling debug name must be reported, got: {verdicts:?}" + ); + assert!( + verdicts + .iter() + .any(|verdict| verdict.contains("result type is not a type declaration")), + "the violation under it must be reported too, got: {verdicts:?}" ); } } -/// The owned-construction verdict for the module a phase just produced, or `None` when it holds. +/// Every owned-construction verdict against the module a phase just produced, empty when it holds. /// /// Phases stage new type/constant declarations in `new_globals` and only splice them into the module /// at the end. Checking the module alone would therefore report an undefined id for every phase in /// between, which drowns out the verdict this exists to show. Merging the staged globals in /// reproduces the module the pipeline is actually building. -fn phase_contract_verdict(ctx: &Ctx) -> Option { +/// +/// All of them, not just the first, because the first is routinely the least interesting: an +/// in-flight module often carries a transient violation a later phase repairs -- a debug `OpName` +/// left pointing at an id a rewrite replaced is the common one -- and the gate's first-failure +/// answer would report only that for every phase it survives, hiding the structural violation the +/// reader is trying to locate. +fn phase_contract_verdict(ctx: &Ctx) -> Vec { let mut module = ctx.module.clone(); module .types_global_values .extend(ctx.new_globals.iter().cloned()); - let (kind, error) = match crate::native::owned_module_failure(&module)? { - crate::native::OwnedModuleFailure::Invalid(error) => ("invalid", error), - crate::native::OwnedModuleFailure::TypeConstruction(error) => ("type-construction", error), - crate::native::OwnedModuleFailure::CfgConstruction(error) => ("cfg-construction", error), - crate::native::OwnedModuleFailure::RawBufferConstruction(error) => { - ("raw-buffer-construction", error) - } - }; - Some(format!("{kind}: {error}")) + crate::native::owned_module_failures(&module, usize::MAX) + .into_iter() + .map(|failure| { + let (kind, error) = match failure { + crate::native::OwnedModuleFailure::Invalid(error) => ("invalid", error), + crate::native::OwnedModuleFailure::TypeConstruction(error) => { + ("type-construction", error) + } + crate::native::OwnedModuleFailure::CfgConstruction(error) => { + ("cfg-construction", error) + } + crate::native::OwnedModuleFailure::RawBufferConstruction(error) => { + ("raw-buffer-construction", error) + } + }; + format!("{kind}: {error}") + }) + .collect() } /// Print whether the in-flight module satisfies the owned construction contract, tagged with the @@ -2283,9 +2340,12 @@ fn phase_contract_verdict(ctx: &Ctx) -> Option { /// and the message is the one the finished module would have failed with. Without it, a contract /// failure reported at the end says nothing about which phase produced it. fn report_phase_contract(ctx: &Ctx, phase: &str) { - match phase_contract_verdict(ctx) { - None => eprintln!("[pass-contract] {phase}: ok"), - Some(verdict) => eprintln!("[pass-contract] {phase}: {verdict}"), + let verdicts = phase_contract_verdict(ctx); + if verdicts.is_empty() { + eprintln!("[pass-contract] {phase}: ok"); + } + for verdict in verdicts { + eprintln!("[pass-contract] {phase}: {verdict}"); } }