Mind is getting blown by multi-stage programming! Does anyone know of any simple examples of it being used as a way of doing offline partial evaluation in compilation? Most of the stuff I see is for run-time code generation. 🤔
Conversation
Is there any inherent difference between "online" and "offline" techniques; rather than just whether you choose to have it run before or after you ship the software?
(I was going to ask if you're familiar with LMS, and link cs.purdue.edu/homes/rompf/pa, but seems that's "online".)
1
Yeah I've seen LMS before, and it looks really interesting! "On Cross-Stage Persistence in Multi-Stage Programming" seems to suggest there is: fos.kuis.kyoto-u.ac.jp/~igarashi/pape
1
2
"MetaOCaml is not very suitable for writing offline generators because of CSP [Cross-Stage Persistence]. Our type system introduces residualizable code types to solve the problem."
1
Haven't yet understood this claim, and the pros and cons. Would also really love to see some example implementations of compilers that do offline partial evaluation using this kind of thing (but that might be hoping for too much).
1
1
There is a difference between partial evaluation and supercompilation but I always forget what it is. There was a bunch of supercompilation-related work on GHC a few years back but I assume you're already familiar with that as well. :)
1
1
So the issue is that if you're generating code and the generated code references an arbitrary object on the heap of the code-generator, either (a) the generated code must run in the address space of the generator or (b) you need to know how to serialize that object into syntax.
1
1
(Or (c) you need to detect and rule out such a circumstance in the first place.)
1
I'm kinda wondering, if the code generator is a normal AOT compiler, whether it couldn't "just" dump the memory for such objects almost-as-is into the generated binary as static data. It'd still need to "know where the pointers are", at least, for to fix them up...
1
Maybe there's cases where the graph-closure of the object contains things for which this really is infeasible... like function pointers into the compiler's own code? I dunno.
I also suppose this wouldn't be on good terms with cross-compilation. :|
1
Replying to
Yeah the cross compilation is interesting - I dunno if you’d need to run an interpreter that emulates the target.... 🤔

