diff --git a/llvm/examples/OrcV2Examples/CMakeLists.txt b/llvm/examples/OrcV2Examples/CMakeLists.txt --- a/llvm/examples/OrcV2Examples/CMakeLists.txt +++ b/llvm/examples/OrcV2Examples/CMakeLists.txt @@ -15,6 +15,7 @@ add_subdirectory(OrcV2CBindingsReflectProcessSymbols) add_subdirectory(OrcV2CBindingsRemovableCode) add_subdirectory(OrcV2CBindingsLazy) +add_subdirectory(OrcV2CBindingsVeryLazy) if(CMAKE_HOST_UNIX) add_subdirectory(LLJITWithRemoteDebugging) diff --git a/llvm/examples/OrcV2Examples/OrcV2CBindingsVeryLazy/CMakeLists.txt b/llvm/examples/OrcV2Examples/OrcV2CBindingsVeryLazy/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/llvm/examples/OrcV2Examples/OrcV2CBindingsVeryLazy/CMakeLists.txt @@ -0,0 +1,15 @@ +set(LLVM_LINK_COMPONENTS + Core + ExecutionEngine + IRReader + JITLink + MC + OrcJIT + Support + Target + nativecodegen + ) + +add_llvm_example(OrcV2CBindingsVeryLazy + OrcV2CBindingsVeryLazy.c + ) diff --git a/llvm/examples/OrcV2Examples/OrcV2CBindingsVeryLazy/OrcV2CBindingsVeryLazy.c b/llvm/examples/OrcV2Examples/OrcV2CBindingsVeryLazy/OrcV2CBindingsVeryLazy.c new file mode 100644 --- /dev/null +++ b/llvm/examples/OrcV2Examples/OrcV2CBindingsVeryLazy/OrcV2CBindingsVeryLazy.c @@ -0,0 +1,301 @@ +//===-------- BasicOrcV2CBindings.c - Basic OrcV2 C Bindings Demo ---------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "llvm-c/Core.h" +#include "llvm-c/Error.h" +#include "llvm-c/IRReader.h" +#include "llvm-c/Initialization.h" +#include "llvm-c/LLJIT.h" +#include "llvm-c/Support.h" +#include "llvm-c/Target.h" + +#include +#include +#include + +int handleError(LLVMErrorRef Err) { + char *ErrMsg = LLVMGetErrorMessage(Err); + fprintf(stderr, "Error: %s\n", ErrMsg); + LLVMDisposeErrorMessage(ErrMsg); + return 1; +} + +// Example IR modules. +// +// Note that in the conditionally compiled modules, FooMod and BarMod, functions +// have been given an _body suffix. This is to ensure that their names do not +// clash with their lazy-reexports. +// For clients who do not wish to rename function bodies (e.g. because they want +// to re-use cached objects between static and JIT compiles) techniques exist to +// avoid renaming. See the lazy-reexports section of the ORCv2 design doc. + +const char FooMod[] = " define i32 @foo_body() { \n" + " entry: \n" + " ret i32 1 \n" + " } \n"; + +const char BarMod[] = " define i32 @bar_body() { \n" + " entry: \n" + " ret i32 2 \n" + " } \n"; + +const char MainMod[] = + " define i32 @entry(i32 %argc) { \n" + " entry: \n" + " %and = and i32 %argc, 1 \n" + " %tobool = icmp eq i32 %and, 0 \n" + " br i1 %tobool, label %if.end, label %if.then \n" + " \n" + " if.then: \n" + " %call = tail call i32 @foo() \n" + " br label %return \n" + " \n" + " if.end: \n" + " %call1 = tail call i32 @bar() \n" + " br label %return \n" + " \n" + " return: \n" + " %retval.0 = phi i32 [ %call, %if.then ], [ %call1, %if.end ] \n" + " ret i32 %retval.0 \n" + " } \n" + " \n" + " declare i32 @foo() \n" + " declare i32 @bar() \n"; + +LLVMErrorRef applyDataLayout(void *Ctx, LLVMModuleRef M) { + LLVMSetDataLayout(M, LLVMOrcLLJITGetDataLayoutStr((LLVMOrcLLJITRef)Ctx)); + return LLVMErrorSuccess; +} + +LLVMErrorRef parseExampleModule(const char *Source, size_t Len, + const char *Name, + LLVMOrcThreadSafeModuleRef *TSM) { + // Create a new ThreadSafeContext and underlying LLVMContext. + LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext(); + + // Get a reference to the underlying LLVMContext. + LLVMContextRef Ctx = LLVMOrcThreadSafeContextGetContext(TSCtx); + + // Wrap Source in a MemoryBuffer + LLVMMemoryBufferRef MB = + LLVMCreateMemoryBufferWithMemoryRange(Source, Len, Name, 1); + + // Parse the LLVM module. + LLVMModuleRef M; + char *ErrMsg; + if (LLVMParseIRInContext(Ctx, MB, &M, &ErrMsg)) { + LLVMErrorRef Err = LLVMCreateStringError(ErrMsg); + LLVMDisposeMessage(ErrMsg); + return Err; + } + + // Our module is now complete. Wrap it and our ThreadSafeContext in a + // ThreadSafeModule. + *TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx); + + // Dispose of our local ThreadSafeContext value. The underlying LLVMContext + // will be kept alive by our ThreadSafeModule, TSM. + LLVMOrcDisposeThreadSafeContext(TSCtx); + + return LLVMErrorSuccess; +} + +void Destroy(void *Ctx) {} + +void Materialize(void *Ctx, LLVMOrcMaterializationResponsibilityRef MR) { + int MainResult = 0; + + size_t NumSymbols; + LLVMOrcSymbolStringPoolEntryRef *Symbols = + LLVMOrcMaterializationResponsibilityGetRequestedSymbols(MR, &NumSymbols); + + assert(NumSymbols == 1); + + LLVMOrcLLJITRef J = (LLVMOrcLLJITRef)Ctx; + LLVMOrcSymbolStringPoolEntryRef Sym = Symbols[0]; + + LLVMOrcThreadSafeModuleRef TSM = 0; + LLVMErrorRef Err; + + LLVMOrcSymbolStringPoolEntryRef FooBody = + LLVMOrcLLJITMangleAndIntern(J, "foo_body"); + LLVMOrcSymbolStringPoolEntryRef BarBody = + LLVMOrcLLJITMangleAndIntern(J, "bar_body"); + + if (Sym == FooBody) { + if ((Err = parseExampleModule(FooMod, strlen(FooMod), "foo-mod", &TSM))) { + MainResult = handleError(Err); + goto cleanup; + } + } else if (Sym == BarBody) { + if ((Err = parseExampleModule(BarMod, strlen(BarMod), "bar-mod", &TSM))) { + MainResult = handleError(Err); + goto cleanup; + } + } else { + MainResult = 1; + goto cleanup; + } + assert(TSM); + + if ((Err = LLVMOrcThreadSafeModuleWithModuleDo(TSM, &applyDataLayout, Ctx))) { + MainResult = handleError(Err); + goto cleanup; + } + +cleanup: + LLVMOrcReleaseSymbolStringPoolEntry(BarBody); + LLVMOrcReleaseSymbolStringPoolEntry(FooBody); + LLVMOrcDisposeSymbols(Symbols); + if (MainResult == 1) { + LLVMOrcMaterializationResponsibilityFailMaterialization(MR); + LLVMOrcDisposeMaterializationResponsibility(MR); + } else { + LLVMOrcIRTransformLayerRef IRLayer = LLVMOrcLLJITGetIRTransformLayer(J); + LLVMOrcIRTransformLayerEmit(IRLayer, MR, TSM); + } + return; +} + +int main(int argc, char *argv[]) { + + int MainResult = 0; + + // Parse command line arguments and initialize LLVM Core. + LLVMParseCommandLineOptions(argc, (const char **)argv, ""); + LLVMInitializeCore(LLVMGetGlobalPassRegistry()); + + // Initialize native target codegen and asm printer. + LLVMInitializeNativeTarget(); + LLVMInitializeNativeAsmPrinter(); + + // Set up a JIT instance. + LLVMOrcLLJITRef J; + const char *TargetTriple; + { + LLVMErrorRef Err; + if ((Err = LLVMOrcCreateLLJIT(&J, 0))) { + MainResult = handleError(Err); + goto llvm_shutdown; + } + TargetTriple = LLVMOrcLLJITGetTripleString(J); + } + + // Add our main module to the JIT. + { + LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J); + LLVMErrorRef Err; + + LLVMOrcThreadSafeModuleRef MainTSM; + if ((Err = parseExampleModule(MainMod, strlen(MainMod), "main-mod", + &MainTSM))) { + MainResult = handleError(Err); + goto jit_cleanup; + } + + if ((Err = LLVMOrcLLJITAddLLVMIRModule(J, MainJD, MainTSM))) { + LLVMOrcDisposeThreadSafeModule(MainTSM); + MainResult = handleError(Err); + goto jit_cleanup; + } + } + + LLVMJITSymbolFlags Flags = { + LLVMJITSymbolGenericFlagsExported | LLVMJITSymbolGenericFlagsCallable, 0}; + LLVMOrcCSymbolFlagsMapPair FooSym = { + LLVMOrcLLJITMangleAndIntern(J, "foo_body"), Flags}; + LLVMOrcCSymbolFlagsMapPair BarSym = { + LLVMOrcLLJITMangleAndIntern(J, "bar_body"), Flags}; + + // add custom MaterializationUnit + { + LLVMOrcMaterializationUnitRef FooMU = + LLVMOrcCreateCustomMaterializationUnit("FooMU", J, &FooSym, 1, NULL, + &Materialize, NULL, &Destroy); + + LLVMOrcMaterializationUnitRef BarMU = + LLVMOrcCreateCustomMaterializationUnit("BarMU", J, &BarSym, 1, NULL, + &Materialize, NULL, &Destroy); + + LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J); + LLVMOrcJITDylibDefine(MainJD, FooMU); + LLVMOrcJITDylibDefine(MainJD, BarMU); + } + + // add lazy reexports + LLVMOrcIndirectStubsManagerRef ISM = + LLVMOrcCreateLocalIndirectStubsManager(TargetTriple); + + LLVMOrcLazyCallThroughManagerRef LCTM; + { + LLVMErrorRef Err; + LLVMOrcExecutionSessionRef ES = LLVMOrcLLJITGetExecutionSession(J); + if ((Err = LLVMOrcCreateLocalLazyCallThroughManager(TargetTriple, ES, 0, + &LCTM))) { + LLVMOrcDisposeIndirectStubsManager(ISM); + MainResult = handleError(Err); + goto jit_cleanup; + } + } + + LLVMOrcCSymbolAliasMapPair ReExports[2] = { + {LLVMOrcLLJITMangleAndIntern(J, "foo"), + {LLVMOrcLLJITMangleAndIntern(J, "foo_body"), Flags}}, + {LLVMOrcLLJITMangleAndIntern(J, "bar"), + {LLVMOrcLLJITMangleAndIntern(J, "bar_body"), Flags}}, + }; + + { + LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J); + LLVMOrcMaterializationUnitRef MU = + LLVMOrcLazyReexports(LCTM, ISM, MainJD, ReExports, 2); + LLVMOrcJITDylibDefine(MainJD, MU); + } + + // Look up the address of our demo entry point. + LLVMOrcJITTargetAddress EntryAddr; + { + LLVMErrorRef Err; + if ((Err = LLVMOrcLLJITLookup(J, &EntryAddr, "entry"))) { + MainResult = handleError(Err); + goto cleanup; + } + } + + // If we made it here then everything succeeded. Execute our JIT'd code. + int32_t (*Entry)(int32_t) = (int32_t(*)(int32_t))EntryAddr; + int32_t Result = Entry(argc); + + printf("--- Result ---\n"); + printf("entry(%i) = %i\n", argc, Result); + +cleanup : { + LLVMOrcDisposeIndirectStubsManager(ISM); + LLVMOrcDisposeLazyCallThroughManager(LCTM); +} + +jit_cleanup: + // Destroy our JIT instance. This will clean up any memory that the JIT has + // taken ownership of. This operation is non-trivial (e.g. it may need to + // JIT static destructors) and may also fail. In that case we want to render + // the error to stderr, but not overwrite any existing return value. + { + LLVMErrorRef Err; + if ((Err = LLVMOrcDisposeLLJIT(J))) { + int NewFailureResult = handleError(Err); + if (MainResult == 0) + MainResult = NewFailureResult; + } + } + +llvm_shutdown: + // Shut down LLVM. + LLVMShutdown(); + + return MainResult; +} diff --git a/llvm/include/llvm-c/LLJIT.h b/llvm/include/llvm-c/LLJIT.h --- a/llvm/include/llvm-c/LLJIT.h +++ b/llvm/include/llvm-c/LLJIT.h @@ -227,6 +227,14 @@ */ LLVMOrcIRTransformLayerRef LLVMOrcLLJITGetIRTransformLayer(LLVMOrcLLJITRef J); +/** + * Get the LLJIT instance's default data layout string. + * + * This string is owned by the LLJIT instance and does not need to be freed + * by the caller. + */ +const char *LLVMOrcLLJITGetDataLayoutStr(LLVMOrcLLJITRef J); + 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 --- a/llvm/include/llvm-c/Orc.h +++ b/llvm/include/llvm-c/Orc.h @@ -145,6 +145,23 @@ */ typedef LLVMOrcCSymbolAliasMapPair *LLVMOrcCSymbolAliasMapPairs; +/** + * A reference to an orc::JITDylib instance. + */ +typedef struct LLVMOrcOpaqueJITDylib *LLVMOrcJITDylibRef; + +typedef struct { + LLVMOrcSymbolStringPoolEntryRef *Symbols; + size_t Length; +} LLVMOrcCSymbolsList; + +typedef struct { + LLVMOrcJITDylibRef JD; + LLVMOrcCSymbolsList Names; +} LLVMOrcCDependenceMapPair; + +typedef LLVMOrcCDependenceMapPair *LLVMOrcCDependenceMapPairs; + /** * Lookup kind. This can be used by definition generators when deciding whether * to produce a definition for a requested symbol. @@ -210,11 +227,6 @@ typedef struct LLVMOrcOpaqueMaterializationResponsibility *LLVMOrcMaterializationResponsibilityRef; -/** - * A reference to an orc::JITDylib instance. - */ -typedef struct LLVMOrcOpaqueJITDylib *LLVMOrcJITDylibRef; - /** * A MaterializationUnit materialize callback. * @@ -597,6 +609,160 @@ size_t NumPairs); // TODO: ImplSymbolMad SrcJDLoc +/** + * Disposes of the passed MaterializationResponsibility object. + * + * This should only be done after the symbols covered by the object have either + * been resolved and emitted (via + * LLVMOrcMaterializationResponsibilityNotifyResolved and + * LLVMOrcMaterializationResponsibilityNotifyEmitted) or failed (via + * LLVMOrcMaterializationResponsibilityFailMaterialization). + */ +void LLVMOrcDisposeMaterializationResponsibility( + LLVMOrcMaterializationResponsibilityRef MR); + +/** + * Returns the target JITDylib that these symbols are being materialized into. + */ +LLVMOrcJITDylibRef LLVMOrcMaterializationResponsibilityGetTargetDylib( + LLVMOrcMaterializationResponsibilityRef MR); + +/** + * Returns the ExecutionSession for this MaterializationResponsibility. + */ +LLVMOrcExecutionSessionRef +LLVMOrcMaterializationResponsibilityGetExecutionSession( + LLVMOrcMaterializationResponsibilityRef MR); + +/** Returns the symbol flags map for this responsibility instance. + * Note: The returned flags may have transient flags (Lazy, Materializing) + * set. These should be stripped with JITSymbolFlags::stripTransientFlags + * before using. + * + * The lenght of the array is returned in NumPairs and the caller is responsible + * for the returned memory and needs to call LLVMOrcDisposeCSymbolFlagsMap. + */ +LLVMOrcCSymbolFlagsMapPairs LLVMOrcMaterializationResponsibilityGetSymbols( + LLVMOrcMaterializationResponsibilityRef MR, size_t *NumPairs); + +/** + * Disposes of the passed LLVMOrcCSymbolFlagsMap. + */ +void LLVMOrcDisposeCSymbolFlagsMap(LLVMOrcCSymbolFlagsMapPairs Pairs); + +/** + * Returns the initialization pseudo-symbol, if any. This symbol will also + * be present in the SymbolFlagsMap for this MaterializationResponsibility + * object. + */ +LLVMOrcSymbolStringPoolEntryRef +LLVMOrcMaterializationResponsibilityGetInitializerSymbol( + LLVMOrcMaterializationResponsibilityRef MR); + +/** + * Returns the names of any symbols covered by this + * MaterializationResponsibility object that have queries pending. This + * information can be used to return responsibility for unrequested symbols + * back to the JITDylib via the delegate method. + */ +LLVMOrcSymbolStringPoolEntryRef * +LLVMOrcMaterializationResponsibilityGetRequestedSymbols( + LLVMOrcMaterializationResponsibilityRef MR, size_t *NumSymbols); + +/** + * Disposes of the passed LLVMOrcSymbolStringPoolEntryRef* . + */ +void LLVMOrcDisposeSymbols(LLVMOrcSymbolStringPoolEntryRef *Symbols); + +/* + * Notifies the target JITDylib that the given symbols have been resolved. + * This will update the given symbols' addresses in the JITDylib, and notify + * any pending queries on the given symbols of their resolution. The given + * symbols must be ones covered by this MaterializationResponsibility + * instance. Individual calls to this method may resolve a subset of the + * symbols, but all symbols must have been resolved prior to calling emit. + * + * This method will return an error if any symbols being resolved have been + * moved to the error state due to the failure of a dependency. If this + * method returns an error then clients should log it and call + * failMaterialize. If no dependencies have been registered for the + * symbols covered by this MaterializationResponsibiility then this method + * is guaranteed to return Error::success() and can be wrapped with cantFail. + */ +LLVMErrorRef LLVMOrcMaterializationResponsibilityNotifyResolved( + LLVMOrcMaterializationResponsibilityRef MR, LLVMOrcCSymbolMapPairs Symbols, + size_t NumPairs); + +/** + * Notifies the target JITDylib (and any pending queries on that JITDylib) + * that all symbols covered by this MaterializationResponsibility instance + * have been emitted. + * + * This method will return an error if any symbols being resolved have been + * moved to the error state due to the failure of a dependency. If this + * method returns an error then clients should log it and call + * failMaterialize. If no dependencies have been registered for the + * symbols covered by this MaterializationResponsibiility then this method + */ +LLVMErrorRef LLVMOrcMaterializationResponsibilityNotifyEmitted( + LLVMOrcMaterializationResponsibilityRef MR); + +/** + * Attempt to claim responsibility for new definitions. This method can be + * used to claim responsibility for symbols that are added to a + * materialization unit during the compilation process (e.g. literal pool + * symbols). Symbol linkage rules are the same as for symbols that are + * defined up front: duplicate strong definitions will result in errors. + * Duplicate weak definitions will be discarded (in which case they will + * not be added to this responsibility instance). + * + * This method can be used by materialization units that want to add + * additional symbols at materialization time (e.g. stubs, compile + * callbacks, metadata) + */ +LLVMErrorRef LLVMOrcMaterializationResponsibilityDefineMaterializing( + LLVMOrcMaterializationResponsibilityRef MR, LLVMOrcCSymbolMapPairs Syms, + size_t NumPairs); + +/** + * Notify all not-yet-emitted covered by this MaterializationResponsibility + * instance that an error has occurred. + * This will remove all symbols covered by this MaterializationResponsibilty + * from the target JITDylib, and send an error to any queries waiting on + * these symbols. + */ +void LLVMOrcMaterializationResponsibilityFailMaterialization( + LLVMOrcMaterializationResponsibilityRef MR); + +/** + * Transfers responsibility to the given MaterializationUnit for all + * symbols defined by that MaterializationUnit. This allows + * materializers to break up work based on run-time information (e.g. + * by introspecting which symbols have actually been looked up and + * materializing only those). + */ +LLVMErrorRef LLVMOrcMaterializationResponsibilityReplace( + LLVMOrcMaterializationResponsibilityRef MR, + LLVMOrcMaterializationUnitRef MU); +/** + * Delegates responsibility for the given symbols to the returned + * materialization responsibility. Useful for breaking up work between + * threads, or different kinds of materialization processes. + */ +LLVMOrcMaterializationResponsibilityRef +LLVMOrcMaterializationResponsibilityDelegate( + LLVMOrcMaterializationResponsibilityRef MR, + LLVMOrcSymbolStringPoolEntryRef *Symbols, size_t NumSymbols); + +void LLVMOrcMaterializationResponsibilityAddDependencies( + LLVMOrcMaterializationResponsibilityRef MR, + LLVMOrcSymbolStringPoolEntryRef Name, + LLVMOrcCDependenceMapPairs Dependencies, size_t NumPairs); + +void LLVMOrcMaterializationResponsibilityAddDependenciesForAll( + LLVMOrcMaterializationResponsibilityRef MR, + LLVMOrcCDependenceMapPairs Dependencies, size_t NumPairs); + /** * Create a "bare" JITDylib. * @@ -845,6 +1011,10 @@ */ void LLVMOrcDisposeObjectLayer(LLVMOrcObjectLayerRef ObjLayer); +void LLVMOrcIRTransformLayerEmit(LLVMOrcIRTransformLayerRef IRTransformLayer, + LLVMOrcMaterializationResponsibilityRef MR, + LLVMOrcThreadSafeModuleRef TSM); + /** * Set the transform function of the provided transform layer, passing through a * pointer to user provided context. diff --git a/llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp b/llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp --- a/llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp +++ b/llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp @@ -240,6 +240,32 @@ return JSF; } +static SymbolMap toSymbolMap(LLVMOrcCSymbolMapPairs Syms, size_t NumPairs) { + SymbolMap SM; + for (size_t I = 0; I != NumPairs; ++I) { + JITSymbolFlags Flags = toJITSymbolFlags(Syms[I].Sym.Flags); + SM[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms[I].Name))] = + JITEvaluatedSymbol(Syms[I].Sym.Address, Flags); + } + return SM; +} + +static SymbolDependenceMap +toSymbolDependenceMap(LLVMOrcCDependenceMapPairs Pairs, size_t NumPairs) { + SymbolDependenceMap SDM; + for (size_t I = 0; I != NumPairs; ++I) { + JITDylib *JD = unwrap(Pairs[I].JD); + SymbolNameSet Names; + + for (size_t J = 0; J != Pairs[I].Names.Length; ++J) { + auto Sym = Pairs[I].Names.Symbols[J]; + Names.insert(OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Sym))); + } + SDM[JD] = Names; + } + return SDM; +} + } // end anonymous namespace void LLVMOrcExecutionSessionSetErrorReporter( @@ -334,13 +360,7 @@ LLVMOrcMaterializationUnitRef LLVMOrcAbsoluteSymbols(LLVMOrcCSymbolMapPairs Syms, size_t NumPairs) { - SymbolMap SM; - for (size_t I = 0; I != NumPairs; ++I) { - JITSymbolFlags Flags = toJITSymbolFlags(Syms[I].Sym.Flags); - SM[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms[I].Name))] = - JITEvaluatedSymbol(Syms[I].Sym.Address, Flags); - } - + SymbolMap SM = toSymbolMap(Syms, NumPairs); return wrap(absoluteSymbols(std::move(SM)).release()); } @@ -364,6 +384,145 @@ .release()); } +void LLVMOrcDisposeMaterializationResponsibility( + LLVMOrcMaterializationResponsibilityRef MR) { + std::unique_ptr TmpMR(unwrap(MR)); +} + +LLVMOrcJITDylibRef LLVMOrcMaterializationResponsibilityGetTargetDylib( + LLVMOrcMaterializationResponsibilityRef MR) { + return wrap(&unwrap(MR)->getTargetJITDylib()); +} + +LLVMOrcExecutionSessionRef +LLVMOrcMaterializationResponsibilityGetExecutionSession( + LLVMOrcMaterializationResponsibilityRef MR) { + return wrap(&unwrap(MR)->getExecutionSession()); +} + +LLVMOrcCSymbolFlagsMapPairs LLVMOrcMaterializationResponsibilityGetSymbols( + LLVMOrcMaterializationResponsibilityRef MR, size_t *NumPairs) { + + auto Symbols = unwrap(MR)->getSymbols(); + LLVMOrcCSymbolFlagsMapPairs Result = static_cast( + safe_malloc(Symbols.size() * sizeof(LLVMOrcCSymbolFlagsMapPair))); + size_t I = 0; + for (auto const &pair : Symbols) { + auto Name = wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(pair.first)); + auto Flags = pair.second; + Result[I] = {Name, {Flags.getRawFlagsValue(), Flags.getTargetFlags()}}; + I++; + } + *NumPairs = Symbols.size(); + return Result; +} + +void LLVMOrcDisposeCSymbolFlagsMap(LLVMOrcCSymbolFlagsMapPairs Pairs) { + free(Pairs); +} + +LLVMOrcSymbolStringPoolEntryRef +LLVMOrcMaterializationResponsibilityGetInitializerSymbol( + LLVMOrcMaterializationResponsibilityRef MR) { + auto Sym = unwrap(MR)->getInitializerSymbol(); + return wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Sym)); +} + +LLVMOrcSymbolStringPoolEntryRef * +LLVMOrcMaterializationResponsibilityGetRequestedSymbols( + LLVMOrcMaterializationResponsibilityRef MR, size_t *NumSymbols) { + + auto Symbols = unwrap(MR)->getRequestedSymbols(); + LLVMOrcSymbolStringPoolEntryRef *Result = + static_cast(safe_malloc( + Symbols.size() * sizeof(LLVMOrcSymbolStringPoolEntryRef))); + size_t I = 0; + for (auto &Name : Symbols) { + Result[I] = wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name)); + I++; + } + *NumSymbols = Symbols.size(); + return Result; +} + +void LLVMOrcDisposeSymbols(LLVMOrcSymbolStringPoolEntryRef *Symbols) { + free(Symbols); +} + +LLVMErrorRef LLVMOrcMaterializationResponsibilityNotifyResolved( + LLVMOrcMaterializationResponsibilityRef MR, LLVMOrcCSymbolMapPairs Symbols, + size_t NumPairs) { + SymbolMap SM = toSymbolMap(Symbols, NumPairs); + return wrap(unwrap(MR)->notifyResolved(std::move(SM))); +} + +LLVMErrorRef LLVMOrcMaterializationResponsibilityNotifyEmitted( + LLVMOrcMaterializationResponsibilityRef MR) { + return wrap(unwrap(MR)->notifyEmitted()); +} + +// TODO(vchuravy) Test +LLVMErrorRef LLVMOrcMaterializationResponsibilityDefineMaterializing( + LLVMOrcMaterializationResponsibilityRef MR, + LLVMOrcCSymbolFlagsMapPairs Syms, size_t NumSyms) { + SymbolFlagsMap SFM; + for (size_t I = 0; I != NumSyms; ++I) + SFM[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms[I].Name))] = + toJITSymbolFlags(Syms[I].Flags); + + return wrap(unwrap(MR)->defineMaterializing(std::move(SFM))); +} + +LLVMErrorRef LLVMOrcMaterializationResponsibilityReplace( + LLVMOrcMaterializationResponsibilityRef MR, + LLVMOrcMaterializationUnitRef MU) { + std::unique_ptr TmpMU(unwrap(MU)); + return wrap(unwrap(MR)->replace(std::move(TmpMU))); +} + +LLVMOrcMaterializationResponsibilityRef +LLVMOrcMaterializationResponsibilityDelegate( + LLVMOrcMaterializationResponsibilityRef MR, + LLVMOrcSymbolStringPoolEntryRef *Symbols, size_t NumSymbols) { + SymbolNameSet Syms; + for (size_t I = 0; I != NumSymbols; I++) { + Syms.insert(OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Symbols[I]))); + } + return wrap(unwrap(MR)->delegate(Syms)->release()); +} + +void LLVMOrcMaterializationResponsibilityAddDependencies( + LLVMOrcMaterializationResponsibilityRef MR, + LLVMOrcSymbolStringPoolEntryRef Name, + LLVMOrcCDependenceMapPairs Dependencies, size_t NumPairs) { + + SymbolDependenceMap SDM = toSymbolDependenceMap(Dependencies, NumPairs); + auto Sym = OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Name)); + unwrap(MR)->addDependencies(Sym, SDM); +} + +void LLVMOrcMaterializationResponsibilityAddDependenciesForAll( + LLVMOrcMaterializationResponsibilityRef MR, + LLVMOrcCDependenceMapPairs Dependencies, size_t NumPairs) { + + SymbolDependenceMap SDM = toSymbolDependenceMap(Dependencies, NumPairs); + unwrap(MR)->addDependenciesForAll(SDM); +} + +void LLVMOrcMaterializationResponsibilityFailMaterialization( + LLVMOrcMaterializationResponsibilityRef MR) { + unwrap(MR)->failMaterialization(); +} + +void LLVMOrcIRTransformLayerEmit(LLVMOrcIRTransformLayerRef IRLayer, + LLVMOrcMaterializationResponsibilityRef MR, + LLVMOrcThreadSafeModuleRef TSM) { + std::unique_ptr TmpTSM(unwrap(TSM)); + unwrap(IRLayer)->emit( + std::unique_ptr(unwrap(MR)), + std::move(*TmpTSM)); +} + LLVMOrcJITDylibRef LLVMOrcExecutionSessionCreateBareJITDylib(LLVMOrcExecutionSessionRef ES, const char *Name) { @@ -749,6 +908,10 @@ return wrap(&unwrap(J)->getIRTransformLayer()); } +const char *LLVMOrcLLJITGetDataLayoutStr(LLVMOrcLLJITRef J) { + return unwrap(J)->getDataLayout().getStringRepresentation().c_str(); +} + LLVMOrcIndirectStubsManagerRef LLVMOrcCreateLocalIndirectStubsManager(const char *TargetTriple) { auto builder = createLocalIndirectStubsManagerBuilder(Triple(TargetTriple)); diff --git a/llvm/test/CMakeLists.txt b/llvm/test/CMakeLists.txt --- a/llvm/test/CMakeLists.txt +++ b/llvm/test/CMakeLists.txt @@ -167,6 +167,7 @@ OrcV2CBindingsRemovableCode OrcV2CBindingsReflectProcessSymbols OrcV2CBindingsLazy + OrcV2CBindingsVeryLazy ) if(CMAKE_HOST_UNIX) list(APPEND LLVM_TEST_DEPENDS diff --git a/llvm/test/lit.cfg.py b/llvm/test/lit.cfg.py --- a/llvm/test/lit.cfg.py +++ b/llvm/test/lit.cfg.py @@ -187,7 +187,8 @@ ToolSubst('OrcV2CBindingsAddObjectFile', unresolved='ignore'), ToolSubst('OrcV2CBindingsRemovableCode', unresolved='ignore'), ToolSubst('OrcV2CBindingsReflectProcessSymbols', unresolved='ignore'), - ToolSubst('OrcV2CBindingsLazy', unresolved='ignore')]) + ToolSubst('OrcV2CBindingsLazy', unresolved='ignore'), + ToolSubst('OrcV2CBindingsVeryLazy', unresolved='ignore')]) llvm_config.add_tool_substitutions(tools, config.llvm_tools_dir) diff --git a/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp b/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp --- a/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp +++ b/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp @@ -354,3 +354,83 @@ int32_t Result = SumFn(1, 1); ASSERT_EQ(2, Result); } + +void Destroy(void *Ctx) {} + +void TargetFn() {} + +void Materialize(void *Ctx, LLVMOrcMaterializationResponsibilityRef MR) { + LLVMOrcJITDylibRef JD = + LLVMOrcMaterializationResponsibilityGetTargetDylib(MR); + ASSERT_TRUE(!!JD); + + LLVMOrcExecutionSessionRef ES = + LLVMOrcMaterializationResponsibilityGetExecutionSession(MR); + ASSERT_TRUE(!!ES); + + LLVMOrcSymbolStringPoolEntryRef InitSym = + LLVMOrcMaterializationResponsibilityGetInitializerSymbol(MR); + ASSERT_TRUE(!InitSym); + + size_t NumSymbols; + LLVMOrcCSymbolFlagsMapPairs Symbols = + LLVMOrcMaterializationResponsibilityGetSymbols(MR, &NumSymbols); + + ASSERT_TRUE(!!Symbols); + ASSERT_EQ(NumSymbols, 1); + + LLVMOrcSymbolStringPoolEntryRef *RequestedSymbols = + LLVMOrcMaterializationResponsibilityGetRequestedSymbols(MR, &NumSymbols); + + ASSERT_TRUE(!!RequestedSymbols); + ASSERT_EQ(NumSymbols, 1); + + LLVMOrcCSymbolFlagsMapPair TargetSym = Symbols[0]; + + ASSERT_EQ(RequestedSymbols[0], TargetSym.Name); + LLVMOrcRetainSymbolStringPoolEntry(TargetSym.Name); + + LLVMOrcDisposeCSymbolFlagsMap(Symbols); + LLVMOrcDisposeSymbols(RequestedSymbols); + + LLVMOrcJITTargetAddress Addr = (LLVMOrcJITTargetAddress)(&TargetFn); + + LLVMJITSymbolFlags Flags = { + LLVMJITSymbolGenericFlagsExported | LLVMJITSymbolGenericFlagsCallable, 0}; + // TODO(lhames & vchuravy) flags returned here and not correct + // ASSERT_EQ(TargetSym.Flags.GenericFlags, Flags.GenericFlags); + // ASSERT_EQ(TargetSym.Flags.TargetFlags, Flags.TargetFlags); + + LLVMJITEvaluatedSymbol Sym = {Addr, Flags}; + LLVMJITCSymbolMapPair Pair = {TargetSym.Name, Sym}; + + LLVMOrcMaterializationResponsibilityNotifyResolved(MR, &Pair, 1); + + // TODO (lhames) test + // - LLVMOrcMaterializationResponsibilityDefineMaterializing( + // - LLVMOrcMaterializationResponsibilityReplace + // - LLVMOrcMaterializationResponsibilityDelegate + // - LLVMOrcMaterializationResponsibilityAddDependencies + // - LLVMOrcMaterializationResponsibilityAddDependenciesForAll + + LLVMOrcMaterializationResponsibilityNotifyEmitted(MR); +} + +TEST_F(OrcCAPITestBase, MaterializationResponsibility) { + LLVMJITSymbolFlags Flags = { + LLVMJITSymbolGenericFlagsExported | LLVMJITSymbolGenericFlagsCallable, 0}; + LLVMOrcCSymbolFlagsMapPair Sym = {LLVMOrcLLJITMangleAndIntern(Jit, "foo"), + Flags}; + + LLVMOrcMaterializationUnitRef MU = LLVMOrcCreateCustomMaterializationUnit( + "MU", Jit, &Sym, 1, NULL, &Materialize, NULL, &Destroy); + LLVMOrcJITDylibRef JD = LLVMOrcLLJITGetMainJITDylib(Jit); + LLVMOrcJITDylibDefine(JD, MU); + + LLVMOrcJITTargetAddress Addr; + if (LLVMErrorRef Err = LLVMOrcLLJITLookup(Jit, &Addr, "foo")) { + FAIL() << "foo was not materialized " << toString(Err); + } + ASSERT_TRUE(!!Addr); + ASSERT_EQ(Addr, (LLVMOrcJITTargetAddress)&TargetFn); +}