<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/108253>108253</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] Enabling external dialects to be shared libs for C API users
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            enhancement
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            stellaraccident,
            jpienaar,
            ftynse,
            makslevental
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          makslevental
      </td>
    </tr>
</table>

<pre>
    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`](https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Tools/Plugins/DialectPlugin.h); [examples/standalone/standalone-plugin](https://github.com/llvm/llvm-project/blob/main/mlir/examples/standalone/standalone-plugin/standalone-plugin.cpp#L28) 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](https://github.com/llvm/llvm-project/blob/6244d87f42775e8d49cf758eeb1909f2ce144e3c/mlir/cmake/modules/AddMLIR.cmake#L409-L410). 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
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](https://github.com/makslevental/julia-standalone-mlir-c-api/actions)).

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

- Windows and Mac (although I'm not sure if `ENABLE_AGGREGATION` is supported on Windows?);
- Some alternative approach such as alluded to here https://discourse.llvm.org/t/extensible-standalone-capi/71865/6 

cc @vchuravy @mofeing @jumerckx
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8WN1z2joW_2uUlzMwtmwwPPDA5x1m09xMmt3u3RdGlg5YjSx5JTmE_35HMgRI6W07d3eZDjXW0fn8na8w5-ROI07IYEYodR6VYpZxLgVqTygldE4o_dpI1IzZ9xdbf9AO33_W7MUpfEXtmQovB4s71vrK2MnlyV1pxGGykI4rJmu0JJsCvqI9-ErqHZSozB6kA95ai9qrA1iEe6nbNyhbD58YJ3T1RWph9g4404SuXGVaJaBEENLx1jkUfZIsSDLtvj-bGq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