Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5593bc8

Browse files
committed
Update cranelift
1 parent 0046ce4 commit 5593bc8

File tree

3 files changed

+100
-75
lines changed

3 files changed

+100
-75
lines changed

src/base.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
99
) {
1010
let tcx = cx.tcx;
1111

12-
// Step 1. Get mir
1312
let mir = tcx.instance_mir(instance.def);
1413

15-
// Step 2. Check fn sig for u128 and i128 and replace those functions with a trap.
14+
// Check fn sig for u128 and i128 and replace those functions with a trap.
1615
{
1716
// FIXME implement u128 and i128 support
1817

19-
// Step 2a. Check sig for u128 and i128
18+
// Check sig for u128 and i128
2019
let fn_sig = tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &instance.fn_sig(tcx));
2120

2221
struct UI128Visitor<'a, 'tcx: 'a>(TyCtxt<'a, 'tcx, 'tcx>, bool);
@@ -35,12 +34,12 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
3534
let mut visitor = UI128Visitor(tcx, false);
3635
fn_sig.visit_with(&mut visitor);
3736

38-
// Step 2b. If found replace function with a trap.
37+
//If found replace function with a trap.
3938
if visitor.1 {
4039
tcx.sess.warn("u128 and i128 are not yet supported. \
4140
Functions using these as args will be replaced with a trap.");
4241

43-
// Step 2b1. Declare function with fake signature
42+
// Declare function with fake signature
4443
let sig = Signature {
4544
params: vec![AbiParam::new(types::INVALID)],
4645
returns: vec![],
@@ -49,7 +48,7 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
4948
let name = tcx.symbol_name(instance).as_str();
5049
let func_id = cx.module.declare_function(&*name, linkage, &sig).unwrap();
5150

52-
// Step 2b2. Create trapping function
51+
// Create trapping function
5352
let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
5453
let mut func_ctx = FunctionBuilderContext::new();
5554
let mut bcx = FunctionBuilder::new(&mut func, &mut func_ctx);
@@ -79,7 +78,7 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
7978
fx.bcx.seal_all_blocks();
8079
fx.bcx.finalize();
8180

82-
// Step 2b3. Define function
81+
// Define function
8382
cx.caches.context.func = func;
8483
cx.module
8584
.define_function(func_id, &mut cx.caches.context)
@@ -89,27 +88,27 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
8988
}
9089
}
9190

92-
// Step 3. Declare function
91+
// Declare function
9392
let (name, sig) = get_function_name_and_sig(tcx, instance, false);
9493
let func_id = cx.module.declare_function(&name, linkage, &sig).unwrap();
9594
let mut debug_context = cx
9695
.debug_context
9796
.as_mut()
9897
.map(|debug_context| FunctionDebugContext::new(tcx, debug_context, mir, &name, &sig));
9998

100-
// Step 4. Make FunctionBuilder
99+
// Make FunctionBuilder
101100
let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
102101
let mut func_ctx = FunctionBuilderContext::new();
103102
let mut bcx = FunctionBuilder::new(&mut func, &mut func_ctx);
104103

105-
// Step 5. Predefine ebb's
104+
// Predefine ebb's
106105
let start_ebb = bcx.create_ebb();
107106
let mut ebb_map: HashMap<BasicBlock, Ebb> = HashMap::new();
108107
for (bb, _bb_data) in mir.basic_blocks().iter_enumerated() {
109108
ebb_map.insert(bb, bcx.create_ebb());
110109
}
111110

112-
// Step 6. Make FunctionCx
111+
// Make FunctionCx
113112
let pointer_type = cx.module.target_config().pointer_type();
114113
let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance);
115114

@@ -131,38 +130,46 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
131130
source_info_set: indexmap::IndexSet::new(),
132131
};
133132

134-
// Step 7. Codegen function
135133
with_unimpl_span(fx.mir.span, || {
136134
crate::abi::codegen_fn_prelude(&mut fx, start_ebb);
137135
codegen_fn_content(&mut fx);
138136
});
139-
let source_info_set = fx.source_info_set.clone();
140137

141-
// Step 8. Write function to file for debugging
138+
// Recover all necessary data from fx, before accessing func will prevent future access to it.
139+
let instance = fx.instance;
140+
let clif_comments = fx.clif_comments;
141+
let source_info_set = fx.source_info_set;
142+
142143
#[cfg(debug_assertions)]
143-
fx.write_clif_file();
144+
crate::pretty_clif::write_clif_file(cx.tcx, "unopt", instance, &func, &clif_comments, None);
144145

145-
// Step 9. Verify function
146-
verify_func(tcx, fx.clif_comments, &func);
146+
// Verify function
147+
verify_func(tcx, &clif_comments, &func);
147148

148-
// Step 10. Define function
149-
cx.caches.context.func = func;
149+
// Define function
150+
let context = &mut cx.caches.context;
151+
context.func = func;
150152
cx.module
151-
.define_function(func_id, &mut cx.caches.context)
153+
.define_function(func_id, context)
152154
.unwrap();
153155

154-
// Step 11. Define debuginfo for function
155-
let context = &cx.caches.context;
156+
let value_ranges = context.build_value_labels_ranges(cx.module.isa()).expect("value location ranges");
157+
158+
// Write optimized function to file for debugging
159+
#[cfg(debug_assertions)]
160+
crate::pretty_clif::write_clif_file(cx.tcx, "opt", instance, &context.func, &clif_comments, Some(&value_ranges));
161+
162+
// Define debuginfo for function
156163
let isa = cx.module.isa();
157164
debug_context
158165
.as_mut()
159166
.map(|x| x.define(tcx, context, isa, &source_info_set));
160167

161-
// Step 12. Clear context to make it usable for the next function
162-
cx.caches.context.clear();
168+
// Clear context to make it usable for the next function
169+
context.clear();
163170
}
164171

165-
fn verify_func(tcx: TyCtxt, writer: crate::pretty_clif::CommentWriter, func: &Function) {
172+
fn verify_func(tcx: TyCtxt, writer: &crate::pretty_clif::CommentWriter, func: &Function) {
166173
let flags = settings::Flags::new(settings::builder());
167174
match ::cranelift::codegen::verify_function(&func, &flags) {
168175
Ok(_) => {}
@@ -171,7 +178,7 @@ fn verify_func(tcx: TyCtxt, writer: crate::pretty_clif::CommentWriter, func: &Fu
171178
let pretty_error = ::cranelift::codegen::print_errors::pretty_verifier_error(
172179
&func,
173180
None,
174-
Some(Box::new(&writer)),
181+
Some(Box::new(writer)),
175182
err,
176183
);
177184
tcx.sess

src/common.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::fmt;
2-
31
use rustc_target::spec::{HasTargetSpec, Target};
42

53
use cranelift_module::Module;
@@ -583,23 +581,6 @@ pub struct FunctionCx<'a, 'tcx: 'a, B: Backend> {
583581
pub source_info_set: indexmap::IndexSet<SourceInfo>,
584582
}
585583

586-
impl<'a, 'tcx: 'a, B: Backend + 'a> fmt::Debug for FunctionCx<'a, 'tcx, B> {
587-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
588-
writeln!(f, "{:?}", self.instance.substs)?;
589-
writeln!(f, "{:?}", self.local_map)?;
590-
591-
let mut clif = String::new();
592-
::cranelift::codegen::write::decorate_function(
593-
&mut &self.clif_comments,
594-
&mut clif,
595-
&self.bcx.func,
596-
None,
597-
)
598-
.unwrap();
599-
writeln!(f, "\n{}", clif)
600-
}
601-
}
602-
603584
impl<'a, 'tcx: 'a, B: Backend> LayoutOf for FunctionCx<'a, 'tcx, B> {
604585
type Ty = Ty<'tcx>;
605586
type TyLayout = TyLayout<'tcx>;

src/pretty_clif.rs

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ use std::borrow::Cow;
22
use std::collections::HashMap;
33
use std::fmt;
44

5-
use cranelift::codegen::entity::SecondaryMap;
6-
use cranelift::codegen::ir::entities::AnyEntity;
7-
use cranelift::codegen::write::{FuncWriter, PlainWriter};
5+
use cranelift::codegen::{
6+
entity::SecondaryMap,
7+
ir::{
8+
self,
9+
entities::AnyEntity,
10+
function::DisplayFunctionAnnotations,
11+
},
12+
write::{FuncWriter, PlainWriter},
13+
ValueLabelsRanges,
14+
};
815

916
use crate::prelude::*;
1017

@@ -184,42 +191,72 @@ impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
184191
}
185192
}
186193
}
194+
}
195+
196+
pub fn write_clif_file<'a, 'tcx: 'a>(
197+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
198+
postfix: &str,
199+
instance: Instance<'tcx>,
200+
func: &ir::Function,
201+
mut clif_comments: &CommentWriter,
202+
value_ranges: Option<&ValueLabelsRanges>,
203+
) {
204+
use std::io::Write;
205+
206+
let symbol_name = tcx.symbol_name(instance).as_str();
207+
let clif_file_name = format!(
208+
"{}/{}__{}.{}.clif",
209+
concat!(env!("CARGO_MANIFEST_DIR"), "/target/out/clif"),
210+
tcx.crate_name(LOCAL_CRATE),
211+
symbol_name,
212+
postfix,
213+
);
187214

188-
pub fn write_clif_file(&mut self) {
189-
use std::io::Write;
215+
let mut clif = String::new();
216+
cranelift::codegen::write::decorate_function(
217+
&mut clif_comments,
218+
&mut clif,
219+
&func,
220+
&DisplayFunctionAnnotations {
221+
isa: Some(&*crate::build_isa(tcx.sess)),
222+
value_ranges,
223+
},
224+
)
225+
.unwrap();
226+
227+
match ::std::fs::File::create(clif_file_name) {
228+
Ok(mut file) => {
229+
let target_triple: ::target_lexicon::Triple =
230+
tcx.sess.target.target.llvm_target.parse().unwrap();
231+
writeln!(file, "test compile").unwrap();
232+
writeln!(file, "set is_pic").unwrap();
233+
writeln!(file, "target {}", target_triple).unwrap();
234+
writeln!(file, "").unwrap();
235+
file.write(clif.as_bytes()).unwrap();
236+
}
237+
Err(e) => {
238+
tcx.sess.warn(&format!("err opening clif file: {:?}", e));
239+
}
240+
}
241+
}
190242

191-
let symbol_name = self.tcx.symbol_name(self.instance).as_str();
192-
let clif_file_name = format!(
193-
"{}/{}__{}.clif",
194-
concat!(env!("CARGO_MANIFEST_DIR"), "/target/out/clif"),
195-
self.tcx.crate_name(LOCAL_CRATE),
196-
symbol_name,
197-
);
243+
impl<'a, 'tcx: 'a, B: Backend + 'a> fmt::Debug for FunctionCx<'a, 'tcx, B> {
244+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
245+
writeln!(f, "{:?}", self.instance.substs)?;
246+
writeln!(f, "{:?}", self.local_map)?;
198247

199248
let mut clif = String::new();
200249
::cranelift::codegen::write::decorate_function(
201250
&mut &self.clif_comments,
202251
&mut clif,
203252
&self.bcx.func,
204-
None,
253+
// FIXME use DisplayFunctionAnnotations::default() instead
254+
&DisplayFunctionAnnotations {
255+
isa: None,
256+
value_ranges: None,
257+
},
205258
)
206259
.unwrap();
207-
208-
match ::std::fs::File::create(clif_file_name) {
209-
Ok(mut file) => {
210-
let target_triple: ::target_lexicon::Triple =
211-
self.tcx.sess.target.target.llvm_target.parse().unwrap();
212-
writeln!(file, "test compile").unwrap();
213-
writeln!(file, "set is_pic").unwrap();
214-
writeln!(file, "target {}", target_triple).unwrap();
215-
writeln!(file, "").unwrap();
216-
file.write(clif.as_bytes()).unwrap();
217-
}
218-
Err(e) => {
219-
self.tcx
220-
.sess
221-
.warn(&format!("err opening clif file: {:?}", e));
222-
}
223-
}
260+
writeln!(f, "\n{}", clif)
224261
}
225262
}

0 commit comments

Comments
 (0)