Skip to content

Commit 6a1288f

Browse files
committed
more handling drop for PreloadMut
1 parent 713b7e5 commit 6a1288f

3 files changed

Lines changed: 71 additions & 11 deletions

File tree

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
188188
codegen_offload_preload(self, tcx, instance, args);
189189
}
190190

191+
fn codegen_offload_preload_mut_drop(
192+
&mut self,
193+
preload_ty: Ty<'tcx>,
194+
place: PlaceRef<'tcx, &'ll llvm::Value>,
195+
) {
196+
let tcx = self.tcx;
197+
dbg!("Dropping PreloadMut; emit offload end mapper");
198+
199+
codegen_offload_preload_mut_drop(self, tcx, preload_ty, place);
200+
}
201+
191202
fn codegen_intrinsic_call(
192203
&mut self,
193204
instance: ty::Instance<'tcx>,
@@ -1911,6 +1922,55 @@ fn codegen_autodiff<'ll, 'tcx>(
19111922
);
19121923
}
19131924

1925+
fn codegen_offload_preload_mut_drop<'ll, 'tcx>(
1926+
bx: &mut Builder<'_, 'll, 'tcx>,
1927+
tcx: TyCtxt<'tcx>,
1928+
preload_ty: Ty<'tcx>,
1929+
place: PlaceRef<'tcx, &'ll llvm::Value>,
1930+
) {
1931+
let cx = bx.cx;
1932+
dbg!("Starting the PreloadMut drop handling!");
1933+
// PreloadMut<'a, T> -> extract T.
1934+
let ty::Adt(_adt_def, generic_args) = preload_ty.kind() else {
1935+
bug!("expected PreloadMut ADT, got {preload_ty:?}");
1936+
};
1937+
1938+
// This should be the `T` parameter of PreloadMut<'a, T>.
1939+
// If this indexes the lifetime in your tree, use the correct type arg index
1940+
// or `generic_args.types().next().unwrap()`.
1941+
let pointee_ty: Ty<'tcx> =
1942+
generic_args.types().next().unwrap_or_else(|| bug!("PreloadMut without type parameter"));
1943+
1944+
// Load field 0: `cpu_ptr: *mut T`.
1945+
let cpu_ptr_place = place.project_field(bx, 0);
1946+
let cpu_ptr_operand = bx.load_operand(cpu_ptr_place);
1947+
1948+
let args: Vec<&'ll Value> = match cpu_ptr_operand.val {
1949+
OperandValue::Immediate(ptr) => vec![ptr],
1950+
OperandValue::Pair(_data, _meta) => {
1951+
bug!("unsized PreloadMut drop not handled yet")
1952+
}
1953+
_ => bug!("unexpected PreloadMut cpu_ptr operand"),
1954+
};
1955+
1956+
let meta = OffloadMetadata::from_ty(tcx, pointee_ty);
1957+
let metadata: &[OffloadMetadata; 1] = &[meta];
1958+
1959+
let types: &Type = cx.layout_of(pointee_ty).llvm_type(cx);
1960+
1961+
let offload_globals_ref = cx.offload_globals.borrow();
1962+
let offload_globals = match offload_globals_ref.as_ref() {
1963+
Some(globals) => globals,
1964+
None => {
1965+
dbg!("Have to initialize offload? This is a bug!");
1966+
return;
1967+
}
1968+
};
1969+
1970+
let target_symbol = cx.generate_local_symbol_name("");
1971+
dbg!("done for now");
1972+
}
1973+
19141974
// For each PreLoad *call*, we now use some of our previous declared globals to move data to the gpu.
19151975
// For now, we only handle the data transfer part of it. Consecutive calls become a no-op on the
19161976
// LLVM side.

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_lint_defs::builtin::TAIL_CALL_TRACK_CALLER;
99
use rustc_middle::mir::{self, AssertKind, InlineAsmMacro, SwitchTargets, UnwindTerminateReason};
1010
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, ValidityRequirement};
1111
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
12-
use rustc_middle::ty::{self, Instance, Ty, TypeVisitableExt};
12+
use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeVisitableExt};
1313
use rustc_middle::{bug, span_bug};
1414
use rustc_session::config::OptLevel;
1515
use rustc_span::{Span, Spanned};
@@ -27,6 +27,14 @@ use crate::mir::IntrinsicResult;
2727
use crate::traits::*;
2828
use crate::{MemFlags, meth};
2929

30+
fn is_preload_mut_type<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
31+
let ty::Adt(adt_def, _) = ty.kind() else {
32+
return false;
33+
};
34+
35+
Some(adt_def.did()) == tcx.lang_items().preload_mut_type()
36+
}
37+
3038
// Indicates if we are in the middle of merging a BB's successor into it. This
3139
// can happen when BB jumps directly to its successor and the successor has no
3240
// other predecessors.
@@ -591,14 +599,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
591599
bx.ret(llval);
592600
}
593601

594-
fn is_preload_mut_type<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
595-
let ty::Adt(adt_def, _) = ty.kind() else {
596-
return false;
597-
};
598-
599-
Some(adt_def.did()) == tcx.lang_items().preload_mut_type()
600-
}
601-
602602
#[tracing::instrument(level = "trace", skip(self, helper, bx))]
603603
fn codegen_drop_terminator(
604604
&mut self,

compiler/rustc_codegen_ssa/src/traits/intrinsic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::BackendTypes;
55
use crate::RetagInfo;
66
use crate::mir::IntrinsicResult;
77
use crate::mir::operand::OperandRef;
8-
use crate::mir::place::PlaceValue;
8+
use crate::mir::place::{PlaceRef, PlaceValue};
99

1010
pub trait IntrinsicCallBuilderMethods<'tcx>: BackendTypes {
1111
/// Higher-level interface to emitting calls to intrinsics
@@ -40,7 +40,7 @@ pub trait IntrinsicCallBuilderMethods<'tcx>: BackendTypes {
4040

4141
fn codegen_offload_preload_mut_drop(
4242
&mut self,
43-
preload_ty: Ty<'tcx>,
43+
preload_ty: ty::Ty<'tcx>,
4444
place: PlaceRef<'tcx, Self::Value>,
4545
);
4646

0 commit comments

Comments
 (0)