ykjit / yk

yk packages
https://ykjit.github.io/yk/
Other
29 stars 7 forks source link

Homogenising hwt + swt and fixing swt #980

Open ltratt opened 7 months ago

ltratt commented 7 months ago

Consider (and I'll use Rust syntax because I'm lazy) code like this:

trait T { fn m() ; }
// A mappable module
struct S1;
impl T for S1 { fn m() { ... } }
// An unmappable module
struct S2;
impl T for S2 { fn m() { ... } }

fn main() {
  let x: Box<dyn T> = if ... { Box::new(S1) } else { Box::new(S2) };
  x.m();
  ...
}

We don't know statically if the indirect call x.m() will call S1::m or S2::m.

hwt solves this because PT merrily traces through both mappable and unmappable functions, so we know at run-time which m was called.

swt, though, doesn't know which m will be called. We could say "if the call to x.m() is followed by a mappable block that isn't ... then we called S1::m otherwise we called S2::m. However, there is a wonderful pathological case where we call S2::m which then calls S1::m: we can't distinguish that case from calling S1::m directly.

The problem is that swt needs to "look into" unmappable functions, but it can't do so. Fortunately we have a probable solution (in conjunction with @ptersilie and @Pavel-Durov). First, swt adds a func_record function at every indirect function which records the function pointer that's about to be called. So our main should be transformed by ykllvm into something like:

fn main() {
  let x: Box<dyn T> = if ... { Box::new(S1) } else { Box::new(S2) };
  func_record(x.m);
  x.m();
  ...
}

func_record allows us to check with 100% precision "do we have mapped blocks for the function pointed to by x?". We then change TraceAction from talking about "Mapped" and "Unmapped" blocks to "Mapped blocks" and "Indirect calls". This allows us to distinguish all the cases we could come up with so far.

This will also require a change in hwt so that it deals with this change to TraceAction.

ltratt commented 6 months ago

https://github.com/ykjit/yk/pull/1086 has identified some more tests that fail in some situations with swt.