1 //! Converting Cranelift IR to text.
2 //!
3 //! The `write` module provides the `write_function` function which converts an IR `Function` to an
4 //! equivalent textual form. This textual form can be read back by the `cranelift-reader` crate.
5 
6 use crate::entity::SecondaryMap;
7 use crate::ir::entities::AnyEntity;
8 use crate::ir::pcc::Fact;
9 use crate::ir::{Block, DataFlowGraph, Function, Inst, SigRef, Type, Value, ValueDef};
10 use crate::packed_option::ReservedValue;
11 use alloc::string::{String, ToString};
12 use alloc::vec::Vec;
13 use core::fmt::{self, Write};
14 
15 /// A `FuncWriter` used to decorate functions during printing.
16 pub trait FuncWriter {
17     /// Write the basic block header for the current function.
18     fn write_block_header(
19         &mut self,
20         w: &mut dyn Write,
21         func: &Function,
22         block: Block,
23         indent: usize,
24     ) -> fmt::Result;
25 
26     /// Write the given `inst` to `w`.
27     fn write_instruction(
28         &mut self,
29         w: &mut dyn Write,
30         func: &Function,
31         aliases: &SecondaryMap<Value, Vec<Value>>,
32         inst: Inst,
33         indent: usize,
34     ) -> fmt::Result;
35 
36     /// Write the preamble to `w`. By default, this uses `write_entity_definition`.
37     fn write_preamble(&mut self, w: &mut dyn Write, func: &Function) -> Result<bool, fmt::Error> {
38         self.super_preamble(w, func)
39     }
40 
41     /// Default impl of `write_preamble`
42     fn super_preamble(&mut self, w: &mut dyn Write, func: &Function) -> Result<bool, fmt::Error> {
43         let mut any = false;
44 
45         for (ss, slot) in func.dynamic_stack_slots.iter() {
46             any = true;
47             self.write_entity_definition(w, func, ss.into(), slot, None)?;
48         }
49 
50         for (ss, slot) in func.sized_stack_slots.iter() {
51             any = true;
52             self.write_entity_definition(w, func, ss.into(), slot, None)?;
53         }
54 
55         for (gv, gv_data) in &func.global_values {
56             any = true;
57             let maybe_fact = func.global_value_facts[gv].as_ref();
58             self.write_entity_definition(w, func, gv.into(), gv_data, maybe_fact)?;
59         }
60 
61         for (mt, mt_data) in &func.memory_types {
62             any = true;
63             self.write_entity_definition(w, func, mt.into(), mt_data, None)?;
64         }
65 
66         // Write out all signatures before functions since function declarations can refer to
67         // signatures.
68         for (sig, sig_data) in &func.dfg.signatures {
69             any = true;
70             self.write_entity_definition(w, func, sig.into(), &sig_data, None)?;
71         }
72 
73         for (fnref, ext_func) in &func.dfg.ext_funcs {
74             if ext_func.signature != SigRef::reserved_value() {
75                 any = true;
76                 self.write_entity_definition(
77                     w,
78                     func,
79                     fnref.into(),
80                     &ext_func.display(Some(&func.params)),
81                     None,
82                 )?;
83             }
84         }
85 
86         for (&cref, cval) in func.dfg.constants.iter() {
87             any = true;
88             self.write_entity_definition(w, func, cref.into(), cval, None)?;
89         }
90 
91         if let Some(limit) = func.stack_limit {
92             any = true;
93             self.write_entity_definition(w, func, AnyEntity::StackLimit, &limit, None)?;
94         }
95 
96         Ok(any)
97     }
98 
99     /// Write an entity definition defined in the preamble to `w`.
100     fn write_entity_definition(
101         &mut self,
102         w: &mut dyn Write,
103         func: &Function,
104         entity: AnyEntity,
105         value: &dyn fmt::Display,
106         maybe_fact: Option<&Fact>,
107     ) -> fmt::Result {
108         self.super_entity_definition(w, func, entity, value, maybe_fact)
109     }
110 
111     /// Default impl of `write_entity_definition`
112     #[allow(unused_variables)]
113     fn super_entity_definition(
114         &mut self,
115         w: &mut dyn Write,
116         func: &Function,
117         entity: AnyEntity,
118         value: &dyn fmt::Display,
119         maybe_fact: Option<&Fact>,
120     ) -> fmt::Result {
121         if let Some(fact) = maybe_fact {
122             writeln!(w, "    {} ! {} = {}", entity, fact, value)
123         } else {
124             writeln!(w, "    {} = {}", entity, value)
125         }
126     }
127 }
128 
129 /// A `PlainWriter` that doesn't decorate the function.
130 pub struct PlainWriter;
131 
132 impl FuncWriter for PlainWriter {
133     fn write_instruction(
134         &mut self,
135         w: &mut dyn Write,
136         func: &Function,
137         aliases: &SecondaryMap<Value, Vec<Value>>,
138         inst: Inst,
139         indent: usize,
140     ) -> fmt::Result {
141         write_instruction(w, func, aliases, inst, indent)
142     }
143 
144     fn write_block_header(
145         &mut self,
146         w: &mut dyn Write,
147         func: &Function,
148         block: Block,
149         indent: usize,
150     ) -> fmt::Result {
151         write_block_header(w, func, block, indent)
152     }
153 }
154 
155 /// Write `func` to `w` as equivalent text.
156 /// Use `isa` to emit ISA-dependent annotations.
157 pub fn write_function(w: &mut dyn Write, func: &Function) -> fmt::Result {
158     decorate_function(&mut PlainWriter, w, func)
159 }
160 
161 /// Create a reverse-alias map from a value to all aliases having that value as a direct target
162 fn alias_map(func: &Function) -> SecondaryMap<Value, Vec<Value>> {
163     let mut aliases = SecondaryMap::<_, Vec<_>>::new();
164     for v in func.dfg.values() {
165         // VADFS returns the immediate target of an alias
166         if let Some(k) = func.dfg.value_alias_dest_for_serialization(v) {
167             aliases[k].push(v);
168         }
169     }
170     aliases
171 }
172 
173 /// Writes `func` to `w` as text.
174 /// write_function_plain is passed as 'closure' to print instructions as text.
175 /// pretty_function_error is passed as 'closure' to add error decoration.
176 pub fn decorate_function<FW: FuncWriter>(
177     func_w: &mut FW,
178     w: &mut dyn Write,
179     func: &Function,
180 ) -> fmt::Result {
181     write!(w, "function ")?;
182     write_spec(w, func)?;
183     writeln!(w, " {{")?;
184     let aliases = alias_map(func);
185     let mut any = func_w.write_preamble(w, func)?;
186     for block in &func.layout {
187         if any {
188             writeln!(w)?;
189         }
190         decorate_block(func_w, w, func, &aliases, block)?;
191         any = true;
192     }
193     writeln!(w, "}}")
194 }
195 
196 //----------------------------------------------------------------------
197 //
198 // Function spec.
199 
200 fn write_spec(w: &mut dyn Write, func: &Function) -> fmt::Result {
201     write!(w, "{}{}", func.name, func.signature)
202 }
203 
204 //----------------------------------------------------------------------
205 //
206 // Basic blocks
207 
208 fn write_arg(w: &mut dyn Write, func: &Function, arg: Value) -> fmt::Result {
209     let ty = func.dfg.value_type(arg);
210     if let Some(f) = &func.dfg.facts[arg] {
211         write!(w, "{} ! {}: {}", arg, f, ty)
212     } else {
213         write!(w, "{}: {}", arg, ty)
214     }
215 }
216 
217 /// Write out the basic block header, outdented:
218 ///
219 ///    block1:
220 ///    block1(v1: i32):
221 ///    block10(v4: f64, v5: b1):
222 ///
223 pub fn write_block_header(
224     w: &mut dyn Write,
225     func: &Function,
226     block: Block,
227     indent: usize,
228 ) -> fmt::Result {
229     let cold = if func.layout.is_cold(block) {
230         " cold"
231     } else {
232         ""
233     };
234 
235     // The `indent` is the instruction indentation. block headers are 4 spaces out from that.
236     write!(w, "{1:0$}{2}", indent - 4, "", block)?;
237 
238     let mut args = func.dfg.block_params(block).iter().cloned();
239     match args.next() {
240         None => return writeln!(w, "{}:", cold),
241         Some(arg) => {
242             write!(w, "(")?;
243             write_arg(w, func, arg)?;
244         }
245     }
246     // Remaining arguments.
247     for arg in args {
248         write!(w, ", ")?;
249         write_arg(w, func, arg)?;
250     }
251     writeln!(w, "){}:", cold)
252 }
253 
254 fn decorate_block<FW: FuncWriter>(
255     func_w: &mut FW,
256     w: &mut dyn Write,
257     func: &Function,
258     aliases: &SecondaryMap<Value, Vec<Value>>,
259     block: Block,
260 ) -> fmt::Result {
261     // Indent all instructions if any srclocs are present.
262     let indent = if func.rel_srclocs().is_empty() { 4 } else { 36 };
263 
264     func_w.write_block_header(w, func, block, indent)?;
265     for a in func.dfg.block_params(block).iter().cloned() {
266         write_value_aliases(w, aliases, a, indent)?;
267     }
268 
269     for inst in func.layout.block_insts(block) {
270         func_w.write_instruction(w, func, aliases, inst, indent)?;
271     }
272 
273     Ok(())
274 }
275 
276 //----------------------------------------------------------------------
277 //
278 // Instructions
279 
280 // Should `inst` be printed with a type suffix?
281 //
282 // Polymorphic instructions may need a suffix indicating the value of the controlling type variable
283 // if it can't be trivially inferred.
284 //
285 fn type_suffix(func: &Function, inst: Inst) -> Option<Type> {
286     let inst_data = &func.dfg.insts[inst];
287     let constraints = inst_data.opcode().constraints();
288 
289     if !constraints.is_polymorphic() {
290         return None;
291     }
292 
293     // If the controlling type variable can be inferred from the type of the designated value input
294     // operand, we don't need the type suffix.
295     if constraints.use_typevar_operand() {
296         let ctrl_var = inst_data.typevar_operand(&func.dfg.value_lists).unwrap();
297         let def_block = match func.dfg.value_def(ctrl_var) {
298             ValueDef::Result(instr, _) => func.layout.inst_block(instr),
299             ValueDef::Param(block, _) => Some(block),
300             ValueDef::Union(..) => None,
301         };
302         if def_block.is_some() && def_block == func.layout.inst_block(inst) {
303             return None;
304         }
305     }
306 
307     let rtype = func.dfg.ctrl_typevar(inst);
308     assert!(
309         !rtype.is_invalid(),
310         "Polymorphic instruction must produce a result"
311     );
312     Some(rtype)
313 }
314 
315 /// Write out any aliases to the given target, including indirect aliases
316 fn write_value_aliases(
317     w: &mut dyn Write,
318     aliases: &SecondaryMap<Value, Vec<Value>>,
319     target: Value,
320     indent: usize,
321 ) -> fmt::Result {
322     let mut todo_stack = vec![target];
323     while let Some(target) = todo_stack.pop() {
324         for &a in &aliases[target] {
325             writeln!(w, "{1:0$}{2} -> {3}", indent, "", a, target)?;
326             todo_stack.push(a);
327         }
328     }
329 
330     Ok(())
331 }
332 
333 fn write_instruction(
334     w: &mut dyn Write,
335     func: &Function,
336     aliases: &SecondaryMap<Value, Vec<Value>>,
337     inst: Inst,
338     indent: usize,
339 ) -> fmt::Result {
340     // Prefix containing source location, encoding, and value locations.
341     let mut s = String::with_capacity(16);
342 
343     // Source location goes first.
344     let srcloc = func.srcloc(inst);
345     if !srcloc.is_default() {
346         write!(s, "{} ", srcloc)?;
347     }
348 
349     // Write out prefix and indent the instruction.
350     write!(w, "{1:0$}", indent, s)?;
351 
352     // Write out the result values, if any.
353     let mut has_results = false;
354     for r in func.dfg.inst_results(inst) {
355         if !has_results {
356             has_results = true;
357             write!(w, "{}", r)?;
358         } else {
359             write!(w, ", {}", r)?;
360         }
361         if let Some(f) = &func.dfg.facts[*r] {
362             write!(w, " ! {}", f)?;
363         }
364     }
365     if has_results {
366         write!(w, " = ")?;
367     }
368 
369     // Then the opcode, possibly with a '.type' suffix.
370     let opcode = func.dfg.insts[inst].opcode();
371 
372     match type_suffix(func, inst) {
373         Some(suf) => write!(w, "{}.{}", opcode, suf)?,
374         None => write!(w, "{}", opcode)?,
375     }
376 
377     write_operands(w, &func.dfg, inst)?;
378     writeln!(w)?;
379 
380     // Value aliases come out on lines after the instruction defining the referent.
381     for r in func.dfg.inst_results(inst) {
382         write_value_aliases(w, aliases, *r, indent)?;
383     }
384     Ok(())
385 }
386 
387 /// Write the operands of `inst` to `w` with a prepended space.
388 pub fn write_operands(w: &mut dyn Write, dfg: &DataFlowGraph, inst: Inst) -> fmt::Result {
389     let pool = &dfg.value_lists;
390     let jump_tables = &dfg.jump_tables;
391     use crate::ir::instructions::InstructionData::*;
392     match dfg.insts[inst] {
393         AtomicRmw { op, args, .. } => write!(w, " {} {}, {}", op, args[0], args[1]),
394         AtomicCas { args, .. } => write!(w, " {}, {}, {}", args[0], args[1], args[2]),
395         LoadNoOffset { flags, arg, .. } => write!(w, "{} {}", flags, arg),
396         StoreNoOffset { flags, args, .. } => write!(w, "{} {}, {}", flags, args[0], args[1]),
397         Unary { arg, .. } => write!(w, " {}", arg),
398         UnaryImm { imm, .. } => write!(w, " {}", imm),
399         UnaryIeee32 { imm, .. } => write!(w, " {}", imm),
400         UnaryIeee64 { imm, .. } => write!(w, " {}", imm),
401         UnaryGlobalValue { global_value, .. } => write!(w, " {}", global_value),
402         UnaryConst {
403             constant_handle, ..
404         } => write!(w, " {}", constant_handle),
405         Binary { args, .. } => write!(w, " {}, {}", args[0], args[1]),
406         BinaryImm8 { arg, imm, .. } => write!(w, " {}, {}", arg, imm),
407         BinaryImm64 { arg, imm, .. } => write!(w, " {}, {}", arg, imm),
408         Ternary { args, .. } => write!(w, " {}, {}, {}", args[0], args[1], args[2]),
409         MultiAry { ref args, .. } => {
410             if args.is_empty() {
411                 write!(w, "")
412             } else {
413                 write!(w, " {}", DisplayValues(args.as_slice(pool)))
414             }
415         }
416         NullAry { .. } => write!(w, " "),
417         TernaryImm8 { imm, args, .. } => write!(w, " {}, {}, {}", args[0], args[1], imm),
418         Shuffle { imm, args, .. } => {
419             let data = dfg.immediates.get(imm).expect(
420                 "Expected the shuffle mask to already be inserted into the immediates table",
421             );
422             write!(w, " {}, {}, {}", args[0], args[1], data)
423         }
424         IntCompare { cond, args, .. } => write!(w, " {} {}, {}", cond, args[0], args[1]),
425         IntCompareImm { cond, arg, imm, .. } => write!(w, " {} {}, {}", cond, arg, imm),
426         IntAddTrap { args, code, .. } => write!(w, " {}, {}, {}", args[0], args[1], code),
427         FloatCompare { cond, args, .. } => write!(w, " {} {}, {}", cond, args[0], args[1]),
428         Jump { destination, .. } => {
429             write!(w, " {}", destination.display(pool))
430         }
431         Brif {
432             arg,
433             blocks: [block_then, block_else],
434             ..
435         } => {
436             write!(w, " {}, {}", arg, block_then.display(pool))?;
437             write!(w, ", {}", block_else.display(pool))
438         }
439         BranchTable { arg, table, .. } => {
440             write!(w, " {}, {}", arg, jump_tables[table].display(pool))
441         }
442         Call {
443             func_ref, ref args, ..
444         } => write!(w, " {}({})", func_ref, DisplayValues(args.as_slice(pool))),
445         CallIndirect {
446             sig_ref, ref args, ..
447         } => {
448             let args = args.as_slice(pool);
449             write!(
450                 w,
451                 " {}, {}({})",
452                 sig_ref,
453                 args[0],
454                 DisplayValues(&args[1..])
455             )
456         }
457         FuncAddr { func_ref, .. } => write!(w, " {}", func_ref),
458         StackLoad {
459             stack_slot, offset, ..
460         } => write!(w, " {}{}", stack_slot, offset),
461         StackStore {
462             arg,
463             stack_slot,
464             offset,
465             ..
466         } => write!(w, " {}, {}{}", arg, stack_slot, offset),
467         DynamicStackLoad {
468             dynamic_stack_slot, ..
469         } => write!(w, " {}", dynamic_stack_slot),
470         DynamicStackStore {
471             arg,
472             dynamic_stack_slot,
473             ..
474         } => write!(w, " {}, {}", arg, dynamic_stack_slot),
475         Load {
476             flags, arg, offset, ..
477         } => write!(w, "{} {}{}", flags, arg, offset),
478         Store {
479             flags,
480             args,
481             offset,
482             ..
483         } => write!(w, "{} {}, {}{}", flags, args[0], args[1], offset),
484         Trap { code, .. } => write!(w, " {}", code),
485         CondTrap { arg, code, .. } => write!(w, " {}, {}", arg, code),
486     }?;
487 
488     let mut sep = "  ; ";
489     for arg in dfg.inst_values(inst) {
490         if let ValueDef::Result(src, _) = dfg.value_def(arg) {
491             let imm = match dfg.insts[src] {
492                 UnaryImm { imm, .. } => imm.to_string(),
493                 UnaryIeee32 { imm, .. } => imm.to_string(),
494                 UnaryIeee64 { imm, .. } => imm.to_string(),
495                 UnaryConst {
496                     constant_handle, ..
497                 } => constant_handle.to_string(),
498                 _ => continue,
499             };
500             write!(w, "{}{} = {}", sep, arg, imm)?;
501             sep = ", ";
502         }
503     }
504     Ok(())
505 }
506 
507 /// Displayable slice of values.
508 struct DisplayValues<'a>(&'a [Value]);
509 
510 impl<'a> fmt::Display for DisplayValues<'a> {
511     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
512         for (i, val) in self.0.iter().enumerate() {
513             if i == 0 {
514                 write!(f, "{}", val)?;
515             } else {
516                 write!(f, ", {}", val)?;
517             }
518         }
519         Ok(())
520     }
521 }
522 
523 #[cfg(test)]
524 mod tests {
525     use crate::cursor::{Cursor, CursorPosition, FuncCursor};
526     use crate::ir::types;
527     use crate::ir::{Function, InstBuilder, StackSlotData, StackSlotKind, UserFuncName};
528     use alloc::string::ToString;
529 
530     #[test]
531     fn basic() {
532         let mut f = Function::new();
533         assert_eq!(f.to_string(), "function u0:0() fast {\n}\n");
534 
535         f.name = UserFuncName::testcase("foo");
536         assert_eq!(f.to_string(), "function %foo() fast {\n}\n");
537 
538         f.create_sized_stack_slot(StackSlotData::new(StackSlotKind::ExplicitSlot, 4));
539         assert_eq!(
540             f.to_string(),
541             "function %foo() fast {\n    ss0 = explicit_slot 4\n}\n"
542         );
543 
544         let block = f.dfg.make_block();
545         f.layout.append_block(block);
546         assert_eq!(
547             f.to_string(),
548             "function %foo() fast {\n    ss0 = explicit_slot 4\n\nblock0:\n}\n"
549         );
550 
551         f.dfg.append_block_param(block, types::I8);
552         assert_eq!(
553             f.to_string(),
554             "function %foo() fast {\n    ss0 = explicit_slot 4\n\nblock0(v0: i8):\n}\n"
555         );
556 
557         f.dfg.append_block_param(block, types::F32.by(4).unwrap());
558         assert_eq!(
559             f.to_string(),
560             "function %foo() fast {\n    ss0 = explicit_slot 4\n\nblock0(v0: i8, v1: f32x4):\n}\n"
561         );
562 
563         {
564             let mut cursor = FuncCursor::new(&mut f);
565             cursor.set_position(CursorPosition::After(block));
566             cursor.ins().return_(&[])
567         };
568         assert_eq!(
569             f.to_string(),
570             "function %foo() fast {\n    ss0 = explicit_slot 4\n\nblock0(v0: i8, v1: f32x4):\n    return\n}\n"
571         );
572     }
573 
574     #[test]
575     fn aliases() {
576         use crate::ir::InstBuilder;
577 
578         let mut func = Function::new();
579         {
580             let block0 = func.dfg.make_block();
581             let mut pos = FuncCursor::new(&mut func);
582             pos.insert_block(block0);
583 
584             // make some detached values for change_to_alias
585             let v0 = pos.func.dfg.append_block_param(block0, types::I32);
586             let v1 = pos.func.dfg.append_block_param(block0, types::I32);
587             let v2 = pos.func.dfg.append_block_param(block0, types::I32);
588             pos.func.dfg.detach_block_params(block0);
589 
590             // alias to a param--will be printed at beginning of block defining param
591             let v3 = pos.func.dfg.append_block_param(block0, types::I32);
592             pos.func.dfg.change_to_alias(v0, v3);
593 
594             // alias to an alias--should print attached to alias, not ultimate target
595             pos.func.dfg.make_value_alias_for_serialization(v0, v2); // v0 <- v2
596 
597             // alias to a result--will be printed after instruction producing result
598             let _dummy0 = pos.ins().iconst(types::I32, 42);
599             let v4 = pos.ins().iadd(v0, v0);
600             pos.func.dfg.change_to_alias(v1, v4);
601             let _dummy1 = pos.ins().iconst(types::I32, 23);
602             let _v7 = pos.ins().iadd(v1, v1);
603         }
604         assert_eq!(
605             func.to_string(),
606             "function u0:0() fast {\nblock0(v3: i32):\n    v0 -> v3\n    v2 -> v0\n    v4 = iconst.i32 42\n    v5 = iadd v0, v0\n    v1 -> v5\n    v6 = iconst.i32 23\n    v7 = iadd v1, v1\n}\n"
607         );
608     }
609 
610     #[test]
611     fn cold_blocks() {
612         let mut func = Function::new();
613         {
614             let mut pos = FuncCursor::new(&mut func);
615 
616             let block0 = pos.func.dfg.make_block();
617             pos.insert_block(block0);
618             pos.func.layout.set_cold(block0);
619 
620             let block1 = pos.func.dfg.make_block();
621             pos.insert_block(block1);
622             pos.func.dfg.append_block_param(block1, types::I32);
623             pos.func.layout.set_cold(block1);
624         }
625 
626         assert_eq!(
627             func.to_string(),
628             "function u0:0() fast {\nblock0 cold:\n\nblock1(v0: i32) cold:\n}\n"
629         );
630     }
631 }
632