Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize for affine values #216

Open
pca006132 opened this issue Dec 27, 2024 · 4 comments
Open

optimize for affine values #216

pca006132 opened this issue Dec 27, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@pca006132
Copy link

For SDFs like the metaballs where $r_i, p_i$ are the radius and the center for the ball $i$, and $g$ be some interpolation from 0 to 1:

$$f(p) = \sum_i g(r_i - \left\vert p - p_i \right\vert )$$

Oftentimes the interpolation function will be capped at 0 or 1, making some of the summands constants. However, currently there is no way to partially evaluate this and collapse those constant summands.

To optimize for this kind of situation, we can try to track affine values, e.g. $r * c_1 + c_2$ where $r$ is a register and $c_1, c_2$ are constants. We can mark some instructions like $r_3 = r_1 + r_2$ as dead if either $r_1$ or $r_2$ is a constant, or they are both affine values with the same register. And we can later convert this result back to an interval when necessary, i.e. when an instruction that does not produce an affine value takes it.

@mkeeter
Copy link
Owner

mkeeter commented Dec 28, 2024

Would short-circuiting Add and Mul opcodes work to simplify this kind of summation? e.g. something like

  • SpecialAdd performs addition, and could be simplified if either side was 0
  • SpecialMul performs multiplication, and could be simplified if either side was 0 or 1

I wrote And and Or opcodes for a similar short-circuiting use case (see #58), but – for reason I've forgotten – didn't give them Mul / Add semantics.

@pca006132
Copy link
Author

Short-circuiting Add and Mul can help, but it would be less general. That said, maybe the less general approach is good enough and can make evaluation/simplification faster :)

@avibryant
Copy link

This is a bit of a hobby horse for me but I've had very good results from this kind of affine representation in simplifying compute DAGs. There's a brief description of how I did it for Rainier at https://rainier.fit/img/rainier.pdf . It can be done as an IR that expands back into normal Add, Mul etc in the bytecode (which is what I did there also).

@pca006132
Copy link
Author

Note that this can also be helpful when you optimize the code statically, for example you can optimize the following without trying to do things like a re-association pass and rerun constant folding.

x1 = x + 1
x2 = x1 + 1
-----------
x1 = x + 1 // may now be dead code
x2 = x + 2

I did this in the manifold implementation (statically, tape optimization is the next thing to do) and this removes some more instructions.

@mkeeter mkeeter added the enhancement New feature or request label Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants