Skip to content

Commit 1bc11e9

Browse files
committed
Let miri and const eval execute intrinsics' fallback bodies
1 parent c672773 commit 1bc11e9

File tree

9 files changed

+71
-45
lines changed

9 files changed

+71
-45
lines changed

compiler/rustc_const_eval/src/const_eval/dummy_machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'mir, 'tcx: 'mir> interpret::Machine<'mir, 'tcx> for DummyMachine {
102102
_destination: &interpret::MPlaceTy<'tcx, Self::Provenance>,
103103
_target: Option<BasicBlock>,
104104
_unwind: UnwindAction,
105-
) -> interpret::InterpResult<'tcx> {
105+
) -> interpret::InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
106106
unimplemented!()
107107
}
108108

compiler/rustc_const_eval/src/const_eval/machine.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -459,16 +459,25 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
459459
dest: &MPlaceTy<'tcx, Self::Provenance>,
460460
target: Option<mir::BasicBlock>,
461461
_unwind: mir::UnwindAction,
462-
) -> InterpResult<'tcx> {
462+
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
463463
// Shared intrinsics.
464464
if ecx.emulate_intrinsic(instance, args, dest, target)? {
465-
return Ok(());
465+
return Ok(None);
466466
}
467467
let intrinsic_name = ecx.tcx.item_name(instance.def_id());
468468

469469
// CTFE-specific intrinsics.
470470
let Some(ret) = target else {
471-
throw_unsup_format!("intrinsic `{intrinsic_name}` is not supported at compile-time");
471+
// Handle diverging intrinsics.
472+
if ecx.tcx.intrinsic(instance.def_id()).unwrap().must_be_overridden {
473+
throw_unsup_format!(
474+
"intrinsic `{intrinsic_name}` is not supported at compile-time"
475+
);
476+
}
477+
return Ok(Some(ty::Instance {
478+
def: ty::InstanceDef::Item(instance.def_id()),
479+
args: instance.args,
480+
}));
472481
};
473482
match intrinsic_name {
474483
sym::ptr_guaranteed_cmp => {
@@ -536,14 +545,21 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
536545
// not the optimization stage.)
537546
sym::is_val_statically_known => ecx.write_scalar(Scalar::from_bool(false), dest)?,
538547
_ => {
539-
throw_unsup_format!(
540-
"intrinsic `{intrinsic_name}` is not supported at compile-time"
541-
);
548+
// We haven't handled the intrinsic, let's see if we can use a fallback body.
549+
if ecx.tcx.intrinsic(instance.def_id()).unwrap().must_be_overridden {
550+
throw_unsup_format!(
551+
"intrinsic `{intrinsic_name}` is not supported at compile-time"
552+
);
553+
}
554+
return Ok(Some(ty::Instance {
555+
def: ty::InstanceDef::Item(instance.def_id()),
556+
args: instance.args,
557+
}));
542558
}
543559
}
544560

545561
ecx.go_to_block(ret);
546-
Ok(())
562+
Ok(None)
547563
}
548564

549565
fn assert_panic(

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
410410
}
411411
self.copy_op(&self.project_index(&input, index)?, dest)?;
412412
}
413-
sym::likely | sym::unlikely | sym::black_box => {
413+
sym::black_box => {
414414
// These just return their argument
415415
self.copy_op(&args[0], dest)?;
416416
}

compiler/rustc_const_eval/src/interpret/machine.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,17 @@ pub trait Machine<'mir, 'tcx: 'mir>: Sized {
215215

216216
/// Directly process an intrinsic without pushing a stack frame. It is the hook's
217217
/// responsibility to advance the instruction pointer as appropriate.
218+
///
219+
/// Returns `None` if the intrinsic was fully handled.
220+
/// Otherwise, returns an `Instance` of the function that implements the intrinsic.
218221
fn call_intrinsic(
219222
ecx: &mut InterpCx<'mir, 'tcx, Self>,
220223
instance: ty::Instance<'tcx>,
221224
args: &[OpTy<'tcx, Self::Provenance>],
222225
destination: &MPlaceTy<'tcx, Self::Provenance>,
223226
target: Option<mir::BasicBlock>,
224227
unwind: mir::UnwindAction,
225-
) -> InterpResult<'tcx>;
228+
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>>;
226229

227230
/// Called to evaluate `Assert` MIR terminators that trigger a panic.
228231
fn assert_panic(

compiler/rustc_const_eval/src/interpret/terminator.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -539,14 +539,28 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
539539
ty::InstanceDef::Intrinsic(def_id) => {
540540
assert!(self.tcx.intrinsic(def_id).is_some());
541541
// FIXME: Should `InPlace` arguments be reset to uninit?
542-
M::call_intrinsic(
542+
if let Some(fallback) = M::call_intrinsic(
543543
self,
544544
instance,
545545
&self.copy_fn_args(args),
546546
destination,
547547
target,
548548
unwind,
549-
)
549+
)? {
550+
assert!(!self.tcx.intrinsic(fallback.def_id()).unwrap().must_be_overridden);
551+
assert!(matches!(fallback.def, ty::InstanceDef::Item(_)));
552+
return self.eval_fn_call(
553+
FnVal::Instance(fallback),
554+
(caller_abi, caller_fn_abi),
555+
args,
556+
with_caller_location,
557+
destination,
558+
target,
559+
unwind,
560+
);
561+
} else {
562+
Ok(())
563+
}
550564
}
551565
ty::InstanceDef::VTableShim(..)
552566
| ty::InstanceDef::ReifyShim(..)

src/tools/miri/src/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
987987
dest: &MPlaceTy<'tcx, Provenance>,
988988
ret: Option<mir::BasicBlock>,
989989
unwind: mir::UnwindAction,
990-
) -> InterpResult<'tcx> {
990+
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
991991
ecx.call_intrinsic(instance, args, dest, ret, unwind)
992992
}
993993

src/tools/miri/src/shims/intrinsics/atomic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
1919
intrinsic_name: &str,
2020
args: &[OpTy<'tcx, Provenance>],
2121
dest: &MPlaceTy<'tcx, Provenance>,
22-
) -> InterpResult<'tcx> {
22+
) -> InterpResult<'tcx, bool> {
2323
let this = self.eval_context_mut();
2424

2525
let intrinsic_structure: Vec<_> = intrinsic_name.split('_').collect();
@@ -113,9 +113,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
113113
this.atomic_rmw_op(args, dest, AtomicOp::Max, rw_ord(ord)?)?;
114114
}
115115

116-
_ => throw_unsup_format!("unimplemented intrinsic: `atomic_{intrinsic_name}`"),
116+
_ => return Ok(false),
117117
}
118-
Ok(())
118+
Ok(true)
119119
}
120120
}
121121

src/tools/miri/src/shims/intrinsics/mod.rs

+19-26
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
2626
dest: &MPlaceTy<'tcx, Provenance>,
2727
ret: Option<mir::BasicBlock>,
2828
_unwind: mir::UnwindAction,
29-
) -> InterpResult<'tcx> {
29+
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
3030
let this = self.eval_context_mut();
3131

3232
// See if the core engine can handle this intrinsic.
3333
if this.emulate_intrinsic(instance, args, dest, ret)? {
34-
return Ok(());
34+
return Ok(None);
3535
}
3636
let intrinsic_name = this.tcx.item_name(instance.def_id());
3737
let intrinsic_name = intrinsic_name.as_str();
@@ -54,16 +54,27 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
5454

5555
// Some intrinsics are special and need the "ret".
5656
match intrinsic_name {
57-
"catch_unwind" => return this.handle_catch_unwind(args, dest, ret),
57+
"catch_unwind" => {
58+
this.handle_catch_unwind(args, dest, ret)?;
59+
return Ok(None);
60+
}
5861
_ => {}
5962
}
6063

6164
// The rest jumps to `ret` immediately.
62-
this.emulate_intrinsic_by_name(intrinsic_name, instance.args, args, dest)?;
65+
if !this.emulate_intrinsic_by_name(intrinsic_name, instance.args, args, dest)? {
66+
if this.tcx.intrinsic(instance.def_id()).unwrap().must_be_overridden {
67+
throw_unsup_format!("unimplemented intrinsic: `{intrinsic_name}`")
68+
}
69+
return Ok(Some(ty::Instance {
70+
def: ty::InstanceDef::Item(instance.def_id()),
71+
args: instance.args,
72+
}))
73+
}
6374

6475
trace!("{:?}", this.dump_place(&dest.clone().into()));
6576
this.go_to_block(ret);
66-
Ok(())
77+
Ok(None)
6778
}
6879

6980
/// Emulates a Miri-supported intrinsic (not supported by the core engine).
@@ -73,7 +84,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
7384
generic_args: ty::GenericArgsRef<'tcx>,
7485
args: &[OpTy<'tcx, Provenance>],
7586
dest: &MPlaceTy<'tcx, Provenance>,
76-
) -> InterpResult<'tcx> {
87+
) -> InterpResult<'tcx, bool> {
7788
let this = self.eval_context_mut();
7889

7990
if let Some(name) = intrinsic_name.strip_prefix("atomic_") {
@@ -84,24 +95,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
8495
}
8596

8697
match intrinsic_name {
87-
// Miri overwriting CTFE intrinsics.
88-
"ptr_guaranteed_cmp" => {
89-
let [left, right] = check_arg_count(args)?;
90-
let left = this.read_immediate(left)?;
91-
let right = this.read_immediate(right)?;
92-
let val = this.wrapping_binary_op(mir::BinOp::Eq, &left, &right)?;
93-
// We're type punning a bool as an u8 here.
94-
this.write_scalar(val.to_scalar(), dest)?;
95-
}
96-
"const_allocate" => {
97-
// For now, for compatibility with the run-time implementation of this, we just return null.
98-
// See <https://github.com/rust-lang/rust/issues/93935>.
99-
this.write_null(dest)?;
100-
}
101-
"const_deallocate" => {
102-
// complete NOP
103-
}
104-
10598
// Raw memory accesses
10699
"volatile_load" => {
107100
let [place] = check_arg_count(args)?;
@@ -425,9 +418,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
425418
throw_machine_stop!(TerminationInfo::Abort(format!("trace/breakpoint trap")))
426419
}
427420

428-
name => throw_unsup_format!("unimplemented intrinsic: `{name}`"),
421+
_ => return Ok(false),
429422
}
430423

431-
Ok(())
424+
Ok(true)
432425
}
433426
}

src/tools/miri/src/shims/intrinsics/simd.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
2222
generic_args: ty::GenericArgsRef<'tcx>,
2323
args: &[OpTy<'tcx, Provenance>],
2424
dest: &MPlaceTy<'tcx, Provenance>,
25-
) -> InterpResult<'tcx> {
25+
) -> InterpResult<'tcx, bool> {
2626
let this = self.eval_context_mut();
2727
match intrinsic_name {
2828
#[rustfmt::skip]
@@ -743,9 +743,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
743743
}
744744
}
745745

746-
name => throw_unsup_format!("unimplemented intrinsic: `simd_{name}`"),
746+
_ => return Ok(false),
747747
}
748-
Ok(())
748+
Ok(true)
749749
}
750750

751751
fn fminmax_op(

0 commit comments

Comments
 (0)