www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - New LDC feature: dynamic compilation

reply Ivan Butygin <ivan.butygin gmail.com> writes:
Main part of this feature is a new UDA ` dynamicCompile`.

You can mark with this attribute any function (including class 
methods and lambdas) and compilation and optimization of this 
function will be deferred to runtime.
Dynamic compiler will use instruction set available on host, so 
application can utilize the best available instructions without 
sacrificing users with old hardware.
You only need to mark top-level function with this UDA, compiler 
will also defer compilation for any function called from root 
with body available.
You need to explicitly compile ` dynamicCompile` functions before 
using any of them.

CompilerSettings settings;
settings.optLevel = 3; // -O3
compileDynamicCode(settings); // compile all dynamic functions

Second feature is ` dynamicCompileConst` variables.
You can mark any global non-thread local variable with this 
attribute.
They are treated as usual from static code but if they are 
treated as compile-time constants by optimizer when accessed from 
` dynamicCompile` function.

Example:
` dynamicCompileConst` __gshared simdSize;

` dynamicCompile` void foo()
{
   if (4 == simdSize)
   {
     // SSE-otimized code
   }
   else if (8 == simdSize)
   {
     // AVX-optimized code
   }
}

...

simdSize = 4;
compileDynamicCode();

Optimizer will treat `simdSize` as constant and remove checks and 
not taken branches.

You shouldn't change these variables from dynamic code (this is 
UB and will probaly crash you)

And you need to recompile dynamic code with 
`compileDynamicCode(...)` if you want changes to these variables 
take effect in dynamic code.


These are highly experimental features so expect major bugs.

Known issues:
* There is debug hook to get final asm generated for 
` dynamicCompile` function but it is disabled now (but you can 
still get original and optimized LLVM IR for them)
* Exceptions doesn't work on windows if there are dynamic 
functions in stack (they work on linux and osx).
* There are codegen issues on win32, use win64
Nov 13 2017
next sibling parent reply Michael V. Franklin <slavo5150 yahoo.com> writes:
On Monday, 13 November 2017 at 19:04:16 UTC, Ivan Butygin wrote:

 You need to explicitly compile ` dynamicCompile` functions 
 before using any of them.
Interesting feature. So is the executable linked to an installed instance of LDC/LLVM to make this happen, or is there some limited compiler embedded in the executable or Druntime? More details about the implementation please. Mike
Nov 13 2017
parent Ivan Butygin <ivan.butygin gmail.com> writes:
On Monday, 13 November 2017 at 19:58:29 UTC, Michael V. Franklin 
wrote:
 Interesting feature.  So is the executable linked to an 
 installed instance of LDC/LLVM to make this happen, or is there 
 some limited compiler embedded in the executable or Druntime?  
 More details about the implementation please.

 Mike
Jit runtime (ldc-jit.dll/so) uses llvm optimizer and backend. Forgot to mention, user need to pass `-enable-dynamic-compile` compiler switch to enable this feature (and jit runtime will only be linked in this case).
Nov 13 2017
prev sibling parent kinke <noone nowhere.com> writes:
Thanks for this post. Please also consider creating a Wiki page 
as that's more easily accessible for posterity (and editable) 
than a forum post here.
Nov 13 2017