celox-backend-x86 0.3.1

Celox x86-64 machine-code backend
Documentation
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
//! Verified non-iterative SSA register-allocation pipeline.

use std::collections::BTreeSet;

use crate::native::mir::{MFunction, VReg};

use super::assignment::AssignmentMap;
use super::cfg::NormalizedCfg;
use super::next_use::NextUseAnalysis;
use super::reload::{PlanningRecipes, PointUse};
use super::spill_plan::{PlannedEdgeOp, PlannedOp, SpillPlan};

pub(super) struct Allocation {
    pub assignment: AssignmentMap,
    pub spill_frame_size: u32,
}

/// Execute scheduling's downstream phases exactly once: W/S planning, SSA
/// reconstruction, late Perm construction, and implicit chordal coloring.
pub(super) fn allocate(
    func: &mut MFunction,
    cfg: &NormalizedCfg,
    next_use: &NextUseAnalysis,
    planning_recipes: &PlanningRecipes,
    constraints: &super::constraints::ConstraintModel,
    trace: Option<&mut super::RegallocTrace>,
    timing: bool,
    verify: bool,
) -> Result<Allocation, super::RegallocError> {
    let register_count = func.target_features.allocatable_register_count();
    let phase = timing.then(crate::timing::now);
    let mut plan = super::spill_plan::plan_with_integrated_schedule(
        func,
        cfg,
        next_use,
        planning_recipes,
        register_count,
        constraints,
    )
    .map_err(|error| {
        super::RegallocError::new(
            "spill planning",
            error.rule,
            error.block,
            error.instruction,
            error.values,
            error.message,
        )
    })?;
    if let Some(trace) = trace {
        trace.mir_after_scheduling = func.to_string();
    }
    if verify {
        plan.verify(func, cfg, register_count).map_err(|error| {
            super::RegallocError::new(
                "spill-plan verification",
                error.rule,
                error.block,
                error.instruction,
                error.values,
                error.message,
            )
        })?;
    }
    if let Some(start) = phase {
        tracing::debug!(
            "[regalloc-timing] ssa spill_plan elapsed={:?}",
            start.elapsed()
        );
    }

    let phase = timing.then(crate::timing::now);
    super::ssa_state_home::select(func, cfg, &mut plan).map_err(|error| {
        super::RegallocError::new(
            "packed state-home selection",
            error.rule,
            error.block,
            error.instruction,
            error.values,
            error.message,
        )
    })?;
    if verify {
        super::ssa_state_home::verify(func, cfg, &plan).map_err(|error| {
            super::RegallocError::new(
                "packed state-home verification",
                error.rule,
                error.block,
                error.instruction,
                error.values,
                error.message,
            )
        })?;
    }
    if let Some(start) = phase {
        tracing::debug!(
            "[regalloc-timing] ssa packed_state_homes homes={} reloads={} elapsed={:?}",
            plan.state_homes.len(),
            plan.state_reload_recipes.len(),
            start.elapsed()
        );
    }

    // Edge coupling operations are chosen by the spill planner, so their
    // materialization points do not exist as MIR uses during the first recipe
    // analysis. Query their exact CFG-isolated insertion points only after the
    // complete plan is available.
    let phase = timing.then(crate::timing::now);
    let requested_points = planner_reload_queries(func, cfg, &plan)?;
    let reload_recipes = super::reload::analyze_with_queries(func, cfg, &requested_points)
        .map_err(|error| {
            super::reload_recipe_error("spill-planner reload-recipe analysis", error)
        })?;
    plan.select_recipe_homes(func, cfg, &reload_recipes)
        .map_err(|error| {
            super::RegallocError::new(
                "spill-home selection",
                error.rule,
                error.block,
                error.instruction,
                error.values,
                error.message,
            )
        })?;
    if verify {
        plan.verify(func, cfg, register_count).map_err(|error| {
            super::RegallocError::new(
                "final spill-plan verification",
                error.rule,
                error.block,
                error.instruction,
                error.values,
                error.message,
            )
        })?;
        plan.verify_recipe_homes(func, cfg, &reload_recipes)
            .map_err(|error| {
                super::RegallocError::new(
                    "recipe-home verification",
                    error.rule,
                    error.block,
                    error.instruction,
                    error.values,
                    error.message,
                )
            })?;
        super::ssa_state_home::verify(func, cfg, &plan).map_err(|error| {
            super::RegallocError::new(
                "final packed state-home verification",
                error.rule,
                error.block,
                error.instruction,
                error.values,
                error.message,
            )
        })?;
        if let Err(error) = super::home_verify::verify(func, cfg, &plan) {
            let (block, instruction) = match error.location {
                Some(super::home_verify::HomeLocation::Point(point)) => {
                    (Some(point.block), Some(point.instruction))
                }
                Some(super::home_verify::HomeLocation::Edge { predecessor, .. }) => {
                    (Some(predecessor), None)
                }
                None => (None, None),
            };
            return Err(super::RegallocError::new(
                "spill-home verification",
                error.rule,
                block,
                instruction,
                error.value.map(|value| VReg(value.0)).into_iter().collect(),
                error.message,
            ));
        }
    }
    if let Some(start) = phase {
        tracing::debug!(
            "[regalloc-timing] ssa planner_reload_recipe_analyze_verify queries={} elapsed={:?}",
            requested_points.len(),
            start.elapsed()
        );
    }

    let phase = timing.then(crate::timing::now);
    let reconstruction = super::reconstruct::reconstruct(
        func,
        cfg,
        &plan,
        next_use,
        &reload_recipes,
        timing,
        verify,
    )
    .map_err(|error| {
        super::RegallocError::new(
            "SSA reconstruction",
            error.rule,
            error.block,
            error.instruction,
            error.values,
            error.message,
        )
    })?;
    if let Some(start) = phase {
        tracing::debug!(
            "[regalloc-timing] ssa reconstruct vregs={} insts={} frame={} shared_reload_blocks={} elapsed={:?}",
            func.vregs.count(),
            func.blocks
                .iter()
                .map(|block| block.insts.len())
                .sum::<usize>(),
            reconstruction.frame_size,
            reconstruction.shared_reload_blocks.len(),
            start.elapsed()
        );
    }
    if verify {
        func.verify_result().map_err(|error| {
            super::RegallocError::mir("SSA reconstruction structural verification", error)
        })?;
    }

    // Shared edge-reload tails add real CFG blocks after the original
    // allocation graph was frozen. Rebuild the normalized graph once, then
    // use it for every independent post-reconstruction proof and coloring
    // phase. No spill planning or allocation retry is performed.
    let reconstructed_cfg = if !reconstruction.shared_reload_blocks.is_empty() {
        let rebuilt = super::cfg::normalize(func)
            .map_err(|error| super::cfg_error("shared reload CFG normalization", error))?;
        if verify {
            rebuilt.verify(func).map_err(|error| {
                super::cfg_error("shared reload CFG normalization verification", error)
            })?;
            func.verify_result().map_err(|error| {
                super::RegallocError::mir("shared reload CFG structural verification", error)
            })?;
        }
        Some(rebuilt)
    } else {
        None
    };
    let cfg = reconstructed_cfg.as_ref().unwrap_or(cfg);

    if verify {
        super::materialized_state_home::verify_materialized_state_homes(
            func,
            cfg,
            &reconstruction.state_stores,
            &reconstruction.state_reloads,
        )
        .map_err(|error| {
            super::RegallocError::new(
                "materialized packed state-home verification",
                error.rule,
                error.block,
                error.instruction,
                error.values,
                error.message,
            )
        })?;
    }

    let inserted_state_writes = reconstruction
        .state_stores
        .iter()
        .map(|store| (store.block, store.write_ordinal))
        .collect::<Vec<_>>();
    if verify {
        super::reload::verify_expected_materialized_reloads_after_state_spills(
            func,
            cfg,
            &reconstruction.recipe_reloads,
            &inserted_state_writes,
            &reconstruction.shared_reload_blocks,
        )
        .map_err(|error| {
            super::reload_recipe_error("spill-planner reload-recipe verification", error)
        })?;
    }

    // Prove the spill result itself fits the machine before Perm boundaries
    // introduce fresh representatives.  This keeps pressure correctness
    // independent from constraint legalization and follows the frozen phase
    // order: reconstruct -> pressure proof -> Perm -> color.
    let phase = timing.then(crate::timing::now);
    if verify {
        let reconstructed_analysis = super::analysis::analyze(func);
        if let Err(error) = super::pressure::verify(func, &reconstructed_analysis, register_count) {
            let message = error.to_string();
            return Err(super::RegallocError::new(
                "reconstructed pressure verification",
                "PRESSURE.EXCEEDS_CAPACITY",
                Some(error.block),
                Some(error.instruction),
                error.values,
                message,
            ));
        }
    }
    if let Some(start) = phase {
        tracing::debug!(
            "[regalloc-timing] ssa reconstructed_pressure_verify elapsed={:?}",
            start.elapsed()
        );
    }

    let phase = timing.then(crate::timing::now);
    let (color_cfg, perms) =
        super::legalize::materialize_constraint_perms(func, cfg, register_count).map_err(
            |error| {
                super::RegallocError::new(
                    "Perm materialization and verification",
                    error.rule,
                    error.block,
                    error.instruction,
                    error.values,
                    error.message,
                )
            },
        )?;
    if verify {
        func.verify_result()
            .map_err(|error| super::RegallocError::mir("Perm structural verification", error))?;
    }
    if let Some(start) = phase {
        tracing::debug!(
            "[regalloc-timing] ssa constraint_perms boundaries={} vregs={} elapsed={:?}",
            perms.boundaries.len(),
            func.vregs.count(),
            start.elapsed()
        );
    }

    let phase = timing.then(crate::timing::now);
    let analysis = super::analysis::analyze(func);
    let coloring = super::color::color_ssa(func, &color_cfg, &analysis, &perms, register_count)
        .map_err(|error| {
            let message = error.to_string();
            super::RegallocError::new(
                "SSA coloring",
                error.rule,
                Some(error.block),
                error.instruction,
                error.value.into_iter().chain(error.related).collect(),
                message,
            )
        })?;
    if verify {
        for (&destination, &register) in &coloring.perm_matching {
            if coloring.assignment.get(destination) != Some(register) {
                return Err(super::RegallocError::new(
                    "SSA coloring verification",
                    "COLOR.PERM_MATCHING_APPLIED",
                    None,
                    None,
                    vec![destination],
                    format!("Perm matching color {register:?} was not applied"),
                ));
            }
        }
    }
    if let Some(start) = phase {
        tracing::debug!("[regalloc-timing] ssa color elapsed={:?}", start.elapsed());
    }

    Ok(Allocation {
        assignment: coloring.assignment,
        spill_frame_size: reconstruction.frame_size,
    })
}

pub(super) fn planner_reload_queries(
    func: &MFunction,
    cfg: &NormalizedCfg,
    plan: &SpillPlan,
) -> Result<BTreeSet<PointUse>, super::RegallocError> {
    let mut requested = BTreeSet::new();
    for &(point, operation) in &plan.point_ops {
        let PlannedOp::Reload { value, .. } = operation else {
            continue;
        };
        requested.insert(PointUse {
            block: point.block,
            instruction: point.instruction,
            value: VReg(value.0),
        });
    }
    for (&(predecessor, successor), operations) in &plan.edge_ops {
        if !operations
            .iter()
            .any(|operation| matches!(operation, PlannedEdgeOp::Reload { .. }))
        {
            continue;
        }
        let Some(predecessor_block) = func.blocks.get(predecessor) else {
            return Err(super::RegallocError::new(
                "spill-planner reload-recipe analysis",
                "RELOAD_RECIPE.EDGE_PREDECESSOR_EXISTS",
                None,
                None,
                Vec::new(),
                format!("edge operation predecessor index {predecessor} is outside function"),
            ));
        };
        let insertion = super::cfg::edge_insertion_point(func, cfg, predecessor, successor)
            .ok_or_else(|| {
                super::RegallocError::new(
                    "spill-planner reload-recipe analysis",
                    "RELOAD_RECIPE.EDGE_POINT",
                    Some(predecessor_block.id),
                    None,
                    Vec::new(),
                    "edge reload has no single-edge materialization point",
                )
            })?;
        let block = &func.blocks[insertion.block];
        if insertion.instruction >= block.insts.len() {
            return Err(super::RegallocError::new(
                "spill-planner reload-recipe analysis",
                "RELOAD_RECIPE.EDGE_POINT",
                Some(block.id),
                Some(insertion.instruction),
                Vec::new(),
                "edge reload insertion point is outside its MIR block",
            ));
        }
        for operation in operations {
            let PlannedEdgeOp::Reload { source, .. } = operation else {
                continue;
            };
            requested.insert(PointUse {
                block: block.id,
                instruction: insertion.instruction,
                value: VReg(source.0),
            });
        }
    }
    Ok(requested)
}