From f35707047e216e99052c15b04fe508d075b6121b Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Fri, 16 Oct 2020 16:50:24 -0700 Subject: [PATCH] [ORC] Break up C-API header Orc.h, and add JITEventListener support. This patch breaks Orc.h up into Orc.h, LLJIT.h and OrcEE.h. Orc.h contain core Orc utilities. LLJIT.h contains LLJIT specific types and functions. OrcEE.h contains types and functions that depend on ExecutionEngine. The intent is that these headers should match future library divisions: Clients who only use Orc.h should only need to link againt the Orc core libraries, clients using LLJIT.h will also need to link against LLVM core, and clients using OrcEE.h will also have to link against ExecutionEngine. In addition to breaking up the Orc.h header this patch introduces functions to: (1) Set the object linking layer creation function on LLJITBuilder. (2) Create an RTDyldObjectLinkingLayer instance (particularly for use in (1)). (3) Register JITEventListeners with an RTDyldObjectLinkingLayer. Together (1), (2) and (3) can be used to force use of RTDyldObjectLinkingLayer as the underlying JIT linker for LLJIT, rather than the platform default, and to register event listeners with the RTDyldObjectLinkingLayer. --- .../OrcV2CBindingsAddObjectFile.c | 2 +- .../OrcV2CBindingsBasicUsage.c | 2 +- .../OrcV2CBindingsReflectProcessSymbols.c | 4 +- .../OrcV2CBindingsRemovableCode.c | 2 +- llvm/include/llvm-c/LLJIT.h | 213 +++++++++++++++++++++ llvm/include/llvm-c/Orc.h | 157 +-------------- llvm/include/llvm-c/OrcEE.h | 55 ++++++ llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp | 40 +++- 8 files changed, 318 insertions(+), 157 deletions(-) create mode 100644 llvm/include/llvm-c/LLJIT.h create mode 100644 llvm/include/llvm-c/OrcEE.h diff --git a/llvm/examples/OrcV2Examples/OrcV2CBindingsAddObjectFile/OrcV2CBindingsAddObjectFile.c b/llvm/examples/OrcV2Examples/OrcV2CBindingsAddObjectFile/OrcV2CBindingsAddObjectFile.c index 49eca25..fca3c75 100644 --- a/llvm/examples/OrcV2Examples/OrcV2CBindingsAddObjectFile/OrcV2CBindingsAddObjectFile.c +++ b/llvm/examples/OrcV2Examples/OrcV2CBindingsAddObjectFile/OrcV2CBindingsAddObjectFile.c @@ -9,7 +9,7 @@ #include "llvm-c/Core.h" #include "llvm-c/Error.h" #include "llvm-c/Initialization.h" -#include "llvm-c/Orc.h" +#include "llvm-c/LLJIT.h" #include "llvm-c/Support.h" #include "llvm-c/Target.h" #include "llvm-c/TargetMachine.h" diff --git a/llvm/examples/OrcV2Examples/OrcV2CBindingsBasicUsage/OrcV2CBindingsBasicUsage.c b/llvm/examples/OrcV2Examples/OrcV2CBindingsBasicUsage/OrcV2CBindingsBasicUsage.c index cc54cc4..d499518 100644 --- a/llvm/examples/OrcV2Examples/OrcV2CBindingsBasicUsage/OrcV2CBindingsBasicUsage.c +++ b/llvm/examples/OrcV2Examples/OrcV2CBindingsBasicUsage/OrcV2CBindingsBasicUsage.c @@ -9,7 +9,7 @@ #include "llvm-c/Core.h" #include "llvm-c/Error.h" #include "llvm-c/Initialization.h" -#include "llvm-c/Orc.h" +#include "llvm-c/LLJIT.h" #include "llvm-c/Support.h" #include "llvm-c/Target.h" diff --git a/llvm/examples/OrcV2Examples/OrcV2CBindingsReflectProcessSymbols/OrcV2CBindingsReflectProcessSymbols.c b/llvm/examples/OrcV2Examples/OrcV2CBindingsReflectProcessSymbols/OrcV2CBindingsReflectProcessSymbols.c index 431276e..455e5ea 100644 --- a/llvm/examples/OrcV2Examples/OrcV2CBindingsReflectProcessSymbols/OrcV2CBindingsReflectProcessSymbols.c +++ b/llvm/examples/OrcV2Examples/OrcV2CBindingsReflectProcessSymbols/OrcV2CBindingsReflectProcessSymbols.c @@ -9,7 +9,7 @@ #include "llvm-c/Core.h" #include "llvm-c/Error.h" #include "llvm-c/Initialization.h" -#include "llvm-c/Orc.h" +#include "llvm-c/LLJIT.h" #include "llvm-c/Support.h" #include "llvm-c/Target.h" @@ -27,7 +27,7 @@ int32_t add(int32_t X, int32_t Y) { return X + Y; } int32_t mul(int32_t X, int32_t Y) { return X * Y; } -int allowedSymbols(LLVMOrcSymbolStringPoolEntryRef Sym, void *Ctx) { +int allowedSymbols(void *Ctx, LLVMOrcSymbolStringPoolEntryRef Sym) { assert(Ctx && "Cannot call allowedSymbols with a null context"); LLVMOrcSymbolStringPoolEntryRef *AllowList = diff --git a/llvm/examples/OrcV2Examples/OrcV2CBindingsRemovableCode/OrcV2CBindingsRemovableCode.c b/llvm/examples/OrcV2Examples/OrcV2CBindingsRemovableCode/OrcV2CBindingsRemovableCode.c index 102224a..5cfc626 100644 --- a/llvm/examples/OrcV2Examples/OrcV2CBindingsRemovableCode/OrcV2CBindingsRemovableCode.c +++ b/llvm/examples/OrcV2Examples/OrcV2CBindingsRemovableCode/OrcV2CBindingsRemovableCode.c @@ -9,7 +9,7 @@ #include "llvm-c/Core.h" #include "llvm-c/Error.h" #include "llvm-c/Initialization.h" -#include "llvm-c/Orc.h" +#include "llvm-c/LLJIT.h" #include "llvm-c/Support.h" #include "llvm-c/Target.h" diff --git a/llvm/include/llvm-c/LLJIT.h b/llvm/include/llvm-c/LLJIT.h new file mode 100644 index 0000000..28eb8bb --- /dev/null +++ b/llvm/include/llvm-c/LLJIT.h @@ -0,0 +1,213 @@ +/*===----------- llvm-c/LLJIT.h - OrcV2 LLJIT C bindings --------*- C++ -*-===*\ +|* *| +|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *| +|* Exceptions. *| +|* See https://llvm.org/LICENSE.txt for license information. *| +|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *| +|* *| +|*===----------------------------------------------------------------------===*| +|* *| +|* This header declares the C interface to the LLJIT class in *| +|* libLLVMOrcJIT.a, which provides a simple MCJIT-like ORC JIT. *| +|* *| +|* Many exotic languages can interoperate with C code but have a harder time *| +|* with C++ due to name mangling. So in addition to C, this interface enables *| +|* tools written in such languages. *| +|* *| +|* Note: This interface is experimental. It is *NOT* stable, and may be *| +|* changed without warning. Only C API usage documentation is *| +|* provided. See the C++ documentation for all higher level ORC API *| +|* details. *| +|* *| +\*===----------------------------------------------------------------------===*/ + +#ifndef LLVM_C_LLJIT_H +#define LLVM_C_LLJIT_H + +#include "llvm-c/Error.h" +#include "llvm-c/Orc.h" +#include "llvm-c/TargetMachine.h" +#include "llvm-c/Types.h" + +LLVM_C_EXTERN_C_BEGIN + +/** + * A function for constructing an ObjectLinkingLayer instance to be used + * by an LLJIT instance. + * + * Clients can call LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator to + * set the creator function to use when constructing an LLJIT instance. + * This can be used to override the default linking layer implementation + * that would otherwise be chosen by LLJITBuilder. + * + * Object linking layers returned by this function will become owned by the + * LLJIT instance. The client is not responsible for managing their lifetimes + * after the function returns. + */ +typedef LLVMOrcObjectLayerRef ( + *LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction)( + void *Ctx, LLVMOrcExecutionSessionRef ES, const char *Triple); + +/** + * A reference to an orc::LLJITBuilder instance. + */ +typedef struct LLVMOrcOpaqueLLJITBuilder *LLVMOrcLLJITBuilderRef; + +/** + * A reference to an orc::LLJIT instance. + */ +typedef struct LLVMOrcOpaqueLLJIT *LLVMOrcLLJITRef; + +/** + * Create an LLVMOrcLLJITBuilder. + * + * The client owns the resulting LLJITBuilder and should dispose of it using + * LLVMOrcDisposeLLJITBuilder once they are done with it. + */ +LLVMOrcLLJITBuilderRef LLVMOrcCreateLLJITBuilder(void); + +/** + * Dispose of an LLVMOrcLLJITBuilderRef. This should only be called if ownership + * has not been passed to LLVMOrcCreateLLJIT (e.g. because some error prevented + * that function from being called). + */ +void LLVMOrcDisposeLLJITBuilder(LLVMOrcLLJITBuilderRef Builder); + +/** + * Set the JITTargetMachineBuilder to be used when constructing the LLJIT + * instance. Calling this function is optional: if it is not called then the + * LLJITBuilder will use JITTargeTMachineBuilder::detectHost to construct a + * JITTargetMachineBuilder. + */ +void LLVMOrcLLJITBuilderSetJITTargetMachineBuilder( + LLVMOrcLLJITBuilderRef Builder, LLVMOrcJITTargetMachineBuilderRef JTMB); + +/** + * Set an ObjectLinkingLayer creator function for this LLJIT instance. + */ +void LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator( + LLVMOrcLLJITBuilderRef Builder, + LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction F, void *Ctx); + +/** + * Create an LLJIT instance from an LLJITBuilder. + * + * This operation takes ownership of the Builder argument: clients should not + * dispose of the builder after calling this function (even if the function + * returns an error). If a null Builder argument is provided then a + * default-constructed LLJITBuilder will be used. + * + * On success the resulting LLJIT instance is uniquely owned by the client and + * automatically manages the memory of all JIT'd code and all modules that are + * transferred to it (e.g. via LLVMOrcLLJITAddLLVMIRModule). Disposing of the + * LLJIT instance will free all memory managed by the JIT, including JIT'd code + * and not-yet compiled modules. + */ +LLVMErrorRef LLVMOrcCreateLLJIT(LLVMOrcLLJITRef *Result, + LLVMOrcLLJITBuilderRef Builder); + +/** + * Dispose of an LLJIT instance. + */ +LLVMErrorRef LLVMOrcDisposeLLJIT(LLVMOrcLLJITRef J); + +/** + * Get a reference to the ExecutionSession for this LLJIT instance. + * + * The ExecutionSession is owned by the LLJIT instance. The client is not + * responsible for managing its memory. + */ +LLVMOrcExecutionSessionRef LLVMOrcLLJITGetExecutionSession(LLVMOrcLLJITRef J); + +/** + * Return a reference to the Main JITDylib. + * + * The JITDylib is owned by the LLJIT instance. The client is not responsible + * for managing its memory. + */ +LLVMOrcJITDylibRef LLVMOrcLLJITGetMainJITDylib(LLVMOrcLLJITRef J); + +/** + * Return the target triple for this LLJIT instance. This string is owned by + * the LLJIT instance and should not be freed by the client. + */ +const char *LLVMOrcLLJITGetTripleString(LLVMOrcLLJITRef J); + +/** + * Returns the global prefix character according to the LLJIT's DataLayout. + */ +char LLVMOrcLLJITGetGlobalPrefix(LLVMOrcLLJITRef J); + +/** + * Mangles the given string according to the LLJIT instance's DataLayout, then + * interns the result in the SymbolStringPool and returns a reference to the + * pool entry. Clients should call LLVMOrcReleaseSymbolStringPoolEntry to + * decrement the ref-count on the pool entry once they are finished with this + * value. + */ +LLVMOrcSymbolStringPoolEntryRef +LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J, const char *UnmangledName); + +/** + * Add a buffer representing an object file to the given JITDylib in the given + * LLJIT instance. This operation transfers ownership of the buffer to the + * LLJIT instance. The buffer should not be disposed of or referenced once this + * function returns. + * + * Resources associated with the given object will be tracked by the given + * JITDylib's default resource tracker. + */ +LLVMErrorRef LLVMOrcLLJITAddObjectFile(LLVMOrcLLJITRef J, LLVMOrcJITDylibRef JD, + LLVMMemoryBufferRef ObjBuffer); + +/** + * Add a buffer representing an object file to the given ResourceTracker's + * JITDylib in the given LLJIT instance. This operation transfers ownership of + * the buffer to the LLJIT instance. The buffer should not be disposed of or + * referenced once this function returns. + * + * Resources associated with the given object will be tracked by ResourceTracker + * RT. + */ +LLVMErrorRef LLVMOrcLLJITAddObjectFileWithRT(LLVMOrcLLJITRef J, + LLVMOrcResourceTrackerRef RT, + LLVMMemoryBufferRef ObjBuffer); + +/** + * Add an IR module to the given JITDylib in the given LLJIT instance. This + * operation transfers ownership of the TSM argument to the LLJIT instance. + * The TSM argument should not be disposed of or referenced once this + * function returns. + * + * Resources associated with the given Module will be tracked by the given + * JITDylib's default resource tracker. + */ +LLVMErrorRef LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J, + LLVMOrcJITDylibRef JD, + LLVMOrcThreadSafeModuleRef TSM); + +/** + * Add an IR module to the given ResourceTracker's JITDylib in the given LLJIT + * instance. This operation transfers ownership of the TSM argument to the LLJIT + * instance. The TSM argument should not be disposed of or referenced once this + * function returns. + * + * Resources associated with the given Module will be tracked by ResourceTracker + * RT. + */ +LLVMErrorRef LLVMOrcLLJITAddLLVMIRModuleWithRT(LLVMOrcLLJITRef J, + LLVMOrcResourceTrackerRef JD, + LLVMOrcThreadSafeModuleRef TSM); + +/** + * Look up the given symbol in the main JITDylib of the given LLJIT instance. + * + * This operation does not take ownership of the Name argument. + */ +LLVMErrorRef LLVMOrcLLJITLookup(LLVMOrcLLJITRef J, + LLVMOrcJITTargetAddress *Result, + const char *Name); + +LLVM_C_EXTERN_C_END + +#endif /* LLVM_C_LLJIT_H */ diff --git a/llvm/include/llvm-c/Orc.h b/llvm/include/llvm-c/Orc.h index 4045e57..183107c 100644 --- a/llvm/include/llvm-c/Orc.h +++ b/llvm/include/llvm-c/Orc.h @@ -231,8 +231,8 @@ typedef LLVMErrorRef (*LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction)( /** * Predicate function for SymbolStringPoolEntries. */ -typedef int (*LLVMOrcSymbolPredicate)(LLVMOrcSymbolStringPoolEntryRef Sym, - void *Ctx); +typedef int (*LLVMOrcSymbolPredicate)(void *Ctx, + LLVMOrcSymbolStringPoolEntryRef Sym); /** * A reference to an orc::ThreadSafeContext instance. @@ -251,14 +251,9 @@ typedef struct LLVMOrcOpaqueJITTargetMachineBuilder *LLVMOrcJITTargetMachineBuilderRef; /** - * A reference to an orc::LLJITBuilder instance. + * A reference to an orc::ObjectLayer instance. */ -typedef struct LLVMOrcOpaqueLLJITBuilder *LLVMOrcLLJITBuilderRef; - -/** - * A reference to an orc::LLJIT instance. - */ -typedef struct LLVMOrcOpaqueLLJIT *LLVMOrcLLJITRef; +typedef struct LLVMOrcOpaqueObjectLayer *LLVMOrcObjectLayerRef; /** * Attach a custom error reporter function to the ExecutionSession. @@ -440,7 +435,7 @@ void LLVMOrcJITDylibAddGenerator(LLVMOrcJITDylibRef JD, * Create a custom generator. */ LLVMOrcDefinitionGeneratorRef LLVMOrcCreateCustomCAPIDefinitionGenerator( - void *Ctx, LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction F); + LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction F, void *Ctx); /** * Get a DynamicLibrarySearchGenerator that will reflect process symbols into @@ -534,147 +529,9 @@ void LLVMOrcDisposeJITTargetMachineBuilder( LLVMOrcJITTargetMachineBuilderRef JTMB); /** - * Create an LLJITTargetMachineBuilder. - * - * The client owns the resulting LLJITBuilder and should dispose of it using - * LLVMOrcDisposeLLJITBuilder once they are done with it. - */ -LLVMOrcLLJITBuilderRef LLVMOrcCreateLLJITBuilder(void); - -/** - * Dispose of an LLVMOrcLLJITBuilderRef. This should only be called if ownership - * has not been passed to LLVMOrcCreateLLJIT (e.g. because some error prevented - * that function from being called). - */ -void LLVMOrcDisposeLLJITBuilder(LLVMOrcLLJITBuilderRef Builder); - -/** - * Set the JITTargetMachineBuilder to be used when constructing the LLJIT - * instance. Calling this function is optional: if it is not called then the - * LLJITBuilder will use JITTargeTMachineBuilder::detectHost to construct a - * JITTargetMachineBuilder. - */ -void LLVMOrcLLJITBuilderSetJITTargetMachineBuilder( - LLVMOrcLLJITBuilderRef Builder, LLVMOrcJITTargetMachineBuilderRef JTMB); - -/** - * Create an LLJIT instance from an LLJITBuilder. - * - * This operation takes ownership of the Builder argument: clients should not - * dispose of the builder after calling this function (even if the function - * returns an error). If a null Builder argument is provided then a - * default-constructed LLJITBuilder will be used. - * - * On success the resulting LLJIT instance is uniquely owned by the client and - * automatically manages the memory of all JIT'd code and all modules that are - * transferred to it (e.g. via LLVMOrcLLJITAddLLVMIRModule). Disposing of the - * LLJIT instance will free all memory managed by the JIT, including JIT'd code - * and not-yet compiled modules. - */ -LLVMErrorRef LLVMOrcCreateLLJIT(LLVMOrcLLJITRef *Result, - LLVMOrcLLJITBuilderRef Builder); - -/** - * Dispose of an LLJIT instance. - */ -LLVMErrorRef LLVMOrcDisposeLLJIT(LLVMOrcLLJITRef J); - -/** - * Get a reference to the ExecutionSession for this LLJIT instance. - * - * The ExecutionSession is owned by the LLJIT instance. The client is not - * responsible for managing its memory. - */ -LLVMOrcExecutionSessionRef LLVMOrcLLJITGetExecutionSession(LLVMOrcLLJITRef J); - -/** - * Return a reference to the Main JITDylib. - * - * The JITDylib is owned by the LLJIT instance. The client is not responsible - * for managing its memory. - */ -LLVMOrcJITDylibRef LLVMOrcLLJITGetMainJITDylib(LLVMOrcLLJITRef J); - -/** - * Return the target triple for this LLJIT instance. This string is owned by - * the LLJIT instance and should not be freed by the client. - */ -const char *LLVMOrcLLJITGetTripleString(LLVMOrcLLJITRef J); - -/** - * Returns the global prefix character according to the LLJIT's DataLayout. - */ -char LLVMOrcLLJITGetGlobalPrefix(LLVMOrcLLJITRef J); - -/** - * Mangles the given string according to the LLJIT instance's DataLayout, then - * interns the result in the SymbolStringPool and returns a reference to the - * pool entry. Clients should call LLVMOrcReleaseSymbolStringPoolEntry to - * decrement the ref-count on the pool entry once they are finished with this - * value. - */ -LLVMOrcSymbolStringPoolEntryRef -LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J, const char *UnmangledName); - -/** - * Add a buffer representing an object file to the given JITDylib in the given - * LLJIT instance. This operation transfers ownership of the buffer to the - * LLJIT instance. The buffer should not be disposed of or referenced once this - * function returns. - * - * Resources associated with the given object will be tracked by the given - * JITDylib's default resource tracker. - */ -LLVMErrorRef LLVMOrcLLJITAddObjectFile(LLVMOrcLLJITRef J, LLVMOrcJITDylibRef JD, - LLVMMemoryBufferRef ObjBuffer); - -/** - * Add a buffer representing an object file to the given ResourceTracker's - * JITDylib in the given LLJIT instance. This operation transfers ownership of - * the buffer to the LLJIT instance. The buffer should not be disposed of or - * referenced once this function returns. - * - * Resources associated with the given object will be tracked by ResourceTracker - * RT. - */ -LLVMErrorRef LLVMOrcLLJITAddObjectFileWithRT(LLVMOrcLLJITRef J, - LLVMOrcResourceTrackerRef RT, - LLVMMemoryBufferRef ObjBuffer); - -/** - * Add an IR module to the given JITDylib in the given LLJIT instance. This - * operation transfers ownership of the TSM argument to the LLJIT instance. - * The TSM argument should not be disposed of or referenced once this - * function returns. - * - * Resources associated with the given Module will be tracked by the given - * JITDylib's default resource tracker. - */ -LLVMErrorRef LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J, - LLVMOrcJITDylibRef JD, - LLVMOrcThreadSafeModuleRef TSM); - -/** - * Add an IR module to the given ResourceTracker's JITDylib in the given LLJIT - * instance. This operation transfers ownership of the TSM argument to the LLJIT - * instance. The TSM argument should not be disposed of or referenced once this - * function returns. - * - * Resources associated with the given Module will be tracked by ResourceTracker - * RT. - */ -LLVMErrorRef LLVMOrcLLJITAddLLVMIRModuleWithRT(LLVMOrcLLJITRef J, - LLVMOrcResourceTrackerRef JD, - LLVMOrcThreadSafeModuleRef TSM); - -/** - * Look up the given symbol in the main JITDylib of the given LLJIT instance. - * - * This operation does not take ownership of the Name argument. + * Dispose of an ObjectLayer. */ -LLVMErrorRef LLVMOrcLLJITLookup(LLVMOrcLLJITRef J, - LLVMOrcJITTargetAddress *Result, - const char *Name); +void LLVMOrcDisposeObjectLayer(LLVMOrcObjectLayerRef ObjLayer); LLVM_C_EXTERN_C_END diff --git a/llvm/include/llvm-c/OrcEE.h b/llvm/include/llvm-c/OrcEE.h new file mode 100644 index 0000000..2435e74 --- /dev/null +++ b/llvm/include/llvm-c/OrcEE.h @@ -0,0 +1,55 @@ +/*===-- llvm-c/OrcEE.h - OrcV2 C bindings ExecutionEngine utils -*- C++ -*-===*\ +|* *| +|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *| +|* Exceptions. *| +|* See https://llvm.org/LICENSE.txt for license information. *| +|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *| +|* *| +|*===----------------------------------------------------------------------===*| +|* *| +|* This header declares the C interface to ExecutionEngine based utils, e.g. *| +|* RTDyldObjectLinkingLayer (based on RuntimeDyld) in Orc. *| +|* *| +|* Many exotic languages can interoperate with C code but have a harder time *| +|* with C++ due to name mangling. So in addition to C, this interface enables *| +|* tools written in such languages. *| +|* *| +|* Note: This interface is experimental. It is *NOT* stable, and may be *| +|* changed without warning. Only C API usage documentation is *| +|* provided. See the C++ documentation for all higher level ORC API *| +|* details. *| +|* *| +\*===----------------------------------------------------------------------===*/ + +#ifndef LLVM_C_ORCEE_H +#define LLVM_C_ORCEE_H + +#include "llvm-c/Error.h" +#include "llvm-c/ExecutionEngine.h" +#include "llvm-c/Orc.h" +#include "llvm-c/TargetMachine.h" +#include "llvm-c/Types.h" + +LLVM_C_EXTERN_C_BEGIN + +/** + * Create a RTDyldObjectLinkingLayer instance using the standard + * SectionMemoryManager for memory management. + */ +LLVMOrcObjectLayerRef +LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager( + LLVMOrcExecutionSessionRef ES); + +/** + * Add the given listener to the given RTDyldObjectLinkingLayer. + * + * Note: Layer must be an RTDyldObjectLinkingLayer instance or + * behavior is undefined. + */ +void LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener( + LLVMOrcObjectLayerRef RTDyldObjLinkingLayer, + LLVMJITEventListenerRef Listener); + +LLVM_C_EXTERN_C_END + +#endif /* LLVM_C_ORCEE_H */ diff --git a/llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp b/llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp index eeeea5b..b91d2a9 100644 --- a/llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp +++ b/llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp @@ -6,11 +6,14 @@ // //===----------------------------------------------------------------------===// +#include "llvm-c/LLJIT.h" #include "llvm-c/Orc.h" #include "llvm-c/TargetMachine.h" #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" #include "llvm/ExecutionEngine/Orc/LLJIT.h" +#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" +#include "llvm/ExecutionEngine/SectionMemoryManager.h" using namespace llvm; using namespace llvm::orc; @@ -77,6 +80,7 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeContext, DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeModule, LLVMOrcThreadSafeModuleRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITTargetMachineBuilder, LLVMOrcJITTargetMachineBuilderRef) +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectLayer, LLVMOrcObjectLayerRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJITBuilder, LLVMOrcLLJITBuilderRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJIT, LLVMOrcLLJITRef) @@ -295,7 +299,7 @@ void LLVMOrcJITDylibAddGenerator(LLVMOrcJITDylibRef JD, } LLVMOrcDefinitionGeneratorRef LLVMOrcCreateCustomCAPIDefinitionGenerator( - void *Ctx, LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction F) { + LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction F, void *Ctx) { auto DG = std::make_unique(Ctx, F); return wrap(DG.release()); } @@ -310,7 +314,7 @@ LLVMErrorRef LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess( DynamicLibrarySearchGenerator::SymbolPredicate Pred; if (Filter) Pred = [=](const SymbolStringPtr &Name) -> bool { - return Filter(wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name)), FilterCtx); + return Filter(FilterCtx, wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name))); }; auto ProcessSymsGenerator = @@ -388,6 +392,10 @@ void LLVMOrcDisposeJITTargetMachineBuilder( delete unwrap(JTMB); } +void lLVMOrcDisposeObjectLayer(LLVMOrcObjectLayerRef ObjLayer) { + delete unwrap(ObjLayer); +} + LLVMOrcLLJITBuilderRef LLVMOrcCreateLLJITBuilder(void) { return wrap(new LLJITBuilder()); } @@ -401,6 +409,17 @@ void LLVMOrcLLJITBuilderSetJITTargetMachineBuilder( unwrap(Builder)->setJITTargetMachineBuilder(*unwrap(JTMB)); } +void LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator( + LLVMOrcLLJITBuilderRef Builder, + LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction F, void *Ctx) { + unwrap(Builder)->setObjectLinkingLayerCreator( + [=](ExecutionSession &ES, const Triple &TT) { + auto TTStr = TT.str(); + return std::unique_ptr( + unwrap(F(Ctx, wrap(&ES), TTStr.c_str()))); + }); +} + LLVMErrorRef LLVMOrcCreateLLJIT(LLVMOrcLLJITRef *Result, LLVMOrcLLJITBuilderRef Builder) { assert(Result && "Result can not be null"); @@ -490,3 +509,20 @@ LLVMErrorRef LLVMOrcLLJITLookup(LLVMOrcLLJITRef J, *Result = Sym->getAddress(); return LLVMErrorSuccess; } + +LLVMOrcObjectLayerRef +LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager( + LLVMOrcExecutionSessionRef ES) { + assert(ES && "ES must not be null"); + return wrap(new RTDyldObjectLinkingLayer( + *unwrap(ES), [] { return std::make_unique(); })); +} + +void LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener( + LLVMOrcObjectLayerRef RTDyldObjLinkingLayer, + LLVMJITEventListenerRef Listener) { + assert(RTDyldObjLinkingLayer && "RTDyldObjLinkingLayer must not be null"); + assert(Listener && "Listener must not be null"); + reinterpret_cast(unwrap(RTDyldObjLinkingLayer)) + ->registerJITEventListener(*unwrap(Listener)); +} -- 2.7.4