pub fn lower_from_spv_ptrs(module: &mut Module, layout_config: &LayoutConfig)
Examples found in repository?
examples/spv-lower-link-qptr-lift.rs (line 90)
5fn main() -> std::io::Result<()> {
6 match &std::env::args().collect::<Vec<_>>()[..] {
7 [_, in_file] => {
8 let in_file_path = Path::new(in_file);
9
10 let save_print_plan = |suffix: &str, plan: spirt::print::Plan| {
11 let pretty = plan.pretty_print();
12 let ext = format!("{suffix}.spirt");
13
14 // FIXME(eddyb) don't allocate whole `String`s here.
15 fs::write(in_file_path.with_extension(&ext), pretty.to_string())?;
16 fs::write(
17 in_file_path.with_extension(ext + ".html"),
18 pretty.render_to_html().with_dark_mode_support().to_html_doc(),
19 )
20 };
21
22 // FIXME(eddyb) adapt the other examples to this style.
23
24 fn eprint_duration<R>(f: impl FnOnce() -> R) -> R {
25 let start = std::time::Instant::now();
26 let r = f();
27 eprint!("[{:8.3}ms] ", start.elapsed().as_secs_f64() * 1000.0);
28 r
29 }
30
31 eprint_duration(|| {
32 let _ = spirt::spv::spec::Spec::get();
33 });
34 eprintln!("spv::spec::Spec::get");
35
36 let cx = Rc::new(spirt::Context::new());
37
38 let multi_version_printing = true;
39 let mut per_pass_module = vec![];
40 let mut after_pass = |pass, module: &spirt::Module| {
41 if multi_version_printing {
42 per_pass_module.push((pass, module.clone()));
43 Ok(())
44 } else {
45 save_print_plan(
46 &format!("after.{pass}"),
47 spirt::print::Plan::for_module(module),
48 )
49 }
50 };
51
52 let mut module =
53 eprint_duration(|| spirt::Module::lower_from_spv_file(cx.clone(), in_file_path))?;
54 eprintln!("Module::lower_from_spv_file({})", in_file_path.display());
55
56 let original_export_count = module.exports.len();
57 eprint_duration(|| {
58 spirt::passes::link::minimize_exports(&mut module, |export_key| {
59 matches!(export_key, spirt::ExportKey::SpvEntryPoint { .. })
60 })
61 });
62 eprintln!(
63 "link::minimize_exports: {} -> {} exports",
64 original_export_count,
65 module.exports.len()
66 );
67 //after_pass("minimize_exports", &module)?;
68
69 // HACK(eddyb) do this late enough to avoid spending time on unused
70 // functions, which `link::minimize_exports` makes unreachable.
71 eprint_duration(|| spirt::passes::legalize::structurize_func_cfgs(&mut module));
72 eprintln!("legalize::structurize_func_cfgs");
73 //after_pass("structurize_func_cfgs", &module)?;
74
75 eprint_duration(|| spirt::passes::link::resolve_imports(&mut module));
76 eprintln!("link::resolve_imports");
77 //after_pass("resolve_imports", &module)?;
78
79 // HACK(eddyb)
80 after_pass("", &module)?;
81
82 // HACK(eddyb) this is roughly what Rust-GPU would need.
83 let layout_config = &spirt::qptr::LayoutConfig {
84 abstract_bool_size_align: (1, 1),
85 logical_ptr_size_align: (4, 4),
86 ..spirt::qptr::LayoutConfig::VULKAN_SCALAR_LAYOUT
87 };
88
89 eprint_duration(|| {
90 spirt::passes::qptr::lower_from_spv_ptrs(&mut module, layout_config)
91 });
92 eprintln!("qptr::lower_from_spv_ptrs");
93 after_pass("qptr::lower_from_spv_ptrs", &module)?;
94
95 eprint_duration(|| spirt::passes::qptr::analyze_uses(&mut module, layout_config));
96 eprintln!("qptr::analyze_uses");
97 after_pass("qptr::analyze_uses", &module)?;
98
99 eprint_duration(|| spirt::passes::qptr::lift_to_spv_ptrs(&mut module, layout_config));
100 eprintln!("qptr::lift_to_spv_ptrs");
101 after_pass("qptr::lift_to_spv_ptrs", &module)?;
102
103 if multi_version_printing {
104 // FIXME(eddyb) use a better suffix than `qptr` (or none).
105 save_print_plan(
106 "qptr",
107 spirt::print::Plan::for_versions(
108 &cx,
109 per_pass_module.iter().map(|(pass, module)| {
110 (
111 // HACK(eddyb)
112 if pass.is_empty() {
113 "initial".into()
114 } else {
115 format!("after {pass}")
116 },
117 module,
118 )
119 }),
120 ),
121 )?;
122 }
123
124 //let out_file_path = in_file_path.with_extension("qptr.spv");
125 //eprint_duration(|| module.lift_to_spv_file(&out_file_path))?;
126 //eprintln!("Module::lift_to_spv_file({})", out_file_path.display());
127
128 Ok(())
129 }
130 args => {
131 eprintln!("Usage: {} IN", args[0]);
132 std::process::exit(1);
133 }
134 }
135}