Skip to content

Commit 84c1190

Browse files
committed
Auto merge of #155576 - jhpratt:rollup-F5kv4Uy, r=jhpratt
Rollup of 5 pull requests Successful merges: - #153411 (Offload slice support) - #154557 (Make E0284 generic argument suggestions more explicit) - #154933 (Suggest removing `&` when awaiting a reference to a future) - #155524 (Fix LLVM offload install docs to use semicolon-separated CMake lists) - #155568 (Update books)
2 parents 4fbae3e + c7ccec1 commit 84c1190

71 files changed

Lines changed: 780 additions & 243 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,19 @@ pub(crate) fn gen_define_handling<'ll>(
448448
transfer.iter().map(|m| m.intersection(valid_begin_mappings).bits()).collect();
449449
let transfer_from: Vec<u64> =
450450
transfer.iter().map(|m| m.intersection(MappingFlags::FROM).bits()).collect();
451+
let valid_kernel_mappings = MappingFlags::LITERAL | MappingFlags::IMPLICIT;
451452
// FIXME(offload): add `OMP_MAP_TARGET_PARAM = 0x20` only if necessary
452-
let transfer_kernel = vec![MappingFlags::TARGET_PARAM.bits(); transfer_to.len()];
453+
let transfer_kernel: Vec<u64> = transfer
454+
.iter()
455+
.map(|m| (m.intersection(valid_kernel_mappings) | MappingFlags::TARGET_PARAM).bits())
456+
.collect();
453457

454458
let actual_sizes = sizes
455459
.iter()
456460
.map(|s| match s {
457461
OffloadSize::Static(sz) => *sz,
458-
OffloadSize::Dynamic => 0,
462+
// NOTE(Sa4dUs): set `.offload_sizes` entry to 0 for sizes that we determine at runtime, just like clang
463+
_ => 0,
459464
})
460465
.collect::<Vec<_>>();
461466
let offload_sizes =
@@ -542,12 +547,20 @@ pub(crate) fn scalar_width<'ll>(cx: &'ll SimpleCx<'_>, ty: &'ll Type) -> u64 {
542547
}
543548

544549
fn get_runtime_size<'ll, 'tcx>(
545-
_cx: &CodegenCx<'ll, 'tcx>,
546-
_val: &'ll Value,
547-
_meta: &OffloadMetadata,
550+
builder: &mut Builder<'_, 'll, 'tcx>,
551+
args: &[&'ll Value],
552+
index: usize,
553+
meta: &OffloadMetadata,
548554
) -> &'ll Value {
549-
// FIXME(Sa4dUs): handle dynamic-size data (e.g. slices)
550-
bug!("offload does not support dynamic sizes yet");
555+
match meta.payload_size {
556+
OffloadSize::Slice { element_size } => {
557+
let length_idx = index + 1;
558+
let length = args[length_idx];
559+
let length_i64 = builder.intcast(length, builder.cx.type_i64(), false);
560+
builder.mul(length_i64, builder.cx.get_const_i64(element_size))
561+
}
562+
_ => bug!("unexpected offload size {:?}", meta.payload_size),
563+
}
551564
}
552565

553566
// For each kernel *call*, we now use some of our previous declared globals to move data to and from
@@ -588,7 +601,7 @@ pub(crate) fn gen_call_handling<'ll, 'tcx>(
588601
let OffloadKernelDims { num_workgroups, threads_per_block, workgroup_dims, thread_dims } =
589602
offload_dims;
590603

591-
let has_dynamic = metadata.iter().any(|m| matches!(m.payload_size, OffloadSize::Dynamic));
604+
let has_dynamic = metadata.iter().any(|m| !matches!(m.payload_size, OffloadSize::Static(_)));
592605

593606
let tgt_decl = offload_globals.launcher_fn;
594607
let tgt_target_kernel_ty = offload_globals.launcher_ty;
@@ -683,9 +696,9 @@ pub(crate) fn gen_call_handling<'ll, 'tcx>(
683696
let gep2 = builder.inbounds_gep(ty, a2, &[i32_0, idx]);
684697
builder.store(geps[i as usize], gep2, Align::EIGHT);
685698

686-
if matches!(metadata[i as usize].payload_size, OffloadSize::Dynamic) {
699+
if !matches!(metadata[i as usize].payload_size, OffloadSize::Static(_)) {
687700
let gep3 = builder.inbounds_gep(ty2, a4, &[i32_0, idx]);
688-
let size_val = get_runtime_size(cx, args[i as usize], &metadata[i as usize]);
701+
let size_val = get_runtime_size(builder, args, i as usize, &metadata[i as usize]);
689702
builder.store(size_val, gep3, Align::EIGHT);
690703
}
691704
}

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,9 +1813,20 @@ fn codegen_offload<'ll, 'tcx>(
18131813
let sig = tcx.instantiate_bound_regions_with_erased(sig);
18141814
let inputs = sig.inputs();
18151815

1816-
let metadata = inputs.iter().map(|ty| OffloadMetadata::from_ty(tcx, *ty)).collect::<Vec<_>>();
1816+
let fn_abi = cx.fn_abi_of_instance(fn_target, ty::List::empty());
18171817

1818-
let types = inputs.iter().map(|ty| cx.layout_of(*ty).llvm_type(cx)).collect::<Vec<_>>();
1818+
let mut metadata = Vec::new();
1819+
let mut types = Vec::new();
1820+
1821+
for (i, arg_abi) in fn_abi.args.iter().enumerate() {
1822+
let ty = inputs[i];
1823+
let decomposed = OffloadMetadata::handle_abi(cx, tcx, ty, arg_abi);
1824+
1825+
for (meta, entry_ty) in decomposed {
1826+
metadata.push(meta);
1827+
types.push(bx.cx.layout_of(entry_ty).llvm_type(bx.cx));
1828+
}
1829+
}
18191830

18201831
let offload_globals_ref = cx.offload_globals.borrow();
18211832
let offload_globals = match offload_globals_ref.as_ref() {

compiler/rustc_middle/src/ty/offload_meta.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
use bitflags::bitflags;
2+
use rustc_abi::{BackendRepr, TyAbiInterface};
3+
use rustc_target::callconv::ArgAbi;
24

35
use crate::ty::{self, PseudoCanonicalInput, Ty, TyCtxt, TypingEnv};
46

7+
#[derive(Debug, Copy, Clone)]
58
pub struct OffloadMetadata {
69
pub payload_size: OffloadSize,
710
pub mode: MappingFlags,
811
}
912

1013
#[derive(Debug, Copy, Clone)]
1114
pub enum OffloadSize {
12-
Dynamic,
1315
Static(u64),
16+
Slice { element_size: u64 },
1417
}
1518

1619
bitflags! {
1720
/// Mirrors `OpenMPOffloadMappingFlags` from Clang/OpenMP.
18-
#[derive(Debug, Copy, Clone)]
21+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
1922
#[repr(transparent)]
2023
pub struct MappingFlags: u64 {
2124
/// No flags.
@@ -62,11 +65,38 @@ impl OffloadMetadata {
6265
mode: MappingFlags::from_ty(tcx, ty),
6366
}
6467
}
68+
69+
pub fn handle_abi<'tcx, C>(
70+
cx: &C,
71+
tcx: TyCtxt<'tcx>,
72+
ty: Ty<'tcx>,
73+
arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
74+
) -> Vec<(Self, Ty<'tcx>)>
75+
where
76+
Ty<'tcx>: TyAbiInterface<'tcx, C>,
77+
{
78+
match arg_abi.layout.backend_repr {
79+
BackendRepr::ScalarPair(_, _) => (0..2)
80+
.map(|i| {
81+
let ty = arg_abi.layout.field(cx, i).ty;
82+
(OffloadMetadata::from_ty(tcx, ty), ty)
83+
})
84+
.collect(),
85+
_ => vec![(OffloadMetadata::from_ty(tcx, ty), ty)],
86+
}
87+
}
6588
}
6689

6790
// FIXME(Sa4dUs): implement a solid logic to determine the payload size
6891
fn get_payload_size<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> OffloadSize {
6992
match ty.kind() {
93+
ty::Slice(elem_ty) => {
94+
let layout = tcx.layout_of(PseudoCanonicalInput {
95+
typing_env: TypingEnv::fully_monomorphized(),
96+
value: *elem_ty,
97+
});
98+
OffloadSize::Slice { element_size: layout.unwrap().size.bytes() }
99+
}
70100
ty::RawPtr(inner, _) | ty::Ref(_, inner, _) => get_payload_size(tcx, *inner),
71101
_ => OffloadSize::Static(
72102
tcx.layout_of(PseudoCanonicalInput {

compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ use rustc_middle::hir::nested_filter;
1515
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, DerefAdjustKind};
1616
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Print, Printer};
1717
use rustc_middle::ty::{
18-
self, GenericArg, GenericArgKind, GenericArgsRef, InferConst, IsSuggestable, Term, TermKind,
19-
Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, TypeckResults,
18+
self, GenericArg, GenericArgKind, GenericArgsRef, GenericParamDefKind, InferConst,
19+
IsSuggestable, Term, TermKind, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
20+
TypeVisitableExt, TypeckResults,
2021
};
2122
use rustc_span::{BytePos, DUMMY_SP, Ident, Span, sym};
2223
use tracing::{debug, instrument, warn};
@@ -592,15 +593,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
592593
(true, parent.prefix.to_string(), parent.name)
593594
});
594595

596+
let param = &generics.own_params[argument_index];
597+
let param_name = param.name.to_string();
598+
595599
infer_subdiags.push(SourceKindSubdiag::GenericLabel {
596600
span,
597601
is_type,
598-
param_name: generics.own_params[argument_index].name.to_string(),
602+
param_name: param_name.clone(),
599603
parent_exists,
600604
parent_prefix,
601605
parent_name,
602606
});
603607

608+
let mut used_fallback = false;
604609
let args = if self.tcx.get_diagnostic_item(sym::iterator_collect_fn)
605610
== Some(generics_def_id)
606611
{
@@ -634,9 +639,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
634639
let mut p = fmt_printer(self, Namespace::TypeNS);
635640
p.comma_sep(generic_args.iter().copied().map(|arg| {
636641
if arg.is_suggestable(self.tcx, true) {
642+
used_fallback = true;
637643
return arg;
638644
}
639-
640645
match arg.kind() {
641646
GenericArgKind::Lifetime(_) => bug!("unexpected lifetime"),
642647
GenericArgKind::Type(_) => self.next_ty_var(DUMMY_SP).into(),
@@ -648,11 +653,31 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
648653
};
649654

650655
if !have_turbofish {
651-
infer_subdiags.push(SourceKindSubdiag::GenericSuggestion {
652-
span: insert_span,
653-
arg_count: generic_args.len(),
654-
args,
655-
});
656+
if generic_args.len() == 1 && used_fallback {
657+
match param.kind {
658+
GenericParamDefKind::Type { .. } => {
659+
infer_subdiags.push(SourceKindSubdiag::GenericTypeSuggestion {
660+
span: insert_span,
661+
param: param_name,
662+
});
663+
}
664+
GenericParamDefKind::Const { .. } => {
665+
infer_subdiags.push(SourceKindSubdiag::ConstGenericSuggestion {
666+
span: insert_span,
667+
param: param_name,
668+
});
669+
}
670+
GenericParamDefKind::Lifetime => {
671+
bug!("unexpected lifetime")
672+
}
673+
}
674+
} else {
675+
infer_subdiags.push(SourceKindSubdiag::GenericSuggestion {
676+
span: insert_span,
677+
arg_count: generic_args.len(),
678+
args,
679+
});
680+
}
656681
}
657682
}
658683
InferSourceKind::FullyQualifiedMethodCall { receiver, successor, args, def_id } => {

0 commit comments

Comments
 (0)