Skip to content

Commit 6280a2f

Browse files
committed
Change __rust_no_alloc_shim_is_unstable to be a function
1 parent d163a28 commit 6280a2f

File tree

14 files changed

+178
-134
lines changed

14 files changed

+178
-134
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,7 @@ jobs:
225225
fi
226226
exit ${STATUS}
227227
env:
228-
AWS_ACCESS_KEY_ID: ${{ env.CACHES_AWS_ACCESS_KEY_ID }}
229-
AWS_SECRET_ACCESS_KEY: ${{ secrets[format('AWS_SECRET_ACCESS_KEY_{0}', env.CACHES_AWS_ACCESS_KEY_ID)] }}
228+
SCCACHE_S3_NO_CREDENTIALS: 1
230229

231230
- name: create github artifacts
232231
run: src/ci/scripts/create-doc-artifacts.sh

compiler/rustc_codegen_cranelift/src/allocator.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Allocator shim
22
// Adapted from rustc
33

4+
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
45
use rustc_ast::expand::allocator::{
56
ALLOCATOR_METHODS, AllocatorKind, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE,
67
alloc_error_handler_name, default_fn_name, global_fn_name,
@@ -97,16 +98,31 @@ fn codegen_inner(
9798
data.define(Box::new([val]));
9899
module.define_data(data_id, &data).unwrap();
99100

100-
let data_id = module
101-
.declare_data(
102-
&mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
103-
Linkage::Export,
104-
false,
105-
false,
106-
)
107-
.unwrap();
108-
let mut data = DataDescription::new();
109-
data.set_align(1);
110-
data.define(Box::new([0]));
111-
module.define_data(data_id, &data).unwrap();
101+
{
102+
let sig = Signature {
103+
call_conv: module.target_config().default_call_conv,
104+
params: vec![],
105+
returns: vec![],
106+
};
107+
let func_id = module
108+
.declare_function(
109+
&mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
110+
Linkage::Export,
111+
&sig,
112+
)
113+
.unwrap();
114+
115+
let mut ctx = Context::new();
116+
ctx.func.signature = sig;
117+
let mut func_ctx = FunctionBuilderContext::new();
118+
let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
119+
120+
let block = bcx.create_block();
121+
bcx.switch_to_block(block);
122+
bcx.ins().return_(&[]);
123+
bcx.seal_all_blocks();
124+
bcx.finalize();
125+
126+
module.define_function(func_id, &mut ctx).unwrap();
127+
}
112128
}

compiler/rustc_codegen_gcc/src/allocator.rs

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub(crate) unsafe fn codegen(
5757
let from_name = mangle_internal_symbol(tcx, &global_fn_name(method.name));
5858
let to_name = mangle_internal_symbol(tcx, &default_fn_name(method.name));
5959

60-
create_wrapper_function(tcx, context, &from_name, &to_name, &types, output);
60+
create_wrapper_function(tcx, context, &from_name, Some(&to_name), &types, output);
6161
}
6262
}
6363

@@ -66,7 +66,7 @@ pub(crate) unsafe fn codegen(
6666
tcx,
6767
context,
6868
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
69-
&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind)),
69+
Some(&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind))),
7070
&[usize, usize],
7171
None,
7272
);
@@ -81,21 +81,21 @@ pub(crate) unsafe fn codegen(
8181
let value = context.new_rvalue_from_int(i8, value as i32);
8282
global.global_set_initializer_rvalue(value);
8383

84-
let name = mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE);
85-
let global = context.new_global(None, GlobalKind::Exported, i8, name);
86-
#[cfg(feature = "master")]
87-
global.add_attribute(VarAttribute::Visibility(symbol_visibility_to_gcc(
88-
tcx.sess.default_visibility(),
89-
)));
90-
let value = context.new_rvalue_from_int(i8, 0);
91-
global.global_set_initializer_rvalue(value);
84+
create_wrapper_function(
85+
tcx,
86+
context,
87+
&mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
88+
None,
89+
&[],
90+
None,
91+
);
9292
}
9393

9494
fn create_wrapper_function(
9595
tcx: TyCtxt<'_>,
9696
context: &Context<'_>,
9797
from_name: &str,
98-
to_name: &str,
98+
to_name: Option<&str>,
9999
types: &[Type<'_>],
100100
output: Option<Type<'_>>,
101101
) {
@@ -124,34 +124,39 @@ fn create_wrapper_function(
124124
// TODO(antoyo): emit unwind tables.
125125
}
126126

127-
let args: Vec<_> = types
128-
.iter()
129-
.enumerate()
130-
.map(|(index, typ)| context.new_parameter(None, *typ, format!("param{}", index)))
131-
.collect();
132-
let callee = context.new_function(
133-
None,
134-
FunctionType::Extern,
135-
output.unwrap_or(void),
136-
&args,
137-
to_name,
138-
false,
139-
);
140-
#[cfg(feature = "master")]
141-
callee.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
142-
143127
let block = func.new_block("entry");
144128

145-
let args = args
146-
.iter()
147-
.enumerate()
148-
.map(|(i, _)| func.get_param(i as i32).to_rvalue())
149-
.collect::<Vec<_>>();
150-
let ret = context.new_call(None, callee, &args);
151-
//llvm::LLVMSetTailCall(ret, True);
152-
if output.is_some() {
153-
block.end_with_return(None, ret);
129+
if let Some(to_name) = to_name {
130+
let args: Vec<_> = types
131+
.iter()
132+
.enumerate()
133+
.map(|(index, typ)| context.new_parameter(None, *typ, format!("param{}", index)))
134+
.collect();
135+
let callee = context.new_function(
136+
None,
137+
FunctionType::Extern,
138+
output.unwrap_or(void),
139+
&args,
140+
to_name,
141+
false,
142+
);
143+
#[cfg(feature = "master")]
144+
callee.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
145+
146+
let args = args
147+
.iter()
148+
.enumerate()
149+
.map(|(i, _)| func.get_param(i as i32).to_rvalue())
150+
.collect::<Vec<_>>();
151+
let ret = context.new_call(None, callee, &args);
152+
//llvm::LLVMSetTailCall(ret, True);
153+
if output.is_some() {
154+
block.end_with_return(None, ret);
155+
} else {
156+
block.end_with_void_return(None);
157+
}
154158
} else {
159+
assert!(output.is_none());
155160
block.end_with_void_return(None);
156161
}
157162

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub(crate) unsafe fn codegen(
5757
let from_name = mangle_internal_symbol(tcx, &global_fn_name(method.name));
5858
let to_name = mangle_internal_symbol(tcx, &default_fn_name(method.name));
5959

60-
create_wrapper_function(tcx, &cx, &from_name, &to_name, &args, output, false);
60+
create_wrapper_function(tcx, &cx, &from_name, Some(&to_name), &args, output, false);
6161
}
6262
}
6363

@@ -66,7 +66,7 @@ pub(crate) unsafe fn codegen(
6666
tcx,
6767
&cx,
6868
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
69-
&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind)),
69+
Some(&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind))),
7070
&[usize, usize], // size, align
7171
None,
7272
true,
@@ -81,11 +81,16 @@ pub(crate) unsafe fn codegen(
8181
let llval = llvm::LLVMConstInt(i8, val as u64, False);
8282
llvm::set_initializer(ll_g, llval);
8383

84-
let name = mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE);
85-
let ll_g = cx.declare_global(&name, i8);
86-
llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
87-
let llval = llvm::LLVMConstInt(i8, 0, False);
88-
llvm::set_initializer(ll_g, llval);
84+
// __rust_no_alloc_shim_is_unstable
85+
create_wrapper_function(
86+
tcx,
87+
&cx,
88+
&mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
89+
None,
90+
&[],
91+
None,
92+
false,
93+
);
8994
}
9095

9196
if tcx.sess.opts.debuginfo != DebugInfo::None {
@@ -99,7 +104,7 @@ fn create_wrapper_function(
99104
tcx: TyCtxt<'_>,
100105
cx: &SimpleCx<'_>,
101106
from_name: &str,
102-
to_name: &str,
107+
to_name: Option<&str>,
103108
args: &[&Type],
104109
output: Option<&Type>,
105110
no_return: bool,
@@ -128,33 +133,38 @@ fn create_wrapper_function(
128133
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[uwtable]);
129134
}
130135

131-
let callee = declare_simple_fn(
132-
&cx,
133-
to_name,
134-
llvm::CallConv::CCallConv,
135-
llvm::UnnamedAddr::Global,
136-
llvm::Visibility::Hidden,
137-
ty,
138-
);
139-
if let Some(no_return) = no_return {
140-
// -> ! DIFlagNoReturn
141-
attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
142-
}
143-
llvm::set_visibility(callee, llvm::Visibility::Hidden);
144-
145136
let llbb = unsafe { llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, c"entry".as_ptr()) };
146-
147137
let mut bx = SBuilder::build(&cx, llbb);
148-
let args = args
149-
.iter()
150-
.enumerate()
151-
.map(|(i, _)| llvm::get_param(llfn, i as c_uint))
152-
.collect::<Vec<_>>();
153-
let ret = bx.call(ty, callee, &args, None);
154-
llvm::LLVMSetTailCall(ret, True);
155-
if output.is_some() {
156-
bx.ret(ret);
138+
139+
if let Some(to_name) = to_name {
140+
let callee = declare_simple_fn(
141+
&cx,
142+
to_name,
143+
llvm::CallConv::CCallConv,
144+
llvm::UnnamedAddr::Global,
145+
llvm::Visibility::Hidden,
146+
ty,
147+
);
148+
if let Some(no_return) = no_return {
149+
// -> ! DIFlagNoReturn
150+
attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
151+
}
152+
llvm::set_visibility(callee, llvm::Visibility::Hidden);
153+
154+
let args = args
155+
.iter()
156+
.enumerate()
157+
.map(|(i, _)| llvm::get_param(llfn, i as c_uint))
158+
.collect::<Vec<_>>();
159+
let ret = bx.call(ty, callee, &args, None);
160+
llvm::LLVMSetTailCall(ret, True);
161+
if output.is_some() {
162+
bx.ret(ret);
163+
} else {
164+
bx.ret_void()
165+
}
157166
} else {
167+
assert!(output.is_none());
158168
bx.ret_void()
159169
}
160170
}

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,7 @@ impl<'ll> CodegenCx<'ll, '_> {
364364

365365
if !def_id.is_local() {
366366
let needs_dll_storage_attr = self.use_dll_storage_attrs
367-
// If the symbol is a foreign item, then don't automatically apply DLLImport, as
368-
// we'll rely on the #[link] attribute instead. BUT, if this is an internal symbol
369-
// then it may be generated by the compiler in some crate, so we do need to apply
370-
// DLLImport when linking with the MSVC linker.
371-
&& (!self.tcx.is_foreign_item(def_id)
372-
|| (self.sess().target.is_like_msvc && fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)))
367+
&& !self.tcx.is_foreign_item(def_id)
373368
// Local definitions can never be imported, so we must not apply
374369
// the DLLImport annotation.
375370
&& !dso_local

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ fn exported_symbols_provider_local<'tcx>(
219219
.chain([
220220
mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
221221
mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
222+
mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
222223
])
223224
{
224225
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
@@ -232,19 +233,6 @@ fn exported_symbols_provider_local<'tcx>(
232233
},
233234
));
234235
}
235-
236-
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(
237-
tcx,
238-
&mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
239-
));
240-
symbols.push((
241-
exported_symbol,
242-
SymbolExportInfo {
243-
level: SymbolExportLevel::Rust,
244-
kind: SymbolExportKind::Data,
245-
used: false,
246-
},
247-
))
248236
}
249237

250238
if tcx.sess.instrument_coverage() || tcx.sess.opts.cg.profile_generate.enabled() {

compiler/rustc_target/src/spec/base/msvc.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,7 @@ use crate::spec::{BinaryFormat, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo
55
pub(crate) fn opts() -> TargetOptions {
66
// Suppress the verbose logo and authorship debugging output, which would needlessly
77
// clog any log files.
8-
let pre_link_args = TargetOptions::link_args(
9-
LinkerFlavor::Msvc(Lld::No),
10-
&[
11-
"/NOLOGO",
12-
// "Symbol is marked as dllimport, but defined in an object file"
13-
// Harmless warning that flags a potential performance improvement: marking a symbol as
14-
// dllimport indirects usage via the `__imp_` symbol, which isn't required if the symbol
15-
// is in the current binary. This is tripped by __rust_no_alloc_shim_is_unstable as it
16-
// is generated by the compiler, but marked as a foreign item (hence the dllimport) in
17-
// the standard library.
18-
"/IGNORE:4286",
19-
],
20-
);
8+
let pre_link_args = TargetOptions::link_args(LinkerFlavor::Msvc(Lld::No), &["/NOLOGO"]);
219

2210
TargetOptions {
2311
linker_flavor: LinkerFlavor::Msvc(Lld::No),

library/alloc/src/alloc.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ unsafe extern "Rust" {
3131
#[rustc_std_internal_symbol]
3232
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
3333

34+
#[rustc_nounwind]
35+
#[rustc_std_internal_symbol]
36+
#[cfg(not(bootstrap))]
37+
fn __rust_no_alloc_shim_is_unstable();
3438
#[rustc_std_internal_symbol]
39+
#[cfg(bootstrap)]
3540
static __rust_no_alloc_shim_is_unstable: u8;
41+
3642
}
3743

3844
/// The global memory allocator.
@@ -88,7 +94,10 @@ pub unsafe fn alloc(layout: Layout) -> *mut u8 {
8894
unsafe {
8995
// Make sure we don't accidentally allow omitting the allocator shim in
9096
// stable code until it is actually stabilized.
97+
#[cfg(bootstrap)]
9198
core::ptr::read_volatile(&__rust_no_alloc_shim_is_unstable);
99+
#[cfg(not(bootstrap))]
100+
__rust_no_alloc_shim_is_unstable();
92101

93102
__rust_alloc(layout.size(), layout.align())
94103
}
@@ -171,7 +180,10 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
171180
unsafe {
172181
// Make sure we don't accidentally allow omitting the allocator shim in
173182
// stable code until it is actually stabilized.
183+
#[cfg(bootstrap)]
174184
core::ptr::read_volatile(&__rust_no_alloc_shim_is_unstable);
185+
#[cfg(not(bootstrap))]
186+
__rust_no_alloc_shim_is_unstable();
175187

176188
__rust_alloc_zeroed(layout.size(), layout.align())
177189
}

0 commit comments

Comments
 (0)