Skip to content

Commit 462251b

Browse files
committed
[ORC-RT] Replace FnTag arg of WrapperFunction::call with generic dispatch arg.
This decouples function argument serialization / deserialization from the function call dispatch mechanism. This will eventually allow us to replace the existing __orc_rt_jit_dispatch function with a system that supports pre-linking parts of the ORC runtime into the executor.
1 parent 3928ede commit 462251b

File tree

5 files changed

+75
-31
lines changed

5 files changed

+75
-31
lines changed

compiler-rt/lib/orc/coff_platform.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "debug.h"
1919
#include "error.h"
20+
#include "jit_dispatch.h"
2021
#include "wrapper_function_utils.h"
2122

2223
#include <array>
@@ -315,9 +316,9 @@ Error COFFPlatformRuntimeState::dlopenFull(JITDylibState &JDS) {
315316
// Call back to the JIT to push the initializers.
316317
Expected<COFFJITDylibDepInfoMap> DepInfoMap((COFFJITDylibDepInfoMap()));
317318
if (auto Err = WrapperFunction<SPSExpected<SPSCOFFJITDylibDepInfoMap>(
318-
SPSExecutorAddr)>::call(&__orc_rt_coff_push_initializers_tag,
319-
DepInfoMap,
320-
ExecutorAddr::fromPtr(JDS.Header)))
319+
SPSExecutorAddr)>::
320+
call(JITDispatch(&__orc_rt_coff_push_initializers_tag), DepInfoMap,
321+
ExecutorAddr::fromPtr(JDS.Header)))
321322
return Err;
322323
if (!DepInfoMap)
323324
return DepInfoMap.takeError();
@@ -445,10 +446,9 @@ COFFPlatformRuntimeState::lookupSymbolInJITDylib(void *header,
445446
std::string_view Sym) {
446447
Expected<ExecutorAddr> Result((ExecutorAddr()));
447448
if (auto Err = WrapperFunction<SPSExpected<SPSExecutorAddr>(
448-
SPSExecutorAddr, SPSString)>::call(&__orc_rt_coff_symbol_lookup_tag,
449-
Result,
450-
ExecutorAddr::fromPtr(header),
451-
Sym))
449+
SPSExecutorAddr,
450+
SPSString)>::call(JITDispatch(&__orc_rt_coff_symbol_lookup_tag),
451+
Result, ExecutorAddr::fromPtr(header), Sym))
452452
return std::move(Err);
453453
return Result;
454454
}

compiler-rt/lib/orc/elfnix_platform.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "common.h"
1515
#include "compiler.h"
1616
#include "error.h"
17+
#include "jit_dispatch.h"
1718
#include "wrapper_function_utils.h"
1819

1920
#include <algorithm>
@@ -352,10 +353,9 @@ ELFNixPlatformRuntimeState::lookupSymbolInJITDylib(void *DSOHandle,
352353
std::string_view Sym) {
353354
Expected<ExecutorAddr> Result((ExecutorAddr()));
354355
if (auto Err = WrapperFunction<SPSExpected<SPSExecutorAddr>(
355-
SPSExecutorAddr, SPSString)>::call(&__orc_rt_elfnix_symbol_lookup_tag,
356-
Result,
357-
ExecutorAddr::fromPtr(DSOHandle),
358-
Sym))
356+
SPSExecutorAddr,
357+
SPSString)>::call(JITDispatch(&__orc_rt_elfnix_symbol_lookup_tag),
358+
Result, ExecutorAddr::fromPtr(DSOHandle), Sym))
359359
return std::move(Err);
360360
return Result;
361361
}
@@ -368,8 +368,9 @@ ELFNixPlatformRuntimeState::getJITDylibInitializersByName(
368368
std::string PathStr(Path.data(), Path.size());
369369
if (auto Err =
370370
WrapperFunction<SPSExpected<SPSELFNixJITDylibInitializerSequence>(
371-
SPSString)>::call(&__orc_rt_elfnix_get_initializers_tag, Result,
372-
Path))
371+
SPSString)>::
372+
call(JITDispatch(&__orc_rt_elfnix_get_initializers_tag, Result),
373+
Path))
373374
return std::move(Err);
374375
return Result;
375376
}

compiler-rt/lib/orc/jit_dispatch.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===------ jit_dispatch.h - Call back to an ORC controller -----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file is a part of the ORC runtime support library.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef ORC_RT_JIT_DISPATCH_H
14+
#define ORC_RT_JIT_DISPATCH_H
15+
16+
#include "common.h"
17+
#include "wrapper_function_utils.h"
18+
19+
namespace orc_rt {
20+
21+
class JITDispatch {
22+
public:
23+
JITDispatch(const void *FnTag) : FnTag(FnTag) {}
24+
25+
WrapperFunctionResult operator()(const char *ArgData, size_t ArgSize) {
26+
// Since the functions cannot be zero/unresolved on Windows, the following
27+
// reference taking would always be non-zero, thus generating a compiler
28+
// warning otherwise.
29+
#if !defined(_WIN32)
30+
if (ORC_RT_UNLIKELY(!&__orc_rt_jit_dispatch_ctx))
31+
return WrapperFunctionResult::createOutOfBandError(
32+
"__orc_rt_jit_dispatch_ctx not set")
33+
.release();
34+
if (ORC_RT_UNLIKELY(!&__orc_rt_jit_dispatch))
35+
return WrapperFunctionResult::createOutOfBandError(
36+
"__orc_rt_jit_dispatch not set")
37+
.release();
38+
#endif
39+
40+
return __orc_rt_jit_dispatch(&__orc_rt_jit_dispatch_ctx, FnTag, ArgData,
41+
ArgSize);
42+
}
43+
44+
private:
45+
const void *FnTag;
46+
};
47+
48+
} // namespace orc_rt
49+
50+
#endif // ORC_RT_JIT_DISPATCH_H

compiler-rt/lib/orc/macho_platform.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "debug.h"
1717
#include "error.h"
1818
#include "interval_map.h"
19+
#include "jit_dispatch.h"
1920
#include "wrapper_function_utils.h"
2021

2122
#include <algorithm>
@@ -915,7 +916,7 @@ Error MachOPlatformRuntimeState::requestPushSymbols(
915916
Error OpErr = Error::success();
916917
if (auto Err = WrapperFunction<SPSError(
917918
SPSExecutorAddr, SPSSequence<SPSTuple<SPSString, bool>>)>::
918-
call(&__orc_rt_macho_push_symbols_tag, OpErr,
919+
call(JITDispatch(&__orc_rt_macho_push_symbols_tag), OpErr,
919920
ExecutorAddr::fromPtr(JDS.Header), Symbols)) {
920921
cantFail(std::move(OpErr));
921922
return std::move(Err);
@@ -1145,8 +1146,9 @@ Error MachOPlatformRuntimeState::dlopenFull(
11451146
// Unlock so that we can accept the initializer update.
11461147
JDStatesLock.unlock();
11471148
if (auto Err = WrapperFunction<SPSExpected<SPSMachOJITDylibDepInfoMap>(
1148-
SPSExecutorAddr)>::call(&__orc_rt_macho_push_initializers_tag,
1149-
DepInfo, ExecutorAddr::fromPtr(JDS.Header)))
1149+
SPSExecutorAddr)>::
1150+
call(JITDispatch(&__orc_rt_macho_push_initializers_tag), DepInfo,
1151+
ExecutorAddr::fromPtr(JDS.Header)))
11501152
return Err;
11511153
JDStatesLock.lock();
11521154

compiler-rt/lib/orc/wrapper_function_utils.h

+6-15
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
#ifndef ORC_RT_WRAPPER_FUNCTION_UTILS_H
1414
#define ORC_RT_WRAPPER_FUNCTION_UTILS_H
1515

16-
#include "orc_rt/c_api.h"
17-
#include "common.h"
1816
#include "error.h"
1917
#include "executor_address.h"
18+
#include "orc_rt/c_api.h"
2019
#include "simple_packed_serialization.h"
2120
#include <type_traits>
2221

@@ -288,30 +287,22 @@ class WrapperFunction<SPSRetTagT(SPSTagTs...)> {
288287
using ResultSerializer = detail::ResultSerializer<SPSRetTagT, RetT>;
289288

290289
public:
291-
template <typename RetT, typename... ArgTs>
292-
static Error call(const void *FnTag, RetT &Result, const ArgTs &...Args) {
290+
template <typename DispatchFn, typename RetT, typename... ArgTs>
291+
static Error call(DispatchFn &&Dispatch, RetT &Result, const ArgTs &...Args) {
293292

294293
// RetT might be an Error or Expected value. Set the checked flag now:
295294
// we don't want the user to have to check the unused result if this
296295
// operation fails.
297296
detail::ResultDeserializer<SPSRetTagT, RetT>::makeSafe(Result);
298297

299-
// Since the functions cannot be zero/unresolved on Windows, the following
300-
// reference taking would always be non-zero, thus generating a compiler
301-
// warning otherwise.
302-
#if !defined(_WIN32)
303-
if (ORC_RT_UNLIKELY(!&__orc_rt_jit_dispatch_ctx))
304-
return make_error<StringError>("__orc_rt_jit_dispatch_ctx not set");
305-
if (ORC_RT_UNLIKELY(!&__orc_rt_jit_dispatch))
306-
return make_error<StringError>("__orc_rt_jit_dispatch not set");
307-
#endif
308298
auto ArgBuffer =
309299
WrapperFunctionResult::fromSPSArgs<SPSArgList<SPSTagTs...>>(Args...);
310300
if (const char *ErrMsg = ArgBuffer.getOutOfBandError())
311301
return make_error<StringError>(ErrMsg);
312302

313-
WrapperFunctionResult ResultBuffer = __orc_rt_jit_dispatch(
314-
&__orc_rt_jit_dispatch_ctx, FnTag, ArgBuffer.data(), ArgBuffer.size());
303+
WrapperFunctionResult ResultBuffer =
304+
Dispatch(ArgBuffer.data(), ArgBuffer.size());
305+
315306
if (auto ErrMsg = ResultBuffer.getOutOfBandError())
316307
return make_error<StringError>(ErrMsg);
317308

0 commit comments

Comments
 (0)