[LLVMdev] Parametric polymorphism

me22 me22.ca at gmail.com
Tue Feb 17 21:08:43 PST 2009


On Tue, Feb 17, 2009 at 12:26, DeLesley SpamBox
<delesley.spambox at googlemail.com> wrote:
> After reading through the documentation, I noticed that llvm seems to
> have one major limitation -- the lack of parametric polymorphism.

I think the problem is deeper than that, in that LLVM has no official
concept of a subtype, so I don't see how the idea of polymorphism
could be defined in it.

> I would like to compile code such as the following:
>
> max <T extends Comparable>(T a, T b) {
>  if (a > b) return a; else return b;
> }
>

Also, "Comparable" implies some kind of function associated with the
type in order to actually perform it, and LLVM has no such association
mechanism.  I'd argue that structural typing means that it cannot,
since I don't want std::pair<double, double>'s operator< to work on
std::complex<double, double>, despite them having the same
representation.

> There are, of course, various ways to implement the above code.  I could
> compile the above function to a fully generic version with boxed arguments,
> but that is very slow for scalar types.  I could also take the C++ template
> route, and generate different IR code for every type instantiation.  However,
> I have spent way too much time fighting with templates and code bloat to
> like that idea.
>

I'd be curious to see if inlining + mem2reg would be able to
automatically unbox that example, if you always generate auto-boxing
code.  Even for things too big to inline, there may be a general pass
to convert the mallocs from boxing into allocas if nothing captures
the argument pointers.

I'm by no means an LLVM or compiler expert, though, so I could be quite wrong.

~ Scott



More information about the llvm-dev mailing list