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

SimpleDeadParameterElimination causes "undefined variable" errors #115

Open
AlexandreTunstall opened this issue Jul 11, 2020 · 0 comments
Open

Comments

@AlexandreTunstall
Copy link

GRIN Version: 310fdc3a184353049213d28c84980345d1ff66fd (current master)

Save the following GRIN program in src.grin and then optimise it with grin src.grin --optimize.

grinMain =
  u.box <- pure (CUnit)
  u.thunk <- store u.box
  w.box <- pure (Ffoo u.thunk u.thunk)
  w.thunk <- store w.box
  foo $ w.thunk u.thunk

foo a.thunk b.thunk =
  (Ffoo c.thunk d.thunk) <- fetch a.thunk
  (CUnit) <- fetch d.thunk
  out.prim_int <- pure 0
  _prim_int_print $ out.prim_int

Optimisation will fail with output:

 PipelineStep: Optimize                                                                       PHASE #1
  PipelineStep: T BindNormalisation                                                           had effect: None (0.001351 ms)
  PipelineStep: T SimpleDeadFunctionElimination                                               had effect: ExpChanged (0.001667 ms)
  PipelineStep: SaveGrin (Rel "SimpleDeadFunctionElimination.grin")                           (0.311913 ms)
  PipelineStep: T SimpleDeadParameterElimination                                              had effect: ExpChanged (0.001111 ms)
  PipelineStep: SaveGrin (Rel "SimpleDeadParameterElimination.grin")                          (0.223380 ms)
 error after SimpleDeadParameterElimination:
undefined variable: d.thunk

illegal code

(Note: SimpleDeadFunctionElimination reports had effect: ExpChanged yet its output is identical to the original program.)

The SimpleDeadParameterElimination step eliminates b.thunk and d.thunk, but (CUnit) <- fetch d.thunk is left intact.
Below is .output/002.SimpleDeadParameterElimination

grinMain =
  u.box <- pure (CUnit)
  u.thunk <- store u.box
  w.box <- pure (Ffoo u.thunk)
  w.thunk <- store w.box
  foo $ w.thunk

foo a.thunk =
  (Ffoo c.thunk) <- fetch a.thunk
  (CUnit) <- fetch d.thunk
  out.prim_int <- pure 0
  _prim_int_print $ out.prim_int

Linting src.grin with --lint shows that the program is correct, so this appears to be due to either a bug in the linter, or a bug in SimpleDeadParameterElimination.

This problem initially arose when I was trying to optimise a generated program (which is also correct according to the linter).
That generated program is 282 lines long, so I opted to hand-write a minimal example instead.

Z-snails added a commit to Z-snails/Idris2-Grin that referenced this issue Mar 15, 2021
- Changed back to 'CPS' (passes the variable the final result is in)
- Added a few more primitives
- Fixed variable indexes in primitive functions
- Moved some functions to Prims.idr beacause they're needed there
Unfortunately the GRIN compiler doesn't work properly yet
see (grin-compiler/grin#115) for details
This issue hasn't seen any interaction since last July so I have no idea if it will be fixed
But I will have a look at the code to see if I can fix it at some point
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant