Skip to content

Commit 7ed06e5

Browse files
committed
Sync from rust 45acf54
2 parents b7cfe2f + e391603 commit 7ed06e5

File tree

3 files changed

+82
-108
lines changed

3 files changed

+82
-108
lines changed

src/abi/pass_mode.rs

Lines changed: 79 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Argument passing
22
3-
use cranelift_codegen::ir::{ArgumentExtension, ArgumentPurpose};
3+
use cranelift_codegen::ir::ArgumentPurpose;
44
use rustc_abi::{Reg, RegKind};
55
use rustc_target::callconv::{
66
ArgAbi, ArgAttributes, ArgExtension as RustcArgExtension, CastTarget, PassMode,
@@ -32,16 +32,26 @@ fn reg_to_abi_param(reg: Reg) -> AbiParam {
3232
AbiParam::new(clif_ty)
3333
}
3434

35-
fn apply_arg_attrs_to_abi_param(mut param: AbiParam, arg_attrs: ArgAttributes) -> AbiParam {
35+
fn apply_attrs_to_abi_param(param: AbiParam, arg_attrs: ArgAttributes) -> AbiParam {
3636
match arg_attrs.arg_ext {
37-
RustcArgExtension::None => {}
38-
RustcArgExtension::Zext => param.extension = ArgumentExtension::Uext,
39-
RustcArgExtension::Sext => param.extension = ArgumentExtension::Sext,
37+
RustcArgExtension::None => param,
38+
RustcArgExtension::Zext => param.uext(),
39+
RustcArgExtension::Sext => param.sext(),
4040
}
41-
param
4241
}
4342

44-
fn cast_target_to_abi_params(cast: &CastTarget) -> SmallVec<[AbiParam; 2]> {
43+
fn cast_target_to_abi_params(cast: &CastTarget) -> SmallVec<[(Size, AbiParam); 2]> {
44+
if let Some(offset_from_start) = cast.rest_offset {
45+
assert!(cast.prefix[1..].iter().all(|p| p.is_none()));
46+
assert_eq!(cast.rest.unit.size, cast.rest.total);
47+
let first = cast.prefix[0].unwrap();
48+
let second = cast.rest.unit;
49+
return smallvec![
50+
(Size::ZERO, reg_to_abi_param(first)),
51+
(offset_from_start, reg_to_abi_param(second))
52+
];
53+
}
54+
4555
let (rest_count, rem_bytes) = if cast.rest.unit.size.bytes() == 0 {
4656
(0, 0)
4757
} else {
@@ -56,33 +66,40 @@ fn cast_target_to_abi_params(cast: &CastTarget) -> SmallVec<[AbiParam; 2]> {
5666
// different types in Cranelift IR. Instead a single array of primitive types is used.
5767

5868
// Create list of fields in the main structure
59-
let mut args = cast
69+
let args = cast
6070
.prefix
6171
.iter()
6272
.flatten()
6373
.map(|&reg| reg_to_abi_param(reg))
64-
.chain((0..rest_count).map(|_| reg_to_abi_param(cast.rest.unit)))
65-
.collect::<SmallVec<_>>();
74+
.chain((0..rest_count).map(|_| reg_to_abi_param(cast.rest.unit)));
75+
76+
let mut res = SmallVec::new();
77+
let mut offset = Size::ZERO;
78+
79+
for arg in args {
80+
res.push((offset, arg));
81+
offset += Size::from_bytes(arg.value_type.bytes());
82+
}
6683

6784
// Append final integer
6885
if rem_bytes != 0 {
6986
// Only integers can be really split further.
7087
assert_eq!(cast.rest.unit.kind, RegKind::Integer);
71-
args.push(reg_to_abi_param(Reg {
72-
kind: RegKind::Integer,
73-
size: Size::from_bytes(rem_bytes),
74-
}));
88+
res.push((
89+
offset,
90+
reg_to_abi_param(Reg { kind: RegKind::Integer, size: Size::from_bytes(rem_bytes) }),
91+
));
7592
}
7693

77-
args
94+
res
7895
}
7996

8097
impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
8198
fn get_abi_param(&self, tcx: TyCtxt<'tcx>) -> SmallVec<[AbiParam; 2]> {
8299
match self.mode {
83100
PassMode::Ignore => smallvec![],
84101
PassMode::Direct(attrs) => match self.layout.backend_repr {
85-
BackendRepr::Scalar(scalar) => smallvec![apply_arg_attrs_to_abi_param(
102+
BackendRepr::Scalar(scalar) => smallvec![apply_attrs_to_abi_param(
86103
AbiParam::new(scalar_to_clif_type(tcx, scalar)),
87104
attrs
88105
)],
@@ -97,34 +114,34 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
97114
let a = scalar_to_clif_type(tcx, a);
98115
let b = scalar_to_clif_type(tcx, b);
99116
smallvec![
100-
apply_arg_attrs_to_abi_param(AbiParam::new(a), attrs_a),
101-
apply_arg_attrs_to_abi_param(AbiParam::new(b), attrs_b),
117+
apply_attrs_to_abi_param(AbiParam::new(a), attrs_a),
118+
apply_attrs_to_abi_param(AbiParam::new(b), attrs_b),
102119
]
103120
}
104121
_ => unreachable!("{:?}", self.layout.backend_repr),
105122
},
106123
PassMode::Cast { ref cast, pad_i32 } => {
107124
assert!(!pad_i32, "padding support not yet implemented");
108-
cast_target_to_abi_params(cast)
125+
cast_target_to_abi_params(cast).into_iter().map(|(_, param)| param).collect()
109126
}
110127
PassMode::Indirect { attrs, meta_attrs: None, on_stack } => {
111128
if on_stack {
112129
// Abi requires aligning struct size to pointer size
113130
let size = self.layout.size.align_to(tcx.data_layout.pointer_align.abi);
114131
let size = u32::try_from(size.bytes()).unwrap();
115-
smallvec![apply_arg_attrs_to_abi_param(
132+
smallvec![apply_attrs_to_abi_param(
116133
AbiParam::special(pointer_ty(tcx), ArgumentPurpose::StructArgument(size),),
117134
attrs
118135
)]
119136
} else {
120-
smallvec![apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), attrs)]
137+
smallvec![apply_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), attrs)]
121138
}
122139
}
123140
PassMode::Indirect { attrs, meta_attrs: Some(meta_attrs), on_stack } => {
124141
assert!(!on_stack);
125142
smallvec![
126-
apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), attrs),
127-
apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), meta_attrs),
143+
apply_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), attrs),
144+
apply_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), meta_attrs),
128145
]
129146
}
130147
}
@@ -133,30 +150,47 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
133150
fn get_abi_return(&self, tcx: TyCtxt<'tcx>) -> (Option<AbiParam>, Vec<AbiParam>) {
134151
match self.mode {
135152
PassMode::Ignore => (None, vec![]),
136-
PassMode::Direct(_) => match self.layout.backend_repr {
137-
BackendRepr::Scalar(scalar) => {
138-
(None, vec![AbiParam::new(scalar_to_clif_type(tcx, scalar))])
139-
}
153+
PassMode::Direct(attrs) => match self.layout.backend_repr {
154+
BackendRepr::Scalar(scalar) => (
155+
None,
156+
vec![apply_attrs_to_abi_param(
157+
AbiParam::new(scalar_to_clif_type(tcx, scalar)),
158+
attrs,
159+
)],
160+
),
140161
BackendRepr::SimdVector { .. } => {
141162
let vector_ty = crate::intrinsics::clif_vector_type(tcx, self.layout);
142-
(None, vec![AbiParam::new(vector_ty)])
163+
(None, vec![apply_attrs_to_abi_param(AbiParam::new(vector_ty), attrs)])
143164
}
144165
_ => unreachable!("{:?}", self.layout.backend_repr),
145166
},
146-
PassMode::Pair(_, _) => match self.layout.backend_repr {
167+
PassMode::Pair(attrs_a, attrs_b) => match self.layout.backend_repr {
147168
BackendRepr::ScalarPair(a, b) => {
148169
let a = scalar_to_clif_type(tcx, a);
149170
let b = scalar_to_clif_type(tcx, b);
150-
(None, vec![AbiParam::new(a), AbiParam::new(b)])
171+
(
172+
None,
173+
vec![
174+
apply_attrs_to_abi_param(AbiParam::new(a), attrs_a),
175+
apply_attrs_to_abi_param(AbiParam::new(b), attrs_b),
176+
],
177+
)
151178
}
152179
_ => unreachable!("{:?}", self.layout.backend_repr),
153180
},
154-
PassMode::Cast { ref cast, .. } => {
155-
(None, cast_target_to_abi_params(cast).into_iter().collect())
156-
}
157-
PassMode::Indirect { attrs: _, meta_attrs: None, on_stack } => {
181+
PassMode::Cast { ref cast, .. } => (
182+
None,
183+
cast_target_to_abi_params(cast).into_iter().map(|(_, param)| param).collect(),
184+
),
185+
PassMode::Indirect { attrs, meta_attrs: None, on_stack } => {
158186
assert!(!on_stack);
159-
(Some(AbiParam::special(pointer_ty(tcx), ArgumentPurpose::StructReturn)), vec![])
187+
(
188+
Some(apply_attrs_to_abi_param(
189+
AbiParam::special(pointer_ty(tcx), ArgumentPurpose::StructReturn),
190+
attrs,
191+
)),
192+
vec![],
193+
)
160194
}
161195
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
162196
unreachable!("unsized return value")
@@ -172,12 +206,14 @@ pub(super) fn to_casted_value<'tcx>(
172206
) -> SmallVec<[Value; 2]> {
173207
let (ptr, meta) = arg.force_stack(fx);
174208
assert!(meta.is_none());
175-
let mut offset = 0;
176209
cast_target_to_abi_params(cast)
177210
.into_iter()
178-
.map(|param| {
179-
let val = ptr.offset_i64(fx, offset).load(fx, param.value_type, MemFlags::new());
180-
offset += i64::from(param.value_type.bytes());
211+
.map(|(offset, param)| {
212+
let val = ptr.offset_i64(fx, offset.bytes() as i64).load(
213+
fx,
214+
param.value_type,
215+
MemFlags::new(),
216+
);
181217
val
182218
})
183219
.collect()
@@ -190,7 +226,7 @@ pub(super) fn from_casted_value<'tcx>(
190226
cast: &CastTarget,
191227
) -> CValue<'tcx> {
192228
let abi_params = cast_target_to_abi_params(cast);
193-
let abi_param_size: u32 = abi_params.iter().map(|param| param.value_type.bytes()).sum();
229+
let abi_param_size: u32 = abi_params.iter().map(|(_, param)| param.value_type.bytes()).sum();
194230
let layout_size = u32::try_from(layout.size.bytes()).unwrap();
195231
let ptr = fx.create_stack_slot(
196232
// Stack slot size may be bigger for example `[u8; 3]` which is packed into an `i32`.
@@ -199,16 +235,13 @@ pub(super) fn from_casted_value<'tcx>(
199235
std::cmp::max(abi_param_size, layout_size),
200236
u32::try_from(layout.align.abi.bytes()).unwrap(),
201237
);
202-
let mut offset = 0;
203238
let mut block_params_iter = block_params.iter().copied();
204-
for param in abi_params {
205-
let val = ptr.offset_i64(fx, offset).store(
239+
for (offset, _) in abi_params {
240+
ptr.offset_i64(fx, offset.bytes() as i64).store(
206241
fx,
207242
block_params_iter.next().unwrap(),
208243
MemFlags::new(),
209-
);
210-
offset += i64::from(param.value_type.bytes());
211-
val
244+
)
212245
}
213246
assert_eq!(block_params_iter.next(), None, "Leftover block param");
214247
CValue::by_ref(ptr, layout)

src/driver/aot.rs

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ use std::thread::JoinHandle;
1111
use cranelift_object::{ObjectBuilder, ObjectModule};
1212
use rustc_codegen_ssa::assert_module_sources::CguReuse;
1313
use rustc_codegen_ssa::back::link::ensure_removed;
14-
use rustc_codegen_ssa::back::metadata::create_compressed_metadata_file;
1514
use rustc_codegen_ssa::base::determine_cgu_reuse;
1615
use rustc_codegen_ssa::{
1716
CodegenResults, CompiledModule, CrateInfo, ModuleKind, errors as ssa_errors,
1817
};
1918
use rustc_data_structures::profiling::SelfProfilerRef;
2019
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2120
use rustc_data_structures::sync::{IntoDynSyncSend, par_map};
22-
use rustc_metadata::EncodedMetadata;
2321
use rustc_metadata::fs::copy_to_stdout;
2422
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
2523
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
@@ -61,8 +59,6 @@ impl<HCX> HashStable<HCX> for OngoingModuleCodegen {
6159
pub(crate) struct OngoingCodegen {
6260
modules: Vec<OngoingModuleCodegen>,
6361
allocator_module: Option<CompiledModule>,
64-
metadata_module: Option<CompiledModule>,
65-
metadata: EncodedMetadata,
6662
crate_info: CrateInfo,
6763
concurrency_limiter: ConcurrencyLimiter,
6864
}
@@ -134,8 +130,6 @@ impl OngoingCodegen {
134130
let codegen_results = CodegenResults {
135131
modules,
136132
allocator_module: self.allocator_module,
137-
metadata_module: self.metadata_module,
138-
metadata: self.metadata,
139133
crate_info: self.crate_info,
140134
};
141135

@@ -646,42 +640,6 @@ fn module_codegen(
646640
}))
647641
}
648642

649-
fn emit_metadata_module(tcx: TyCtxt<'_>, metadata: &EncodedMetadata) -> CompiledModule {
650-
use rustc_middle::mir::mono::CodegenUnitNameBuilder;
651-
652-
let _timer = tcx.sess.timer("write compressed metadata");
653-
654-
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
655-
let metadata_cgu_name = cgu_name_builder
656-
.build_cgu_name(LOCAL_CRATE, ["crate"], Some("metadata"))
657-
.as_str()
658-
.to_string();
659-
660-
let tmp_file = tcx.output_filenames(()).temp_path_for_cgu(
661-
OutputType::Metadata,
662-
&metadata_cgu_name,
663-
tcx.sess.invocation_temp.as_deref(),
664-
);
665-
666-
let symbol_name = rustc_middle::middle::exported_symbols::metadata_symbol_name(tcx);
667-
let obj = create_compressed_metadata_file(tcx.sess, metadata, &symbol_name);
668-
669-
if let Err(err) = std::fs::write(&tmp_file, obj) {
670-
tcx.dcx().fatal(format!("error writing metadata object file: {}", err));
671-
}
672-
673-
CompiledModule {
674-
name: metadata_cgu_name,
675-
kind: ModuleKind::Metadata,
676-
object: Some(tmp_file),
677-
dwarf_object: None,
678-
bytecode: None,
679-
assembly: None,
680-
llvm_ir: None,
681-
links_from_incr_cache: Vec::new(),
682-
}
683-
}
684-
685643
fn emit_allocator_module(tcx: TyCtxt<'_>) -> Option<CompiledModule> {
686644
let mut allocator_module = make_module(tcx.sess, "allocator_shim".to_string());
687645
let created_alloc_shim = crate::allocator::codegen(tcx, &mut allocator_module);
@@ -706,11 +664,7 @@ fn emit_allocator_module(tcx: TyCtxt<'_>) -> Option<CompiledModule> {
706664
}
707665
}
708666

709-
pub(crate) fn run_aot(
710-
tcx: TyCtxt<'_>,
711-
metadata: EncodedMetadata,
712-
need_metadata_module: bool,
713-
) -> Box<OngoingCodegen> {
667+
pub(crate) fn run_aot(tcx: TyCtxt<'_>) -> Box<OngoingCodegen> {
714668
// FIXME handle `-Ctarget-cpu=native`
715669
let target_cpu = match tcx.sess.opts.cg.target_cpu {
716670
Some(ref name) => name,
@@ -726,8 +680,6 @@ pub(crate) fn run_aot(
726680
return Box::new(OngoingCodegen {
727681
modules: vec![],
728682
allocator_module: None,
729-
metadata_module: None,
730-
metadata,
731683
crate_info: CrateInfo::new(tcx, target_cpu),
732684
concurrency_limiter: ConcurrencyLimiter::new(0),
733685
});
@@ -787,14 +739,9 @@ pub(crate) fn run_aot(
787739

788740
let allocator_module = emit_allocator_module(tcx);
789741

790-
let metadata_module =
791-
if need_metadata_module { Some(emit_metadata_module(tcx, &metadata)) } else { None };
792-
793742
Box::new(OngoingCodegen {
794743
modules,
795744
allocator_module,
796-
metadata_module,
797-
metadata,
798745
crate_info: CrateInfo::new(tcx, target_cpu),
799746
concurrency_limiter: concurrency_limiter.0,
800747
})

src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use cranelift_codegen::isa::TargetIsa;
4646
use cranelift_codegen::settings::{self, Configurable};
4747
use rustc_codegen_ssa::traits::CodegenBackend;
4848
use rustc_codegen_ssa::{CodegenResults, TargetConfig};
49-
use rustc_metadata::EncodedMetadata;
5049
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
5150
use rustc_session::Session;
5251
use rustc_session::config::OutputFilenames;
@@ -238,12 +237,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
238237
println!("Cranelift version: {}", cranelift_codegen::VERSION);
239238
}
240239

241-
fn codegen_crate(
242-
&self,
243-
tcx: TyCtxt<'_>,
244-
metadata: EncodedMetadata,
245-
need_metadata_module: bool,
246-
) -> Box<dyn Any> {
240+
fn codegen_crate(&self, tcx: TyCtxt<'_>) -> Box<dyn Any> {
247241
info!("codegen crate {}", tcx.crate_name(LOCAL_CRATE));
248242
let config = self.config.clone().unwrap_or_else(|| {
249243
BackendConfig::from_opts(&tcx.sess.opts.cg.llvm_args)
@@ -256,7 +250,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
256250
#[cfg(not(feature = "jit"))]
257251
tcx.dcx().fatal("jit support was disabled when compiling rustc_codegen_cranelift");
258252
} else {
259-
driver::aot::run_aot(tcx, metadata, need_metadata_module)
253+
driver::aot::run_aot(tcx)
260254
}
261255
}
262256

0 commit comments

Comments
 (0)