Run-time monomorphization

Jon Harrop <>
Wed, 05 Dec 2007 11:25:46 +0000

          From comp.compilers

Related articles
Run-time monomorphization (Jon Harrop) (2007-12-05)
| List of all articles for this month |

From: Jon Harrop <>
Newsgroups: comp.compilers
Date: Wed, 05 Dec 2007 11:25:46 +0000
Organization: Flying Frog Consultancy Ltd.
Keywords: optimize, question
Posted-Date: 08 Dec 2007 01:12:30 EST

Some compilers like Stalin (for Scheme) and MLton (for SML) use
whole-program optimizations to completely remove the run-time cost of
polymorphism. In contrast, languages like OCaml often suffer a 2x slowdown
due to polymorphism in performance critical functions (due to run-time
dispatch inserted into inner loops and not hoisted) that must then be
optimized manually.

If I'm using LLVM to JIT compile code for an OCaml-like language, can I
monomorphize function definitions on the fly easily?

May I conjecture how:

. Fetching a handle to a function "f" at its call site uses a
function "get".

. The application "get f" is memoized with respect to the instantiations of
the type variables of "f" used at its call site.

Consider the example:

    val List.fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a
    let sum_int = List.fold_left (+) 0
    let sum_float = List.fold_left (+.) 0.

The call to "List.fold_left" inside "sum_int" uses 'a = 'b = int and a
version of "List.fold_left" is JIT compiled specifically for those types.

Similarly for "sum_float" with 'a = 'b = float.

Dr Jon D Harrop, Flying Frog Consultancy Ltd.

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.