From 7be5695fad669d58c104fc5c0db12a391e00f0e2 Mon Sep 17 00:00:00 2001 From: Changming Sun Date: Tue, 20 Aug 2019 12:04:10 -0700 Subject: [PATCH] Remove --whole-archive (#1655) --- cmake/onnxruntime.cmake | 15 ++++------- onnxruntime/core/framework/allocator.cc | 34 ++++++++++++------------- tools/ci_build/gen_def.py | 12 ++++++++- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/cmake/onnxruntime.cmake b/cmake/onnxruntime.cmake index 91508a8aa8..0100bd8b5b 100644 --- a/cmake/onnxruntime.cmake +++ b/cmake/onnxruntime.cmake @@ -19,13 +19,13 @@ foreach(f ${ONNXRUNTIME_PROVIDER_NAMES}) list(APPEND SYMBOL_FILES "${ONNXRUNTIME_ROOT}/core/providers/${f}/symbols.txt") endforeach() -add_custom_command(OUTPUT ${SYMBOL_FILE} - COMMAND ${PYTHON_EXECUTABLE} "${REPO_ROOT}/tools/ci_build/gen_def.py" --version_file "${ONNXRUNTIME_ROOT}/../VERSION_NUMBER" --src_root "${ONNXRUNTIME_ROOT}" --config ${ONNXRUNTIME_PROVIDER_NAMES} --style=${OUTPUT_STYLE} --output ${SYMBOL_FILE} +add_custom_command(OUTPUT ${SYMBOL_FILE} ${CMAKE_CURRENT_BINARY_DIR}/generated_source.c + COMMAND ${PYTHON_EXECUTABLE} "${REPO_ROOT}/tools/ci_build/gen_def.py" --version_file "${ONNXRUNTIME_ROOT}/../VERSION_NUMBER" --src_root "${ONNXRUNTIME_ROOT}" --config ${ONNXRUNTIME_PROVIDER_NAMES} --style=${OUTPUT_STYLE} --output ${SYMBOL_FILE} --output_source ${CMAKE_CURRENT_BINARY_DIR}/generated_source.c DEPENDS ${SYMBOL_FILES} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -add_custom_target(onnxruntime_generate_def ALL DEPENDS ${SYMBOL_FILE}) -add_library(onnxruntime SHARED ${onnxruntime_session_srcs}) +add_custom_target(onnxruntime_generate_def ALL DEPENDS ${SYMBOL_FILE} ${CMAKE_CURRENT_BINARY_DIR}/generated_source.c) +add_library(onnxruntime SHARED ${CMAKE_CURRENT_BINARY_DIR}/generated_source.c) set_target_properties(onnxruntime PROPERTIES VERSION ${ORT_VERSION}) add_dependencies(onnxruntime onnxruntime_generate_def ${onnxruntime_EXTERNAL_DEPENDENCIES}) target_include_directories(onnxruntime PRIVATE ${ONNXRUNTIME_ROOT}) @@ -37,12 +37,8 @@ endif() if(UNIX) if (APPLE) - set(BEGIN_WHOLE_ARCHIVE -Xlinker -all_load) - set(END_WHOLE_ARCHIVE -Xlinker -noall_load) set(ONNXRUNTIME_SO_LINK_FLAG "-Xlinker -dead_strip") else() - set(BEGIN_WHOLE_ARCHIVE -Xlinker --whole-archive) - set(END_WHOLE_ARCHIVE -Xlinker --no-whole-archive) set(ONNXRUNTIME_SO_LINK_FLAG "-Xlinker --version-script=${SYMBOL_FILE} -Xlinker --no-undefined -Xlinker --gc-sections") endif() else() @@ -59,7 +55,7 @@ endif() #The BEGIN_WHOLE_ARCHIVE/END_WHOLE_ARCHIVE part should contain the implementations of all the C API functions target_link_libraries(onnxruntime PRIVATE - ${BEGIN_WHOLE_ARCHIVE} + onnxruntime_session ${onnxruntime_libs} ${PROVIDERS_CUDA} ${PROVIDERS_MKLDNN} @@ -72,7 +68,6 @@ target_link_libraries(onnxruntime PRIVATE onnxruntime_util ${onnxruntime_tvm_libs} onnxruntime_framework - ${END_WHOLE_ARCHIVE} onnxruntime_graph onnxruntime_common onnxruntime_mlas diff --git a/onnxruntime/core/framework/allocator.cc b/onnxruntime/core/framework/allocator.cc index b8847a0080..8991bbe6a0 100644 --- a/onnxruntime/core/framework/allocator.cc +++ b/onnxruntime/core/framework/allocator.cc @@ -10,8 +10,7 @@ namespace onnxruntime { void* CPUAllocator::Alloc(size_t size) { - if (size <= 0) - return nullptr; + if (size <= 0) return nullptr; void* p; size_t alignment = MlasGetPreferredBufferAlignment(); #if _MSC_VER @@ -35,54 +34,53 @@ void CPUAllocator::Free(void* p) { #endif } -const OrtAllocatorInfo& CPUAllocator::Info() const { - return *allocator_info_; -} +const OrtAllocatorInfo& CPUAllocator::Info() const { return *allocator_info_; } } // namespace onnxruntime -std::ostream& operator<<(std::ostream& out, const OrtAllocatorInfo& info) { - return (out << info.ToString()); -} +std::ostream& operator<<(std::ostream& out, const OrtAllocatorInfo& info) { return (out << info.ToString()); } ORT_API_STATUS_IMPL(OrtCreateAllocatorInfo, _In_ const char* name1, OrtAllocatorType type, int id1, OrtMemType mem_type1, _Out_ OrtAllocatorInfo** out) { if (strcmp(name1, onnxruntime::CPU) == 0) { *out = new OrtAllocatorInfo(name1, type, OrtDevice(), id1, mem_type1); } else if (strcmp(name1, onnxruntime::CUDA) == 0) { - *out = new OrtAllocatorInfo(name1, type, OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, static_cast(id1)), id1, mem_type1); + *out = new OrtAllocatorInfo( + name1, type, OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, static_cast(id1)), id1, + mem_type1); } else if (strcmp(name1, onnxruntime::CUDA_PINNED) == 0) { - *out = new OrtAllocatorInfo(name1, type, OrtDevice(OrtDevice::CPU, OrtDevice::MemType::CUDA_PINNED, static_cast(id1)), id1, mem_type1); + *out = new OrtAllocatorInfo( + name1, type, OrtDevice(OrtDevice::CPU, OrtDevice::MemType::CUDA_PINNED, static_cast(id1)), + id1, mem_type1); } else { return OrtCreateStatus(ORT_INVALID_ARGUMENT, "Specified device is not supported."); } return nullptr; } -ORT_API(void, OrtReleaseAllocatorInfo, _Frees_ptr_opt_ OrtAllocatorInfo* p) { - delete p; -} +ORT_API(void, OrtReleaseAllocatorInfo, _Frees_ptr_opt_ OrtAllocatorInfo* p) { delete p; } -ORT_API_STATUS_IMPL(OrtAllocatorInfoGetName, _In_ OrtAllocatorInfo* ptr, _Out_ const char** out) { +ORT_API_STATUS_IMPL(OrtAllocatorInfoGetName, _In_ const OrtAllocatorInfo* ptr, _Out_ const char** out) { *out = ptr->name; return nullptr; } -ORT_API_STATUS_IMPL(OrtAllocatorInfoGetId, _In_ OrtAllocatorInfo* ptr, _Out_ int* out) { +ORT_API_STATUS_IMPL(OrtAllocatorInfoGetId, _In_ const OrtAllocatorInfo* ptr, _Out_ int* out) { *out = ptr->id; return nullptr; } -ORT_API_STATUS_IMPL(OrtAllocatorInfoGetMemType, _In_ OrtAllocatorInfo* ptr, _Out_ OrtMemType* out) { +ORT_API_STATUS_IMPL(OrtAllocatorInfoGetMemType, _In_ const OrtAllocatorInfo* ptr, _Out_ OrtMemType* out) { *out = ptr->mem_type; return nullptr; } -ORT_API_STATUS_IMPL(OrtAllocatorInfoGetType, _In_ OrtAllocatorInfo* ptr, _Out_ OrtAllocatorType* out) { +ORT_API_STATUS_IMPL(OrtAllocatorInfoGetType, _In_ const OrtAllocatorInfo* ptr, _Out_ OrtAllocatorType* out) { *out = ptr->type; return nullptr; } -ORT_API_STATUS_IMPL(OrtCompareAllocatorInfo, _In_ const OrtAllocatorInfo* info1, _In_ const OrtAllocatorInfo* info2, _Out_ int* out) { +ORT_API_STATUS_IMPL(OrtCompareAllocatorInfo, _In_ const OrtAllocatorInfo* info1, _In_ const OrtAllocatorInfo* info2, + _Out_ int* out) { *out = (*info1 == *info2) ? 0 : -1; return nullptr; } diff --git a/tools/ci_build/gen_def.py b/tools/ci_build/gen_def.py index 41eb75abde..6ed654db6a 100755 --- a/tools/ci_build/gen_def.py +++ b/tools/ci_build/gen_def.py @@ -7,6 +7,7 @@ def parse_arguments(): parser = argparse.ArgumentParser() parser.add_argument("--src_root", required=True, help="input symbol file") parser.add_argument("--output", required=True, help="output file") + parser.add_argument("--output_source", required=True, help="output file") parser.add_argument("--version_file", required=True, help="VERSION_NUMBER file") parser.add_argument("--style", required=True, choices=["gcc", "vc"]) parser.add_argument("--config", required=True, nargs="+") @@ -14,7 +15,6 @@ def parse_arguments(): args = parse_arguments() print("Generating symbol file for %s" % str(args.config)) - with open(args.version_file, 'r') as f: VERSION_STRING=f.read().strip(); @@ -52,3 +52,13 @@ with open(args.output, 'w') as file: file.write(" local:\n") file.write(" *;\n") file.write("}; \n") + +with open(args.output_source, 'w') as file: + file.write("#include \n") + for c in args.config: + file.write("#include \n" % (c,c)) + file.write("void* GetFunctionEntryByName(const char* name){\n") + for symbol in symbols: + file.write("if(strcmp(name,\"%s\") ==0) return (void*)&%s;\n" % (symbol,symbol)) + file.write("return NULL;\n"); + file.write("}\n"); \ No newline at end of file