Skip to content

Commit 5a8ca9a

Browse files
committed
Miri function identity hack: account for possible inlining
1 parent f13f37f commit 5a8ca9a

File tree

1 file changed

+12
-6
lines changed
  • compiler/rustc_middle/src/mir/interpret

1 file changed

+12
-6
lines changed

compiler/rustc_middle/src/mir/interpret/mod.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ use std::num::NonZero;
126126
use std::sync::atomic::{AtomicU32, Ordering};
127127

128128
use rustc_ast::LitKind;
129+
use rustc_attr::InlineAttr;
129130
use rustc_data_structures::fx::FxHashMap;
130131
use rustc_data_structures::sync::{HashMapExt, Lock};
131132
use rustc_data_structures::tiny_list::TinyList;
@@ -555,16 +556,21 @@ impl<'tcx> TyCtxt<'tcx> {
555556
pub fn reserve_and_set_fn_alloc(self, instance: Instance<'tcx>) -> AllocId {
556557
// Functions cannot be identified by pointers, as asm-equal functions can get deduplicated
557558
// by the linker (we set the "unnamed_addr" attribute for LLVM) and functions can be
558-
// duplicated across crates.
559-
// We thus generate a new `AllocId` for every mention of a function. This means that
560-
// `main as fn() == main as fn()` is false, while `let x = main as fn(); x == x` is true.
561-
// However, formatting code relies on function identity (see #58320), so we only do
562-
// this for generic functions. Lifetime parameters are ignored.
559+
// duplicated across crates. We thus generate a new `AllocId` for every mention of a
560+
// function. This means that `main as fn() == main as fn()` is false, while `let x = main as
561+
// fn(); x == x` is true. However, formatting code relies on function identity (see #58320)
562+
// -- that's likely broken, but for now we have to support it to make Miri work. So we
563+
// identify whether codegen will actually emit duplicate functions. It does that when they
564+
// have non-lifetime generics, or when they can be inlined.
563565
let is_generic = instance
564566
.args
565567
.into_iter()
566568
.any(|kind| !matches!(kind.unpack(), GenericArgKind::Lifetime(_)));
567-
if is_generic {
569+
let can_be_inlined = match self.codegen_fn_attrs(instance.def_id()).inline {
570+
InlineAttr::Never => false,
571+
_ => true,
572+
};
573+
if is_generic || can_be_inlined {
568574
// Get a fresh ID.
569575
let mut alloc_map = self.alloc_map.lock();
570576
let id = alloc_map.reserve();

0 commit comments

Comments
 (0)