cranelift_codegen_meta/cdsl/
settings.rs

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
use std::iter;

#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub(crate) struct BoolSettingIndex(usize);

#[derive(Hash, PartialEq, Eq)]
pub(crate) struct BoolSetting {
    pub default: bool,
    pub bit_offset: u8,
    pub predicate_number: u8,
}

#[derive(Hash, PartialEq, Eq)]
pub(crate) enum SpecificSetting {
    Bool(BoolSetting),
    Enum(Vec<&'static str>),
    Num(u8),
}

#[derive(Hash, PartialEq, Eq)]
pub(crate) struct Setting {
    pub name: &'static str,
    pub description: &'static str,
    pub comment: &'static str,
    pub specific: SpecificSetting,
    pub byte_offset: u8,
}

impl Setting {
    pub fn default_byte(&self) -> u8 {
        match self.specific {
            SpecificSetting::Bool(BoolSetting {
                default,
                bit_offset,
                ..
            }) => {
                if default {
                    1 << bit_offset
                } else {
                    0
                }
            }
            SpecificSetting::Enum(_) => 0,
            SpecificSetting::Num(default) => default,
        }
    }

    fn byte_for_value(&self, v: bool) -> u8 {
        match self.specific {
            SpecificSetting::Bool(BoolSetting { bit_offset, .. }) => {
                if v {
                    1 << bit_offset
                } else {
                    0
                }
            }
            _ => panic!("byte_for_value shouldn't be used for non-boolean settings."),
        }
    }

    fn byte_mask(&self) -> u8 {
        match self.specific {
            SpecificSetting::Bool(BoolSetting { bit_offset, .. }) => 1 << bit_offset,
            _ => panic!("byte_for_value shouldn't be used for non-boolean settings."),
        }
    }
}

#[derive(Hash, PartialEq, Eq, Copy, Clone)]
pub(crate) struct PresetIndex(usize);

#[derive(Hash, PartialEq, Eq)]
pub(crate) enum PresetType {
    BoolSetting(BoolSettingIndex),
    OtherPreset(PresetIndex),
}

impl From<BoolSettingIndex> for PresetType {
    fn from(bool_setting_index: BoolSettingIndex) -> Self {
        PresetType::BoolSetting(bool_setting_index)
    }
}
impl From<PresetIndex> for PresetType {
    fn from(value: PresetIndex) -> Self {
        PresetType::OtherPreset(value)
    }
}

#[derive(Hash, PartialEq, Eq)]
pub(crate) struct Preset {
    pub name: &'static str,
    pub description: &'static str,
    values: Vec<BoolSettingIndex>,
}

impl Preset {
    pub fn layout(&self, group: &SettingGroup) -> Vec<(u8, u8)> {
        let mut layout: Vec<(u8, u8)> = iter::repeat((0, 0))
            .take(group.settings_size as usize)
            .collect();
        for bool_index in &self.values {
            let setting = &group.settings[bool_index.0];
            let mask = setting.byte_mask();
            let val = setting.byte_for_value(true);
            assert!((val & !mask) == 0);
            let (ref mut l_mask, ref mut l_val) =
                *layout.get_mut(setting.byte_offset as usize).unwrap();
            *l_mask |= mask;
            *l_val = (*l_val & !mask) | val;
        }
        layout
    }

    pub fn setting_names<'a>(
        &'a self,
        group: &'a SettingGroup,
    ) -> impl Iterator<Item = &'static str> + 'a {
        self.values
            .iter()
            .map(|bool_index| group.settings[bool_index.0].name)
    }
}

pub(crate) struct SettingGroup {
    pub name: &'static str,
    pub settings: Vec<Setting>,
    pub bool_start_byte_offset: u8,
    pub settings_size: u8,
    pub presets: Vec<Preset>,
    pub predicates: Vec<Predicate>,
}

impl SettingGroup {
    fn num_bool_settings(&self) -> u8 {
        self.settings
            .iter()
            .filter(|s| matches!(s.specific, SpecificSetting::Bool(_)))
            .count() as u8
    }

    pub fn byte_size(&self) -> u8 {
        let num_predicates = self.num_bool_settings() + (self.predicates.len() as u8);
        self.bool_start_byte_offset + (num_predicates + 7) / 8
    }
}

/// This is the basic information needed to track the specific parts of a setting when building
/// them.
pub(crate) enum ProtoSpecificSetting {
    Bool(bool),
    Enum(Vec<&'static str>),
    Num(u8),
}

/// This is the information provided during building for a setting.
struct ProtoSetting {
    name: &'static str,
    description: &'static str,
    comment: &'static str,
    specific: ProtoSpecificSetting,
}

#[derive(Hash, PartialEq, Eq)]
pub(crate) enum PredicateNode {
    OwnedBool(BoolSettingIndex),
    SharedBool(&'static str, &'static str),
    And(Box<PredicateNode>, Box<PredicateNode>),
}

impl From<BoolSettingIndex> for PredicateNode {
    fn from(bool_setting_index: BoolSettingIndex) -> Self {
        PredicateNode::OwnedBool(bool_setting_index)
    }
}

impl<'a> From<(BoolSettingIndex, &'a SettingGroup)> for PredicateNode {
    fn from(val: (BoolSettingIndex, &'a SettingGroup)) -> Self {
        let (index, group) = (val.0, val.1);
        let setting = &group.settings[index.0];
        PredicateNode::SharedBool(group.name, setting.name)
    }
}

impl PredicateNode {
    fn render(&self, group: &SettingGroup) -> String {
        match *self {
            PredicateNode::OwnedBool(bool_setting_index) => format!(
                "{}.{}()",
                group.name, group.settings[bool_setting_index.0].name
            ),
            PredicateNode::SharedBool(ref group_name, ref bool_name) => {
                format!("{group_name}.{bool_name}()")
            }
            PredicateNode::And(ref lhs, ref rhs) => {
                format!("{} && {}", lhs.render(group), rhs.render(group))
            }
        }
    }
}

struct ProtoPredicate {
    pub name: &'static str,
    node: PredicateNode,
}

pub(crate) type SettingPredicateNumber = u8;

pub(crate) struct Predicate {
    pub name: &'static str,
    node: PredicateNode,
    pub number: SettingPredicateNumber,
}

impl Predicate {
    pub fn render(&self, group: &SettingGroup) -> String {
        self.node.render(group)
    }
}

pub(crate) struct SettingGroupBuilder {
    name: &'static str,
    settings: Vec<ProtoSetting>,
    presets: Vec<Preset>,
    predicates: Vec<ProtoPredicate>,
}

impl SettingGroupBuilder {
    pub fn new(name: &'static str) -> Self {
        Self {
            name,
            settings: Vec::new(),
            presets: Vec::new(),
            predicates: Vec::new(),
        }
    }

    fn add_setting(
        &mut self,
        name: &'static str,
        description: &'static str,
        comment: &'static str,
        specific: ProtoSpecificSetting,
    ) {
        self.settings.push(ProtoSetting {
            name,
            description,
            comment,
            specific,
        })
    }

    pub fn add_bool(
        &mut self,
        name: &'static str,
        description: &'static str,
        comment: &'static str,
        default: bool,
    ) -> BoolSettingIndex {
        assert!(
            self.predicates.is_empty(),
            "predicates must be added after the boolean settings"
        );
        self.add_setting(
            name,
            description,
            comment,
            ProtoSpecificSetting::Bool(default),
        );
        BoolSettingIndex(self.settings.len() - 1)
    }

    pub fn add_enum(
        &mut self,
        name: &'static str,
        description: &'static str,
        comment: &'static str,
        values: Vec<&'static str>,
    ) {
        self.add_setting(
            name,
            description,
            comment,
            ProtoSpecificSetting::Enum(values),
        );
    }

    pub fn add_num(
        &mut self,
        name: &'static str,
        description: &'static str,
        comment: &'static str,
        default: u8,
    ) {
        self.add_setting(
            name,
            description,
            comment,
            ProtoSpecificSetting::Num(default),
        );
    }

    pub fn add_predicate(&mut self, name: &'static str, node: PredicateNode) {
        self.predicates.push(ProtoPredicate { name, node });
    }

    pub fn add_preset(
        &mut self,
        name: &'static str,
        description: &'static str,
        args: Vec<PresetType>,
    ) -> PresetIndex {
        let mut values = Vec::new();
        for arg in args {
            match arg {
                PresetType::OtherPreset(index) => {
                    values.extend(self.presets[index.0].values.iter());
                }
                PresetType::BoolSetting(index) => values.push(index),
            }
        }
        self.presets.push(Preset {
            name,
            description,
            values,
        });
        PresetIndex(self.presets.len() - 1)
    }

    /// Compute the layout of the byte vector used to represent this settings
    /// group.
    ///
    /// The byte vector contains the following entries in order:
    ///
    /// 1. Byte-sized settings like `NumSetting` and `EnumSetting`.
    /// 2. `BoolSetting` settings.
    /// 3. Precomputed named predicates.
    /// 4. Other numbered predicates, including parent predicates that need to be accessible by
    ///    number.
    ///
    /// Set `self.settings_size` to the length of the byte vector prefix that
    /// contains the settings. All bytes after that are computed, not
    /// configured.
    ///
    /// Set `self.boolean_offset` to the beginning of the numbered predicates,
    /// 2. in the list above.
    ///
    /// Assign `byte_offset` and `bit_offset` fields in all settings.
    pub fn build(self) -> SettingGroup {
        let mut group = SettingGroup {
            name: self.name,
            settings: Vec::new(),
            bool_start_byte_offset: 0,
            settings_size: 0,
            presets: Vec::new(),
            predicates: Vec::new(),
        };

        let mut byte_offset = 0;

        // Assign the non-boolean settings first.
        for s in &self.settings {
            let specific = match s.specific {
                ProtoSpecificSetting::Bool(..) => continue,
                ProtoSpecificSetting::Enum(ref values) => SpecificSetting::Enum(values.clone()),
                ProtoSpecificSetting::Num(default) => SpecificSetting::Num(default),
            };

            group.settings.push(Setting {
                name: s.name,
                description: s.description,
                comment: s.comment,
                byte_offset,
                specific,
            });

            byte_offset += 1;
        }

        group.bool_start_byte_offset = byte_offset;

        let mut predicate_number = 0;

        // Then the boolean settings.
        for s in &self.settings {
            let default = match s.specific {
                ProtoSpecificSetting::Bool(default) => default,
                ProtoSpecificSetting::Enum(_) | ProtoSpecificSetting::Num(_) => continue,
            };
            group.settings.push(Setting {
                name: s.name,
                description: s.description,
                comment: s.comment,
                byte_offset: byte_offset + predicate_number / 8,
                specific: SpecificSetting::Bool(BoolSetting {
                    default,
                    bit_offset: predicate_number % 8,
                    predicate_number,
                }),
            });
            predicate_number += 1;
        }

        assert!(
            group.predicates.is_empty(),
            "settings_size is the byte size before adding predicates"
        );
        group.settings_size = group.byte_size();

        // Sort predicates by name to ensure the same order as the Python code.
        let mut predicates = self.predicates;
        predicates.sort_by_key(|predicate| predicate.name);

        group
            .predicates
            .extend(predicates.into_iter().map(|predicate| {
                let number = predicate_number;
                predicate_number += 1;
                Predicate {
                    name: predicate.name,
                    node: predicate.node,
                    number,
                }
            }));

        group.presets.extend(self.presets);

        group
    }
}