Skip to main content

rustc_codegen_spirv/
lib.rs

1// HACK(eddyb) start of `rustc_codegen_ssa` crate-level attributes (see `build.rs`).
2#![feature(box_patterns)]
3#![feature(file_buffered)]
4#![feature(negative_impls)]
5#![feature(string_from_utf8_lossy_owned)]
6#![feature(trait_alias)]
7#![feature(try_blocks)]
8#![recursion_limit = "256"]
9// HACK(eddyb) end of `rustc_codegen_ssa` crate-level attributes (see `build.rs`).
10
11//! Welcome to the API documentation for the `rust-gpu` project, this API is
12//! unstable and mainly intended for developing on the project itself. This is
13//! the API documentation for `rustc_codegen_spirv` which is not that useful on
14//! its own. You might also be interested in the following crates. There's also
15//! the [Rust GPU Dev Guide][gpu-dev-guide] which contains more user-level
16//! information on how to use and setup `rust-gpu`.
17//!
18//! - [`spirv-builder`]
19//! - [`spirv-std`]
20//! - [`spirv-tools`]
21//! - [`spirv-tools-sys`]
22//!
23//! [gpu-dev-guide]: https://rust-gpu.github.io/rust-gpu/book
24//! [`spirv-builder`]: https://rust-gpu.github.io/rust-gpu/api/spirv_builder
25//! [`spirv-std`]: https://rust-gpu.github.io/rust-gpu/api/spirv_std
26//! [`spirv-tools`]: https://rust-gpu.github.io/rust-gpu/api/spirv_tools
27//! [`spirv-tools-sys`]: https://rust-gpu.github.io/rust-gpu/api/spirv_tools_sys
28#![feature(rustc_private)]
29// In `rustc_codegen_spirv_disable_pqp_cg_ssa` mode we stop `include!`ing the
30// patched `rustc_codegen_ssa`, so these copied crate-level feature gates can
31// become locally unused even though the default build still needs them.
32#![cfg_attr(rustc_codegen_spirv_disable_pqp_cg_ssa, allow(unused_features))]
33// crate-specific exceptions:
34#![allow(
35    clippy::enum_glob_use,      // pretty useful pattern with some codegen'd enums (e.g. rspirv::spirv::Op)
36    clippy::todo,               // still lots to implement :)
37
38    // FIXME(eddyb) new warnings from 1.83 rustup, apply their suggested changes.
39    mismatched_lifetime_syntaxes,
40)]
41
42// Unfortunately, this will not fail fast when compiling, but rather will wait for
43// rustc_codegen_spirv to be compiled. Putting this in build.rs will solve that problem, however,
44// that creates the much worse problem that then running `cargo check` will cause
45// rustc_codegen_spirv to be *compiled* instead of merely checked, something that takes
46// significantly longer. So, the trade-off between detecting a bad configuration slower for a
47// faster `cargo check` is worth it.
48#[cfg(all(feature = "use-compiled-tools", feature = "use-installed-tools"))]
49compile_error!(
50    "Either \"use-compiled-tools\" (enabled by default) or \"use-installed-tools\" may be enabled."
51);
52
53// HACK(eddyb) `build.rs` copies `rustc_codegen_ssa` (from the `rustc-dev` component)
54// and patches it to produce a "pqp" ("pre-`qptr`-patched") version that maintains
55// compatibility with "legacy" Rust-GPU pointer handling (mainly typed `alloca`s).
56//
57// FIXME(eddyb) get rid of this as soon as it's not needed anymore.
58#[cfg(not(rustc_codegen_spirv_disable_pqp_cg_ssa))]
59include!(concat!(env!("OUT_DIR"), "/pqp_cg_ssa.rs"));
60
61// HACK(eddyb) guide `rustc` to finding the right deps in the sysroot, which
62// (sadly) has to be outside `include!` to have any effect whatsoever.
63// FIXME(eddyb) this only really handles `bitflags`, not `object`.
64#[cfg(not(rustc_codegen_spirv_disable_pqp_cg_ssa))]
65mod _rustc_codegen_ssa_transitive_deps_hack {
66    extern crate rustc_codegen_ssa as _;
67}
68
69// NOTE(eddyb) `mod maybe_pqp_cg_ssa` is defined by the above `include`, when
70// in the (default for now) `pqp_cg_ssa` mode (see `build.rs`).
71#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
72use rustc_codegen_ssa as maybe_pqp_cg_ssa;
73
74// FIXME(eddyb) remove all `#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]`
75// as soon as they're not needed anymore (i.e. using `rustc_codegen_ssa` again).
76#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
77extern crate rustc_abi;
78extern crate rustc_apfloat;
79#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
80extern crate rustc_arena;
81#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
82extern crate rustc_ast;
83#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
84extern crate rustc_attr_parsing;
85#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
86extern crate rustc_codegen_ssa;
87#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
88extern crate rustc_data_structures;
89extern crate rustc_driver;
90#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
91extern crate rustc_errors;
92#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
93extern crate rustc_hashes;
94#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
95extern crate rustc_hir;
96#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
97extern crate rustc_index;
98extern crate rustc_interface;
99#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
100extern crate rustc_metadata;
101#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
102extern crate rustc_middle;
103#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
104extern crate rustc_session;
105#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
106extern crate rustc_span;
107#[cfg(rustc_codegen_spirv_disable_pqp_cg_ssa)]
108extern crate rustc_target;
109
110macro_rules! assert_ty_eq {
111    ($codegen_cx:expr, $left:expr, $right:expr) => {
112        assert!(
113            $left == $right,
114            "Expected types to be equal:\n{}\n==\n{}",
115            $codegen_cx.debug_type($left),
116            $codegen_cx.debug_type($right)
117        )
118    };
119}
120
121mod abi;
122mod attr;
123mod builder;
124mod builder_spirv;
125mod codegen_cx;
126mod custom_decorations;
127mod custom_insts;
128mod link;
129mod linker;
130mod naga_transpile;
131mod spirv_type;
132mod spirv_type_constraints;
133mod symbols;
134pub mod target;
135mod target_feature;
136
137use crate::maybe_pqp_cg_ssa::back::write::ThinLtoInput;
138use builder::Builder;
139use codegen_cx::CodegenCx;
140use maybe_pqp_cg_ssa::back::lto::ThinModule;
141use maybe_pqp_cg_ssa::back::write::{
142    CodegenContext, FatLtoInput, ModuleConfig, OngoingCodegen, SharedEmitter,
143    TargetMachineFactoryFn,
144};
145use maybe_pqp_cg_ssa::base::maybe_create_entry_wrapper;
146use maybe_pqp_cg_ssa::mono_item::MonoItemExt;
147use maybe_pqp_cg_ssa::traits::{
148    CodegenBackend, ExtraBackendMethods, ModuleBufferMethods, WriteBackendMethods,
149};
150use maybe_pqp_cg_ssa::{
151    CompiledModule, CompiledModules, CrateInfo, ModuleCodegen, ModuleKind, TargetConfig,
152};
153use rspirv::binary::Assemble;
154use rustc_ast::expand::allocator::AllocatorMethod;
155use rustc_data_structures::fx::FxIndexMap;
156use rustc_data_structures::profiling::SelfProfilerRef;
157use rustc_errors::DiagCtxtHandle;
158use rustc_metadata::EncodedMetadata;
159use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
160use rustc_middle::mir::pretty::write_mir_pretty;
161use rustc_middle::mono::{MonoItem, MonoItemData};
162use rustc_middle::ty::print::with_no_trimmed_paths;
163use rustc_middle::ty::{InstanceKind, TyCtxt};
164use rustc_session::Session;
165use rustc_session::config::{self, OutputFilenames, OutputType};
166use rustc_span::symbol::Symbol;
167use std::any::Any;
168use std::fs;
169use std::io::Cursor;
170use std::io::Write;
171use std::path::{Path, PathBuf};
172use std::sync::Arc;
173use tracing::error;
174
175fn dump_mir(tcx: TyCtxt<'_>, mono_items: &[(MonoItem<'_>, MonoItemData)], path: &Path) {
176    fs::create_dir_all(path.parent().unwrap()).unwrap();
177    let mut file = fs::File::create(path).unwrap();
178    for &(mono_item, _) in mono_items {
179        if let MonoItem::Fn(instance) = mono_item
180            && matches!(instance.def, InstanceKind::Item(_))
181        {
182            let mut mir = Cursor::new(Vec::new());
183            if write_mir_pretty(tcx, Some(instance.def_id()), &mut mir).is_ok() {
184                writeln!(file, "{}", String::from_utf8(mir.into_inner()).unwrap()).unwrap();
185            }
186        }
187    }
188}
189
190#[derive(Clone)]
191struct SpirvCodegenBackend;
192
193impl CodegenBackend for SpirvCodegenBackend {
194    fn init(&self, sess: &Session) {
195        // Set up logging/tracing. See https://github.com/Rust-GPU/rust-gpu/issues/192.
196        init_logging(sess);
197    }
198
199    fn target_config(&self, sess: &Session) -> TargetConfig {
200        let cmdline = sess.opts.cg.target_feature.split(',');
201        let cfg = sess.target.options.features.split(',');
202
203        let target_features: Vec<_> = cfg
204            .chain(cmdline)
205            .filter(|l| l.starts_with('+'))
206            .map(|l| &l[1..])
207            .filter(|l| !l.is_empty())
208            .map(Symbol::intern)
209            .collect();
210
211        // HACK(eddyb) this should be a superset of `target_features`,
212        // which *additionally* also includes unstable target features,
213        // but there is no reason to make a distinction for SPIR-V ones.
214        let unstable_target_features = target_features.clone();
215
216        TargetConfig {
217            target_features,
218            unstable_target_features,
219
220            // FIXME(eddyb) support and/or emulate `f16` and `f128`.
221            has_reliable_f16: false,
222            has_reliable_f16_math: false,
223            has_reliable_f128: false,
224            has_reliable_f128_math: false,
225        }
226    }
227
228    fn provide(&self, providers: &mut rustc_middle::util::Providers) {
229        // FIXME(eddyb) this is currently only passed back to us, specifically
230        // into `target_machine_factory` (which is a noop), but it might make
231        // sense to move some of the target feature parsing into here.
232        providers.queries.global_backend_features = |_tcx, ()| vec![];
233
234        crate::abi::provide(providers);
235        crate::attr::provide(&mut providers.queries);
236    }
237
238    fn target_cpu(&self, sess: &Session) -> String {
239        sess.opts
240            .cg
241            .target_cpu
242            .clone()
243            .unwrap_or_else(|| sess.target.cpu.to_string())
244    }
245
246    fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>, crate_info: &CrateInfo) -> Box<dyn Any> {
247        Box::new(maybe_pqp_cg_ssa::base::codegen_crate(Self, tcx, crate_info))
248    }
249
250    fn join_codegen(
251        &self,
252        ongoing_codegen: Box<dyn Any>,
253        sess: &Session,
254        _outputs: &OutputFilenames,
255    ) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
256        ongoing_codegen
257            .downcast::<OngoingCodegen<Self>>()
258            .expect("Expected OngoingCodegen, found Box<Any>")
259            .join(sess)
260    }
261
262    fn link(
263        &self,
264        sess: &Session,
265        compiled_modules: CompiledModules,
266        crate_info: CrateInfo,
267        metadata: EncodedMetadata,
268        outputs: &OutputFilenames,
269    ) {
270        let timer = sess.timer("link_crate");
271        link::link(
272            sess,
273            &compiled_modules,
274            &crate_info,
275            &metadata,
276            outputs,
277            crate_info.local_crate_name.as_str(),
278        );
279        drop(timer);
280    }
281
282    fn name(&self) -> &'static str {
283        "SpirvCodegenBackend"
284    }
285}
286
287struct SpirvModuleBuffer(Vec<u32>);
288
289impl SpirvModuleBuffer {
290    fn as_bytes(&self) -> &[u8] {
291        spirv_tools::binary::from_binary(&self.0)
292    }
293}
294impl ModuleBufferMethods for SpirvModuleBuffer {
295    fn data(&self) -> &[u8] {
296        self.as_bytes()
297    }
298}
299
300impl SpirvCodegenBackend {
301    fn optimize_common(
302        _cgcx: &CodegenContext,
303        module: &mut ModuleCodegen<<Self as WriteBackendMethods>::Module>,
304    ) {
305        // Apply DCE ("dead code elimination") to modules before ever serializing
306        // them as `.spv` files (technically, `.rcgu.o` files inside `.rlib`s),
307        // that will later get linked (potentially many times, esp. if this is
308        // some big upstream library, e.g. `core` itself), and will therefore
309        // benefit from not having to clean up all sorts of unreachable helpers.
310        linker::dce::dce(&mut module.module_llvm);
311
312        // FIXME(eddyb) run as many optimization passes as possible, not just DCE.
313    }
314}
315
316impl WriteBackendMethods for SpirvCodegenBackend {
317    type Module = rspirv::dr::Module;
318    type TargetMachine = ();
319    type ModuleBuffer = SpirvModuleBuffer;
320    type ThinData = ();
321
322    // FIXME(eddyb) reuse the "merge" stage of `crate::linker` for this, or even
323    // consider setting `requires_lto = true` in the target specs and moving the
324    // entirety of `crate::linker` into this stage (lacking diagnostics may be
325    // an issue - it's surprising `CodegenBackend::link` has `Session` at all).
326    fn optimize_and_codegen_fat_lto(
327        cgcx: &CodegenContext,
328        _prof: &SelfProfilerRef,
329        _shared_emitter: &SharedEmitter,
330        _tm_factory: TargetMachineFactoryFn<Self>,
331        _exported_symbols_for_lto: &[String],
332        _each_linked_rlib_for_lto: &[PathBuf],
333        _modules: Vec<FatLtoInput<Self>>,
334    ) -> CompiledModule {
335        assert!(
336            cgcx.lto == rustc_session::config::Lto::Fat,
337            "`optimize_and_codegen_fat_lto` should \
338             only be invoked due to `-Clto` (or equivalent)"
339        );
340        unreachable!("Rust-GPU does not support fat LTO")
341    }
342
343    fn run_thin_lto(
344        _cgcx: &CodegenContext,
345        _prof: &SelfProfilerRef,
346        _dcx: DiagCtxtHandle<'_>,
347        _exported_symbols_for_lto: &[String],
348        _each_linked_rlib_for_lto: &[PathBuf],
349        _modules: Vec<ThinLtoInput<Self>>,
350    ) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
351        // Note(@firestar99): gcc impl this as unreachable as well
352        unreachable!()
353    }
354
355    fn optimize(
356        cgcx: &CodegenContext,
357        _prof: &SelfProfilerRef,
358        _shared_emitter: &SharedEmitter,
359        module: &mut ModuleCodegen<Self::Module>,
360        _config: &ModuleConfig,
361    ) {
362        Self::optimize_common(cgcx, module);
363    }
364
365    fn optimize_and_codegen_thin(
366        cgcx: &CodegenContext,
367        prof: &SelfProfilerRef,
368        shared_emitter: &SharedEmitter,
369        _tm_factory: TargetMachineFactoryFn<Self>,
370        thin_module: ThinModule<Self>,
371    ) -> CompiledModule {
372        // FIXME(eddyb) the inefficiency of Module -> [u8] -> Module roundtrips
373        // comes from upstream and it applies to `rustc_codegen_llvm` as well,
374        // eventually it should be properly addressed (for `ThinLocal` at least).
375        let mut module = ModuleCodegen {
376            module_llvm: link::with_rspirv_loader(|loader| {
377                rspirv::binary::parse_bytes(thin_module.data(), loader)
378            })
379            .unwrap(),
380            name: thin_module.name().to_string(),
381            kind: ModuleKind::Regular,
382            thin_lto_buffer: None,
383        };
384        Self::optimize_common(cgcx, &mut module);
385        Self::codegen(cgcx, prof, shared_emitter, module, &cgcx.module_config)
386    }
387
388    fn codegen(
389        cgcx: &CodegenContext,
390        _prof: &SelfProfilerRef,
391        _shared_emitter: &SharedEmitter,
392        module: ModuleCodegen<Self::Module>,
393        _config: &ModuleConfig,
394    ) -> CompiledModule {
395        let kind = module.kind;
396        let name = module.name;
397        let module_buffer = Self::serialize_module(module.module_llvm, false);
398
399        let path = cgcx.output_filenames.temp_path_for_cgu(
400            OutputType::Object,
401            &name,
402            cgcx.invocation_temp.as_deref(),
403        );
404        fs::write(&path, module_buffer.as_bytes()).unwrap();
405
406        CompiledModule {
407            name,
408            kind,
409            object: Some(path),
410            dwarf_object: None,
411            bytecode: None,
412            assembly: None,
413            llvm_ir: None,
414            links_from_incr_cache: vec![],
415        }
416    }
417
418    fn serialize_module(module: Self::Module, _is_thin: bool) -> Self::ModuleBuffer {
419        SpirvModuleBuffer(module.assemble())
420    }
421
422    fn target_machine_factory(
423        &self,
424        _sess: &Session,
425        _opt_level: config::OptLevel,
426        _target_features: &[String],
427    ) -> TargetMachineFactoryFn<Self> {
428        Arc::new(|_, _| ())
429    }
430}
431
432impl ExtraBackendMethods for SpirvCodegenBackend {
433    fn codegen_allocator(&self, _: TyCtxt<'_>, _: &str, _: &[AllocatorMethod]) -> Self::Module {
434        todo!()
435    }
436
437    fn compile_codegen_unit<'tcx>(
438        &self,
439        tcx: TyCtxt<'tcx>,
440        cgu_name: Symbol,
441    ) -> (ModuleCodegen<Self::Module>, u64) {
442        let _timer = tcx
443            .prof
444            .verbose_generic_activity_with_arg("codegen_module", cgu_name.to_string());
445
446        // TODO: Do dep_graph stuff
447        let cgu = tcx.codegen_unit(cgu_name);
448
449        let mut cx = CodegenCx::new(tcx, cgu);
450        let do_codegen = |cx: &mut CodegenCx<'tcx>| {
451            let mono_items = cgu.items_in_deterministic_order(cx.tcx);
452
453            if let Some(dir) = &cx.codegen_args.dump_mir {
454                dump_mir(tcx, mono_items.as_slice(), &dir.join(cgu_name.to_string()));
455            }
456
457            for &(mono_item, mono_item_data) in mono_items.iter() {
458                mono_item.predefine::<Builder<'_, '_>>(
459                    cx,
460                    cgu_name.as_str(),
461                    mono_item_data.linkage,
462                    mono_item_data.visibility,
463                );
464            }
465
466            // ... and now that we have everything pre-defined, fill out those definitions.
467            for &(mono_item, mono_item_data) in &mono_items {
468                tracing::trace!(?mono_item, "defining");
469                mono_item.define::<Builder<'_, '_>>(cx, cgu_name.as_str(), mono_item_data);
470            }
471
472            if let Some(_entry) = maybe_create_entry_wrapper::<Builder<'_, '_>>(cx, cgu) {
473                // attributes::sanitize(&cx, SanitizerSet::empty(), entry);
474            }
475        };
476        // HACK(eddyb) mutable access needed for `mono_item.define::<...>(cx, ...)`
477        // but that alone leads to needless cloning and smuggling a mutable borrow
478        // through `DumpModuleOnPanic` (for both its `Drop` impl and `do_codegen`).
479        if let Some(path) = cx.codegen_args.dump_module_on_panic.clone() {
480            let module_dumper = DumpModuleOnPanic {
481                cx: &mut cx,
482                path: &path,
483            };
484            with_no_trimmed_paths!(do_codegen(module_dumper.cx));
485            drop(module_dumper);
486        } else {
487            with_no_trimmed_paths!(do_codegen(&mut cx));
488        }
489
490        (
491            ModuleCodegen {
492                name: cgu_name.to_string(),
493                module_llvm: cx.finalize_module(),
494                kind: ModuleKind::Regular,
495                thin_lto_buffer: None,
496            },
497            0,
498        )
499    }
500}
501
502struct DumpModuleOnPanic<'a, 'cx, 'tcx> {
503    cx: &'cx mut CodegenCx<'tcx>,
504    path: &'a Path,
505}
506
507impl Drop for DumpModuleOnPanic<'_, '_, '_> {
508    fn drop(&mut self) {
509        if std::thread::panicking() {
510            if self.path.has_root() {
511                self.cx.builder.dump_module(self.path);
512            } else {
513                error!("{}", self.cx.builder.dump_module_str());
514            }
515        }
516    }
517}
518
519/// This is the entrypoint for a hot plugged `rustc_codegen_spirv`
520#[unsafe(no_mangle)]
521pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
522    // Tweak rustc's default ICE panic hook, to direct people to `rust-gpu`.
523    rustc_driver::install_ice_hook("https://github.com/rust-gpu/rust-gpu/issues/new", |dcx| {
524        dcx.handle().note(concat!(
525            "`rust-gpu` version `",
526            env!("CARGO_PKG_VERSION"),
527            "`"
528        ));
529    });
530
531    Box::new(SpirvCodegenBackend)
532}
533
534// Set up logging/tracing. See https://github.com/Rust-GPU/rust-gpu/issues/192.
535fn init_logging(sess: &Session) {
536    use std::env::{self, VarError};
537    use std::io::{self, IsTerminal};
538    use tracing_subscriber::layer::SubscriberExt;
539
540    // Set up the default subscriber with optional filtering.
541    let filter = tracing_subscriber::EnvFilter::from_env("RUSTGPU_LOG");
542    #[cfg(not(rustc_codegen_spirv_disable_pqp_cg_ssa))]
543    let filter = filter.add_directive("rustc_codegen_spirv::maybe_pqp_cg_ssa=off".parse().unwrap());
544    let subscriber = tracing_subscriber::Registry::default().with(filter);
545
546    #[derive(Debug, Default)]
547    enum OutputFormat {
548        #[default]
549        Tree,
550        Flat,
551        Json,
552    }
553
554    let output_format = match env::var("RUSTGPU_LOG_FORMAT").as_deref() {
555        Ok("tree") | Err(VarError::NotPresent) => OutputFormat::Tree,
556        Ok("flat") => OutputFormat::Flat,
557        Ok("json") => OutputFormat::Json,
558        Ok(value) => sess.dcx().fatal(format!(
559            "invalid output format value '{value}': expected one of tree, flat, or json",
560        )),
561        Err(VarError::NotUnicode(value)) => sess.dcx().fatal(format!(
562            "invalid output format value '{}': expected one of tree, flat, or json",
563            value.to_string_lossy()
564        )),
565    };
566
567    let subscriber: Box<dyn tracing::Subscriber + Send + Sync> = match output_format {
568        OutputFormat::Tree => {
569            // TODO(@LegNeato): Query dcx color support when rustc exposes it.
570            let color_logs = match env::var("RUSTGPU_LOG_COLOR").as_deref() {
571                Ok("always") => true,
572                Ok("never") => false,
573                Ok("auto") | Err(VarError::NotPresent) => io::stderr().is_terminal(),
574                Ok(value) => sess.dcx().fatal(format!(
575                    "invalid log color value '{value}': expected one of always, never, or auto",
576                )),
577                Err(VarError::NotUnicode(value)) => sess.dcx().fatal(format!(
578                    "invalid log color value '{}': expected one of always, never, or auto",
579                    value.to_string_lossy()
580                )),
581            };
582
583            let tree_layer = tracing_tree::HierarchicalLayer::default()
584                .with_writer(io::stderr)
585                .with_ansi(color_logs)
586                .with_targets(true)
587                .with_wraparound(10)
588                .with_verbose_exit(true)
589                .with_verbose_entry(true)
590                .with_indent_amount(2);
591
592            #[cfg(debug_assertions)]
593            let tree_layer = tree_layer.with_thread_ids(true).with_thread_names(true);
594
595            Box::new(subscriber.with(tree_layer))
596        }
597        OutputFormat::Flat => Box::new(subscriber),
598        OutputFormat::Json => Box::new(subscriber.with(tracing_subscriber::fmt::layer().json())),
599    };
600    tracing::subscriber::set_global_default(subscriber).unwrap();
601}