Skip to content

[mlir] Workaround for export lib generation on Windows for mlir_arm_sme_abi_stubs #73147

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

Merged
merged 3 commits into from
Nov 23, 2023

Conversation

Hardcode84
Copy link
Contributor

@Hardcode84 Hardcode84 commented Nov 22, 2023

Using mlir cmake in downstream project fails with error

CMake Error at D:/projs/llvm/llvm-install/lib/cmake/mlir/MLIRTargets.cmake:2537 (message):
  The imported target "mlir_arm_sme_abi_stubs" references the file

     "D:/projs/llvm/llvm-install/lib/mlir_arm_sme_abi_stubs.lib"

  but this file does not exist.  Possible reasons include:

  * The file was deleted, renamed, or moved to another location.

  * An install or uninstall procedure did not complete successfully.

  * The installation package was faulty and contained

     "D:/projs/llvm/llvm-install/lib/cmake/mlir/MLIRTargets.cmake"

  but not all the files it references.

Call Stack (most recent call first):
  D:/projs/llvm/llvm-install/lib/cmake/mlir/MLIRConfig.cmake:37 (include)
  mlir/CMakeLists.txt:5 (find_package)

Windows cmake needs export libaries but it seems they are only being generated if you have at least one exported symbol.
Add export attributes to symbols.

Not sure what the best approach to fix this (probably we should just disable this lib on windows entirely), but it fixed things for me locally.

@llvmbot
Copy link
Member

llvmbot commented Nov 22, 2023

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-execution-engine

Author: Ivan Butygin (Hardcode84)

Changes

Using mlir cmake in downstream project fails with error

CMake Error at D:/projs/llvm/llvm-install/lib/cmake/mlir/MLIRTargets.cmake:2537 (message):
  The imported target "mlir_arm_sme_abi_stubs" references the file

     "D:/projs/llvm/llvm-install/lib/mlir_arm_sme_abi_stubs.lib"

  but this file does not exist.  Possible reasons include:

  * The file was deleted, renamed, or moved to another location.

  * An install or uninstall procedure did not complete successfully.

  * The installation package was faulty and contained

     "D:/projs/llvm/llvm-install/lib/cmake/mlir/MLIRTargets.cmake"

  but not all the files it references.

Call Stack (most recent call first):
  D:/projs/llvm/llvm-install/lib/cmake/mlir/MLIRConfig.cmake:37 (include)
  mlir/CMakeLists.txt:5 (find_package)

Windows cmake needs export libaries but it seems they are only being generated if you have at least one exported symbol. Add a dummy symbol to lib (export macros are copied from other mlir runnner libs).

Not sure what a best approach to fix this (probably we should just disable this lib on windows entirely), but it fixed things for me locally.


Full diff: https://github.com/llvm/llvm-project/pull/73147.diff

2 Files Affected:

  • (modified) mlir/lib/ExecutionEngine/ArmSMEStubs.cpp (+19)
  • (modified) mlir/lib/ExecutionEngine/CMakeLists.txt (+1)
diff --git a/mlir/lib/ExecutionEngine/ArmSMEStubs.cpp b/mlir/lib/ExecutionEngine/ArmSMEStubs.cpp
index f9f64ad5e5ac81c..66df7900aeb9c53 100644
--- a/mlir/lib/ExecutionEngine/ArmSMEStubs.cpp
+++ b/mlir/lib/ExecutionEngine/ArmSMEStubs.cpp
@@ -10,6 +10,21 @@
 #include <cstdint>
 #include <iostream>
 
+#ifdef _WIN32
+#ifndef MLIR_RUNNERUTILS_EXPORT
+#ifdef mlir_arm_sme_abi_stubs_EXPORTS
+// We are building this library
+#define MLIR_ARNSMEABISTUBS_EXPORT __declspec(dllexport)
+#else
+// We are using this library
+#define MLIR_ARNSMEABISTUBS_EXPORT __declspec(dllimport)
+#endif // mlir_runner_utils_EXPORTS
+#endif // MLIR_RUNNERUTILS_EXPORT
+#else
+// Non-windows: use visibility attributes.
+#define MLIR_ARNSMEABISTUBS_EXPORT __attribute__((visibility("default")))
+#endif // _WIN32
+
 // The actual implementation of these routines is in:
 // compiler-rt/lib/builtins/aarch64/sme-abi.S. These stubs allow the current
 // ArmSME tests to run without depending on compiler-rt. This works as we don't
@@ -18,6 +33,10 @@
 // a path to an alternate implementation.
 
 extern "C" {
+void MLIR_ARNSMEABISTUBS_EXPORT _mlir_arm_sme_abi_stubs_exportstub() {
+  // TODO: is function is not used, need at least one exported symbol on Windows
+  // to generate export library.
+}
 
 bool LLVM_ATTRIBUTE_WEAK __aarch64_sme_accessible() {
   // The ArmSME tests are run within an emulator so we assume SME is available.
diff --git a/mlir/lib/ExecutionEngine/CMakeLists.txt b/mlir/lib/ExecutionEngine/CMakeLists.txt
index fe139661f2bbb5a..70c5a07ad1ab237 100644
--- a/mlir/lib/ExecutionEngine/CMakeLists.txt
+++ b/mlir/lib/ExecutionEngine/CMakeLists.txt
@@ -181,6 +181,7 @@ if(LLVM_ENABLE_PIC)
   add_mlir_library(mlir_arm_sme_abi_stubs
     SHARED
     ArmSMEStubs.cpp)
+  target_compile_definitions(mlir_arm_sme_abi_stubs PRIVATE mlir_arm_sme_abi_stubs_EXPORTS)
 
   if(MLIR_ENABLE_CUDA_RUNNER)
     # Configure CUDA support. Using check_language first allows us to give a

Comment on lines 36 to 39
void MLIR_ARNSMEABISTUBS_EXPORT _mlir_arm_sme_abi_stubs_exportstub() {
// TODO: is function is not used, need at least one exported symbol on Windows
// to generate export library.
}
Copy link
Member

Choose a reason for hiding this comment

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

Rather than add this dummy function, I think you can just add MLIR_ARNSMEABISTUBS_EXPORT to one (or all) of the functions already here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This makes sense, although I can't test it immediately on non-windows, does those tests run on GH precommit runners?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

@MacDue MacDue Nov 22, 2023

Choose a reason for hiding this comment

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

They're not run as part of pre-commit, but this looks like it should be an NFC on Linux. I can give this patch a quick test tomorrow though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, thanks

…sme_abi_stubs`

Using mlir smake in downstream project faild with error
```
CMake Error at D:/projs/llvm/llvm-install/lib/cmake/mlir/MLIRTargets.cmake:2537 (message):
  The imported target "mlir_arm_sme_abi_stubs" references the file

     "D:/projs/llvm/llvm-install/lib/mlir_arm_sme_abi_stubs.lib"

  but this file does not exist.  Possible reasons include:

  * The file was deleted, renamed, or moved to another location.

  * An install or uninstall procedure did not complete successfully.

  * The installation package was faulty and contained

     "D:/projs/llvm/llvm-install/lib/cmake/mlir/MLIRTargets.cmake"

  but not all the files it references.

Call Stack (most recent call first):
  D:/projs/llvm/llvm-install/lib/cmake/mlir/MLIRConfig.cmake:37 (include)
  mlir/CMakeLists.txt:5 (find_package)
```

Windows cmake needs export libaries but it seems they are only being generated if you have at least one exported symbol.
Add a dummy symbol to lib (export macros is copied from other mlir runnner libs).
@@ -181,6 +181,7 @@ if(LLVM_ENABLE_PIC)
add_mlir_library(mlir_arm_sme_abi_stubs
SHARED
ArmSMEStubs.cpp)
target_compile_definitions(mlir_arm_sme_abi_stubs PRIVATE mlir_arm_sme_abi_stubs_EXPORTS)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The case is surprising to me, is this something we do in the other libs?

Copy link
Contributor

Choose a reason for hiding this comment

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

To work on Windows, yes. There are some ways to tell it to indiscriminately export everything but I don't think we use that.

Copy link
Contributor Author

@Hardcode84 Hardcode84 Nov 23, 2023

Choose a reason for hiding this comment

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

LLVM_ATTRIBUTE_WEAK is expanding to empty macro on Windows, which resulted in dll without any exported symbols, and export lib is not being generated for such dll (I don't know if it's general cmake behavior, llvm cmake or microsoft linker behavior). Adding __declspec(dllexport) fixed it. We don't see this for other libs because all of them export some symbols.

Copy link
Collaborator

@joker-eph joker-eph left a comment

Choose a reason for hiding this comment

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

@stellaraccident knows this better than I do, but this does not strikes me as obviously wrong just now.

@@ -181,6 +181,7 @@ if(LLVM_ENABLE_PIC)
add_mlir_library(mlir_arm_sme_abi_stubs
SHARED
ArmSMEStubs.cpp)
target_compile_definitions(mlir_arm_sme_abi_stubs PRIVATE mlir_arm_sme_abi_stubs_EXPORTS)
Copy link
Contributor

Choose a reason for hiding this comment

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

To work on Windows, yes. There are some ways to tell it to indiscriminately export everything but I don't think we use that.

#endif // mlir_arm_sme_abi_stubs_EXPORTS
#endif // MLIR_ARMSMEABISTUBS_EXPORT
#else
#define MLIR_ARMSMEABISTUBS_EXPORT LLVM_ATTRIBUTE_WEAK
Copy link
Contributor

Choose a reason for hiding this comment

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

To be fully resilient, you would also add __attribute__((visibility("default")), which would make this target compatible with -fvisiblity=hidden build modes (which gets used for a lot of deployment cases because it generates more efficient binaries).

I might suggest lining the terminology and style of ifdefs with how we do this for the CAPI: https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir-c/Support.h#L46

I think:

  • #ifdef _WIN32 -> #if (defined(_WIN32) || defined(__CYGWIN__))
  • MLIR_ARMSMEABISTUBS_EXPORT -> MLIR_ARMSMEABISTUBS_EXPORTED

Since this is unconditionally/always a shared library, you can leave out the check for an equiv of MLIR_CAPI_ENABLE_WINDOWS_DLL_DECLSPEC.

Copy link
Member

@MacDue MacDue left a comment

Choose a reason for hiding this comment

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

LGTM (aside from the outstanding comments) 👍

@Hardcode84
Copy link
Contributor Author

With __attribute__((visibility("default")) it's no longer NFC on linux, so let's hope it works )

@Hardcode84 Hardcode84 merged commit 6248c24 into llvm:main Nov 23, 2023
@Hardcode84 Hardcode84 deleted the fix-armsme-exports branch November 23, 2023 12:23
Hardcode84 added a commit that referenced this pull request Nov 23, 2023
…lir_arm_sme_abi_stubs` (#73147)"

This reverts commit 6248c24.

broke the bots
@Hardcode84
Copy link
Contributor Author

Of course it broke

@Hardcode84
Copy link
Contributor Author

Sorry, I'll leave __attribute__((visibility("default")) part for someone else, my only goal here is to fix win build.

Hardcode84 added a commit to Hardcode84/llvm-project that referenced this pull request Nov 23, 2023
…sme_abi_stubs` (llvm#73147)

Using mlir cmake in downstream project fails with error
```
CMake Error at D:/projs/llvm/llvm-install/lib/cmake/mlir/MLIRTargets.cmake:2537 (message):
  The imported target "mlir_arm_sme_abi_stubs" references the file

     "D:/projs/llvm/llvm-install/lib/mlir_arm_sme_abi_stubs.lib"

  but this file does not exist.  Possible reasons include:

  * The file was deleted, renamed, or moved to another location.

  * An install or uninstall procedure did not complete successfully.

  * The installation package was faulty and contained

     "D:/projs/llvm/llvm-install/lib/cmake/mlir/MLIRTargets.cmake"

  but not all the files it references.

Call Stack (most recent call first):
  D:/projs/llvm/llvm-install/lib/cmake/mlir/MLIRConfig.cmake:37 (include)
  mlir/CMakeLists.txt:5 (find_package)
```

Windows cmake needs export libaries but it seems they are only being
generated if you have at least one exported symbol.
Add export attributes to symbols.

Not sure what the best approach to fix this (probably we should just
disable this lib on windows entirely), but it fixed things for me
locally.
Hardcode84 added a commit that referenced this pull request Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants