Dlang dynamic compilation

Ivan Butygin via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Mon Nov 21 10:59:17 PST 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



More information about the Digitalmars-d-announce mailing list