Skip to content

Commit 5072a52

Browse files
committed
[ORC] Add C API support for defining absolute symbols.
Also tweaks the definition of TryToGenerate to make it dovetail more neatly with the new function.
1 parent e2b7d59 commit 5072a52

File tree

2 files changed

+136
-31
lines changed

2 files changed

+136
-31
lines changed

llvm/include/llvm-c/Orc.h

Lines changed: 87 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,35 @@ LLVM_C_EXTERN_C_BEGIN
3838
*/
3939
typedef uint64_t LLVMOrcJITTargetAddress;
4040

41+
/**
42+
* Represents generic linkage flags for a symbol definition.
43+
*/
44+
typedef enum {
45+
LLVMJITSymbolGenericFlagsExported = 1U << 0,
46+
LLVMJITSymbolGenericFlagsWeak = 1U << 1
47+
} LLVMJITSymbolGenericFlags;
48+
49+
/**
50+
* Represents target specific flags for a symbol definition.
51+
*/
52+
typedef uint8_t LLVMJITTargetSymbolFlags;
53+
54+
/**
55+
* Represents the linkage flags for a symbol definition.
56+
*/
57+
typedef struct {
58+
uint8_t GenericFlags;
59+
uint8_t TargetFlags;
60+
} LLVMJITSymbolFlags;
61+
62+
/**
63+
* Represents an evaluated symbol address and flags.
64+
*/
65+
typedef struct {
66+
LLVMOrcJITTargetAddress Address;
67+
LLVMJITSymbolFlags Flags;
68+
} LLVMJITEvaluatedSymbol;
69+
4170
/**
4271
* A reference to an orc::ExecutionSession instance.
4372
*/
@@ -59,6 +88,20 @@ typedef struct LLVMOrcOpaqueSymbolStringPool *LLVMOrcSymbolStringPoolRef;
5988
typedef struct LLVMOrcOpaqueSymbolStringPoolEntry
6089
*LLVMOrcSymbolStringPoolEntryRef;
6190

91+
/**
92+
* Represents a pair of a symbol name and an evaluated symbol.
93+
*/
94+
typedef struct {
95+
LLVMOrcSymbolStringPoolEntryRef Name;
96+
LLVMJITEvaluatedSymbol Sym;
97+
} LLVMJITCSymbolMapPair;
98+
99+
/**
100+
* Represents a list of (SymbolStringPtr, JITEvaluatedSymbol) pairs that can be
101+
* used to construct a SymbolMap.
102+
*/
103+
typedef LLVMJITCSymbolMapPair *LLVMOrcCSymbolMapPairs;
104+
62105
/**
63106
* Lookup kind. This can be used by definition generators when deciding whether
64107
* to produce a definition for a requested symbol.
@@ -111,6 +154,11 @@ typedef struct {
111154
*/
112155
typedef LLVMOrcCLookupSetElement *LLVMOrcCLookupSet;
113156

157+
/**
158+
* A reference to an orc::MaterializationUnit.
159+
*/
160+
typedef struct LLVMOrcOpaqueMaterializationUnit *LLVMOrcMaterializationUnitRef;
161+
114162
/**
115163
* A reference to an orc::JITDylib instance.
116164
*/
@@ -178,7 +226,7 @@ typedef LLVMErrorRef (*LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction)(
178226
LLVMOrcDefinitionGeneratorRef GeneratorObj, void *Ctx,
179227
LLVMOrcLookupStateRef *LookupState, LLVMOrcLookupKind Kind,
180228
LLVMOrcJITDylibRef JD, LLVMOrcJITDylibLookupFlags JDLookupFlags,
181-
LLVMOrcCLookupSet LookupSet);
229+
LLVMOrcCLookupSet LookupSet, size_t LookupSetSize);
182230

183231
/**
184232
* Predicate function for SymbolStringPoolEntries.
@@ -271,22 +319,6 @@ void LLVMOrcRetainSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S);
271319
*/
272320
void LLVMOrcReleaseSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S);
273321

274-
/**
275-
* Return a reference to a newly created resource tracker associated with JD.
276-
* The tracker is returned with an initial ref-count of 1, and must be released
277-
* with LLVMOrcReleaseResourceTracker when no longer needed.
278-
*/
279-
LLVMOrcResourceTrackerRef
280-
LLVMOrcJITDylibCreateResourceTracker(LLVMOrcJITDylibRef JD);
281-
282-
/**
283-
* Return a reference to the default resource tracker for the given JITDylib.
284-
* This operation will increase the retain count of the tracker: Clients should
285-
* call LLVMOrcReleaseResourceTracker when the result is no longer needed.
286-
*/
287-
LLVMOrcResourceTrackerRef
288-
LLVMOrcJITDylibGetDefaultResourceTracker(LLVMOrcJITDylibRef JD);
289-
290322
/**
291323
* Reduces the ref-count of a ResourceTracker.
292324
*/
@@ -313,6 +345,18 @@ LLVMErrorRef LLVMOrcResourceTrackerRemove(LLVMOrcResourceTrackerRef RT);
313345
void LLVMOrcDisposeDefinitionGenerator(
314346
LLVMOrcDefinitionGeneratorRef DG);
315347

348+
/**
349+
* Dispose of a MaterializationUnit.
350+
*/
351+
void LLVMOrcDisposeMaterializationUnit(LLVMOrcMaterializationUnitRef MU);
352+
353+
/**
354+
* Create a MaterializationUnit to define the given symbols as pointing to
355+
* the corresponding raw addresses.
356+
*/
357+
LLVMOrcMaterializationUnitRef
358+
LLVMOrcAbsoluteSymbols(LLVMOrcCSymbolMapPairs Syms, size_t NumPairs);
359+
316360
/**
317361
* Create a "bare" JITDylib.
318362
*
@@ -349,6 +393,32 @@ LLVMOrcExecutionSessionCreateJITDylib(LLVMOrcExecutionSessionRef ES,
349393
*/
350394
LLVMOrcJITDylibRef LLVMOrcExecutionSessionGetJITDylibByName(const char *Name);
351395

396+
/**
397+
* Return a reference to a newly created resource tracker associated with JD.
398+
* The tracker is returned with an initial ref-count of 1, and must be released
399+
* with LLVMOrcReleaseResourceTracker when no longer needed.
400+
*/
401+
LLVMOrcResourceTrackerRef
402+
LLVMOrcJITDylibCreateResourceTracker(LLVMOrcJITDylibRef JD);
403+
404+
/**
405+
* Return a reference to the default resource tracker for the given JITDylib.
406+
* This operation will increase the retain count of the tracker: Clients should
407+
* call LLVMOrcReleaseResourceTracker when the result is no longer needed.
408+
*/
409+
LLVMOrcResourceTrackerRef
410+
LLVMOrcJITDylibGetDefaultResourceTracker(LLVMOrcJITDylibRef JD);
411+
412+
/**
413+
* Add the given MaterializationUnit to the given JITDylib.
414+
*
415+
* If this operation succeeds then JITDylib JD will take ownership of MU.
416+
* If the operation fails then ownership remains with the caller who should
417+
* call LLVMOrcDisposeMaterializationUnit to destroy it.
418+
*/
419+
LLVMErrorRef LLVMOrcJITDylibDefine(LLVMOrcJITDylibRef JD,
420+
LLVMOrcMaterializationUnitRef MU);
421+
352422
/**
353423
* Calls remove on all trackers associated with this JITDylib, see
354424
* JITDylib::clear().

llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class OrcV2CAPIHelper {
3131
return Result;
3232
}
3333

34+
static SymbolStringPtr retainSymbolStringPtr(PoolEntryPtr P) {
35+
return SymbolStringPtr(P);
36+
}
37+
3438
static PoolEntryPtr getRawPoolEntryPtr(const SymbolStringPtr &S) {
3539
return S.S;
3640
}
@@ -61,6 +65,8 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionSession, LLVMOrcExecutionSessionRef)
6165
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SymbolStringPool, LLVMOrcSymbolStringPoolRef)
6266
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcV2CAPIHelper::PoolEntry,
6367
LLVMOrcSymbolStringPoolEntryRef)
68+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MaterializationUnit,
69+
LLVMOrcMaterializationUnitRef)
6470
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITDylib, LLVMOrcJITDylibRef)
6571
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ResourceTracker, LLVMOrcResourceTrackerRef)
6672
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DefinitionGenerator,
@@ -120,6 +126,8 @@ class CAPIDefinitionGenerator final : public DefinitionGenerator {
120126
CLookupSet.reserve(LookupSet.size());
121127
for (auto &KV : LookupSet) {
122128
LLVMOrcSymbolLookupFlags SLF;
129+
LLVMOrcSymbolStringPoolEntryRef Name =
130+
::wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(KV.first));
123131
switch (KV.second) {
124132
case SymbolLookupFlags::RequiredSymbol:
125133
SLF = LLVMOrcSymbolLookupFlagsRequiredSymbol;
@@ -128,16 +136,13 @@ class CAPIDefinitionGenerator final : public DefinitionGenerator {
128136
SLF = LLVMOrcSymbolLookupFlagsWeaklyReferencedSymbol;
129137
break;
130138
}
131-
132-
CLookupSet.push_back(
133-
{::wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(KV.first)), SLF});
139+
CLookupSet.push_back({Name, SLF});
134140
}
135-
CLookupSet.push_back({nullptr, LLVMOrcSymbolLookupFlagsRequiredSymbol});
136141

137142
// Run the C TryToGenerate function.
138-
auto Err =
139-
unwrap(TryToGenerate(::wrap(this), Ctx, &LSR, CLookupKind, ::wrap(&JD),
140-
CJDLookupFlags, CLookupSet.data()));
143+
auto Err = unwrap(TryToGenerate(::wrap(this), Ctx, &LSR, CLookupKind,
144+
::wrap(&JD), CJDLookupFlags,
145+
CLookupSet.data(), CLookupSet.size()));
141146

142147
// Restore the lookup state.
143148
OrcV2CAPIHelper::resetLookupState(LS, ::unwrap(LSR));
@@ -214,9 +219,32 @@ LLVMErrorRef LLVMOrcResourceTrackerRemove(LLVMOrcResourceTrackerRef RT) {
214219
return wrap(TmpRT->remove());
215220
}
216221

217-
void LLVMOrcDisposeDefinitionGenerator(
218-
LLVMOrcDefinitionGeneratorRef DG) {
219-
delete unwrap(DG);
222+
void LLVMOrcDisposeDefinitionGenerator(LLVMOrcDefinitionGeneratorRef DG) {
223+
std::unique_ptr<DefinitionGenerator> TmpDG(unwrap(DG));
224+
}
225+
226+
void LLVMOrcDisposeMaterializationUnit(LLVMOrcMaterializationUnitRef MU) {
227+
std::unique_ptr<MaterializationUnit> TmpMU(unwrap(MU));
228+
}
229+
230+
LLVMOrcMaterializationUnitRef
231+
LLVMOrcAbsoluteSymbols(LLVMOrcCSymbolMapPairs Syms, size_t NumPairs) {
232+
SymbolMap SM;
233+
for (size_t I = 0; I != NumPairs; ++I) {
234+
JITSymbolFlags Flags;
235+
236+
if (Syms[I].Sym.Flags.GenericFlags & LLVMJITSymbolGenericFlagsExported)
237+
Flags |= JITSymbolFlags::Exported;
238+
if (Syms[I].Sym.Flags.GenericFlags & LLVMJITSymbolGenericFlagsWeak)
239+
Flags |= JITSymbolFlags::Weak;
240+
241+
Flags.getTargetFlags() = Syms[I].Sym.Flags.TargetFlags;
242+
243+
SM[OrcV2CAPIHelper::retainSymbolStringPtr(unwrap(Syms[I].Name))] =
244+
JITEvaluatedSymbol(Syms[I].Sym.Address, Flags);
245+
}
246+
247+
return wrap(absoluteSymbols(std::move(SM)).release());
220248
}
221249

222250
LLVMOrcJITDylibRef
@@ -242,6 +270,17 @@ LLVMOrcExecutionSessionGetJITDylibByName(LLVMOrcExecutionSessionRef ES,
242270
return wrap(unwrap(ES)->getJITDylibByName(Name));
243271
}
244272

273+
LLVMErrorRef LLVMOrcJITDylibDefine(LLVMOrcJITDylibRef JD,
274+
LLVMOrcMaterializationUnitRef MU) {
275+
std::unique_ptr<MaterializationUnit> TmpMU(unwrap(MU));
276+
277+
if (auto Err = unwrap(JD)->define(TmpMU)) {
278+
TmpMU.release();
279+
return wrap(std::move(Err));
280+
}
281+
return LLVMErrorSuccess;
282+
}
283+
245284
LLVMErrorRef LLVMOrcJITDylibClear(LLVMOrcJITDylibRef JD) {
246285
return wrap(unwrap(JD)->clear());
247286
}
@@ -251,10 +290,6 @@ void LLVMOrcJITDylibAddGenerator(LLVMOrcJITDylibRef JD,
251290
unwrap(JD)->addGenerator(std::unique_ptr<DefinitionGenerator>(unwrap(DG)));
252291
}
253292

254-
void LLVMOrcDisposeDefinitionGenerator(LLVMOrcDefinitionGeneratorRef DG) {
255-
std::unique_ptr<DefinitionGenerator> TmpDG(unwrap(DG));
256-
}
257-
258293
LLVMOrcDefinitionGeneratorRef LLVMOrcCreateCustomCAPIDefinitionGenerator(
259294
void *Ctx, LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction F) {
260295
auto DG = std::make_unique<CAPIDefinitionGenerator>(Ctx, F);

0 commit comments

Comments
 (0)