Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Runtime's dependence to LLVM (WIP) #3071

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: Apache-2.0

if (ONNX_MLIR_ENABLE_PYRUNTIME_LIGHT)
add_subdirectory(Support)
add_subdirectory(Runtime)
# Accelerators introduces a target AcceleratorsInc. Define a dummy one here
add_custom_target(AcceleratorsInc
Expand Down
4 changes: 2 additions & 2 deletions src/Runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ set_target_properties(OMTensorUtils
POSITION_INDEPENDENT_CODE TRUE
)

if (ONNX_MLIR_ENABLE_PYRUNTIME_LIGHT)
add_compile_definitions(ENABLE_PYRUNTIME_LIGHT)
# Windows use the wrapper class for dynamic library provided by llvm
if (WIN32)
add_onnx_mlir_library(OMExecutionSession
ExecutionSession.cpp

Expand Down
45 changes: 20 additions & 25 deletions src/Runtime/ExecutionSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@
#include <sstream>
#include <vector>

#ifndef ENABLE_PYRUNTIME_LIGHT
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Path.h"
#else
#include <algorithm>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: please add // Please no references to LLVM in the else path just as a reminder that we don't reintroduce dependences on LLVM.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those llvm includes are removed because they are replaced with STLs of C++.

#include <dlfcn.h>
#endif
#include <filesystem>

#include "ExecutionSession.hpp"
#include "OMTensorListHelper.hpp"
Expand All @@ -48,24 +44,23 @@ void ExecutionSession::Init(

// If there is no tag, use the model filename without extension as a tag.
if (tag == "") {
// ToFix: equivalent implementation of llvm utilities.
// The would not be an urgent issue, because tag is usually "NONE"
#ifndef ENABLE_PYRUNTIME_LIGHT
std::string fname = llvm::sys::path::filename(sharedLibPath).str();
llvm::SmallString<256> fnameWithoutExt(fname);
llvm::sys::path::replace_extension(fnameWithoutExt, "");
tag = fnameWithoutExt.str().lower();
#endif
// Implement directly with c++
std::filesystem::path path(sharedLibPath);
tag = path.stem().string();
std::transform(tag.begin(), tag.end(), tag.begin(),
[](unsigned char c) { return std::tolower(c); });
}

// tag = "NONE" to use functions without tag.
std::string lowDashTag;
// ToFix: equivalent implementation of llv::StringRef
#ifndef ENABLE_PYRUNTIME_LIGHT
// Assume tag is always NONE
if (!llvm::StringRef(tag).equals_insensitive("NONE"))

// Replace the llvm implementation:
// llvm::StringRef(tag).equals_insensitive("NONE")
std::string none = "NONE";
bool equal_insensitive = std::equal(tag.begin(), tag.end(), none.begin(),
none.end(), [](char a, char b) { return tolower(a) == tolower(b); });
if (!equal_insensitive)
lowDashTag = "_" + tag;
#endif

#if defined(_WIN32)
// Use functions without tags on Windows since we cannot define at compile
Expand All @@ -75,7 +70,7 @@ void ExecutionSession::Init(
#endif

// Init symbols used by execution session.
#ifndef ENABLE_PYRUNTIME_LIGHT
#if defined(_WIN32)
_sharedLibraryHandle =
llvm::sys::DynamicLibrary::getLibrary(sharedLibPath.c_str());
if (!_sharedLibraryHandle.isValid())
Expand All @@ -89,7 +84,7 @@ void ExecutionSession::Init(
#endif

std::string queryEntryPointsNameWithTag = _queryEntryPointsName + lowDashTag;
#ifndef ENABLE_PYRUNTIME_LIGHT
#if defined(_WIN32)
_queryEntryPointsFunc = reinterpret_cast<queryEntryPointsFuncType>(
_sharedLibraryHandle.getAddressOfSymbol(
queryEntryPointsNameWithTag.c_str()));
Expand All @@ -103,7 +98,7 @@ void ExecutionSession::Init(
reportSymbolLoadingError(queryEntryPointsNameWithTag));

std::string inputSignatureNameWithTag = _inputSignatureName + lowDashTag;
#ifndef ENABLE_PYRUNTIME_LIGHT
#if defined(_WIN32)
_inputSignatureFunc = reinterpret_cast<signatureFuncType>(
_sharedLibraryHandle.getAddressOfSymbol(
inputSignatureNameWithTag.c_str()));
Expand All @@ -116,7 +111,7 @@ void ExecutionSession::Init(
reportSymbolLoadingError(inputSignatureNameWithTag));

std::string outputSignatureNameWithTag = _outputSignatureName + lowDashTag;
#ifndef ENABLE_PYRUNTIME_LIGHT
#if defined(_WIN32)
_outputSignatureFunc = reinterpret_cast<signatureFuncType>(
_sharedLibraryHandle.getAddressOfSymbol(
outputSignatureNameWithTag.c_str()));
Expand Down Expand Up @@ -150,7 +145,7 @@ void ExecutionSession::Init(
}

ExecutionSession::~ExecutionSession() {
#ifndef ENABLE_PYRUNTIME_LIGHT
#if defined(_WIN32)
if (_sharedLibraryHandle.isValid())
llvm::sys::DynamicLibrary::closeLibrary(_sharedLibraryHandle);
#else
Expand All @@ -173,7 +168,7 @@ const std::string *ExecutionSession::queryEntryPoints(
void ExecutionSession::setEntryPoint(const std::string &entryPointName) {
if (!isInitialized)
throw std::runtime_error(reportInitError());
#ifndef ENABLE_PYRUNTIME_LIGHT
#if defined(_WIN32)
_entryPointFunc = reinterpret_cast<entryPointFuncType>(
_sharedLibraryHandle.getAddressOfSymbol(entryPointName.c_str()));
#else
Expand Down
7 changes: 3 additions & 4 deletions src/Runtime/ExecutionSession.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
#include "OnnxMlirRuntime.h"

// LLVM provides the wrapper class, llvm::sys::DynamicLibrary, for dynamic
// library. When PYRUNTIME_LIGHT is built without the LLVM, the handle type for
// dynamic library in Linux is used. DynamicLibraryHandleType is defined for
// the two cases.
#ifndef ENABLE_PYRUNTIME_LIGHT
// library. Use this library only on Windows. Therefore, the Runtime does not
// depend on llvm for Runtime component.
#if defined(_WIN32)
#include "llvm/Support/DynamicLibrary.h"
typedef llvm::sys::DynamicLibrary DynamicLibraryHandleType;
#else
Expand Down
16 changes: 0 additions & 16 deletions src/Runtime/OMTensor.inc
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,14 @@
#include "src/Runtime/OMTensorHelper.hpp"
#endif

#ifndef ENABLE_PYRUNTIME_LIGHT
#include "src/Support/SmallFPConversion.h"
#endif

#if defined(__APPLE__)
// __errno_location() is called from krnl::emitErrNo() to set errno in
// generated llvm IR, but it somehow doesn't come out of the box on MacOS.
int *__attribute__((__weak__)) __errno_location(void) { return &errno; }
#endif

#ifdef ENABLE_PYRUNTIME_LIGHT
// The implementation depends on src/Utility and llvm. Will be solved in
// another PR. Here are just dummy definitions for compilation
float om_f16_to_f32(uint16_t a) {
return (float) 0;
}

uint16_t om_f32_to_f16(uint16_t a) {
return (uint16_t) 0;
}
#endif



// On some platforms LLVM generates f16 conversion code that calls some
// of these C runtime functions.
// TODO: Figure out if we could get these with llvm/compiler-rt in a
Expand Down
20 changes: 12 additions & 8 deletions src/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# SPDX-License-Identifier: Apache-2.0

add_onnx_mlir_library(OMSmallFPConversion
SmallFPConversion.c
)
set_target_properties(OMSmallFPConversion
PROPERTIES
LANGUAGE C
)

if (ONNX_MLIR_ENABLE_PYRUNTIME_LIGHT)
return()
endif()

add_onnx_mlir_library(OMDiagnostic
Diagnostic.cpp

Expand Down Expand Up @@ -34,11 +46,3 @@ add_onnx_mlir_library(OMMlirUtilities
OMSmallFPConversion
MLIRIR
)

add_onnx_mlir_library(OMSmallFPConversion
SmallFPConversion.c
)
set_target_properties(OMSmallFPConversion
PROPERTIES
LANGUAGE C
)
13 changes: 11 additions & 2 deletions test/modellib/ModelLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
//
//===----------------------------------------------------------------------===//

#ifndef _WIN32
#include <dlfcn.h>
#endif

#include "mlir/IR/BuiltinOps.h"

#include "include/OnnxMlirRuntime.h"
Expand Down Expand Up @@ -69,9 +73,14 @@ bool ModelLibBuilder::checkInstructionFromEnv(
bool ModelLibBuilder::checkInstruction(const std::string instructionName) {
if (instructionName.empty())
return true;
llvm::sys::DynamicLibrary sharedLibraryHandle =
DynamicLibraryHandleType sharedLibraryHandle =
exec->getSharedLibraryHandle();
void *addr = sharedLibraryHandle.getAddressOfSymbol(instructionName.c_str());
void *addr;
#if defined(_WIN32)
addr = sharedLibraryHandle.getAddressOfSymbol(instructionName.c_str());
#else
addr = dlsym(sharedLibraryHandle, instructionName.c_str());
#endif
if (!addr) {
printf("%s not found.\n", instructionName.c_str());
return false;
Expand Down
Loading