Skip to content

InlineApply causes non-defined function when self recursive #125

@Z-snails

Description

@Z-snails

When the function apply includes a call to itself, InlineApply inlines the apply function, and replaces the occurance of it with apply.0, however this function doesn't exist.

test.grin

 apply fnp argp =
    fn <- fetch fnp
    case fn of
        (P1MyFun) ->
            x <- store (CNull)
            apply argp fnp
        (P1OtherFun) ->
            pure (CNull)

grinMain =
    fn1 <- store (P1MyFun)
    arg1 <- store (P1OtherFun)
    apply fn1 arg1

grin --optimize test.grin

.output/002.InlineApply.grin



grinMain =
  fn1 <- store (P1MyFun)
  arg1 <- store (P1OtherFun)
  do
    fnp.0 <- pure fn1
    argp.0 <- pure arg1
    fn.0 <- fetch fnp.0
    case fn.0 of
      (P1MyFun) ->
        x.0 <- store (CNull)
        apply.0 $ argp.0 fnp.0 -- Problem is here
      (P1OtherFun) ->
        pure (CNull)

This is a handwritten example, but I encountered this problem when compiling a simple program with my idris2 grin backend, as inlining of other functions caused apply to be self recursive.

I can think of 2 solutions:

  1. don't inline apply if it's self recursive - this could reduce effectiveness of other optimisations
  2. keep the definition of apply around after inling.

I don't know the grin codebase very well, but I'll have a go at fixing this. I really don't know the codebase or recursion schemes, so I can't fix it (at least not without learning recursion schemes first).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions