llvm / llvm/llvm-project

[mlir] ASAN ODR violations in `mlir-pdll` when `LLVM_LINK_LLVM_DYLIB=ON`

Open
#162,426 1 comment 0 reactions 0 assignees View on GitHub
mlir
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

There appears to be a circular dependency issue with the `[MLIR` `mlir-pdll` tool](https://github.com/llvm/llvm-project/blob/main/mlir/tools/mlir-pdll/CMakeLists.txt):

```cmake
set(LIBS
MLIRIR
MLIRPDLLAST
MLIRPDLLCodeGen
MLIRPDLLODS
MLIRPDLLParser
)

add_tablegen(mlir-pdll MLIR_PDLL
DESTINATION "${MLIR_TOOLS_INSTALL_DIR}"
EXPORT MLIR
mlir-pdll.cpp

DEPENDS
${LIBS}
)
```

Some of the libraries in `LIBS` here will link `libLLVM` when `LLVM_LINK_LLVM_DYLIB=ON`. This includes MLIRIR but also `MLIRPDLLCodeGen` and others. TableGen binaries are explictly linked statically against LLVM.

This results in ODR violation messages when building with both ASAN and `LLVM_LINK_LLVM_DYLIB=ON`:

```
==1842017==ERROR: AddressSanitizer: odr-violation (0x61a21d591220):
[1] size=1 'llvm::vfs::FileSystem::ID' /llvm-project/llvm/lib/Support/VirtualFileSystem.cpp in /build/bin/mlir-pdll
[2] size=1 'llvm::vfs::FileSystem::ID' /llvm-project/llvm/lib/Support/VirtualFileSystem.cpp in /build/bin/../lib/libLLVM.so.21.0git
These globals were registered at these points:
...
```

Looking at the dependency graph, we need to force `mlir-pdll` to link all its dependencies statically. That can be done by swapping out targets that aren't explicitly declared STATIc (e.g. `MLIRIR`) with object lib (`obj.MLIRIR`) targets if available, However, you have to transitively resolve all the dependencies required. It looks like the set it needs is (`MLIRIR` , `MLIRSupport` , `MLIRPDLDialect`, and some builtin op interface libraries). In some cases, object libraries are currently not built (e.g. if `BUILD_SHARED_LIBS=ON` and `LLVM_LINK_LLVM_DYLIB=ON` and (too be added in [PR#155488](https://github.com/llvm/llvm-project/pull/155488)) `MLIR_BUILD_MLIR_DYLIB=OFF`. Therefore we would need to start explicitly requesting that object libraries be built for at least this subset of libraries.

Contributor guide

Open the contributing guide

Research direction

Start with mlir/tools/mlir-pdll/CMakeLists.txt and inspect the dependency graph for MLIRIR, MLIRSupport, MLIRPDLLCodeGen, MLIRPDLLODS, and MLIRPDLLParser. Reproduce with ASAN, LLVM_LINK_LLVM_DYLIB=ON, and the shared-library configurations described in the issue, then verify that mlir-pdll no longer reports ODR violations and that required object libraries are built.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake
Domain
build-system, compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.