Compute scope issue with analyzer: invalid simplification

Problem: the analyzer_ in CodeGenLLVM and derived classes can generate invalid code for outlined functions.

Say, you have code like this

let x = y in
  // attr compute_scope
  blah = x

Then it gets outlined in codegen_cpu (for example):

let x = y in
  call foo(x)

foo(x) {
  blah = x
}

Now, if you were to run analyzer_->Simplify on the body of foo, you’d get

foo(x) {
  blah = y
}

Because the analyzer_ knows that x is same as y (because of the Let statemement), but doesn’t know that y is no longer available in the outlined function foo.

What should we do?

  • P0: The simplest thing would be to create a new analyzer_ object in the same way as we create new var_map_ for outlined functions, but this may sacrifice performance.
  • P1: The other option would be to “unbind” all variables in the analyzer whose corresponding expressions have undefined variables. This would require changes to the Analyzer class: (1) we’d need to be able to create a copy of Analyzer (which is forbidden now), and (2) we’d need to create a function restrict or something like that to unbind all variables with definitions from outside of a given set.

Thanks @kparzysz , this indeed can be a problem, perhaps we could start with P0 for now?

Will do. I’ll try to come up with a testcase (this originally showed up in our downstream code).

PR for P0: https://github.com/apache/tvm/pull/9117

I didn’t come up with a testcase, because for the problem to appear in the upstream code, Simplify would have to be called after outlining, still within CodeGenCPU (and there are no calls to Simplify in there). We have that in our downstream code in CodeGenHexagon, but that code is not present upstream at the moment.