[LLVMdev] Generics

FlyLanguage flylanguage at gmail.com
Wed Sep 14 06:38:25 PDT 2011

> Hi,
> I'm about designing the compiler backend of my script language and I
> have generic types in the language. An example is the array. An
> array#int means array of integers. You can compare it to Java's
> generics. Whenever i have a variable of the type "generic", the type
> should be replaced by the really used type.
> Now the question is: how do i represent them in llvm without losing too
> much memory or CPU cycles?
> I figured out three ways to do it. Which do you think is the best?
> 1. Duplicating the implementations for each used type
> very hard to implement because it needs a lot of restructuring the backend
> 2. Defining data type "generic" as a record of all possible types
> would be fast but memory intensive; would need an optimization pass that
> removes unused variables from a struct (is there such pass?)
> 3. Defining data type "generic" as a union of all possible types
> would safe a lot of memory, but would destroy all optimizations because
> of the bitcasts; i dont know how to implement unions in llvm
> 3 is the way it is done in the interpreter, so i would prefer a way that
> is very close to 3.
> Suggestions?

Do type erasure before emitting llvm ir.

More information about the llvm-dev mailing list