Browse Source

`cranelift-frontend`: Fix stack maps and liveness for loops (#9071)

* cranelift-frontend: Fix stack maps and liveness for loops

Previously, we were not properly handling back edges. This manifested in values
incorrectly being considered not-live inside loop bodies where they definitely
were live. Consider the following example:

    block0:
      v0 = needs stack map

    block1:
      call foo(v0)
      call foo(v0)
      jump block1

We were previously considering `v0` live only for the first `call foo(v0)` but
not the second, because we mistakenly concluded that `v0` would not be used
again after that second `call`. While it won't be used again in *this* iteration
of the loop, it will be used again in the *next* iteration of the loop.

Trevor and I initially tried implementing a clever trick suggested by Chris
where, if we know the minimum post-order index of all of a block's transitive
predecessors, we can continue to compute liveness in a single pass over the
IR. We believed we could compute the minimum predecessor post-order index via
dynamic programming. It turns out, however, that approach doesn't provide
correct answers out of the box for certain kinds of irreducible control flow,
only nearly correct answers, and would require an additional clever fix-up pass
afterwards. We deemed this cleverness on cleverness unacceptable.

Instead, Trevor and I opted to implement a worklist algorithm where we process
blocks to a fixed-point. This has the advantages of being obviously correct and
producing more-precise results. It has the disadvantage of requiring multiple
passes over the IR in the face of loops and back edges. Because this analysis is
only used when needs-stack-map values are present (i.e. when the function uses
GC values) and is otherwise skipped, this additional compile-time overhead is
tolerable.

Co-Authored-By: Trevor Elliott <telliott@fastly.com>

* Add and update some comments based on review

---------

Co-authored-by: Trevor Elliott <telliott@fastly.com>
pull/9073/head
Nick Fitzgerald 3 months ago
committed by GitHub
parent
commit
dbc503f3fe
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 11
      cranelift/frontend/src/frontend.rs
  2. 1522
      cranelift/frontend/src/frontend/safepoints.rs
  3. 4
      cranelift/frontend/src/lib.rs

11
cranelift/frontend/src/frontend.rs

@ -1,7 +1,6 @@
//! A frontend for building Cranelift IR from other languages.
use crate::ssa::{SSABuilder, SideEffects};
use crate::variable::Variable;
use alloc::collections::BTreeSet;
use alloc::vec::Vec;
use core::fmt::{self, Debug};
use cranelift_codegen::cursor::{Cursor, CursorPosition, FuncCursor};
@ -34,7 +33,7 @@ pub struct FunctionBuilderContext {
types: SecondaryMap<Variable, Type>,
stack_map_vars: EntitySet<Variable>,
stack_map_values: EntitySet<Value>,
dfs: Dfs,
safepoints: safepoints::SafepointSpiller,
}
/// Temporary object used to build a single Cranelift IR [`Function`].
@ -75,14 +74,14 @@ impl FunctionBuilderContext {
types,
stack_map_vars,
stack_map_values,
dfs,
safepoints,
} = self;
ssa.clear();
status.clear();
types.clear();
stack_map_values.clear();
stack_map_vars.clear();
dfs.clear();
safepoints.clear();
}
fn is_empty(&self) -> bool {
@ -732,7 +731,9 @@ impl<'a> FunctionBuilder<'a> {
}
if !self.func_ctx.stack_map_values.is_empty() {
self.insert_safepoint_spills();
self.func_ctx
.safepoints
.run(&mut self.func, &self.func_ctx.stack_map_values);
}
// Clear the state (but preserve the allocated buffers) in preparation

1522
cranelift/frontend/src/frontend/safepoints.rs

File diff suppressed because it is too large

4
cranelift/frontend/src/lib.rs

@ -167,9 +167,9 @@ extern crate alloc;
extern crate std;
#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
use hashbrown::{HashMap, HashSet};
#[cfg(feature = "std")]
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
pub use crate::frontend::{FuncInstBuilder, FunctionBuilder, FunctionBuilderContext};
pub use crate::switch::Switch;

Loading…
Cancel
Save