]> git.lizzy.rs Git - rust.git/blob - src/config.rs
Fold `vtable_trait_upcasting_coercion_new_vptr_slot` logic into obligation processing.
[rust.git] / src / config.rs
1 use std::env;
2 use std::str::FromStr;
3
4 fn bool_env_var(key: &str) -> bool {
5     env::var(key).as_ref().map(|val| &**val) == Ok("1")
6 }
7
8 /// The mode to use for compilation.
9 #[derive(Copy, Clone, Debug)]
10 pub enum CodegenMode {
11     /// AOT compile the crate. This is the default.
12     Aot,
13     /// JIT compile and execute the crate.
14     Jit,
15     /// JIT compile and execute the crate, but only compile functions the first time they are used.
16     JitLazy,
17 }
18
19 impl FromStr for CodegenMode {
20     type Err = String;
21
22     fn from_str(s: &str) -> Result<Self, Self::Err> {
23         match s {
24             "aot" => Ok(CodegenMode::Aot),
25             "jit" => Ok(CodegenMode::Jit),
26             "jit-lazy" => Ok(CodegenMode::JitLazy),
27             _ => Err(format!("Unknown codegen mode `{}`", s)),
28         }
29     }
30 }
31
32 /// Configuration of cg_clif as passed in through `-Cllvm-args` and various env vars.
33 #[derive(Clone, Debug)]
34 pub struct BackendConfig {
35     /// Should the crate be AOT compiled or JIT executed.
36     ///
37     /// Defaults to AOT compilation. Can be set using `-Cllvm-args=mode=...`.
38     pub codegen_mode: CodegenMode,
39
40     /// When JIT mode is enable pass these arguments to the program.
41     ///
42     /// Defaults to the value of `CG_CLIF_JIT_ARGS`.
43     pub jit_args: Vec<String>,
44
45     /// Display the time it took to perform codegen for a crate.
46     ///
47     /// Defaults to true when the `CG_CLIF_DISPLAY_CG_TIME` env var is set to 1 or false otherwise.
48     /// Can be set using `-Cllvm-args=display_cg_time=...`.
49     pub display_cg_time: bool,
50
51     /// The register allocator to use.
52     ///
53     /// Defaults to the value of `CG_CLIF_REGALLOC` or `backtracking` otherwise. Can be set using
54     /// `-Cllvm-args=regalloc=...`.
55     pub regalloc: String,
56
57     /// Enable the Cranelift ir verifier for all compilation passes. If not set it will only run
58     /// once before passing the clif ir to Cranelift for compilation.
59     ///
60     /// Defaults to true when the `CG_CLIF_ENABLE_VERIFIER` env var is set to 1 or when cg_clif is
61     /// compiled with debug assertions enabled or false otherwise. Can be set using
62     /// `-Cllvm-args=enable_verifier=...`.
63     pub enable_verifier: bool,
64
65     /// Don't cache object files in the incremental cache. Useful during development of cg_clif
66     /// to make it possible to use incremental mode for all analyses performed by rustc without
67     /// caching object files when their content should have been changed by a change to cg_clif.
68     ///
69     /// Defaults to true when the `CG_CLIF_DISABLE_INCR_CACHE` env var is set to 1 or false
70     /// otherwise. Can be set using `-Cllvm-args=disable_incr_cache=...`.
71     pub disable_incr_cache: bool,
72 }
73
74 impl Default for BackendConfig {
75     fn default() -> Self {
76         BackendConfig {
77             codegen_mode: CodegenMode::Aot,
78             jit_args: {
79                 let args = std::env::var("CG_CLIF_JIT_ARGS").unwrap_or_else(|_| String::new());
80                 args.split(' ').map(|arg| arg.to_string()).collect()
81             },
82             display_cg_time: bool_env_var("CG_CLIF_DISPLAY_CG_TIME"),
83             regalloc: std::env::var("CG_CLIF_REGALLOC")
84                 .unwrap_or_else(|_| "backtracking".to_string()),
85             enable_verifier: cfg!(debug_assertions) || bool_env_var("CG_CLIF_ENABLE_VERIFIER"),
86             disable_incr_cache: bool_env_var("CG_CLIF_DISABLE_INCR_CACHE"),
87         }
88     }
89 }
90
91 impl BackendConfig {
92     /// Parse the configuration passed in using `-Cllvm-args`.
93     pub fn from_opts(opts: &[String]) -> Result<Self, String> {
94         fn parse_bool(name: &str, value: &str) -> Result<bool, String> {
95             value.parse().map_err(|_| format!("failed to parse value `{}` for {}", value, name))
96         }
97
98         let mut config = BackendConfig::default();
99         for opt in opts {
100             if let Some((name, value)) = opt.split_once('=') {
101                 match name {
102                     "mode" => config.codegen_mode = value.parse()?,
103                     "display_cg_time" => config.display_cg_time = parse_bool(name, value)?,
104                     "regalloc" => config.regalloc = value.to_string(),
105                     "enable_verifier" => config.enable_verifier = parse_bool(name, value)?,
106                     "disable_incr_cache" => config.disable_incr_cache = parse_bool(name, value)?,
107                     _ => return Err(format!("Unknown option `{}`", name)),
108                 }
109             } else {
110                 return Err(format!("Invalid option `{}`", opt));
111             }
112         }
113
114         Ok(config)
115     }
116 }