From 6bebdd91bbe51bb62a2b369ba43b4c38e3799e9d Mon Sep 17 00:00:00 2001 From: Yu Gong Date: Wed, 25 Oct 2023 10:29:05 +0800 Subject: [PATCH 1/2] Update OMModel.java for dll load on windows part 1 of emitjni on windows Signed-off-by: Yu Gong --- .../jni/src/com/ibm/onnxmlir/OMModel.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Runtime/jni/src/com/ibm/onnxmlir/OMModel.java b/src/Runtime/jni/src/com/ibm/onnxmlir/OMModel.java index 568929d050..6ecf2ca40a 100644 --- a/src/Runtime/jni/src/com/ibm/onnxmlir/OMModel.java +++ b/src/Runtime/jni/src/com/ibm/onnxmlir/OMModel.java @@ -124,7 +124,23 @@ public class OMModel { JarFile jf = new JarFile(jar.toFile()); for (Enumeration 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); From 0b8b817e06ccdfcb8eb8418cecbbabcc75f72f85 Mon Sep 17 00:00:00 2001 From: Yu Gong Date: Wed, 25 Oct 2023 10:30:12 +0800 Subject: [PATCH 2/2] Update CompilerUtils.cpp for extract jnidummy.c.obj part 2 of emitjni on windows Signed-off-by: Yu Gong --- src/Compiler/CompilerUtils.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Compiler/CompilerUtils.cpp b/src/Compiler/CompilerUtils.cpp index 68c890c003..2dd887374d 100644 --- a/src/Compiler/CompilerUtils.cpp +++ b/src/Compiler/CompilerUtils.cpp @@ -420,8 +420,14 @@ static int genModelObject( static int genJniObject(const mlir::OwningOpRef &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") @@ -429,6 +435,8 @@ static int genJniObject(const mlir::OwningOpRef &module, .appendStr(jniSharedLibPath) .appendStr(llvm::sys::path::filename(jniObjPath).str()) .exec(llvm::sys::path::parent_path(jniObjPath).str()); +#endif + return rc != 0 ? CompilerFailureInGenJniObj : CompilerSuccess; } @@ -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); @@ -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