-
Notifications
You must be signed in to change notification settings - Fork 119
support scalar pairs properly #640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -557,35 +557,60 @@ impl<'tcx> DecodedFormatArgs<'tcx> { | |
| if let Some((template_id, template_ty_id, rt_args_ptr_id, rt_args_ptr_ty_id)) = | ||
| split_fmt_args | ||
| { | ||
| let ctor = if let (Some(template_len), Some(rt_args_count)) = ( | ||
| if let (Some(template_len), Some(rt_args_count)) = ( | ||
| const_ptr_to_composite_len(template_id) | ||
| .or_else(|| array_len_from_ptr_type(template_ty_id)), | ||
| const_ptr_to_composite_len(rt_args_ptr_id) | ||
| .or_else(|| array_len_from_ptr_type(rt_args_ptr_ty_id)), | ||
| ) { | ||
| FmtArgsCtor::NewTemplate { | ||
| template_len, | ||
| rt_args_count, | ||
| } | ||
| ( | ||
| FmtArgsCtor::NewTemplate { | ||
| template_len, | ||
| rt_args_count, | ||
| }, | ||
| SmallVec::<[Word; 8]>::from_slice(&[template_id, rt_args_ptr_id]), | ||
| ) | ||
| } else if let Some(&[Inst::Call(_, callee_id, ref call_args)]) = | ||
| try_rev_take(-1).as_deref() | ||
| && call_args.len() == 2 | ||
| && [call_args[0], call_args[1]] == [template_id, rt_args_ptr_id] | ||
| { | ||
| // Consume the matched call instruction. | ||
| try_rev_take(1).unwrap(); | ||
| lookup_fmt_args_ctor(callee_id)? | ||
| ( | ||
| lookup_fmt_args_ctor(callee_id)?, | ||
| SmallVec::<[Word; 8]>::from_slice(&[template_id, rt_args_ptr_id]), | ||
| ) | ||
| } else if let Some( | ||
| &[ | ||
| Inst::Call(call_ret_id, callee_id, ref call_args), | ||
| Inst::CompositeExtract(extracted0, from0, 0), | ||
| Inst::CompositeExtract(extracted1, from1, 1), | ||
| ], | ||
| ) = try_rev_take(-3).as_deref() | ||
| && [from0, from1] == [call_ret_id; 2] | ||
| && [extracted0, extracted1] == [template_id, rt_args_ptr_id] | ||
| { | ||
| // Newer rustc, since `BackendRepr::ScalarPair` args are no | ||
| // longer forced to `PassMode::Direct`, returns the whole | ||
| // `fmt::Arguments` from its `new_*` constructor as a scalar | ||
| // pair, and splits it (via `OpCompositeExtract`s) into the | ||
| // two scalar values passed to the panic entry-point. | ||
| // | ||
| // The constructor's own arguments (i.e. `pieces`/`template` | ||
| // and the `rt::Argument` slice pointers) still carry the | ||
| // recoverable const data, so use those, like the aggregate | ||
| // (non-split) `Call`+`extract`+`insert` case does below. | ||
| let call_args_storage = call_args.iter().copied().collect(); | ||
| // Consume the matched call + both `OpCompositeExtract`s. | ||
| try_rev_take(3).unwrap(); | ||
| (lookup_fmt_args_ctor(callee_id)?, call_args_storage) | ||
|
Comment on lines
+584
to
+607
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only reason I don't like this has to do with it feeling misplaced, likely a consequence of the |
||
| } else { | ||
| // We failed to recover constructor metadata for an already-split | ||
| // `fmt::Arguments` value. Keep panic lowering sound by falling | ||
| // back to an unknown panic message, without requiring decompilation. | ||
| return Ok(decoded_format_args); | ||
| }; | ||
|
|
||
| ( | ||
| ctor, | ||
| SmallVec::<[Word; 8]>::from_slice(&[template_id, rt_args_ptr_id]), | ||
| ) | ||
| } | ||
| } else { | ||
| // Newer rustc can pass the `fmt::Arguments::new_*` result directly to | ||
| // panic entry points (single trailing call), while older versions go | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,8 @@ use rspirv::spirv::{ | |
| BuiltIn, Decoration, Dim, ExecutionModel, FunctionControl, StorageClass, Word, | ||
| }; | ||
| use rustc_abi::FieldsShape; | ||
| use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue}; | ||
| use rustc_codegen_ssa::mir::place::PlaceRef; | ||
| use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, BuilderMethods, MiscCodegenMethods as _}; | ||
| use rustc_data_structures::fx::FxHashMap; | ||
| use rustc_errors::MultiSpan; | ||
|
|
@@ -87,22 +89,7 @@ impl<'tcx> CodegenCx<'tcx> { | |
| }; | ||
| for (arg_abi, hir_param) in fn_abi.args.iter().zip(hir_params) { | ||
| match arg_abi.mode { | ||
| PassMode::Direct(_) | PassMode::Ignore => {} | ||
| PassMode::Pair(..) => { | ||
| // FIXME(eddyb) implement `ScalarPair` `Input`s, or change | ||
| // the `FnAbi` readjustment to only use `PassMode::Pair` for | ||
| // pointers to `!Sized` types, but not other `ScalarPair`s. | ||
| if !matches!(arg_abi.layout.ty.kind(), ty::Ref(..)) { | ||
| self.tcx.dcx().span_err( | ||
| hir_param.ty_span, | ||
| format!( | ||
| "entry point parameter type not yet supported \ | ||
| (`{}` has `ScalarPair` ABI but is not a `&T`)", | ||
| arg_abi.layout.ty | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
| PassMode::Direct(_) | PassMode::Pair(..) | PassMode::Ignore => {} | ||
| _ => span_bug!( | ||
| hir_param.ty_span, | ||
| "query hooks should've made this `PassMode` impossible: {:#?}", | ||
|
|
@@ -517,14 +504,6 @@ impl<'tcx> CodegenCx<'tcx> { | |
| vs layout:\n{value_layout:#?}", | ||
| entry_arg_abi.layout.ty | ||
| ); | ||
| if is_pair && !is_unsized { | ||
| // If PassMode is Pair, then we need to fill in the second part of the pair with a | ||
| // value. We currently only do that with unsized types, so if a type is a pair for some | ||
| // other reason (e.g. a tuple), we bail. | ||
| self.tcx | ||
| .dcx() | ||
| .span_fatal(hir_param.ty_span, "pair type not supported yet") | ||
| } | ||
| // FIXME(eddyb) should this talk about "typed buffers" instead of "interface blocks"? | ||
| // FIXME(eddyb) should we talk about "descriptor indexing" or | ||
| // actually use more reasonable terms like "resource arrays"? | ||
|
|
@@ -647,8 +626,8 @@ impl<'tcx> CodegenCx<'tcx> { | |
| } | ||
| } | ||
|
|
||
| let value_len = if is_pair { | ||
| // We've already emitted an error, fill in a placeholder value | ||
| let value_len = if is_pair && is_unsized { | ||
| // For wide references (e.g., slices), the second component is a length. | ||
|
Comment on lines
-650
to
+630
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Losing the old comment makes this look unsound ( Also, the variables make this additionally confusing, because
Not to mention that the condition for " Also, Minimal fix: include the error justification in a comment, use |
||
| Some(bx.undef(self.type_isize())) | ||
| } else { | ||
| None | ||
|
|
@@ -693,6 +672,22 @@ impl<'tcx> CodegenCx<'tcx> { | |
| call_args.push(value); | ||
| assert_eq!(value_len, None); | ||
| } | ||
| PassMode::Pair(..) => { | ||
| // Load both elements of the scalar pair from the input variable. | ||
| assert_eq!(storage_class, Ok(StorageClass::Input)); | ||
| let OperandRef { | ||
| val: OperandValue::Pair(v0, v1), | ||
| .. | ||
| } = bx.load_operand(PlaceRef::new_sized( | ||
| value_ptr.unwrap(), | ||
| entry_arg_abi.layout, | ||
| )) | ||
|
Comment on lines
+681
to
+684
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think This would also force |
||
| else { | ||
| unreachable!(); | ||
| }; | ||
| call_args.extend([v0, v1]); | ||
| assert_eq!(value_len, None); | ||
| } | ||
| _ => unreachable!(), | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // build-pass | ||
| // compile-flags: -C target-feature=+Int64 | ||
|
|
||
| use spirv_std::spirv; | ||
|
|
||
| #[spirv(fragment)] | ||
| pub fn main_future_proof( | ||
| #[spirv(flat)] input: (u64, u32), | ||
| out: &mut (u64, u32), | ||
| #[spirv(storage_buffer, descriptor_set = 0, binding = 0)] buffer_in: &(u64, u32), | ||
| #[spirv(storage_buffer, descriptor_set = 1, binding = 0)] buffer_out: &mut (u64, u32), | ||
| ) { | ||
| *out = trans0(trans_ref(buffer_in)); | ||
| *buffer_out = trans1(input); | ||
| } | ||
|
|
||
| pub fn trans0(arg: (u64, u32)) -> (u64, u32) { | ||
| (arg.0 + 1, arg.1 - 1) | ||
| } | ||
|
|
||
| pub fn trans1((a, b): (u64, u32)) -> (u64, u32) { | ||
| (a * 2, b * 3) | ||
| } | ||
|
|
||
| pub fn trans_ref((a, b): &(u64, u32)) -> (u64, u32) { | ||
| (a - 1, b - 1) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reminds me that a longstanding refactor has been replacing all
ty: TyAndLayouttolayout: TyAndLayout(because it derefs to the layout, but not the type, so e.g.layout.tyandlayout.size, instead ofty.tyandty.sizeor the redundantty.layout.size).IIRC I regretted not naming it
LayoutWithTyor similar, to make clearer the intent.