Newbie Question: not using local variables

I'm writing an ML-like language, and using LLVM as my target back end. I have one question though, and thought I'd throw it out onto the list. In my language, variables aren't mutable- once assigned, they can be shadowed, but not changed. And shadowing of variables is handled by alpha-renaming. What I mean by this that when I see:
   let x = 3 in ...
the value of x can not be changed. It can be shadowed, like:
   let x = f () in
   let x = x + 4 in
but one of the first things I do is rename all the variables so they're unique, so the above code might get changed into:
   let x_5734 = f () in
   let x_8643 = x_5734 + 4 in

Now, my question is this: is there any downside to not having any stack-based local variables at all- just put everything into registers and let the register allocation code decide what needs to get spilled onto the stack? In other words, is there any problem with generating the following IR for the above code:
   %r17 = call i64 @f(i64 0)
   %r18 = add i64 %r17 4

The upside of doing this is simpler code generation, and hopefully more efficient code, as I am explicitly telling the optimizer I don't care where this variable lives.

But, I'm somewhat worried that by not having any local variables at all, this may put too much pressure on the register allocator (especially for large functions). Also, how to attach debugging information to a register is something I need to figure out.

Thoughts? Opinions? Is this a good way to proceed, or a bad way?


It should work fine, since one of the first things LLVM does is to
turn as many allocas into registers as it can via mem2reg.


What Reid said, plus: the worst case of putting pressure on the register
allocator is that it spills something to the stack. Forcing the use of local
variables, by contrast, is basically just spilling to the stack all the time, so
you're no better off than you were before.


Hello Brian,

There is actually one potential downside to using only LLVM registers: it will (at the moment) prevent you from using any sort of moving garbage collector (copying, compacting, or generational) for your language. The reason is that LLVM’s garbage collection infrastructure does not (yet) compute register maps, only stack maps, and thus does not automatically re-load potentially moved pointers after safe points.

Also, IIRC debug metadata must be attached to stack slots.

But these issues are not worth worrying about yet. Do the simple, clear, and obvious thing first, then change tack if it proves to be insufficient for your needs.

Have fun!
– Ben

FWIW this is was true in past but not strictly true anymore, though it is a road less travelled at -O0. One would normally use @llvm.dbg.declare intrinsic to map variable info with the stack slots. Now, you can use @llvm.dbg.value intrinsic to map variable info with a llvm register.