= Compiler Multiverse support = This page documents the proposal for a compile feature to enable run time multiverse support. [[TableOfContents(4)]] gcc could be extended to generalize the binary patching technique used in the Linux kernel for use for any application. What this provides is variable optimized run time multiverse support. = Technical feasibility = Its known in theory this should technically be possible given that: * variable attributes exist, this would be used * functions can be cloned, even with special variable settings = Example = Let's consider the simple case of using a bool which only has two possible values: * true * false Code that would want to make use of this would annotate that the bool can be optimized for binary patching using a special attribute, for instance: {{{ static __attribute__((multiverse)) bool some_value; int foo(void) { if (some_value) { do_a(); } else { do_b(); } } int main(int argc, char **argv) { if (argc > 1) some_value = true; gcc_runtime_optimize_variable(&some_value); } }}} In this example when gcc would see some_value has the ''multiverse'' annotation, it would clone the foo() function twice, for two different possible ''universes'', one where some_val is true, and one where some_val is false. The references to the some_val locations in code are also stored for later use (generic some_val use, where some_val = true, some_val = false) in a special section to allow for run time patching. Applications get to hint during run time when a variable is deemed stable, this means that the variable value will not change for either: * a very long time * forever If an application, or the kernel, determines that a configuration variable is now deemed "stable", it can enable the application to put itself into an application specific safe state (for instance perhaps disallow concurrent threads, disable preemption, perhaps machine_stop() in the kernel). After this application specific safe-state call, a generic function would be called that would run time patch either the first few bytes of some generic functions or all invocations of the generic functions with the specialized variant for the current value of some_val. = Benefits = Benefits once this happens: * code executes and performs runs as efficienbtly as if some_val would have just been a const bool, or as if some_val would never had needed to be runtime determined * no register clobbering * no [id]cache pollution * ability to identify and compartmentalize ''dead-code'' * ability to decide how to handle dead-code at run time: * freeing unused code after variable ''stable'' state is reached * ability to enable debugging phase to ensure code that should not runs does not run, for instance by marking dead-code as not present = Negative effects = Expected negative effects to consider: * function cloning would increase the final size of a binary * due to size impact, use of this functionality should be limited = Potential uses = Expected potential uses: * Linux's UP/SMP patching * Linux's pv-ops * Linux's alternative patching based on cpu features * QEMU's kvm_enabled(), xen_enabled(), etc * user space JIT with runtime cpu feature detection * anything that wants to optimize for cpu features * dpdk network filter features = Feature inception = This idea was suggested by Alexander Graf while discussing an alternative to [http://kernelnewbies.org/KernelProjects/kernel-sandboxing Linker Table solution] with Luis Rodriguez to address [http://kernelnewbies.org/KernelProjects/kernel-sandboxing dead code] in the Linux kernel. This is a development idea for an extension to gcc to generalize dead-code support with compiler support. = Interested developers = If you are interested in helping complete it on gcc please reach out to: * agraf@suse.de * mcgrof@kernel.org * valentinrothberg@gmail.com