From 64bd3599011e0b28fa737168aef390ff5b0a35e3 Mon Sep 17 00:00:00 2001 From: Giovanni Bussi <giovanni.bussi@gmail.com> Date: Mon, 14 May 2018 17:06:09 +0200 Subject: [PATCH] Moved include asmjit to CompiledExpression.cpp Currently, this allows avoiding an exploding number of warnings originating from amsjit header files to be shown when compiling. In general, it better decouples asmjit from lepton. Later on we might allow one to switch asmjit on/off with an environment variable if desired. --- src/lepton/CompiledExpression.cpp | 21 ++++++++++++++++++++- src/lepton/CompiledExpression.h | 29 ++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/lepton/CompiledExpression.cpp b/src/lepton/CompiledExpression.cpp index 847707243..8f12e8ace 100644 --- a/src/lepton/CompiledExpression.cpp +++ b/src/lepton/CompiledExpression.cpp @@ -64,6 +64,9 @@ #include "CompiledExpression.h" #include "Operation.h" #include "ParsedExpression.h" +#ifdef __PLUMED_HAS_ASMJIT + #include "asmjit/asmjit.h" +#endif #include <utility> namespace PLMD { @@ -73,6 +76,19 @@ using namespace std; using namespace asmjit; #endif +AsmJitRuntimePtr::AsmJitRuntimePtr() +#ifdef __PLUMED_HAS_ASMJIT + : ptr(new asmjit::JitRuntime) +#endif +{} + +AsmJitRuntimePtr::~AsmJitRuntimePtr() +{ +#ifdef __PLUMED_HAS_ASMJIT + delete static_cast<asmjit::JitRuntime*>(ptr); +#endif +} + CompiledExpression::CompiledExpression() : jitCode(NULL) { } @@ -225,8 +241,11 @@ static double evaluateOperation(Operation* op, double* args) { return op->evaluate(args, *dummyVariables); } +static void generateSingleArgCall(X86Compiler& c, X86Xmm& dest, X86Xmm& arg, double (*function)(double)); + void CompiledExpression::generateJitCode() { CodeHolder code; + auto & runtime(*static_cast<asmjit::JitRuntime*>(runtimeptr.get())); code.init(runtime.getCodeInfo()); X86Assembler a(&code); X86Compiler c(&code); @@ -431,7 +450,7 @@ void CompiledExpression::generateJitCode() { jitCode = (void*) func0; } -void CompiledExpression::generateSingleArgCall(X86Compiler& c, X86Xmm& dest, X86Xmm& arg, double (*function)(double)) { +void generateSingleArgCall(X86Compiler& c, X86Xmm& dest, X86Xmm& arg, double (*function)(double)) { X86Gp fn = c.newIntPtr(); c.mov(fn, imm_ptr((void*) function)); CCFuncCall* call = c.call(fn, FuncSignature1<double, double>(CallConv::kIdHost)); diff --git a/src/lepton/CompiledExpression.h b/src/lepton/CompiledExpression.h index 76af31224..66b38078e 100644 --- a/src/lepton/CompiledExpression.h +++ b/src/lepton/CompiledExpression.h @@ -71,13 +71,31 @@ #include <string> #include <utility> #include <vector> -#ifdef __PLUMED_HAS_ASMJIT - #include "asmjit/asmjit.h" -#endif namespace PLMD { namespace lepton { +// Utility class. +// Implement an unique pointer to asmjit::JitRuntime. +// Needed to decouple asmjit header file from this one. +class AsmJitRuntimePtr { +/// if ASMJIT is not defined, just set the pointer to null + void* ptr=nullptr; +public: +/// constructor + AsmJitRuntimePtr(); +/// destructor + ~AsmJitRuntimePtr(); +/// deleted copy constructor + AsmJitRuntimePtr(const AsmJitRuntimePtr&) = delete; +/// deleted assignment + AsmJitRuntimePtr & operator=(const AsmJitRuntimePtr&) = delete; +/// get the pointer + void* get() { + return ptr; + } +}; + class Operation; class ParsedExpression; @@ -133,12 +151,9 @@ private: mutable std::vector<double> argValues; std::map<std::string, double> dummyVariables; void* jitCode; -#ifdef __PLUMED_HAS_ASMJIT void generateJitCode(); - void generateSingleArgCall(asmjit::X86Compiler& c, asmjit::X86Xmm& dest, asmjit::X86Xmm& arg, double (*function)(double)); std::vector<double> constants; - asmjit::JitRuntime runtime; -#endif + AsmJitRuntimePtr runtimeptr; }; } // namespace lepton -- GitLab