1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
mod constant;
mod declare;
mod entry;
mod type_;

use crate::builder::{ExtInst, InstructionTable};
use crate::builder_spirv::{BuilderCursor, BuilderSpirv, SpirvConst, SpirvValue, SpirvValueKind};
use crate::custom_decorations::{CustomDecoration, SrcLocDecoration, ZombieDecoration};
use crate::spirv_type::{SpirvType, SpirvTypePrinter, TypeCache};
use crate::symbols::Symbols;
use crate::target::SpirvTarget;

use rspirv::dr::{Module, Operand};
use rspirv::spirv::{Decoration, LinkageType, Op, Word};
use rustc_ast::ast::{InlineAsmOptions, InlineAsmTemplatePiece};
use rustc_codegen_ssa::mir::debuginfo::{FunctionDebugContext, VariableKind};
use rustc_codegen_ssa::traits::{
    AsmMethods, BackendTypes, DebugInfoMethods, GlobalAsmOperandRef, MiscMethods,
};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_middle::mir;
use rustc_middle::mir::mono::CodegenUnit;
use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt};
use rustc_middle::ty::{Instance, ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
use rustc_session::Session;
use rustc_span::symbol::Symbol;
use rustc_span::{SourceFile, Span, DUMMY_SP};
use rustc_target::abi::call::{FnAbi, PassMode};
use rustc_target::abi::{AddressSpace, HasDataLayout, TargetDataLayout};
use rustc_target::spec::{HasTargetSpec, Target};
use std::cell::RefCell;
use std::collections::BTreeSet;
use std::iter::once;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::str::FromStr;

pub struct CodegenCx<'tcx> {
    pub tcx: TyCtxt<'tcx>,
    pub codegen_unit: &'tcx CodegenUnit<'tcx>,
    /// Spir-v module builder
    pub builder: BuilderSpirv<'tcx>,
    /// Map from MIR function to spir-v function ID
    pub instances: RefCell<FxHashMap<Instance<'tcx>, SpirvValue>>,
    /// Map from function ID to parameter list
    pub function_parameter_values: RefCell<FxHashMap<Word, Vec<SpirvValue>>>,
    pub type_cache: TypeCache<'tcx>,
    /// Cache generated vtables
    pub vtables: RefCell<FxHashMap<(Ty<'tcx>, Option<PolyExistentialTraitRef<'tcx>>), SpirvValue>>,
    pub ext_inst: RefCell<ExtInst>,
    /// Invalid SPIR-V IDs that should be stripped from the final binary,
    /// each with its own reason and span that should be used for reporting
    /// (in the event that the value is actually needed)
    zombie_decorations:
        RefCell<FxHashMap<Word, (ZombieDecoration<'tcx>, Option<SrcLocDecoration<'tcx>>)>>,
    /// Cache of all the builtin symbols we need
    pub sym: Rc<Symbols>,
    pub instruction_table: InstructionTable,
    pub libm_intrinsics: RefCell<FxHashMap<Word, super::builder::libm_intrinsics::LibmIntrinsic>>,

    /// All `panic!(...)`s and builtin panics (from MIR `Assert`s) call into one
    /// of these lang items, which we always replace with an "abort".
    pub panic_entry_point_ids: RefCell<FxHashSet<Word>>,

    /// `core::fmt::Arguments::new_{v1,const}` instances (for Rust 2021 panics).
    pub fmt_args_new_fn_ids: RefCell<FxHashSet<Word>>,

    /// `core::fmt::rt::Argument::new_*::<T>` instances (for panics' `format_args!`),
    /// with their `T` type (i.e. of the value being formatted), and formatting
    /// "specifier" as a `char` (' ' for `Display`, `x` for `LowerHex`, etc.)
    pub fmt_rt_arg_new_fn_ids_to_ty_and_spec: RefCell<FxHashMap<Word, (Ty<'tcx>, char)>>,

    /// Intrinsic for loading a <T> from a &[u32]. The PassMode is the mode of the <T>.
    pub buffer_load_intrinsic_fn_id: RefCell<FxHashMap<Word, &'tcx PassMode>>,
    /// Intrinsic for storing a <T> into a &[u32]. The PassMode is the mode of the <T>.
    pub buffer_store_intrinsic_fn_id: RefCell<FxHashMap<Word, &'tcx PassMode>>,

    /// Some runtimes (e.g. intel-compute-runtime) disallow atomics on i8 and i16, even though it's allowed by the spec.
    /// This enables/disables them.
    pub i8_i16_atomics_allowed: bool,

    pub codegen_args: CodegenArgs,

    /// Information about the SPIR-V target.
    pub target: SpirvTarget,
}

impl<'tcx> CodegenCx<'tcx> {
    pub fn new(tcx: TyCtxt<'tcx>, codegen_unit: &'tcx CodegenUnit<'tcx>) -> Self {
        let sym = Symbols::get();

        let mut feature_names = tcx
            .sess
            .target_features
            .iter()
            .map(|s| s.as_str())
            .collect::<Vec<_>>();

        // target_features is a HashSet, not a Vec, so we need to sort to have deterministic
        // compilation - otherwise, the order of capabilities in binaries depends on the iteration
        // order of the hashset. Sort by the string, since that's easy.
        feature_names.sort_unstable();

        let features = feature_names
            .into_iter()
            .map(|s| s.parse())
            .collect::<Result<_, String>>()
            .unwrap_or_else(|error| {
                tcx.sess.err(error);
                Vec::new()
            });

        let codegen_args = CodegenArgs::from_session(tcx.sess);
        let target = tcx.sess.target.llvm_target.parse().unwrap();

        Self {
            tcx,
            codegen_unit,
            builder: BuilderSpirv::new(tcx, &sym, &target, &features),
            instances: Default::default(),
            function_parameter_values: Default::default(),
            type_cache: Default::default(),
            vtables: Default::default(),
            ext_inst: Default::default(),
            zombie_decorations: Default::default(),
            target,
            sym,
            instruction_table: InstructionTable::new(),
            libm_intrinsics: Default::default(),
            panic_entry_point_ids: Default::default(),
            fmt_args_new_fn_ids: Default::default(),
            fmt_rt_arg_new_fn_ids_to_ty_and_spec: Default::default(),
            buffer_load_intrinsic_fn_id: Default::default(),
            buffer_store_intrinsic_fn_id: Default::default(),
            i8_i16_atomics_allowed: false,
            codegen_args,
        }
    }

    /// See comment on `BuilderCursor`
    pub fn emit_global(&self) -> std::cell::RefMut<'_, rspirv::dr::Builder> {
        self.builder.builder(BuilderCursor {
            function: None,
            block: None,
        })
    }

    /// See comment on `BuilderCursor`
    pub fn emit_with_cursor(
        &self,
        cursor: BuilderCursor,
    ) -> std::cell::RefMut<'_, rspirv::dr::Builder> {
        self.builder.builder(cursor)
    }

    #[track_caller]
    pub fn lookup_type(&self, ty: Word) -> SpirvType<'tcx> {
        self.type_cache.lookup(ty)
    }

    pub fn debug_type(&self, ty: Word) -> SpirvTypePrinter<'_, 'tcx> {
        self.lookup_type(ty).debug(ty, self)
    }

    pub fn type_ptr_to(&self, ty: Word) -> Word {
        SpirvType::Pointer { pointee: ty }.def(DUMMY_SP, self)
    }

    pub fn type_ptr_to_ext(&self, ty: Word, _address_space: AddressSpace) -> Word {
        SpirvType::Pointer { pointee: ty }.def(DUMMY_SP, self)
    }

    /// Zombie system:
    ///
    /// If something unrepresentable is encountered, we don't want to fail
    /// the compilation. Instead, we emit something bogus (usually it's fairly
    /// faithful, though, e.g. `u128` emits `OpTypeInt 128 0`), and then mark the
    /// resulting ID as a "zombie". We continue compiling the rest of the crate,
    /// then, at the very end, anything that transitively references a zombie value
    /// is stripped from the binary.
    ///
    /// Errors will only be emitted (by `linker::zombies`) for reachable zombies.
    pub fn zombie_with_span(&self, word: Word, span: Span, reason: &str) {
        self.zombie_decorations.borrow_mut().insert(
            word,
            (
                ZombieDecoration {
                    // FIXME(eddyb) this could take advantage of `Cow` and use
                    // either `&'static str` or `String`, on a case-by-case basis.
                    reason: reason.to_string().into(),
                },
                SrcLocDecoration::from_rustc_span(span, &self.builder),
            ),
        );
    }
    pub fn zombie_no_span(&self, word: Word, reason: &str) {
        self.zombie_with_span(word, DUMMY_SP, reason);
    }

    pub fn finalize_module(self) -> Module {
        let mut result = self.builder.finalize();
        result
            .annotations
            .extend(self.zombie_decorations.into_inner().into_iter().flat_map(
                |(id, (zombie, src_loc))| {
                    [zombie.encode_to_inst(id)]
                        .into_iter()
                        .chain(src_loc.map(|src_loc| src_loc.encode_to_inst(id)))
                },
            ));
        result
    }

    pub fn set_linkage(&self, target: Word, name: String, linkage: LinkageType) {
        self.emit_global().decorate(
            target,
            Decoration::LinkageAttributes,
            once(Operand::LiteralString(name)).chain(once(Operand::LinkageType(linkage))),
        );
    }
}

#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
pub enum SpirvMetadata {
    #[default]
    None,
    NameVariables,
    Full,
}

pub struct CodegenArgs {
    pub module_output_type: ModuleOutputType,
    pub disassemble: bool,
    pub disassemble_fn: Option<String>,
    pub disassemble_entry: Option<String>,
    pub disassemble_globals: bool,

    pub spirv_metadata: SpirvMetadata,

    pub run_spirv_val: bool,

    // spirv-val flags
    pub relax_struct_store: bool,
    pub relax_logical_pointer: bool,
    pub relax_block_layout: Option<bool>,
    pub uniform_buffer_standard_layout: bool,
    pub scalar_block_layout: bool,
    pub skip_block_layout: bool,

    pub run_spirv_opt: bool,

    // spirv-opt flags
    pub preserve_bindings: bool,

    /// All options pertinent to `rustc_codegen_spirv::linker` specifically.
    //
    // FIXME(eddyb) should these be handled as `-C linker-args="..."` instead?
    pub linker_opts: crate::linker::Options,

    // NOTE(eddyb) these are debugging options that used to be env vars
    // (for more information see `docs/src/codegen-args.md`).
    pub dump_mir: Option<PathBuf>,
    pub dump_module_on_panic: Option<PathBuf>,
    pub dump_pre_link: Option<PathBuf>,
    pub dump_post_link: Option<PathBuf>,
}

impl CodegenArgs {
    pub fn from_session(sess: &Session) -> Self {
        match CodegenArgs::parse(&sess.opts.cg.llvm_args) {
            Ok(ok) => ok,
            Err(err) => sess.fatal(format!("Unable to parse llvm-args: {err}")),
        }
    }

    // FIXME(eddyb) `structopt` would come a long way to making this nicer.
    pub fn parse(args: &[String]) -> Result<Self, rustc_session::getopts::Fail> {
        use rustc_session::getopts;

        // FIXME(eddyb) figure out what casing ("Foo bar" vs "foo bar") to use
        // for the descriptions, `rustc` seems a bit inconsistent itself on this.

        let mut opts = getopts::Options::new();
        opts.optflag("h", "help", "Display this message");
        opts.optopt(
            "",
            "module-output",
            "single output or multiple output",
            "[single|multiple]",
        );
        opts.optflag("", "disassemble", "print module to stderr");
        opts.optopt("", "disassemble-fn", "print function to stderr", "NAME");
        opts.optopt(
            "",
            "disassemble-entry",
            "print entry point to stderr",
            "NAME",
        );
        opts.optflag("", "disassemble-globals", "print globals to stderr");

        opts.optopt("", "spirv-metadata", "how much metadata to include", "");

        // FIXME(eddyb) clean up this `no-` "negation prefix" situation.
        opts.optflag(
            "",
            "no-spirv-val",
            "disables running spirv-val on the final output",
        );

        opts.optflag("", "relax-struct-store", "Allow store from one struct type to a different type with compatible layout and members.");
        opts.optflag("", "relax-logical-pointer", "Allow allocating an object of a pointer type and returning a pointer value from a function in logical addressing mode");
        opts.optflag("", "relax-block-layout", "Enable VK_KHR_relaxed_block_layout when checking standard uniform, storage buffer, and push constant layouts. This is the default when targeting Vulkan 1.1 or later.");
        opts.optflag("", "uniform-buffer-standard-layout", "Enable VK_KHR_uniform_buffer_standard_layout when checking standard uniform buffer layouts.");
        opts.optflag("", "scalar-block-layout", "Enable VK_EXT_scalar_block_layout when checking standard uniform, storage buffer, and push constant layouts. Scalar layout rules are more permissive than relaxed block layout so in effect this will override the --relax-block-layout option.");
        opts.optflag("", "skip-block-layout", "Skip checking standard uniform/storage buffer layout. Overrides any --relax-block-layout or --scalar-block-layout option.");

        // FIXME(eddyb) clean up this `no-` "negation prefix" situation.
        opts.optflag(
            "",
            "no-spirv-opt",
            "disables running spirv-opt on the final output",
        );

        opts.optflag(
            "",
            "preserve-bindings",
            "Preserve unused descriptor bindings. Useful for reflection.",
        );

        // Linker options.
        // FIXME(eddyb) should these be handled as `-C linker-args="..."` instead?
        {
            // FIXME(eddyb) clean up this `no-` "negation prefix" situation.
            opts.optflag("", "no-dce", "disables running dead code elimination");
            opts.optflag(
                "",
                "no-compact-ids",
                "disables compaction of SPIR-V IDs at the end of linking",
            );
            opts.optflag(
                "",
                "no-early-report-zombies",
                "delays reporting zombies (to allow more legalization)",
            );
            opts.optflag(
                "",
                "no-infer-storage-classes",
                "disables SPIR-V Storage Class inference",
            );
            opts.optflag("", "no-structurize", "disables CFG structurization");

            opts.optmulti(
                "",
                "spirt-passes",
                "enable additional SPIR-T passes (comma-separated)",
                "PASSES",
            );
            opts.optopt(
                "",
                "abort-strategy",
                "select a non-default abort (i.e. panic) strategy - see `spirv-builder` docs",
                "STRATEGY",
            );

            // NOTE(eddyb) these are debugging options that used to be env vars
            // (for more information see `docs/src/codegen-args.md`).
            opts.optopt(
                "",
                "dump-post-merge",
                "dump the merged module immediately after merging, to a file in DIR",
                "DIR",
            );
            opts.optopt(
                "",
                "dump-post-split",
                "dump modules immediately after multimodule splitting, to files in DIR",
                "DIR",
            );
            opts.optopt(
                "",
                "dump-spirt-passes",
                "dump the SPIR-T module across passes, to a (pair of) file(s) in DIR",
                "DIR",
            );
            opts.optflag(
                "",
                "spirt-strip-custom-debuginfo-from-dumps",
                "strip custom debuginfo instructions when dumping SPIR-T",
            );
            opts.optflag(
                "",
                "spirt-keep-debug-sources-in-dumps",
                "keep file contents debuginfo when dumping SPIR-T",
            );
            opts.optflag(
                "",
                "specializer-debug",
                "enable debug logging for the specializer",
            );
            opts.optopt(
                "",
                "specializer-dump-instances",
                "dump all instances inferred by the specializer, to FILE",
                "FILE",
            );
            opts.optflag("", "print-all-zombie", "prints all removed zombies");
            opts.optflag(
                "",
                "print-zombie",
                "prints everything removed (even transitively) due to zombies",
            );
        }

        // NOTE(eddyb) these are debugging options that used to be env vars
        // (for more information see `docs/src/codegen-args.md`).
        opts.optopt(
            "",
            "dump-mir",
            "dump every MIR body codegen sees, to files in DIR",
            "DIR",
        );
        opts.optopt(
            "",
            "dump-module-on-panic",
            "if codegen panics, dump the (partially) emitted module, to FILE",
            "FILE",
        );
        opts.optopt(
            "",
            "dump-pre-link",
            "dump all input modules to the linker, to files in DIR",
            "DIR",
        );
        opts.optopt(
            "",
            "dump-post-link",
            "dump all output modules from the linker, to files in DIR",
            "DIR",
        );

        let matches = opts.parse(args)?;

        let help_flag_positions: BTreeSet<_> = ["h", "help"]
            .iter()
            .flat_map(|&name| matches.opt_positions(name))
            .collect();
        if !help_flag_positions.is_empty() {
            // HACK(eddyb) this tries to be a bit nicer to end-users, when they
            // use `spirv-builder` (and so the `RUSTGPU_CODEGEN_ARGS` env var,
            // to set codegen args), as mentioning `-Cllvm-args` is suboptimal.
            let spirv_builder_env_var = "RUSTGPU_CODEGEN_ARGS";
            let help_flag_comes_from_spirv_builder_env_var = std::env::var(spirv_builder_env_var)
                .ok()
                .and_then(|args_from_env| {
                    let args_from_env: Vec<_> = args_from_env.split_whitespace().collect();
                    if args_from_env.is_empty() {
                        return None;
                    }

                    // HACK(eddyb) this may be a bit inefficient but we want to
                    // make sure that *at least one* of the `-h`/`--help` flags
                    // came from the `spirv-builder`-supported env var *and*
                    // that the env var's contents are fully contained in the
                    // `-C llvm-args` this `rustc` invocation is seeing.
                    args.windows(args_from_env.len())
                        .enumerate()
                        .filter(|&(_, w)| w == args_from_env)
                        .map(|(w_start, w)| w_start..w_start + w.len())
                        .flat_map(|w_range| help_flag_positions.range(w_range))
                        .next()
                })
                .is_some();
            let codegen_args_lhs = if help_flag_comes_from_spirv_builder_env_var {
                spirv_builder_env_var
            } else {
                "rustc -Cllvm-args"
            };
            println!(
                "{}",
                opts.usage(&format!(
                    "Usage: {codegen_args_lhs}=\"...\" with `...` from:"
                ))
            );
            // HACK(eddyb) this avoids `Cargo` continuing after the message is printed.
            std::process::exit(1);
        }

        let module_output_type =
            matches.opt_get_default("module-output", ModuleOutputType::Single)?;
        let disassemble = matches.opt_present("disassemble");
        let disassemble_fn = matches.opt_str("disassemble-fn");
        let disassemble_entry = matches.opt_str("disassemble-entry");
        let disassemble_globals = matches.opt_present("disassemble-globals");

        let spirv_metadata = matches.opt_str("spirv-metadata");

        // FIXME(eddyb) clean up this `no-` "negation prefix" situation.
        let run_spirv_val = !matches.opt_present("no-spirv-val");

        let relax_struct_store = matches.opt_present("relax-struct-store");
        let relax_logical_pointer = matches.opt_present("relax-logical-pointer");
        let relax_block_layout = matches.opt_present("relax-block-layout");
        let uniform_buffer_standard_layout = matches.opt_present("uniform-buffer-standard-layout");
        let scalar_block_layout = matches.opt_present("scalar-block-layout");
        let skip_block_layout = matches.opt_present("skip-block-layout");

        // FIXME(eddyb) clean up this `no-` "negation prefix" situation.
        let run_spirv_opt = !matches.opt_present("no-spirv-opt");

        let preserve_bindings = matches.opt_present("preserve-bindings");

        let relax_block_layout = if relax_block_layout { Some(true) } else { None };

        let spirv_metadata = match spirv_metadata.as_deref() {
            None => SpirvMetadata::None,
            Some("full") => SpirvMetadata::Full,
            Some("name-variables") => SpirvMetadata::NameVariables,
            Some(v) => {
                return Err(rustc_session::getopts::Fail::UnrecognizedOption(
                    v.to_string(),
                ));
            }
        };

        let matches_opt_path = |name| matches.opt_str(name).map(PathBuf::from);
        let matches_opt_dump_dir_path = |name| {
            matches_opt_path(name).map(|path| {
                if path.is_file() {
                    std::fs::remove_file(&path).unwrap();
                }
                std::fs::create_dir_all(&path).unwrap();
                path
            })
        };
        // FIXME(eddyb) should these be handled as `-C linker-args="..."` instead?
        let linker_opts = crate::linker::Options {
            // FIXME(eddyb) clean up this `no-` "negation prefix" situation.
            dce: !matches.opt_present("no-dce"),
            compact_ids: !matches.opt_present("no-compact-ids"),
            early_report_zombies: !matches.opt_present("no-early-report-zombies"),
            infer_storage_classes: !matches.opt_present("no-infer-storage-classes"),
            structurize: !matches.opt_present("no-structurize"),
            spirt_passes: matches
                .opt_strs("spirt-passes")
                .iter()
                .flat_map(|s| s.split(','))
                .map(|s| s.to_string())
                .collect(),

            abort_strategy: matches.opt_str("abort-strategy"),

            // FIXME(eddyb) deduplicate between `CodegenArgs` and `linker::Options`.
            emit_multiple_modules: module_output_type == ModuleOutputType::Multiple,
            spirv_metadata,
            keep_link_exports: false,

            // NOTE(eddyb) these are debugging options that used to be env vars
            // (for more information see `docs/src/codegen-args.md`).
            dump_post_merge: matches_opt_dump_dir_path("dump-post-merge"),
            dump_post_split: matches_opt_dump_dir_path("dump-post-split"),
            dump_spirt_passes: matches_opt_dump_dir_path("dump-spirt-passes"),
            spirt_strip_custom_debuginfo_from_dumps: matches
                .opt_present("spirt-strip-custom-debuginfo-from-dumps"),
            spirt_keep_debug_sources_in_dumps: matches
                .opt_present("spirt-keep-debug-sources-in-dumps"),
            specializer_debug: matches.opt_present("specializer-debug"),
            specializer_dump_instances: matches_opt_path("specializer-dump-instances"),
            print_all_zombie: matches.opt_present("print-all-zombie"),
            print_zombie: matches.opt_present("print-zombie"),
        };

        Ok(Self {
            module_output_type,
            disassemble,
            disassemble_fn,
            disassemble_entry,
            disassemble_globals,

            spirv_metadata,

            run_spirv_val,

            relax_struct_store,
            relax_logical_pointer,
            relax_block_layout,
            uniform_buffer_standard_layout,
            scalar_block_layout,
            skip_block_layout,

            run_spirv_opt,

            preserve_bindings,

            linker_opts,

            // NOTE(eddyb) these are debugging options that used to be env vars
            // (for more information see `docs/src/codegen-args.md`).
            dump_mir: matches_opt_dump_dir_path("dump-mir"),
            dump_module_on_panic: matches_opt_path("dump-module-on-panic"),
            dump_pre_link: matches_opt_dump_dir_path("dump-pre-link"),
            dump_post_link: matches_opt_dump_dir_path("dump-post-link"),
        })
    }

    pub fn do_disassemble(&self, module: &Module) {
        fn compact_ids(module: &mut rspirv::dr::Function) -> u32 {
            let mut remap = std::collections::HashMap::new();
            let mut insert = |current_id: &mut u32| {
                let len = remap.len();
                *current_id = *remap.entry(*current_id).or_insert_with(|| len as u32 + 1);
            };
            module.all_inst_iter_mut().for_each(|inst| {
                if let Some(ref mut result_id) = &mut inst.result_id {
                    insert(result_id);
                }
                if let Some(ref mut result_type) = &mut inst.result_type {
                    insert(result_type);
                }
                inst.operands.iter_mut().for_each(|op| {
                    if let Some(w) = op.id_ref_any_mut() {
                        insert(w);
                    }
                });
            });
            remap.len() as u32 + 1
        }

        use rspirv::binary::Disassemble;

        if self.disassemble {
            eprintln!("{}", module.disassemble());
        }

        if let Some(func) = &self.disassemble_fn {
            let id = module
                .debug_names
                .iter()
                .find(|inst| {
                    inst.class.opcode == rspirv::spirv::Op::Name
                        && inst.operands[1].unwrap_literal_string() == func
                })
                .unwrap_or_else(|| {
                    panic!(
                        "no function with the name `{}` found in:\n{}\n",
                        func,
                        module.disassemble()
                    )
                })
                .operands[0]
                .unwrap_id_ref();
            let mut func = module
                .functions
                .iter()
                .find(|f| f.def_id().unwrap() == id)
                .unwrap()
                .clone();
            // Compact to make IDs more stable
            compact_ids(&mut func);
            eprintln!("{}", func.disassemble());
        }

        if let Some(entry) = &self.disassemble_entry {
            let id = module
                .entry_points
                .iter()
                .filter(|inst| inst.class.opcode == rspirv::spirv::Op::EntryPoint)
                .find(|inst| inst.operands[2].unwrap_literal_string() == entry)
                .unwrap_or_else(|| {
                    panic!(
                        "no entry point with the name `{}` found in:\n{}\n",
                        entry,
                        module.disassemble()
                    )
                })
                .operands[1]
                .unwrap_id_ref();
            let mut func = module
                .functions
                .iter()
                .find(|f| f.def_id().unwrap() == id)
                .unwrap()
                .clone();
            // Compact to make IDs more stable
            compact_ids(&mut func);
            eprintln!("{}", func.disassemble());
        }

        if self.disassemble_globals {
            for inst in module.global_inst_iter() {
                // HACK: On Windows, paths are printed like `OpString "D:\\dir\\blah"`.
                // Unfortunately, compiletest will only normalize `D:\dir\blah` to `$DIR/blah` -
                // one backslash, not two. So, when disassembling for compiletest, check if the
                // argument to OpString can be parsed as an absolute path, and if it is, replace it
                // with just the filename component of the path.
                if inst.class.opcode == Op::String {
                    let path = Path::new(inst.operands[0].unwrap_literal_string());
                    if path.is_absolute() {
                        if let Some(file_name) = path.file_name() {
                            let mut inst = inst.clone();
                            inst.operands[0] = Operand::LiteralString(format!(
                                "$OPSTRING_FILENAME/{}",
                                file_name.to_string_lossy(),
                            ));
                            eprintln!("{}", inst.disassemble());
                            continue;
                        }
                    }
                }
                eprintln!("{}", inst.disassemble());
            }
        }
    }
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ModuleOutputType {
    Single,
    Multiple,
}

impl FromStr for ModuleOutputType {
    type Err = rustc_session::getopts::Fail;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "single" => Ok(Self::Single),
            "multiple" => Ok(Self::Multiple),
            v => Err(Self::Err::UnrecognizedOption(v.to_string())),
        }
    }
}

impl<'tcx> BackendTypes for CodegenCx<'tcx> {
    type Value = SpirvValue;
    type Function = SpirvValue;

    type BasicBlock = Word;
    type Type = Word;
    // Funclet: A structure representing an active landing pad for the duration of a basic block. (??)
    // https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_llvm/common/struct.Funclet.html
    type Funclet = ();

    type DIScope = ();
    type DILocation = ();
    type DIVariable = ();
}

impl<'tcx> HasTyCtxt<'tcx> for CodegenCx<'tcx> {
    fn tcx(&self) -> TyCtxt<'tcx> {
        self.tcx
    }
}

impl<'tcx> HasDataLayout for CodegenCx<'tcx> {
    fn data_layout(&self) -> &TargetDataLayout {
        &self.tcx.data_layout
    }
}

impl<'tcx> HasTargetSpec for CodegenCx<'tcx> {
    fn target_spec(&self) -> &Target {
        &self.tcx.sess.target
    }
}

impl<'tcx> HasParamEnv<'tcx> for CodegenCx<'tcx> {
    fn param_env(&self) -> ParamEnv<'tcx> {
        ParamEnv::reveal_all()
    }
}

impl<'tcx> MiscMethods<'tcx> for CodegenCx<'tcx> {
    #[allow(clippy::type_complexity)]
    fn vtables(
        &self,
    ) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<PolyExistentialTraitRef<'tcx>>), Self::Value>> {
        &self.vtables
    }

    fn check_overflow(&self) -> bool {
        self.tcx.sess.overflow_checks()
    }

    fn get_fn(&self, instance: Instance<'tcx>) -> Self::Function {
        self.get_fn_ext(instance)
    }

    // NOTE(eddyb) see the comment on `SpirvValueKind::FnAddr`, this should
    // be fixed upstream, so we never see any "function pointer" values being
    // created just to perform direct calls.
    fn get_fn_addr(&self, instance: Instance<'tcx>) -> Self::Value {
        let function = self.get_fn(instance);
        let span = self.tcx.def_span(instance.def_id());

        let ty = SpirvType::Pointer {
            pointee: function.ty,
        }
        .def(span, self);

        // Create these `OpUndef`s up front, instead of on-demand in `SpirvValue::def`,
        // because `SpirvValue::def` can't use `cx.emit()`.
        self.def_constant(ty, SpirvConst::ZombieUndefForFnAddr);

        SpirvValue {
            kind: SpirvValueKind::FnAddr {
                function: function.def_cx(self),
            },
            ty,
        }
    }

    fn eh_personality(&self) -> Self::Value {
        todo!()
    }

    fn sess(&self) -> &Session {
        self.tcx.sess
    }

    fn codegen_unit(&self) -> &'tcx CodegenUnit<'tcx> {
        self.codegen_unit
    }

    fn set_frame_pointer_type(&self, _llfn: Self::Function) {
        todo!()
    }

    fn apply_target_cpu_attr(&self, _llfn: Self::Function) {
        todo!()
    }

    fn declare_c_main(&self, _fn_type: Self::Type) -> Option<Self::Function> {
        todo!()
    }
}

impl<'tcx> DebugInfoMethods<'tcx> for CodegenCx<'tcx> {
    fn create_vtable_debuginfo(
        &self,
        _ty: Ty<'tcx>,
        _trait_ref: Option<PolyExistentialTraitRef<'tcx>>,
        _vtable: Self::Value,
    ) {
        // Ignore.
    }

    fn dbg_scope_fn(
        &self,
        _: rustc_middle::ty::Instance<'tcx>,
        _: &FnAbi<'tcx, Ty<'tcx>>,
        _: Option<Self::Function>,
    ) -> Self::DIScope {
        todo!()
    }

    fn dbg_loc(&self, _: Self::DIScope, _: Option<Self::DILocation>, _: Span) -> Self::DILocation {
        todo!()
    }

    fn create_function_debug_context(
        &self,
        _instance: Instance<'tcx>,
        _fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
        _llfn: Self::Function,
        _mir: &mir::Body<'tcx>,
    ) -> Option<FunctionDebugContext<'tcx, Self::DIScope, Self::DILocation>> {
        // TODO: This is ignored. Do we want to implement this at some point?
        None
    }

    fn extend_scope_to_file(
        &self,
        _scope_metadata: Self::DIScope,
        _file: &SourceFile,
    ) -> Self::DIScope {
        todo!()
    }

    fn debuginfo_finalize(&self) {
        todo!()
    }

    fn create_dbg_var(
        &self,
        _variable_name: Symbol,
        _variable_type: Ty<'tcx>,
        _scope_metadata: Self::DIScope,
        _variable_kind: VariableKind,
        _span: Span,
    ) -> Self::DIVariable {
        todo!()
    }
}

impl<'tcx> AsmMethods<'tcx> for CodegenCx<'tcx> {
    fn codegen_global_asm(
        &self,
        _template: &[InlineAsmTemplatePiece],
        _operands: &[GlobalAsmOperandRef<'tcx>],
        _options: InlineAsmOptions,
        _line_spans: &[Span],
    ) {
        todo!()
    }
}