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

patch for --EmitJNI on windows #2582

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 18 additions & 4 deletions src/Compiler/CompilerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,23 @@ static int genModelObject(
static int genJniObject(const mlir::OwningOpRef<ModuleOp> &module,
std::string jniSharedLibPath, std::string jniObjPath) {
Command ar(/*exePath=*/getToolPath("ar", true));
#ifdef _WIN32
int rc = ar.appendStr("/extract:jnidummy.c.obj")
//.appendStr(llvm::sys::path::filename(jniObjPath).str())
.appendStr(jniSharedLibPath)
.exec(llvm::sys::path::parent_path(jniObjPath).str());
#else
int rc = ar.appendStr("x")
// old version of ar does not support --output so comment out
// old version of ar does not support --output so comment out
// for now and use the optional wdir for exec() to get around
// the problem.
//.appendStr("--output")
//.appendStr(llvm::sys::path::parent_path(jniObjPath).str())
.appendStr(jniSharedLibPath)
.appendStr(llvm::sys::path::filename(jniObjPath).str())
.exec(llvm::sys::path::parent_path(jniObjPath).str());
#endif

return rc != 0 ? CompilerFailureInGenJniObj : CompilerSuccess;
}

Expand Down Expand Up @@ -548,11 +556,17 @@ static int compileModuleToJniJar(
StringRef outputDir = llvm::sys::path::parent_path(outputNameNoExt);
if (outputDir.empty())
outputDir = StringRef(".");

#ifdef _WIN32
std::string jniSharedLibPath = getLibraryPath() + "/jniruntime.lib";
#else
std::string jniSharedLibPath = getLibraryPath() + "/libjniruntime.a";

#endif
llvm::SmallString<8> jniObjDir(outputDir);
#ifdef _WIN32
llvm::sys::path::append(jniObjDir, "jnidummy.c.obj");
#else
llvm::sys::path::append(jniObjDir, "jnidummy.c.o");
#endif
std::string jniObjPath = llvm::StringRef(jniObjDir).str();

rc = genJniObject(module, jniSharedLibPath, jniObjPath);
Expand All @@ -565,7 +579,7 @@ static int compileModuleToJniJar(
llvm::sys::path::append(jniLibDir, "libmodel");
std::string jniLibBase = llvm::StringRef(jniLibDir).str();

#if defined(__APPLE__) && defined(__clang__)
#if (defined(__APPLE__) && defined(__clang__)) || defined(_WIN32)
#define NOEXECSTACK \
{}
#else
Expand Down
16 changes: 16 additions & 0 deletions src/Runtime/jni/src/com/ibm/onnxmlir/OMModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,23 @@ public class OMModel {
JarFile jf = new JarFile(jar.toFile());
for (Enumeration<JarEntry> e = jf.entries(); e.hasMoreElements(); ) {
String libFile = e.nextElement().getName();
//add dll load on windows
if (libFile.endsWith(".dll")) {
try {
Path lib = Paths.get(tmpDir.toString(), libFile);
Path libDir = lib.getParent();
Files.createDirectories(libDir);
// Copy .dll to the temporary directory
Files.copy(jf.getInputStream(jf.getEntry(libFile)), lib,
StandardCopyOption.REPLACE_EXISTING);
// Load the temporary .dll copy
System.load(lib.toString());
logger.finer(lib.toString() + " loaded");

} catch (IOException e2) {
logger.severe(e2.getMessage());
}
} // if
if (libFile.endsWith(".so")) {
try {
Path lib = Paths.get(tmpDir.toString(), libFile);
Expand Down
Loading