Opened 17 months ago

Last modified 16 months ago

#248 new enhancement

Lazily generate prelude functions on demande.

Reported by: Thierry Delisle Owned by:
Priority: major Component: prelude
Version: 1.0 Keywords:


Prelude functions are currenctly generated as regular C, except when used through assertions. In that case, the assertions require a function pointer so we have a function definition for each prelude function in libcfa.

This is a performance problem since it stops inlining of polymorphic function using basic types.

A solution to this would be to generate the functions for each source files as static inline.

However, doing this naively doesn't work because the definition of prelude functions is self-referential. The solution is to only insert the bodies of the needed prelude function on demande before code generation.

Change History (1)

comment:1 Changed 16 months ago by Thierry Delisle

Here is minimal example where this is a problem:

forall(T | { T ?+=?(T&, T); })
	struct wrap {
		T val;

	static inline wrap(T) ?+?(wrap(T) lhs, const wrap(T) & rhs) {
		lhs.val += rhs.val;
		return lhs;

wrap(int) a, b;
int foo() {
	return (a + b).val;

Looking at the output assembly for this example, compiled with -O3, we see calls to _X12_constructorFv_ii_intrinsic___1, _X19_operator_addassignFi_ii_intrinsic___1 and _X11_destructorFv_i_intrinsic___1. All three of these functions are trivial and we want them to be inlined.

Note: See TracTickets for help on using tickets.