[Mlir-commits] [mlir] [MLIR][ExecutionEngine] Restore internal _mlir_{*} symbol lookup (PR #159941)

Balint Cristian llvmlistbot at llvm.org
Sat Sep 20 12:08:11 PDT 2025


https://github.com/cbalint13 created https://github.com/llvm/llvm-project/pull/159941

During clang-tidy fixes, ```_mlir_{alloc,aligned_alloc,free,aligned_free}``` were lost, see the commit: https://github.com/llvm/llvm-project/commit/c599650a0d2a1ae512490ca303e46ef81931c520

* Current ```mlir{Alloc,AlignedAlloc,Free,AlignedFree}``` are [dangling declarations](https://github.com/llvm/llvm-project/blob/efd96afedf2c0f6f2cc34cf5a9a7e3e78f592255/mlir/lib/ExecutionEngine/CRunnerUtils.cpp#L150-L174).
* This PR restores the previous behaviour and utility but also keep clang-tidy happy.

------

#### Issue details

* Prior to this, using [mlir-runner-testcase.py.gz](https://github.com/user-attachments/files/22444224/mlir-runner-testcase.py.gz) reveals the following missing symbols:
```
-> pm.add("finalize-memref-to-llvm{use-generic-functions=false}")
$ mlir-runner-testcase.py
Could not compile malloc:
  Symbols not found: [ _mlir_malloc ]
Generated object file 'obj.o' for inspection.
[cbalint at yoda work]$ mcedit mlir-runner-testcase.py 

```

```
-> pm.add("finalize-memref-to-llvm{use-generic-functions=false use-aligned-alloc=1}")
$ mlir-runner-testcase.py
Could not compile aligned_alloc:
  Symbols not found: [ _mlir_aligned_alloc ]
Generated object file 'obj.o' for inspection.
```

* Using this PR, symbols resolves and are picked up properly:

```
$ ./mlir-runner-testcase.py 
Generated object file 'obj.o' for inspection.

$ readelf -Ws obj.o 
Symbol table '.symtab' contains 7 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS LLVMDialectModule
     2: 0000000000000000    98 FUNC    GLOBAL DEFAULT    3 main
     3: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND aligned_alloc
     4: 0000000000000070   101 FUNC    GLOBAL DEFAULT    3 _mlir_ciface_main
     5: 00000000000000e0   108 FUNC    GLOBAL DEFAULT    3 _mlir_main
     6: 0000000000000150   111 FUNC    GLOBAL DEFAULT    3 _mlir__mlir_ciface_main
``` 

**Note:** Of course one can still use ```use-generic-functions=true``` with his own custom shared library, but the scope of this PR is to restore a the functionality back to simply what ```libmlir_c_runner_utils.so``` already ships as builtins.


>From a342136449eacdc08547a776b7d711e119b45b7a Mon Sep 17 00:00:00 2001
From: Balint Cristian <cristian.balint at gmail.com>
Date: Sat, 20 Sep 2025 21:42:16 +0300
Subject: [PATCH] [MLIR][ExecutionEngine] Restore internal _mlir_{*} symbol
 lookup

---
 mlir/lib/ExecutionEngine/ExecutionEngine.cpp | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/mlir/lib/ExecutionEngine/ExecutionEngine.cpp b/mlir/lib/ExecutionEngine/ExecutionEngine.cpp
index 52162a43aeae3..85775aefd1a02 100644
--- a/mlir/lib/ExecutionEngine/ExecutionEngine.cpp
+++ b/mlir/lib/ExecutionEngine/ExecutionEngine.cpp
@@ -134,7 +134,27 @@ void ExecutionEngine::setupTargetTripleAndDataLayout(Module *llvmModule,
   llvmModule->setTargetTriple(tm->getTargetTriple());
 }
 
+static StringRef lookupBuiltinCRunnerNames(StringRef name) {
+  static const llvm::StringMap<StringRef> nameMap = {
+      {"alloc", "mlirAlloc"},
+      {"aligned_alloc", "mlirAlignedAlloc"},
+      {"malloc", "mlirAlloc"},
+      {"free", "mlirFree"},
+      {"aligned_free", "mlirAlignedFree"}};
+
+  auto it = nameMap.find(name);
+  if (it != nameMap.end()) {
+    return it->second;
+  }
+
+  return {};
+}
+
 static std::string makePackedFunctionName(StringRef name) {
+  auto cfuncName = lookupBuiltinCRunnerNames(name);
+  if (!cfuncName.empty())
+    return cfuncName.str();
+
   return "_mlir_" + name.str();
 }
 



More information about the Mlir-commits mailing list