Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hot recompilation for C++

I was recently amazed to see Java code being automatically recompiled and injected into a running program. Since modern C++ compilers (eg. LLVM-based) start investigating JIT compilation, I am wondering if there is any work made on this topic.

Update: By "hot recompilation", I mean editing the code, recompiling a specific part of the executable and running it without restarting the program. The common use case would be a game engine with an infinite loop where you would edit some code in the rendering step, and see the changes on the next frame.

What is the research state of hot recompilation for C++? Is there any working implementation?

like image 515
Warren Seine Avatar asked Feb 02 '23 14:02

Warren Seine


2 Answers

It's possible that by "hot recompilation" you mean something like "Edit and continue" in Visual C++.

Maybe that link constitutes an answer to your question.

But it would be easier if you would define the term you're asking about, "hot recompilation", more clearly (as I'm writing this it's not well defined).

added: "Edit and continue" for C++ was apparently introduced with Visual C++ 6.0, in the 1990's. So it's only slightly amazing that some Java implementation can do it now. <g> However, the /Zi switch that enables edit-and-continue does also, as I recall, change the behavior of __LINE__ so that e.g. the original ScopeGuard implementation doesn't work (one then has to use Microsoft-specific __COUNTER__).

Cheers & hth.,

like image 71
Cheers and hth. - Alf Avatar answered Feb 05 '23 04:02

Cheers and hth. - Alf


From automatically recompiled and injected into a running program. I assume you're talking about the JVM actually watching the program execution and for example changing predicted branch values at runtime to minimize jumps and un-pipelining.

This can be done in Java because there is a separate well defined intermediate stage between the source code and the actual machine instructions. This would enable it to substitute intermediate code at runtime, possibly improving performance.

In C++ the program is built directly into a particular architecture's machine language and on most hardware, code pages are read only for various reasons including preventing accidental and malicious code changes.

Now, what you could do in C++ is use something like he Clang library to rebuild sections of code into a shared object and then use dlopen etc to open the recompiled shared object to pick up the new version of the machine code. This of course requires your program to be a lot smarter than a Java program has to be to take advantage of the JVM. I believe that g++/gprof have a mode where profiling data can be used to affect g++'s optimizations however, maybe that's what you're looking for?

like image 31
Mark B Avatar answered Feb 05 '23 05:02

Mark B