Skip to content

Commit cecd7a9

Browse files
committed
Add clif comments when in release mode
Fixes rust-lang#1130
1 parent 4ca3384 commit cecd7a9

File tree

13 files changed

+117
-102
lines changed

13 files changed

+117
-102
lines changed

src/abi/comments.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ use cranelift_codegen::entity::EntityRef;
1111
use crate::prelude::*;
1212

1313
pub(super) fn add_args_header_comment(fx: &mut FunctionCx<'_, '_, '_>) {
14-
fx.add_global_comment(
15-
"kind loc.idx param pass mode ty".to_string(),
16-
);
14+
if fx.clif_comments.enabled() {
15+
fx.add_global_comment(
16+
"kind loc.idx param pass mode ty".to_string(),
17+
);
18+
}
1719
}
1820

1921
pub(super) fn add_arg_comment<'tcx>(
@@ -25,6 +27,10 @@ pub(super) fn add_arg_comment<'tcx>(
2527
arg_abi_mode: PassMode,
2628
arg_layout: TyAndLayout<'tcx>,
2729
) {
30+
if !fx.clif_comments.enabled() {
31+
return;
32+
}
33+
2834
let local = if let Some(local) = local {
2935
Cow::Owned(format!("{:?}", local))
3036
} else {
@@ -59,17 +65,22 @@ pub(super) fn add_arg_comment<'tcx>(
5965
}
6066

6167
pub(super) fn add_locals_header_comment(fx: &mut FunctionCx<'_, '_, '_>) {
62-
fx.add_global_comment(String::new());
63-
fx.add_global_comment(
64-
"kind local ty size align (abi,pref)".to_string(),
65-
);
68+
if fx.clif_comments.enabled() {
69+
fx.add_global_comment(String::new());
70+
fx.add_global_comment(
71+
"kind local ty size align (abi,pref)".to_string(),
72+
);
73+
}
6674
}
6775

6876
pub(super) fn add_local_place_comments<'tcx>(
6977
fx: &mut FunctionCx<'_, '_, 'tcx>,
7078
place: CPlace<'tcx>,
7179
local: Local,
7280
) {
81+
if !fx.clif_comments.enabled() {
82+
return;
83+
}
7384
let TyAndLayout { ty, layout } = place.layout();
7485
let rustc_target::abi::Layout { size, align, abi: _, variants: _, fields: _, largest_niche: _ } =
7586
layout;
@@ -90,7 +101,7 @@ pub(super) fn add_local_place_comments<'tcx>(
90101
} else {
91102
Cow::Borrowed("")
92103
};
93-
match ptr.base_and_offset() {
104+
match ptr.debug_base_and_offset() {
94105
(crate::pointer::PointerBase::Addr(addr), offset) => {
95106
("reuse", format!("storage={}{}{}", addr, offset, meta).into())
96107
}

src/abi/mod.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Handling of everything related to the calling convention. Also fills `fx.local_map`.
22
3-
#[cfg(debug_assertions)]
43
mod comments;
54
mod pass_mode;
65
mod returning;
@@ -75,8 +74,9 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
7574
let func_id = import_function(self.tcx, self.cx.module, inst);
7675
let func_ref = self.cx.module.declare_func_in_func(func_id, &mut self.bcx.func);
7776

78-
#[cfg(debug_assertions)]
79-
self.add_comment(func_ref, format!("{:?}", inst));
77+
if self.clif_comments.enabled() {
78+
self.add_comment(func_ref, format!("{:?}", inst));
79+
}
8080

8181
func_ref
8282
}
@@ -92,8 +92,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
9292
let func_id = self.cx.module.declare_function(&name, Linkage::Import, &sig).unwrap();
9393
let func_ref = self.cx.module.declare_func_in_func(func_id, &mut self.bcx.func);
9494
let call_inst = self.bcx.ins().call(func_ref, args);
95-
#[cfg(debug_assertions)]
96-
{
95+
if self.clif_comments.enabled() {
9796
self.add_comment(call_inst, format!("easy_call {}", name));
9897
}
9998
let results = self.bcx.inst_results(call_inst);
@@ -149,7 +148,6 @@ fn make_local_place<'tcx>(
149148
CPlace::new_stack_slot(fx, layout)
150149
};
151150

152-
#[cfg(debug_assertions)]
153151
self::comments::add_local_place_comments(fx, place, local);
154152

155153
place
@@ -163,7 +161,6 @@ pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_
163161

164162
let ssa_analyzed = crate::analyze::analyze(fx);
165163

166-
#[cfg(debug_assertions)]
167164
self::comments::add_args_header_comment(fx);
168165

169166
let mut block_params_iter = fx.bcx.func.dfg.block_params(start_block).to_vec().into_iter();
@@ -228,7 +225,6 @@ pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_
228225
fx.fn_abi = Some(fn_abi);
229226
assert!(block_params_iter.next().is_none(), "arg_value left behind");
230227

231-
#[cfg(debug_assertions)]
232228
self::comments::add_locals_header_comment(fx);
233229

234230
for (local, arg_kind, ty) in func_params {
@@ -256,7 +252,6 @@ pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_
256252
CPlace::for_ptr(addr, val.layout())
257253
};
258254

259-
#[cfg(debug_assertions)]
260255
self::comments::add_local_place_comments(fx, place, local);
261256

262257
assert_eq!(fx.local_map.push(place), local);
@@ -392,8 +387,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
392387
let (func_ref, first_arg) = match instance {
393388
// Trait object call
394389
Some(Instance { def: InstanceDef::Virtual(_, idx), .. }) => {
395-
#[cfg(debug_assertions)]
396-
{
390+
if fx.clif_comments.enabled() {
397391
let nop_inst = fx.bcx.ins().nop();
398392
fx.add_comment(
399393
nop_inst,
@@ -414,8 +408,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
414408

415409
// Indirect call
416410
None => {
417-
#[cfg(debug_assertions)]
418-
{
411+
if fx.clif_comments.enabled() {
419412
let nop_inst = fx.bcx.ins().nop();
420413
fx.add_comment(nop_inst, "indirect call");
421414
}

src/abi/pass_mode.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ pub(super) fn adjust_arg_for_abi<'tcx>(
248248
/// as necessary.
249249
pub(super) fn cvalue_for_param<'tcx>(
250250
fx: &mut FunctionCx<'_, '_, 'tcx>,
251-
#[cfg_attr(not(debug_assertions), allow(unused_variables))] local: Option<mir::Local>,
252-
#[cfg_attr(not(debug_assertions), allow(unused_variables))] local_field: Option<usize>,
251+
local: Option<mir::Local>,
252+
local_field: Option<usize>,
253253
arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
254254
block_params_iter: &mut impl Iterator<Item = Value>,
255255
) -> Option<CValue<'tcx>> {
@@ -263,7 +263,6 @@ pub(super) fn cvalue_for_param<'tcx>(
263263
})
264264
.collect::<SmallVec<[_; 2]>>();
265265

266-
#[cfg(debug_assertions)]
267266
crate::abi::comments::add_arg_comment(
268267
fx,
269268
"arg",

src/abi/returning.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ pub(super) fn codegen_return_param<'tcx>(
8484
}
8585
};
8686

87-
#[cfg(not(debug_assertions))]
88-
let _ = ret_param;
89-
90-
#[cfg(debug_assertions)]
9187
crate::abi::comments::add_arg_comment(
9288
fx,
9389
"ret",

src/base.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) {
219219
codegen_stmt(fx, block, stmt);
220220
}
221221

222-
#[cfg(debug_assertions)]
223-
{
222+
if fx.clif_comments.enabled() {
224223
let mut terminator_head = "\n".to_string();
225224
bb_data.terminator().kind.fmt_head(&mut terminator_head).unwrap();
226225
let inst = fx.bcx.func.layout.last_inst(block).unwrap();
@@ -433,12 +432,14 @@ fn codegen_stmt<'tcx>(
433432

434433
fx.set_debug_loc(stmt.source_info);
435434

436-
#[cfg(false_debug_assertions)]
435+
#[cfg(disabled)]
437436
match &stmt.kind {
438437
StatementKind::StorageLive(..) | StatementKind::StorageDead(..) => {} // Those are not very useful
439438
_ => {
440-
let inst = fx.bcx.func.layout.last_inst(cur_block).unwrap();
441-
fx.add_comment(inst, format!("{:?}", stmt));
439+
if fx.clif_comments.enabled() {
440+
let inst = fx.bcx.func.layout.last_inst(cur_block).unwrap();
441+
fx.add_comment(inst, format!("{:?}", stmt));
442+
}
442443
}
443444
}
444445

src/common.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
361361
let _ = self.cx.module.define_data(msg_id, &data_ctx);
362362

363363
let local_msg_id = self.cx.module.declare_data_in_func(msg_id, self.bcx.func);
364-
#[cfg(debug_assertions)]
365-
{
364+
if self.clif_comments.enabled() {
366365
self.add_comment(local_msg_id, msg);
367366
}
368367
self.bcx.ins().global_value(self.pointer_type, local_msg_id)

src/constant.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ pub(crate) fn codegen_tls_ref<'tcx>(
8282
) -> CValue<'tcx> {
8383
let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false);
8484
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
85-
#[cfg(debug_assertions)]
86-
fx.add_comment(local_data_id, format!("tls {:?}", def_id));
85+
if fx.clif_comments.enabled() {
86+
fx.add_comment(local_data_id, format!("tls {:?}", def_id));
87+
}
8788
let tls_ptr = fx.bcx.ins().tls_value(fx.pointer_type, local_data_id);
8889
CValue::by_val(tls_ptr, layout)
8990
}
@@ -95,8 +96,9 @@ fn codegen_static_ref<'tcx>(
9596
) -> CPlace<'tcx> {
9697
let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false);
9798
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
98-
#[cfg(debug_assertions)]
99-
fx.add_comment(local_data_id, format!("{:?}", def_id));
99+
if fx.clif_comments.enabled() {
100+
fx.add_comment(local_data_id, format!("{:?}", def_id));
101+
}
100102
let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
101103
assert!(!layout.is_unsized(), "unsized statics aren't supported");
102104
assert!(
@@ -182,8 +184,9 @@ pub(crate) fn codegen_const_value<'tcx>(
182184
data_id_for_alloc_id(fx.cx.module, ptr.alloc_id, alloc.mutability);
183185
let local_data_id =
184186
fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
185-
#[cfg(debug_assertions)]
186-
fx.add_comment(local_data_id, format!("{:?}", ptr.alloc_id));
187+
if fx.clif_comments.enabled() {
188+
fx.add_comment(local_data_id, format!("{:?}", ptr.alloc_id));
189+
}
187190
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
188191
}
189192
Some(GlobalAlloc::Function(instance)) => {
@@ -198,8 +201,9 @@ pub(crate) fn codegen_const_value<'tcx>(
198201
let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false);
199202
let local_data_id =
200203
fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
201-
#[cfg(debug_assertions)]
202-
fx.add_comment(local_data_id, format!("{:?}", def_id));
204+
if fx.clif_comments.enabled() {
205+
fx.add_comment(local_data_id, format!("{:?}", def_id));
206+
}
203207
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
204208
}
205209
None => bug!("missing allocation {:?}", ptr.alloc_id),
@@ -240,8 +244,9 @@ fn pointer_for_allocation<'tcx>(
240244
let data_id = data_id_for_alloc_id(fx.cx.module, alloc_id, alloc.mutability);
241245

242246
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
243-
#[cfg(debug_assertions)]
244-
fx.add_comment(local_data_id, format!("{:?}", alloc_id));
247+
if fx.clif_comments.enabled() {
248+
fx.add_comment(local_data_id, format!("{:?}", alloc_id));
249+
}
245250
let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
246251
crate::pointer::Pointer::new(global_ptr)
247252
}

src/inline_asm.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ fn call_inline_asm<'tcx>(
197197
offset: None,
198198
size: u32::try_from(slot_size.bytes()).unwrap(),
199199
});
200-
#[cfg(debug_assertions)]
201-
fx.add_comment(stack_slot, "inline asm scratch slot");
200+
if fx.clif_comments.enabled() {
201+
fx.add_comment(stack_slot, "inline asm scratch slot");
202+
}
202203

203204
let inline_asm_func = fx
204205
.cx
@@ -214,8 +215,9 @@ fn call_inline_asm<'tcx>(
214215
)
215216
.unwrap();
216217
let inline_asm_func = fx.cx.module.declare_func_in_func(inline_asm_func, &mut fx.bcx.func);
217-
#[cfg(debug_assertions)]
218-
fx.add_comment(inline_asm_func, asm_name);
218+
if fx.clif_comments.enabled() {
219+
fx.add_comment(inline_asm_func, asm_name);
220+
}
219221

220222
for (_reg, offset, value) in inputs {
221223
fx.bcx.ins().stack_store(value, stack_slot, i32::try_from(offset.bytes()).unwrap());

0 commit comments

Comments
 (0)