Skip to content

[MLIR] Enabling external dialects to be shared libs for C API users #108253

Open
@makslevental

Description

@makslevental

Disclaimer: everything below is currently re Linux but Mac/Windows can/should be discussed.

Some people would like to

  1. Build MLIR with MLIR_LINK_MLIR_DYLIB=ON;
  2. Build external DialectXYZ as a dylib using artifacts of step 1;
  3. Runtime load libDialectXYZ.so.

This is actually possible today using DialectPlugin; examples/standalone/standalone-plugin basically demos/exercises this (modulo the MLIR_LINK_MLIR_DYLIB=ON but I've tested with DYLIB=ON too). For C++ API users there are no issues; so e.g., mlir-opt, being a C++ API user, has no problems because mlir-opt and libMLIRStandalone.so will both link against the same libMLIR.so.

What's not possible today is doing this same type of thing through the C API because currently libMLIR-C doesn't link against libMLIR even in the case of MLIR_LINK_MLIR_DYLIB=ON because of some logic I don't quite understand. So today what occurs is

$ ldd libStandaloneCAPI.so
         libMLIRStandalone.so -> libMLIR.so
         libMLIR-C.so

where libMLIR-C.so duplicates everything in libMLIR.so (typeids, etc) and so everything blows up.

What you want is

$ ldd libStandaloneCAPI.so
         libMLIRStandalone.so -┐ 
                               |-> libMLIR.so           
         libMLIR-C.so ---------┘ 

and then everything will work.

Turns out an MVP patch is very small:

diff --git a/mlir/cmake/modules/AddMLIR.cmake b/mlir/cmake/modules/AddMLIR.cmake
index a3324705c525..700a8de26723 100644
--- a/mlir/cmake/modules/AddMLIR.cmake
+++ b/mlir/cmake/modules/AddMLIR.cmake
@@ -410,7 +410,11 @@ function(add_mlir_library name)
     # which excludes it if the ultimate link target is excluding the library.
     set(NEW_LINK_LIBRARIES)
     get_target_property(CURRENT_LINK_LIBRARIES  ${name} LINK_LIBRARIES)
-    get_mlir_filtered_link_libraries(NEW_LINK_LIBRARIES ${CURRENT_LINK_LIBRARIES})
+    if(MLIR_LINK_MLIR_DYLIB)
+      set(NEW_LINK_LIBRARIES "MLIR")
+    else()
+      get_mlir_filtered_link_libraries(NEW_LINK_LIBRARIES ${CURRENT_LINK_LIBRARIES})
+    endif()
     set_target_properties(${name} PROPERTIES LINK_LIBRARIES "${NEW_LINK_LIBRARIES}")
     list(APPEND AGGREGATE_DEPS ${NEW_LINK_LIBRARIES})
     set_target_properties(${name} PROPERTIES

(tested/verified here).

And now I open the floor for discussion; possible topics include:

cc @vchuravy @mofeing @jumerckx

Metadata

Metadata

Labels

enhancementImproving things as opposed to bug fixing, e.g. new or missing featuremlir

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions