1 //! Shared settings module.
2 //!
3 //! This module defines data structures to access the settings defined in the meta language.
4 //!
5 //! Each settings group is translated to a `Flags` struct either in this module or in its
6 //! ISA-specific `settings` module. The struct provides individual getter methods for all of the
7 //! settings as well as computed predicate flags.
8 //!
9 //! The `Flags` struct is immutable once it has been created. A `Builder` instance is used to
10 //! create it.
11 //!
12 //! # Example
13 //! ```
14 //! use cranelift_codegen::settings::{self, Configurable};
15 //!
16 //! let mut b = settings::builder();
17 //! b.set("opt_level", "speed_and_size");
18 //!
19 //! let f = settings::Flags::new(b);
20 //! assert_eq!(f.opt_level(), settings::OptLevel::SpeedAndSize);
21 //! ```
22 
23 use crate::constant_hash::{probe, simple_hash};
24 use crate::isa::TargetIsa;
25 use alloc::boxed::Box;
26 use alloc::string::{String, ToString};
27 use core::fmt;
28 use core::str;
29 
30 /// A string-based configurator for settings groups.
31 ///
32 /// The `Configurable` protocol allows settings to be modified by name before a finished `Flags`
33 /// struct is created.
34 pub trait Configurable {
35     /// Set the string value of any setting by name.
36     ///
37     /// This can set any type of setting whether it is numeric, boolean, or enumerated.
38     fn set(&mut self, name: &str, value: &str) -> SetResult<()>;
39 
40     /// Enable a boolean setting or apply a preset.
41     ///
42     /// If the identified setting isn't a boolean or a preset, a `BadType` error is returned.
43     fn enable(&mut self, name: &str) -> SetResult<()>;
44 }
45 
46 /// Represents the kind of setting.
47 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
48 pub enum SettingKind {
49     /// The setting is an enumeration.
50     Enum,
51     /// The setting is a number.
52     Num,
53     /// The setting is a boolean.
54     Bool,
55     /// The setting is a preset.
56     Preset,
57 }
58 
59 /// Represents an available builder setting.
60 ///
61 /// This is used for iterating settings in a builder.
62 #[derive(Clone, Copy, Debug)]
63 pub struct Setting {
64     /// The name of the setting.
65     pub name: &'static str,
66     /// The description of the setting.
67     pub description: &'static str,
68     /// The kind of the setting.
69     pub kind: SettingKind,
70     /// The supported values of the setting (for enum values).
71     pub values: Option<&'static [&'static str]>,
72 }
73 
74 /// Represents a setting value.
75 ///
76 /// This is used for iterating values in `Flags`.
77 pub struct Value {
78     /// The name of the setting associated with this value.
79     pub name: &'static str,
80     pub(crate) detail: detail::Detail,
81     pub(crate) values: Option<&'static [&'static str]>,
82     pub(crate) value: u8,
83 }
84 
85 impl Value {
86     /// Gets the kind of setting.
87     pub fn kind(&self) -> SettingKind {
88         match &self.detail {
89             detail::Detail::Enum { .. } => SettingKind::Enum,
90             detail::Detail::Num => SettingKind::Num,
91             detail::Detail::Bool { .. } => SettingKind::Bool,
92             detail::Detail::Preset => unreachable!(),
93         }
94     }
95 
96     /// Gets the enum value if the value is from an enum setting.
97     pub fn as_enum(&self) -> Option<&'static str> {
98         self.values.map(|v| v[self.value as usize])
99     }
100 
101     /// Gets the numerical value if the value is from a num setting.
102     pub fn as_num(&self) -> Option<u8> {
103         match &self.detail {
104             detail::Detail::Num => Some(self.value),
105             _ => None,
106         }
107     }
108 
109     /// Gets the boolean value if the value is from a boolean setting.
110     pub fn as_bool(&self) -> Option<bool> {
111         match &self.detail {
112             detail::Detail::Bool { bit } => Some(self.value & (1 << bit) != 0),
113             _ => None,
114         }
115     }
116 
117     /// Builds a string from the current value
118     pub fn value_string(&self) -> String {
119         match self.kind() {
120             SettingKind::Enum => self.as_enum().map(|b| b.to_string()),
121             SettingKind::Num => self.as_num().map(|b| b.to_string()),
122             SettingKind::Bool => self.as_bool().map(|b| b.to_string()),
123             SettingKind::Preset => unreachable!(),
124         }
125         .unwrap()
126     }
127 }
128 
129 impl fmt::Display for Value {
130     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131         if let Some(enum_variant) = self.as_enum() {
132             write!(f, "{}={}", self.name, enum_variant)
133         } else if let Some(num) = self.as_num() {
134             write!(f, "{}={}", self.name, num)
135         } else if let Some(b) = self.as_bool() {
136             if b {
137                 write!(f, "{}=1", self.name)
138             } else {
139                 write!(f, "{}=0", self.name)
140             }
141         } else {
142             unreachable!()
143         }
144     }
145 }
146 
147 /// Collect settings values based on a template.
148 #[derive(Clone, Hash)]
149 pub struct Builder {
150     template: &'static detail::Template,
151     bytes: Box<[u8]>,
152 }
153 
154 impl Builder {
155     /// Create a new builder with defaults and names from the given template.
156     pub fn new(tmpl: &'static detail::Template) -> Self {
157         Self {
158             template: tmpl,
159             bytes: tmpl.defaults.into(),
160         }
161     }
162 
163     /// Extract contents of builder once everything is configured.
164     pub fn state_for(&self, name: &str) -> &[u8] {
165         assert_eq!(name, self.template.name);
166         &self.bytes
167     }
168 
169     /// Iterates the available settings in the builder.
170     pub fn iter(&self) -> impl Iterator<Item = Setting> + use<> {
171         let template = self.template;
172 
173         template.descriptors.iter().map(move |d| {
174             let (kind, values) = match d.detail {
175                 detail::Detail::Enum { last, enumerators } => {
176                     let values = template.enums(last, enumerators);
177                     (SettingKind::Enum, Some(values))
178                 }
179                 detail::Detail::Num => (SettingKind::Num, None),
180                 detail::Detail::Bool { .. } => (SettingKind::Bool, None),
181                 detail::Detail::Preset => (SettingKind::Preset, None),
182             };
183 
184             Setting {
185                 name: d.name,
186                 description: d.description,
187                 kind,
188                 values,
189             }
190         })
191     }
192 
193     /// Set the value of a single bit.
194     fn set_bit(&mut self, offset: usize, bit: u8, value: bool) {
195         let byte = &mut self.bytes[offset];
196         let mask = 1 << bit;
197         if value {
198             *byte |= mask;
199         } else {
200             *byte &= !mask;
201         }
202     }
203 
204     /// Apply a preset. The argument is a slice of (mask, value) bytes.
205     fn apply_preset(&mut self, values: &[(u8, u8)]) {
206         for (byte, &(mask, value)) in self.bytes.iter_mut().zip(values) {
207             *byte = (*byte & !mask) | value;
208         }
209     }
210 
211     /// Look up a descriptor by name.
212     fn lookup(&self, name: &str) -> SetResult<(usize, detail::Detail)> {
213         match probe(self.template, name, simple_hash(name)) {
214             Err(_) => Err(SetError::BadName(name.to_string())),
215             Ok(entry) => {
216                 let d = &self.template.descriptors[self.template.hash_table[entry] as usize];
217                 Ok((d.offset as usize, d.detail))
218             }
219         }
220     }
221 }
222 
223 fn parse_bool_value(value: &str) -> SetResult<bool> {
224     match value {
225         "true" | "on" | "yes" | "1" => Ok(true),
226         "false" | "off" | "no" | "0" => Ok(false),
227         _ => Err(SetError::BadValue("bool".to_string())),
228     }
229 }
230 
231 fn parse_enum_value(value: &str, choices: &[&str]) -> SetResult<u8> {
232     match choices.iter().position(|&tag| tag == value) {
233         Some(idx) => Ok(idx as u8),
234         None => Err(SetError::BadValue(format!(
235             "any among {}",
236             choices.join(", ")
237         ))),
238     }
239 }
240 
241 impl Configurable for Builder {
242     fn enable(&mut self, name: &str) -> SetResult<()> {
243         use self::detail::Detail;
244         let (offset, detail) = self.lookup(name)?;
245         match detail {
246             Detail::Bool { bit } => {
247                 self.set_bit(offset, bit, true);
248                 Ok(())
249             }
250             Detail::Preset => {
251                 self.apply_preset(&self.template.presets[offset..]);
252                 Ok(())
253             }
254             _ => Err(SetError::BadType),
255         }
256     }
257 
258     fn set(&mut self, name: &str, value: &str) -> SetResult<()> {
259         use self::detail::Detail;
260         let (offset, detail) = self.lookup(name)?;
261         match detail {
262             Detail::Bool { bit } => {
263                 self.set_bit(offset, bit, parse_bool_value(value)?);
264             }
265             Detail::Num => {
266                 self.bytes[offset] = value
267                     .parse()
268                     .map_err(|_| SetError::BadValue("number".to_string()))?;
269             }
270             Detail::Enum { last, enumerators } => {
271                 self.bytes[offset] =
272                     parse_enum_value(value, self.template.enums(last, enumerators))?;
273             }
274             Detail::Preset => return Err(SetError::BadName(name.to_string())),
275         }
276         Ok(())
277     }
278 }
279 
280 /// An error produced when changing a setting.
281 #[derive(Debug, PartialEq, Eq)]
282 pub enum SetError {
283     /// No setting by this name exists.
284     BadName(String),
285 
286     /// Type mismatch for setting (e.g., setting an enum setting as a bool).
287     BadType,
288 
289     /// This is not a valid value for this setting.
290     BadValue(String),
291 }
292 
293 impl std::error::Error for SetError {}
294 
295 impl fmt::Display for SetError {
296     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
297         match self {
298             SetError::BadName(name) => write!(f, "No existing setting named '{name}'"),
299             SetError::BadType => {
300                 write!(f, "Trying to set a setting with the wrong type")
301             }
302             SetError::BadValue(value) => {
303                 write!(f, "Unexpected value for a setting, expected {value}")
304             }
305         }
306     }
307 }
308 
309 /// A result returned when changing a setting.
310 pub type SetResult<T> = Result<T, SetError>;
311 
312 /// A reference to just the boolean predicates of a settings object.
313 ///
314 /// The settings objects themselves are generated and appear in the `isa/*/settings.rs` modules.
315 /// Each settings object provides a `predicate_view()` method that makes it possible to query
316 /// ISA predicates by number.
317 #[derive(Clone, Copy, Hash)]
318 pub struct PredicateView<'a>(&'a [u8]);
319 
320 impl<'a> PredicateView<'a> {
321     /// Create a new view of a precomputed predicate vector.
322     ///
323     /// See the `predicate_view()` method on the various `Flags` types defined for each ISA.
324     pub fn new(bits: &'a [u8]) -> Self {
325         PredicateView(bits)
326     }
327 
328     /// Check a numbered predicate.
329     pub fn test(self, p: usize) -> bool {
330         self.0[p / 8] & (1 << (p % 8)) != 0
331     }
332 }
333 
334 /// Implementation details for generated code.
335 ///
336 /// This module holds definitions that need to be public so the can be instantiated by generated
337 /// code in other modules.
338 pub mod detail {
339     use crate::constant_hash;
340     use core::fmt;
341     use core::hash::Hash;
342 
343     /// An instruction group template.
344     #[derive(Hash)]
345     pub struct Template {
346         /// Name of the instruction group.
347         pub name: &'static str,
348         /// List of setting descriptors.
349         pub descriptors: &'static [Descriptor],
350         /// Union of all enumerators.
351         pub enumerators: &'static [&'static str],
352         /// Hash table of settings.
353         pub hash_table: &'static [u16],
354         /// Default values.
355         pub defaults: &'static [u8],
356         /// Pairs of (mask, value) for presets.
357         pub presets: &'static [(u8, u8)],
358     }
359 
360     impl Template {
361         /// Get enumerators corresponding to a `Details::Enum`.
362         pub fn enums(&self, last: u8, enumerators: u16) -> &[&'static str] {
363             let from = enumerators as usize;
364             let len = usize::from(last) + 1;
365             &self.enumerators[from..from + len]
366         }
367 
368         /// Format a setting value as a TOML string. This is mostly for use by the generated
369         /// `Display` implementation.
370         pub fn format_toml_value(
371             &self,
372             detail: Detail,
373             byte: u8,
374             f: &mut fmt::Formatter,
375         ) -> fmt::Result {
376             match detail {
377                 Detail::Bool { bit } => write!(f, "{}", (byte & (1 << bit)) != 0),
378                 Detail::Num => write!(f, "{byte}"),
379                 Detail::Enum { last, enumerators } => {
380                     if byte <= last {
381                         let tags = self.enums(last, enumerators);
382                         write!(f, "\"{}\"", tags[usize::from(byte)])
383                     } else {
384                         write!(f, "{byte}")
385                     }
386                 }
387                 // Presets aren't printed. They are reflected in the other settings.
388                 Detail::Preset { .. } => Ok(()),
389             }
390         }
391     }
392 
393     /// The template contains a hash table for by-name lookup.
394     impl<'a> constant_hash::Table<&'a str> for Template {
395         fn len(&self) -> usize {
396             self.hash_table.len()
397         }
398 
399         fn key(&self, idx: usize) -> Option<&'a str> {
400             let e = self.hash_table[idx] as usize;
401             if e < self.descriptors.len() {
402                 Some(self.descriptors[e].name)
403             } else {
404                 None
405             }
406         }
407     }
408 
409     /// A setting descriptor holds the information needed to generically set and print a setting.
410     ///
411     /// Each settings group will be represented as a constant DESCRIPTORS array.
412     #[derive(Hash)]
413     pub struct Descriptor {
414         /// Lower snake-case name of setting as defined in meta.
415         pub name: &'static str,
416 
417         /// The description of the setting.
418         pub description: &'static str,
419 
420         /// Offset of byte containing this setting.
421         pub offset: u32,
422 
423         /// Additional details, depending on the kind of setting.
424         pub detail: Detail,
425     }
426 
427     /// The different kind of settings along with descriptor bits that depend on the kind.
428     #[derive(Clone, Copy, Hash)]
429     pub enum Detail {
430         /// A boolean setting only uses one bit, numbered from LSB.
431         Bool {
432             /// 0-7.
433             bit: u8,
434         },
435 
436         /// A numerical setting uses the whole byte.
437         Num,
438 
439         /// An Enum setting uses a range of enumerators.
440         Enum {
441             /// Numerical value of last enumerator, allowing for 1-256 enumerators.
442             last: u8,
443 
444             /// First enumerator in the ENUMERATORS table.
445             enumerators: u16,
446         },
447 
448         /// A preset is not an individual setting, it is a collection of settings applied at once.
449         ///
450         /// The `Descriptor::offset` field refers to the `PRESETS` table.
451         Preset,
452     }
453 
454     impl Detail {
455         /// Check if a detail is a Detail::Preset. Useful because the Descriptor
456         /// offset field has a different meaning when the detail is a preset.
457         pub fn is_preset(self) -> bool {
458             match self {
459                 Self::Preset => true,
460                 _ => false,
461             }
462         }
463     }
464 }
465 
466 // Include code generated by `meta/gen_settings.rs`. This file contains a public `Flags` struct
467 // with an implementation for all of the settings defined in
468 // `cranelift-codegen/meta/src/shared/settings.rs`.
469 include!(concat!(env!("OUT_DIR"), "/settings.rs"));
470 
471 /// Wrapper containing flags and optionally a `TargetIsa` trait object.
472 ///
473 /// A few passes need to access the flags but only optionally a target ISA. The `FlagsOrIsa`
474 /// wrapper can be used to pass either, and extract the flags so they are always accessible.
475 #[derive(Clone, Copy)]
476 pub struct FlagsOrIsa<'a> {
477     /// Flags are always present.
478     pub flags: &'a Flags,
479 
480     /// The ISA may not be present.
481     pub isa: Option<&'a dyn TargetIsa>,
482 }
483 
484 impl<'a> From<&'a Flags> for FlagsOrIsa<'a> {
485     fn from(flags: &'a Flags) -> FlagsOrIsa<'a> {
486         FlagsOrIsa { flags, isa: None }
487     }
488 }
489 
490 impl<'a> From<&'a dyn TargetIsa> for FlagsOrIsa<'a> {
491     fn from(isa: &'a dyn TargetIsa) -> FlagsOrIsa<'a> {
492         FlagsOrIsa {
493             flags: isa.flags(),
494             isa: Some(isa),
495         }
496     }
497 }
498 
499 #[cfg(test)]
500 mod tests {
501     use super::Configurable;
502     use super::SetError::*;
503     use super::{builder, Flags};
504     use alloc::string::ToString;
505 
506     #[test]
507     fn display_default() {
508         let b = builder();
509         let f = Flags::new(b);
510         let actual = f.to_string();
511         let expected = r#"[shared]
512 regalloc_algorithm = "backtracking"
513 opt_level = "none"
514 tls_model = "none"
515 stack_switch_model = "none"
516 libcall_call_conv = "isa_default"
517 probestack_size_log2 = 12
518 probestack_strategy = "outline"
519 bb_padding_log2_minus_one = 0
520 regalloc_checker = false
521 regalloc_verbose_logs = false
522 enable_alias_analysis = true
523 enable_verifier = true
524 enable_pcc = false
525 is_pic = false
526 use_colocated_libcalls = false
527 enable_float = true
528 enable_nan_canonicalization = false
529 enable_pinned_reg = false
530 enable_atomics = true
531 enable_safepoints = false
532 enable_llvm_abi_extensions = false
533 enable_multi_ret_implicit_sret = false
534 unwind_info = true
535 preserve_frame_pointers = false
536 machine_code_cfg_info = false
537 enable_probestack = false
538 enable_jump_tables = true
539 enable_heap_access_spectre_mitigation = true
540 enable_table_access_spectre_mitigation = true
541 enable_incremental_compilation_cache_checks = false
542 "#;
543         if actual != expected {
544             panic!(
545                 "Default settings do not match expectations:\n\n{}",
546                 similar::TextDiff::from_lines(expected, &actual)
547                     .unified_diff()
548                     .header("expected", "actual")
549             );
550         }
551         assert_eq!(f.opt_level(), super::OptLevel::None);
552     }
553 
554     #[test]
555     fn modify_bool() {
556         let mut b = builder();
557         assert_eq!(b.enable("not_there"), Err(BadName("not_there".to_string())));
558         assert_eq!(b.enable("enable_atomics"), Ok(()));
559         assert_eq!(b.set("enable_atomics", "false"), Ok(()));
560 
561         let f = Flags::new(b);
562         assert_eq!(f.enable_atomics(), false);
563     }
564 
565     #[test]
566     fn modify_string() {
567         let mut b = builder();
568         assert_eq!(
569             b.set("not_there", "true"),
570             Err(BadName("not_there".to_string()))
571         );
572         assert_eq!(
573             b.set("enable_atomics", ""),
574             Err(BadValue("bool".to_string()))
575         );
576         assert_eq!(
577             b.set("enable_atomics", "best"),
578             Err(BadValue("bool".to_string()))
579         );
580         assert_eq!(
581             b.set("opt_level", "true"),
582             Err(BadValue(
583                 "any among none, speed, speed_and_size".to_string()
584             ))
585         );
586         assert_eq!(b.set("opt_level", "speed"), Ok(()));
587         assert_eq!(b.set("enable_atomics", "0"), Ok(()));
588 
589         let f = Flags::new(b);
590         assert_eq!(f.enable_atomics(), false);
591         assert_eq!(f.opt_level(), super::OptLevel::Speed);
592     }
593 }
594