Skip to content

Commit 706a23e

Browse files
celinvaltedinski
authored andcommitted
Undo changes to rustc CodegenBackend trait (rust-lang#706)
The goal is to eventually consume rustc as is (issue rust-lang#42). In this change, we moved the json file generations out of the linker to the codegen method. The RMC behavior is still the same.
1 parent 65baaea commit 706a23e

File tree

5 files changed

+54
-60
lines changed

5 files changed

+54
-60
lines changed

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,24 +339,27 @@ impl CodegenBackend for LlvmCodegenBackend {
339339
&self,
340340
ongoing_codegen: Box<dyn Any>,
341341
sess: &Session,
342-
) -> Result<(Box<dyn Any>, FxHashMap<WorkProductId, WorkProduct>), ErrorReported> {
342+
) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorReported> {
343343
let (codegen_results, work_products) = ongoing_codegen
344344
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
345345
.expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
346346
.join(sess);
347-
Ok((Box::new(codegen_results), work_products))
347+
348+
sess.time("llvm_dump_timing_file", || {
349+
if sess.opts.debugging_opts.llvm_time_trace {
350+
llvm_util::time_trace_profiler_finish("llvm_timings.json");
351+
}
352+
});
353+
354+
Ok((codegen_results, work_products))
348355
}
349356

350357
fn link(
351358
&self,
352359
sess: &Session,
353-
codegen_results: Box<dyn Any>,
360+
codegen_results: CodegenResults,
354361
outputs: &OutputFilenames,
355362
) -> Result<(), ErrorReported> {
356-
let codegen_results = codegen_results
357-
.downcast::<CodegenResults>()
358-
.expect("Expected CodegenResults, found Box<Any>");
359-
360363
use crate::back::archive::LlvmArchiveBuilder;
361364
use rustc_codegen_ssa::back::link::link_binary;
362365

compiler/rustc_codegen_rmc/src/compiler_interface.rs

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use cbmc::goto_program::SymbolTable;
1111
use cbmc::InternedString;
1212
use rmc_restrictions::VtableCtxResults;
1313
use rustc_codegen_ssa::traits::CodegenBackend;
14+
use rustc_codegen_ssa::{CodegenResults, CrateInfo};
1415
use rustc_data_structures::fx::FxHashMap;
1516
use rustc_errors::ErrorReported;
1617
use rustc_metadata::EncodedMetadata;
@@ -28,15 +29,6 @@ use std::iter::FromIterator;
2829
use std::path::PathBuf;
2930
use tracing::{debug, warn};
3031

31-
// #[derive(RustcEncodable, RustcDecodable)]
32-
pub struct GotocCodegenResult {
33-
pub crate_name: rustc_span::Symbol,
34-
pub metadata: RmcMetadata,
35-
pub symtab: SymbolTable,
36-
pub type_map: BTreeMap<InternedString, InternedString>,
37-
pub vtable_restrictions: Option<VtableCtxResults>,
38-
}
39-
4032
#[derive(Clone)]
4133
pub struct GotocCodegenBackend();
4234

@@ -58,14 +50,12 @@ impl CodegenBackend for GotocCodegenBackend {
5850
fn codegen_crate<'tcx>(
5951
&self,
6052
tcx: TyCtxt<'tcx>,
61-
_metadata: EncodedMetadata,
62-
_need_metadata_module: bool,
53+
rustc_metadata: EncodedMetadata,
54+
need_metadata_module: bool,
6355
) -> Box<dyn Any> {
64-
use rustc_hir::def_id::LOCAL_CRATE;
65-
6656
super::utils::init();
6757

68-
check_options(&tcx.sess);
58+
check_options(&tcx.sess, need_metadata_module);
6959

7060
let codegen_units: &'tcx [CodegenUnit<'_>] = tcx.collect_and_partition_mono_items(()).1;
7161
let mut c = GotocCtx::new(tcx);
@@ -134,7 +124,7 @@ impl CodegenBackend for GotocCodegenBackend {
134124
);
135125

136126
// Map MIR types to GotoC types
137-
let type_map =
127+
let type_map: BTreeMap<InternedString, InternedString> =
138128
BTreeMap::from_iter(c.type_map.into_iter().map(|(k, v)| (k, v.to_string().into())));
139129

140130
// Get the vtable function pointer restrictions if requested
@@ -146,51 +136,53 @@ impl CodegenBackend for GotocCodegenBackend {
146136

147137
let metadata = RmcMetadata { proof_harnesses: c.proof_harnesses };
148138

149-
Box::new(GotocCodegenResult {
150-
crate_name: tcx.crate_name(LOCAL_CRATE) as rustc_span::Symbol,
151-
metadata,
152-
symtab,
153-
type_map,
154-
vtable_restrictions,
155-
})
139+
// No output should be generated if user selected no_codegen.
140+
if !tcx.sess.opts.debugging_opts.no_codegen && tcx.sess.opts.output_types.should_codegen() {
141+
let outputs = tcx.output_filenames(());
142+
let base_filename = outputs.output_path(OutputType::Object);
143+
write_file(&base_filename, "symtab.json", &symtab);
144+
write_file(&base_filename, "type_map.json", &type_map);
145+
write_file(&base_filename, "rmc-metadata.json", &metadata);
146+
// If they exist, write out vtable virtual call function pointer restrictions
147+
if let Some(restrictions) = vtable_restrictions {
148+
write_file(&base_filename, "restrictions.json", &restrictions);
149+
}
150+
}
151+
152+
let work_products = FxHashMap::<WorkProductId, WorkProduct>::default();
153+
Box::new((
154+
CodegenResults {
155+
modules: vec![],
156+
allocator_module: None,
157+
metadata_module: None,
158+
metadata: rustc_metadata,
159+
crate_info: CrateInfo::new(tcx, symtab.machine_model().architecture().to_string()),
160+
},
161+
work_products,
162+
))
156163
}
157164

158165
fn join_codegen(
159166
&self,
160167
ongoing_codegen: Box<dyn Any>,
161168
_sess: &Session,
162-
) -> Result<(Box<dyn Any>, FxHashMap<WorkProductId, WorkProduct>), ErrorReported> {
163-
Ok((ongoing_codegen, FxHashMap::default()))
169+
) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorReported> {
170+
Ok(*ongoing_codegen
171+
.downcast::<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>)>()
172+
.unwrap())
164173
}
165174

166175
fn link(
167176
&self,
168-
sess: &Session,
169-
codegen_results: Box<dyn Any>,
170-
outputs: &OutputFilenames,
177+
_sess: &Session,
178+
_codegen_results: CodegenResults,
179+
_outputs: &OutputFilenames,
171180
) -> Result<(), ErrorReported> {
172-
let result = codegen_results
173-
.downcast::<GotocCodegenResult>()
174-
.expect("in link: codegen_results is not a GotocCodegenResult");
175-
176-
// No output should be generated if user selected no_codegen.
177-
if !sess.opts.debugging_opts.no_codegen && sess.opts.output_types.should_codegen() {
178-
// "path.o"
179-
let base_filename = outputs.path(OutputType::Object);
180-
write_file(&base_filename, "symtab.json", &result.symtab);
181-
write_file(&base_filename, "type_map.json", &result.type_map);
182-
write_file(&base_filename, "rmc-metadata.json", &result.metadata);
183-
// If they exist, write out vtable virtual call function pointer restrictions
184-
if let Some(restrictions) = result.vtable_restrictions {
185-
write_file(&base_filename, "restrictions.json", &restrictions);
186-
}
187-
}
188-
189181
Ok(())
190182
}
191183
}
192184

193-
fn check_options(session: &Session) {
185+
fn check_options(session: &Session, need_metadata_module: bool) {
194186
if !session.overflow_checks() {
195187
session.err("RMC requires overflow checks in order to provide a sound analysis.");
196188
}
@@ -202,6 +194,10 @@ fn check_options(session: &Session) {
202194
);
203195
}
204196

197+
if need_metadata_module {
198+
session.err("RMC cannot generate metadata module.")
199+
}
200+
205201
session.abort_if_errors();
206202
}
207203

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub trait CodegenBackend {
9797
&self,
9898
ongoing_codegen: Box<dyn Any>,
9999
sess: &Session,
100-
) -> Result<(Box<dyn Any>, FxHashMap<WorkProductId, WorkProduct>), ErrorReported>;
100+
) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorReported>;
101101

102102
/// This is called on the returned `Box<dyn Any>` from `join_codegen`
103103
///
@@ -107,7 +107,7 @@ pub trait CodegenBackend {
107107
fn link(
108108
&self,
109109
sess: &Session,
110-
codegen_results: Box<dyn Any>,
110+
codegen_results: CodegenResults,
111111
outputs: &OutputFilenames,
112112
) -> Result<(), ErrorReported>;
113113
}

compiler/rustc_driver/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,7 @@ impl RustcDefaultCalls {
608608
json::decode(&rlink_data).unwrap_or_else(|err| {
609609
sess.fatal(&format!("failed to decode rlink: {}", err));
610610
});
611-
let result =
612-
compiler.codegen_backend().link(sess, Box::new(codegen_results), &outputs);
611+
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);
613612
abort_on_err(result, sess);
614613
} else {
615614
sess.fatal("rlink must be a file")

compiler/rustc_interface/src/queries.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,6 @@ impl Linker {
364364
}
365365

366366
if sess.opts.debugging_opts.no_link {
367-
use rustc_codegen_ssa::CodegenResults;
368-
let codegen_results = codegen_results
369-
.downcast::<CodegenResults>()
370-
.expect("Expected CodegenResults, found Box<Any>");
371367
// FIXME: use a binary format to encode the `.rlink` file
372368
let rlink_data = json::encode(&codegen_results).map_err(|err| {
373369
sess.fatal(&format!("failed to encode rlink: {}", err));

0 commit comments

Comments
 (0)