[mlir] Link mlir_runner_utils statically into cuda/rocm-runtime-wrappers.

The runtime-wrappers depend on LLVMSupport, pulling in static initialization code (e.g. command line arguments). Dynamically loading multiple such libraries results in ODR violoations.

So far this has not been an issue, but in D94421, I would like to load both the async-runtime and the cuda-runtime-wrappers as part of a cuda-runner integration test. When doing this, code that asserts that an option category is only registered once fails (note that I've only experienced this in Google's bazel where the async-runtime depends on LLVMSupport, but a similar issue would happen in cmake if more than one runtime-wrapper starts to depend on LLVMSupport).

The underlying issue is that we have a mix of static and dynamic linking. If all dependencies were loaded as shared objects (i.e. if LLVMSupport was linked dynamically to the runtime wrappers), each dependency would only get loaded once. However, linking dependencies dynamically would require special attention to paths (one could dynamically load the dependencies first given explicit paths). The simpler approach seems to be to link all dependencies statically into a single shared object.

This change basically applies the same logic that we have in the c_runner_utils: we have a shared object target that can be loaded dynamically, and we have a static library target that can be linked to other runtime-wrapper shared object targets.

Reviewed By: herhut

Differential Revision: https://reviews.llvm.org/D94399
This commit is contained in:
Christian Sigg
2021-01-11 13:04:09 +01:00
parent 29aaae2814
commit cf50f4f764
19 changed files with 83 additions and 17 deletions

View File

@@ -26,7 +26,7 @@
#define MLIR_CRUNNERUTILS_EXPORT __declspec(dllimport)
#endif // mlir_c_runner_utils_EXPORTS
#endif // MLIR_CRUNNERUTILS_EXPORT
#else
#else // _WIN32
#define MLIR_CRUNNERUTILS_EXPORT
#define MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS
#endif // _WIN32

View File

@@ -80,6 +80,7 @@ add_mlir_library(mlir_c_runner_utils
EXCLUDE_FROM_LIBMLIR
)
set_property(TARGET mlir_c_runner_utils PROPERTY CXX_STANDARD 11)
target_compile_definitions(mlir_c_runner_utils PRIVATE mlir_c_runner_utils_EXPORTS)
add_mlir_library(mlir_c_runner_utils_static
CRunnerUtils.cpp
@@ -88,7 +89,6 @@ add_mlir_library(mlir_c_runner_utils_static
EXCLUDE_FROM_LIBMLIR
)
set_property(TARGET mlir_c_runner_utils_static PROPERTY CXX_STANDARD 11)
target_compile_definitions(mlir_c_runner_utils PRIVATE mlir_c_runner_utils_EXPORTS)
add_mlir_library(mlir_runner_utils
SHARED
@@ -101,6 +101,15 @@ add_mlir_library(mlir_runner_utils
)
target_compile_definitions(mlir_runner_utils PRIVATE mlir_runner_utils_EXPORTS)
add_mlir_library(mlir_runner_utils_static
RunnerUtils.cpp
EXCLUDE_FROM_LIBMLIR
LINK_LIBS PUBLIC
mlir_c_runner_utils_static
)
add_mlir_library(mlir_async_runtime
SHARED
AsyncRuntime.cpp
@@ -113,3 +122,13 @@ add_mlir_library(mlir_async_runtime
)
set_property(TARGET mlir_async_runtime PROPERTY CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(mlir_async_runtime PRIVATE mlir_async_runtime_EXPORTS)
add_mlir_library(mlir_async_runtime_static
AsyncRuntime.cpp
EXCLUDE_FROM_LIBMLIR
LINK_LIBS PUBLIC
mlir_c_runner_utils_static
${LLVM_PTHREAD_LIB}
)

View File

@@ -1,4 +1,7 @@
// RUN: mlir-cuda-runner %s --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-cuda-runner %s \
// RUN: --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
func @main() {
%data = alloc() : memref<2x6xi32>

View File

@@ -1,4 +1,7 @@
// RUN: mlir-cuda-runner %s --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-cuda-runner %s \
// RUN: --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
func @main() {
%data = alloc() : memref<2x6xi32>

View File

@@ -1,4 +1,7 @@
// RUN: mlir-cuda-runner %s --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-cuda-runner %s \
// RUN: --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
func @main() {
%data = alloc() : memref<2x6xi32>

View File

@@ -1,4 +1,7 @@
// RUN: mlir-cuda-runner %s --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-cuda-runner %s \
// RUN: --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
// CHECK-COUNT-8: [{{(5356, ){12}5356}}]
func @main() {

View File

@@ -1,4 +1,7 @@
// RUN: mlir-cuda-runner %s --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-cuda-runner %s \
// RUN: --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
func @main() {
%data = alloc() : memref<2x6xi32>

View File

@@ -1,4 +1,7 @@
// RUN: mlir-cuda-runner %s --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-cuda-runner %s \
// RUN: --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
// CHECK: [{{(35, ){34}35}}]
func @main() {

View File

@@ -1,4 +1,7 @@
// RUN: mlir-cuda-runner %s --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-cuda-runner %s \
// RUN: --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
func @main() {
%data = alloc() : memref<2x6xi32>

View File

@@ -1,4 +1,7 @@
// RUN: mlir-cuda-runner %s --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-cuda-runner %s \
// RUN: --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
func @other_func(%arg0 : f32, %arg1 : memref<?xf32>) {
%cst = constant 1 : index

View File

@@ -1,4 +1,7 @@
// RUN: mlir-cuda-runner %s --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-cuda-runner %s \
// RUN: --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
func @main() {
%data = alloc() : memref<2x6xf32>

View File

@@ -1,4 +1,7 @@
// RUN: mlir-cuda-runner %s --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-cuda-runner %s \
// RUN: --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
// CHECK: [4, 5, 6, 7, 0, 1, 2, 3, 12, -1, -1, -1, 8]
func @main() {

View File

@@ -1,4 +1,7 @@
// RUN: mlir-cuda-runner %s --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-cuda-runner %s \
// RUN: --shared-libs=%cuda_wrapper_library_dir/libcuda-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
// CHECK: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
func @main() {

View File

@@ -1,4 +1,7 @@
// RUN: mlir-rocm-runner %s --shared-libs=%rocm_wrapper_library_dir/librocm-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-rocm-runner %s \
// RUN: --shared-libs=%rocm_wrapper_library_dir/librocm-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
func @other_func(%arg0 : f32, %arg1 : memref<?xf32>) {
%c0 = constant 0 : index

View File

@@ -1,4 +1,7 @@
// RUN: mlir-rocm-runner %s --shared-libs=%rocm_wrapper_library_dir/librocm-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-rocm-runner %s \
// RUN: --shared-libs=%rocm_wrapper_library_dir/librocm-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
// CHECK: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
func @main() {

View File

@@ -1,4 +1,7 @@
// RUN: mlir-rocm-runner %s --shared-libs=%rocm_wrapper_library_dir/librocm-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-rocm-runner %s \
// RUN: --shared-libs=%rocm_wrapper_library_dir/librocm-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
func @vecadd(%arg0 : memref<?xf32>, %arg1 : memref<?xf32>, %arg2 : memref<?xf32>) {
%c0 = constant 0 : index

View File

@@ -1,4 +1,7 @@
// RUN: mlir-rocm-runner %s --shared-libs=%rocm_wrapper_library_dir/librocm-runtime-wrappers%shlibext,%linalg_test_lib_dir/libmlir_runner_utils%shlibext --entry-point-result=void | FileCheck %s
// RUN: mlir-rocm-runner %s \
// RUN: --shared-libs=%rocm_wrapper_library_dir/librocm-runtime-wrappers%shlibext
// RUN: --entry-point-result=void \
// RUN: | FileCheck %s
func @vectransferx2(%arg0 : memref<?xf32>, %arg1 : memref<?xf32>) {
%cst = constant 1 : index

View File

@@ -37,6 +37,7 @@ if(MLIR_CUDA_RUNNER_ENABLED)
target_link_libraries(cuda-runtime-wrappers
PUBLIC
LLVMSupport
mlir_runner_utils_static
${CUDA_RUNTIME_LIBRARY}
)

View File

@@ -61,6 +61,7 @@ if(MLIR_ROCM_RUNNER_ENABLED)
target_link_libraries(rocm-runtime-wrappers
PUBLIC
LLVMSupport
mlir_runner_utils_static
${ROCM_RUNTIME_LIBRARY}
)