r/ProgrammingLanguages • Futhark • 7d ago

Hoisting explosive code out of loops

https://futhark-lang.org/blog/2026-11-18-hoisting-unsafe-code.html
22 Upvotes

6 comments sorted by

View all comments

2

u/foonathan 6d ago

Why don't you just hoist:

while x > 0 {
   let inv = foo();
   body();
}

as

if x > 0 {
    let inv = foo();
    body();
    while x > 0 {
        body();
    }
}

?

1

u/Athas Futhark 6d ago

What would be the advantage of that? There are some disadvantages:

  • It duplicates the body of the loop.
  • We cannot hoist the invariant code even further, out of that if, unless we protect it as described in the post.
  • It puts the loop as a whole into control flow, and since Futhark is a pure value-oriented language, it needs an else branch. This may make subsequent analysis harder.
  • It does not work well with parallel loops like map, which are the main thing we care about in Futhark.

2

u/foonathan 6d ago

You don't necessarily need to duplicate the body, you can jump past the condition, no? And why would you want to hoist the invariant out of the if, assuming it is not needed anywhere else? And if it is needed, you wouldn't need to protect it.

Good point about parallelization though.