Skip to content

Commit 81af658

Browse files
committed
Make clif ir debug output a bit nicer
1 parent 04c7e5a commit 81af658

File tree

4 files changed

+66
-31
lines changed

4 files changed

+66
-31
lines changed

src/abi/mod.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,11 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
185185
let sig = Signature { params, returns, call_conv: self.target_config.default_call_conv };
186186
let func_id = self.module.declare_function(name, Linkage::Import, &sig).unwrap();
187187
let func_ref = self.module.declare_func_in_func(func_id, &mut self.bcx.func);
188+
let call_inst = self.bcx.ins().call(func_ref, args);
188189
if self.clif_comments.enabled() {
189190
self.add_comment(func_ref, format!("{:?}", name));
190-
let inst = self.bcx.func.layout.last_inst(self.bcx.current_block().unwrap()).unwrap();
191-
self.add_comment(inst, format!("lib_call {}", name));
191+
self.add_comment(call_inst, format!("lib_call {}", name));
192192
}
193-
let call_inst = self.bcx.ins().call(func_ref, args);
194193
let results = self.bcx.inst_results(call_inst);
195194
assert!(results.len() <= 2, "{}", results.len());
196195
results
@@ -530,7 +529,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
530529
Some(Instance { def: InstanceKind::Virtual(_, idx), .. }) => {
531530
if fx.clif_comments.enabled() {
532531
let nop_inst = fx.bcx.ins().nop();
533-
fx.add_comment(
532+
fx.add_post_comment(
534533
nop_inst,
535534
with_no_trimmed_paths!(format!(
536535
"virtual call; self arg pass mode: {:?}",
@@ -556,7 +555,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
556555
None => {
557556
if fx.clif_comments.enabled() {
558557
let nop_inst = fx.bcx.ins().nop();
559-
fx.add_comment(nop_inst, "indirect call");
558+
fx.add_post_comment(nop_inst, "indirect call");
560559
}
561560

562561
let func = func.load_scalar(fx);
@@ -586,18 +585,17 @@ pub(crate) fn codegen_terminator_call<'tcx>(
586585
adjust_call_for_c_variadic(fx, &fn_abi, source_info, func_ref, &mut call_args);
587586
}
588587

589-
if fx.clif_comments.enabled() {
590-
let nop_inst = fx.bcx.ins().nop();
591-
with_no_trimmed_paths!(fx.add_comment(nop_inst, format!("abi: {:?}", fn_abi)));
592-
}
593-
594588
let call_inst = match func_ref {
595589
CallTarget::Direct(func_ref) => fx.bcx.ins().call(func_ref, &call_args),
596590
CallTarget::Indirect(sig, func_ptr) => {
597591
fx.bcx.ins().call_indirect(sig, func_ptr, &call_args)
598592
}
599593
};
600594

595+
if fx.clif_comments.enabled() {
596+
with_no_trimmed_paths!(fx.add_comment(call_inst, format!("abi: {:?}", fn_abi)));
597+
}
598+
601599
fx.bcx.func.dfg.inst_results(call_inst).iter().copied().collect::<SmallVec<[Value; 2]>>()
602600
});
603601

src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
315315
bb_data.terminator().kind.fmt_head(&mut terminator_head).unwrap();
316316
});
317317
let inst = fx.bcx.func.layout.last_inst(block).unwrap();
318-
fx.add_comment(inst, terminator_head);
318+
fx.add_post_comment(inst, terminator_head);
319319
}
320320

321321
let source_info = bb_data.terminator().source_info;
@@ -570,7 +570,7 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
570570
if fx.clif_comments.enabled() {
571571
let inst = fx.bcx.func.layout.last_inst(cur_block).unwrap();
572572
with_no_trimmed_paths!({
573-
fx.add_comment(inst, format!("{:?}", stmt));
573+
fx.add_post_comment(inst, format!("{:?}", stmt));
574574
});
575575
}
576576
}

src/pretty_clif.rs

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,41 @@
88
//! target x86_64
99
//!
1010
//! function u0:22(i64) -> i8, i8 system_v {
11-
//! ; symbol _ZN97_$LT$example..IsNotEmpty$u20$as$u20$mini_core..FnOnce$LT$$LP$$RF$$RF$$u5b$u16$u5d$$C$$RP$$GT$$GT$9call_once17hd517c453d67c0915E
12-
//! ; instance Instance { def: Item(WithOptConstParam { did: DefId(0:42 ~ example[4e51]::{impl#0}::call_once), const_param_did: None }), args: [ReErased, ReErased] }
13-
//! ; abi FnAbi { args: [ArgAbi { layout: TyAndLayout { ty: IsNotEmpty, layout: Layout { size: Size(0 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, abi: Aggregate { sized: true }, fields: Arbitrary { offsets: [], memory_index: [] }, largest_niche: None, variants: Single { index: 0 } } }, mode: Ignore }, ArgAbi { layout: TyAndLayout { ty: &&[u16], layout: Layout { size: Size(8 bytes), align: AbiAndPrefAlign { abi: Align(8 bytes), pref: Align(8 bytes) }, abi: Scalar(Initialized { value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), fields: Primitive, largest_niche: Some(Niche { offset: Size(0 bytes), value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), variants: Single { index: 0 } } }, mode: Direct(ArgAttributes { regular: NonNull | NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: Some(Align(8 bytes)) }) }], ret: ArgAbi { layout: TyAndLayout { ty: (u8, u8), layout: Layout { size: Size(2 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, abi: ScalarPair(Initialized { value: Int(I8, false), valid_range: 0..=255 }, Initialized { value: Int(I8, false), valid_range: 0..=255 }), fields: Arbitrary { offsets: [Size(0 bytes), Size(1 bytes)], memory_index: [0, 1] }, largest_niche: None, variants: Single { index: 0 } } }, mode: Pair(ArgAttributes { regular: NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: None }, ArgAttributes { regular: NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: None }) }, c_variadic: false, fixed_count: 1, conv: Rust, can_unwind: false }
11+
//! ; symbol _ZN97_$LT$example..IsNotEmpty$u20$as$u20$mini_core..FnOnce$LT$$LP$$RF$$RF$$u5b$u16$u5d$$C$$RP$$GT$$GT$9call_once17hd361e9f5c3d1c4deE
12+
//! ; instance Instance { def: Item(DefId(0:42 ~ example[3895]::{impl#0}::call_once)), args: ['{erased}, '{erased}] }
13+
//! ; abi FnAbi { args: [ArgAbi { layout: TyAndLayout { ty: IsNotEmpty, layout: Layout { size: Size(0 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, backend_repr: Memory { sized: true }, fields: Arbitrary { offsets: [], memory_index: [] }, largest_niche: None, uninhabited: false, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), randomization_seed: 12266848898570219025 } }, mode: Ignore }, ArgAbi { layout: TyAndLayout { ty: &&[u16], layout: Layout { size: Size(8 bytes), align: AbiAndPrefAlign { abi: Align(8 bytes), pref: Align(8 bytes) }, backend_repr: Scalar(Initialized { value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), fields: Primitive, largest_niche: Some(Niche { offset: Size(0 bytes), value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), uninhabited: false, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes), randomization_seed: 281492156579847 } }, mode: Direct(ArgAttributes { regular: NonNull | NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: Some(Align(8 bytes)) }) }], ret: ArgAbi { layout: TyAndLayout { ty: (u8, u8), layout: Layout { size: Size(2 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, backend_repr: ScalarPair(Initialized { value: Int(I8, false), valid_range: 0..=255 }, Initialized { value: Int(I8, false), valid_range: 0..=255 }), fields: Arbitrary { offsets: [Size(0 bytes), Size(1 bytes)], memory_index: [0, 1] }, largest_niche: None, uninhabited: false, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), randomization_seed: 71776127651151873 } }, mode: Pair(ArgAttributes { regular: NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: None }, ArgAttributes { regular: NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: None }) }, c_variadic: false, fixed_count: 1, conv: Rust, can_unwind: false }
1414
//!
1515
//! ; kind loc.idx param pass mode ty
16-
//! ; ssa _0 (u8, u8) 2b 1, 8 var=(0, 1)
16+
//! ; ssa _0 (u8, u8) 2b 1 var=(0, 1)
1717
//! ; ret _0 - Pair(ArgAttributes { regular: NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: None }, ArgAttributes { regular: NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: None }) (u8, u8)
1818
//! ; arg _1 - Ignore IsNotEmpty
19-
//! ; arg _2.0 = v0 Direct(ArgAttributes { regular: NonNull | NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: Some(Align(8 bytes)) }) &&[u16]
19+
//! ; arg _2.0 = v0 Direct(ArgAttributes { regular: NonNull | NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: Some(Align(8 bytes)) }) &'{erased} &'{erased} [u16]
2020
//!
21-
//! ; kind local ty size align (abi,pref)
22-
//! ; zst _1 IsNotEmpty 0b 1, 8 align=8,offset=
23-
//! ; stack _2 (&&[u16],) 8b 8, 8 storage=ss0
24-
//! ; ssa _3 &mut IsNotEmpty 8b 8, 8 var=2
21+
//! ; kind local ty size align (abi)
22+
//! ; zst _1 IsNotEmpty 0b 1 align=1,offset=
23+
//! ; stack _2 (&'{erased} &'{erased} [u16],) 8b 8 storage=ss0
24+
//! ; ssa _3 &'{erased} mut IsNotEmpty 8b 8 var=2
2525
//!
26-
//! ss0 = explicit_slot 16
26+
//! ss0 = explicit_slot 16, align = 16
2727
//! sig0 = (i64, i64) -> i8, i8 system_v
28-
//! fn0 = colocated u0:23 sig0 ; Instance { def: Item(WithOptConstParam { did: DefId(0:46 ~ example[4e51]::{impl#1}::call_mut), const_param_did: None }), args: [ReErased, ReErased] }
28+
//! fn0 = colocated u0:23 sig0 ; Instance { def: Item(DefId(0:46 ~ example[3895]::{impl#1}::call_mut)), args: ['{erased}, '{erased}] }
2929
//!
3030
//! block0(v0: i64):
3131
//! nop
32-
//! ; write_cvalue: Addr(Pointer { base: Stack(ss0), offset: Offset32(0) }, None): &&[u16] <- ByVal(v0): &&[u16]
32+
//! ; write_cvalue: Addr(Pointer { base: Stack(ss0), offset: Offset32(0) }, None): &'{erased} &'{erased} [u16] <- ByVal(v0): &'{erased} &'{erased} [u16]
3333
//! stack_store v0, ss0
3434
//! jump block1
3535
//!
3636
//! block1:
3737
//! nop
3838
//! ; _3 = &mut _1
39-
//! v1 = iconst.i64 8
40-
//! ; write_cvalue: Var(_3, var2): &mut IsNotEmpty <- ByVal(v1): &mut IsNotEmpty
39+
//! v1 = iconst.i64 1
40+
//! ; write_cvalue: Var(_3, var2): &'{erased} mut IsNotEmpty <- ByVal(v1): &'{erased} mut IsNotEmpty
4141
//! ;
42-
//! ; _0 = <IsNotEmpty as mini_core::FnMut<(&&[u16],)>>::call_mut(move _3, _2)
42+
//! ; _0 = <IsNotEmpty as mini_core::FnMut<(&&[u16],)>>::call_mut(move _3, copy _2)
4343
//! v2 = stack_load.i64 ss0
44-
//! v3, v4 = call fn0(v1, v2) ; v1 = 8
44+
//! ; abi: FnAbi { args: [ArgAbi { layout: TyAndLayout { ty: &mut IsNotEmpty, layout: Layout { size: Size(8 bytes), align: AbiAndPrefAlign { abi: Align(8 bytes), pref: Align(8 bytes) }, backend_repr: Scalar(Initialized { value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), fields: Primitive, largest_niche: Some(Niche { offset: Size(0 bytes), value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), uninhabited: false, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes), randomization_seed: 281492156579847 } }, mode: Direct(ArgAttributes { regular: NonNull | NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: Some(Align(1 bytes)) }) }, ArgAbi { layout: TyAndLayout { ty: &&[u16], layout: Layout { size: Size(8 bytes), align: AbiAndPrefAlign { abi: Align(8 bytes), pref: Align(8 bytes) }, backend_repr: Scalar(Initialized { value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), fields: Primitive, largest_niche: Some(Niche { offset: Size(0 bytes), value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), uninhabited: false, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes), randomization_seed: 281492156579847 } }, mode: Direct(ArgAttributes { regular: NonNull | NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: Some(Align(8 bytes)) }) }], ret: ArgAbi { layout: TyAndLayout { ty: (u8, u8), layout: Layout { size: Size(2 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, backend_repr: ScalarPair(Initialized { value: Int(I8, false), valid_range: 0..=255 }, Initialized { value: Int(I8, false), valid_range: 0..=255 }), fields: Arbitrary { offsets: [Size(0 bytes), Size(1 bytes)], memory_index: [0, 1] }, largest_niche: None, uninhabited: false, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), randomization_seed: 71776127651151873 } }, mode: Pair(ArgAttributes { regular: NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: None }, ArgAttributes { regular: NoUndef, arg_ext: None, pointee_size: Size(0 bytes), pointee_align: None }) }, c_variadic: false, fixed_count: 1, conv: Rust, can_unwind: false }
45+
//! v3, v4 = call fn0(v1, v2) ; v1 = 1
4546
//! v5 -> v3
4647
//! v6 -> v4
4748
//! ; write_cvalue: VarPair(_0, var0, var1): (u8, u8) <- ByValPair(v3, v4): (u8, u8)
@@ -73,6 +74,7 @@ pub(crate) struct CommentWriter {
7374
enabled: bool,
7475
global_comments: Vec<String>,
7576
entity_comments: FxHashMap<AnyEntity, String>,
77+
inst_post_comments: FxHashMap<Inst, String>,
7678
}
7779

7880
impl CommentWriter {
@@ -95,7 +97,12 @@ impl CommentWriter {
9597
vec![]
9698
};
9799

98-
CommentWriter { enabled, global_comments, entity_comments: FxHashMap::default() }
100+
CommentWriter {
101+
enabled,
102+
global_comments,
103+
entity_comments: FxHashMap::default(),
104+
inst_post_comments: FxHashMap::default(),
105+
}
99106
}
100107
}
101108

@@ -127,6 +134,25 @@ impl CommentWriter {
127134
}
128135
}
129136
}
137+
138+
pub(crate) fn add_post_comment<S: Into<String> + AsRef<str>>(
139+
&mut self,
140+
entity: Inst,
141+
comment: S,
142+
) {
143+
debug_assert!(self.enabled);
144+
145+
use std::collections::hash_map::Entry;
146+
match self.inst_post_comments.entry(entity) {
147+
Entry::Occupied(mut occ) => {
148+
occ.get_mut().push('\n');
149+
occ.get_mut().push_str(comment.as_ref());
150+
}
151+
Entry::Vacant(vac) => {
152+
vac.insert(comment.into());
153+
}
154+
}
155+
}
130156
}
131157

132158
impl FuncWriter for &'_ CommentWriter {
@@ -188,10 +214,13 @@ impl FuncWriter for &'_ CommentWriter {
188214
inst: Inst,
189215
indent: usize,
190216
) -> fmt::Result {
191-
PlainWriter.write_instruction(w, func, aliases, inst, indent)?;
192217
if let Some(comment) = self.entity_comments.get(&inst.into()) {
193218
writeln!(w, "; {}", comment.replace('\n', "\n; "))?;
194219
}
220+
PlainWriter.write_instruction(w, func, aliases, inst, indent)?;
221+
if let Some(comment) = self.inst_post_comments.get(&inst) {
222+
writeln!(w, "; {}", comment.replace('\n', "\n; "))?;
223+
}
195224
Ok(())
196225
}
197226
}
@@ -208,6 +237,14 @@ impl FunctionCx<'_, '_, '_> {
208237
) {
209238
self.clif_comments.add_comment(entity, comment);
210239
}
240+
241+
pub(crate) fn add_post_comment<S: Into<String> + AsRef<str>>(
242+
&mut self,
243+
entity: Inst,
244+
comment: S,
245+
) {
246+
self.clif_comments.add_post_comment(entity, comment);
247+
}
211248
}
212249

213250
pub(crate) fn should_write_ir(tcx: TyCtxt<'_>) -> bool {

src/value_and_place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ impl<'tcx> CPlace<'tcx> {
593593

594594
if fx.clif_comments.enabled() {
595595
let inst = fx.bcx.func.layout.last_inst(fx.bcx.current_block().unwrap()).unwrap();
596-
fx.add_comment(
596+
fx.add_post_comment(
597597
inst,
598598
format!(
599599
"{}: {:?}: {:?} <- {:?}: {:?}",

0 commit comments

Comments
 (0)