Thread overview
Dlang dynamic compilation
Nov 21, 2016
Ivan Butygin
Nov 22, 2016
Nordlöw
Nov 22, 2016
Faux Amis
Nov 22, 2016
Minty Fresh
November 21, 2016
Hi all
Here is my small project - ability to move some code optimization and generation to runtime.
Any function can be marked with special attribute and llvm bitcode for it will saved in binary.
Then during runtime it will be optimized and compiled (and it can use best available cpu instructions).

@runtimeCompile void bar()
{
    ...
}

...

// Somewhere in main
rtCompileProcess(settings); // Compile
bar();

Another feature - ability to create "runtime compiletime variables" - you can set them during runtime and then they will be treated as constants during optimization and codegeneration.

@runtimeCompile __gshared int var;
@runtimeCompile void bar()
{
    // var is treated as constant in generated code and subject to constant propagation, loop unrolling, etc
    foreach(int i; 0..var)
    {
    }

    if(var == 42) // This block will be removed comletely if var is not 42
    {
    }
}

// Somewhere in main
var = 42;
rtCompileProcess(settings); // Compile
bar();

var = 13; // To see changed value in runtime compiled code recompilation is required
rtCompileProcess(settings); // Compile
bar();

This is an early prototype and mostly untested but simple examples work.

Hacked ldc sources are here:
https://github.com/Hardcode84/ldc/tree/runtime_compile

November 22, 2016
On Monday, 21 November 2016 at 18:59:17 UTC, Ivan Butygin wrote:
> Hacked ldc sources are here:
> https://github.com/Hardcode84/ldc/tree/runtime_compile

This could be used to accelerate genetic algorithms at run-time.
November 22, 2016
On Monday, 21 November 2016 at 18:59:17 UTC, Ivan Butygin wrote:
> Hacked ldc sources are here:
> https://github.com/Hardcode84/ldc/tree/runtime_compile

Very cool. Although @runtimeCompile does peeve me, as it seems unnecessarily verbose (being longer than any other attribute, I think).

I'd have gone with @rtc or similar so as not to clutter function and variable declarations, but those are just my thoughts on language readability.
November 22, 2016
On 2016-11-22 12:51, Nordlöw wrote:
> On Monday, 21 November 2016 at 18:59:17 UTC, Ivan Butygin wrote:
>> Hacked ldc sources are here:
>> https://github.com/Hardcode84/ldc/tree/runtime_compile
>
> This could be used to accelerate genetic algorithms at run-time.

https://en.wikipedia.org/wiki/Genetic_algorithm

;)