cucapra/braid

Generalize Emitter to support the LLVM backend

sampsyo opened this issue · 4 comments

Currently, the Emitter interface assumes that you're producing strings. This works for generating JavaScript and GLSL, but it won't work for LLVM, where we'll need the functions to produce llvm.Value and other such objects.

One way to get started might be to make Emitter a generic interface: i.e., Emitter<string> would be the current configuration. Or, it might be less disruptive to make the current Emitter inherit from a new BaseEmitter that adds more generality.

Likewise, we might be able to generalize some of the stuff in emitutils.ts.

After staring at this for a little while, it seems to me that there is a second big difference between the existing backends and the new LLVM backend: statefulness. In the string-based backends, a call to emit any expression returns a string; each function is pure. In the LLVM backend, there's really no way around using an imperative style: a call to emit an expression will insert new instructions into the program via calls to the Builder.

For that reason, I totally agree with your strategy in #5 to start with a totally separate LLVMEmitter that doesn't share anything with the base emitters… and then later try to merge what we can. But the two styles will be so different that merging them might not be as straightforward as we thought.

For performance reasons, we might even eventually want to move the old emitters to the new imperative style. Passing around short strings everywhere is probably not great for efficiency.

OK, after having done my fiddling, I think that's my conclusion for now: let's stick with building a totally separate backend that doesn't reuse much of the existing infrastructure. If this means duplicating a bunch of stuff in emitutil and emitter, so be it—let's build something messy now and see what we can align later.

Thanks for talking through this!

Sounds like a plan. Will do!

I'll try to at least make it clear what's duplicated though. Just so it's easier to clean things up, should we ever decide to.