[Mlir-commits] [mlir] [MLIR][Python] Add LLVM include directories to mlir-py cmake (PR #181779)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Feb 17 11:17:11 PST 2026


https://github.com/RattataKing updated https://github.com/llvm/llvm-project/pull/181779

>From eba07cca08750a00fea81e7b1973a8b33f6358f1 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Tue, 17 Feb 2026 05:43:38 +0000
Subject: [PATCH 1/5] Drop unused include

---
 mlir/lib/Bindings/Python/IRAffine.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/mlir/lib/Bindings/Python/IRAffine.cpp b/mlir/lib/Bindings/Python/IRAffine.cpp
index 131481b830c4d..116e20ee834e9 100644
--- a/mlir/lib/Bindings/Python/IRAffine.cpp
+++ b/mlir/lib/Bindings/Python/IRAffine.cpp
@@ -24,7 +24,6 @@
 // clang-format on
 #include "mlir-c/IntegerSet.h"
 #include "mlir/Bindings/Python/Nanobind.h"
-#include "mlir/Support/LLVM.h"
 
 namespace nb = nanobind;
 using namespace mlir;

>From 612efa2a2418c81711053aae909116c6b432929b Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Tue, 17 Feb 2026 05:52:14 +0000
Subject: [PATCH 2/5] Add llvm include dirs to mlir-py bindings

---
 mlir/cmake/modules/AddMLIRPython.cmake | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index 54c59f41404b7..a1f1f340c3d9a 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -8,6 +8,19 @@
 # nomenclature, adds libraries.
 ################################################################################
 
+# INTERFACE library that provides the include directories needed to compile
+# against MLIR CAPI headers. The CAPI headers (e.g., mlir/CAPI/Wrap.h)
+# transitively include LLVM headers (via mlir/Support/LLVM.h), so consumers
+# need the LLVM include paths even if they don't link against LLVMSupport.
+# Defined here (rather than in lib/CAPI/) so it is available to both in-tree
+# and external project builds that include this module.
+if(NOT TARGET MLIRCAPIHeaderDeps)
+  add_library(MLIRCAPIHeaderDeps INTERFACE)
+  target_include_directories(MLIRCAPIHeaderDeps INTERFACE
+    $<TARGET_PROPERTY:LLVMSupport,INTERFACE_INCLUDE_DIRECTORIES>
+  )
+endif()
+
 # Function: declare_mlir_python_sources
 # Declares pure python sources as part of a named grouping that can be built
 # later.
@@ -441,6 +454,7 @@ function(add_mlir_python_modules name)
         OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX}/_mlir_libs"
         MLIR_BINDINGS_PYTHON_NB_DOMAIN ${ARG_MLIR_BINDINGS_PYTHON_NB_DOMAIN}
         LINK_LIBS PRIVATE
+          MLIRCAPIHeaderDeps
           ${sources_target}
           ${ARG_COMMON_CAPI_LINK_LIBS}
           ${support_libs}
@@ -474,10 +488,14 @@ function(add_mlir_python_modules name)
         MLIR_BINDINGS_PYTHON_NB_DOMAIN ${ARG_MLIR_BINDINGS_PYTHON_NB_DOMAIN}
         _PRIVATE_SUPPORT_LIB
         LINK_LIBS PRIVATE
-        # LLVMSupport is intentionally removed to avoid introducing an LLVM dependency
-        # for the mlir-python bindings. Do not add new dependencies on the C++ LLVM/MLIR
-        # libraries; use the C++ standard library instead, or wrap LLVM functionality in
-        # the C API first.
+        # LLVMSupport is intentionally removed to avoid introducing an LLVM link
+        # dependency for the mlir-python bindings. Do not add new link dependencies
+        # on the C++ LLVM/MLIR libraries; use the C++ standard library instead, or
+        # wrap LLVM functionality in the C API first.
+        # MLIRCAPIHeaderDeps is an include-only (INTERFACE) target that provides
+        # LLVM include directories needed to compile against CAPI headers. It does
+        # not introduce any link or runtime dependency on LLVM.
+          MLIRCAPIHeaderDeps
           ${sources_target}
           ${ARG_COMMON_CAPI_LINK_LIBS}
       )

>From d66beccb870b9e6c40473489aaf8650b07175847 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Tue, 17 Feb 2026 17:08:09 +0000
Subject: [PATCH 3/5] Revert "Add llvm include dirs to mlir-py bindings"

This reverts commit 612efa2a2418c81711053aae909116c6b432929b.
---
 mlir/cmake/modules/AddMLIRPython.cmake | 26 ++++----------------------
 1 file changed, 4 insertions(+), 22 deletions(-)

diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index a1f1f340c3d9a..54c59f41404b7 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -8,19 +8,6 @@
 # nomenclature, adds libraries.
 ################################################################################
 
-# INTERFACE library that provides the include directories needed to compile
-# against MLIR CAPI headers. The CAPI headers (e.g., mlir/CAPI/Wrap.h)
-# transitively include LLVM headers (via mlir/Support/LLVM.h), so consumers
-# need the LLVM include paths even if they don't link against LLVMSupport.
-# Defined here (rather than in lib/CAPI/) so it is available to both in-tree
-# and external project builds that include this module.
-if(NOT TARGET MLIRCAPIHeaderDeps)
-  add_library(MLIRCAPIHeaderDeps INTERFACE)
-  target_include_directories(MLIRCAPIHeaderDeps INTERFACE
-    $<TARGET_PROPERTY:LLVMSupport,INTERFACE_INCLUDE_DIRECTORIES>
-  )
-endif()
-
 # Function: declare_mlir_python_sources
 # Declares pure python sources as part of a named grouping that can be built
 # later.
@@ -454,7 +441,6 @@ function(add_mlir_python_modules name)
         OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX}/_mlir_libs"
         MLIR_BINDINGS_PYTHON_NB_DOMAIN ${ARG_MLIR_BINDINGS_PYTHON_NB_DOMAIN}
         LINK_LIBS PRIVATE
-          MLIRCAPIHeaderDeps
           ${sources_target}
           ${ARG_COMMON_CAPI_LINK_LIBS}
           ${support_libs}
@@ -488,14 +474,10 @@ function(add_mlir_python_modules name)
         MLIR_BINDINGS_PYTHON_NB_DOMAIN ${ARG_MLIR_BINDINGS_PYTHON_NB_DOMAIN}
         _PRIVATE_SUPPORT_LIB
         LINK_LIBS PRIVATE
-        # LLVMSupport is intentionally removed to avoid introducing an LLVM link
-        # dependency for the mlir-python bindings. Do not add new link dependencies
-        # on the C++ LLVM/MLIR libraries; use the C++ standard library instead, or
-        # wrap LLVM functionality in the C API first.
-        # MLIRCAPIHeaderDeps is an include-only (INTERFACE) target that provides
-        # LLVM include directories needed to compile against CAPI headers. It does
-        # not introduce any link or runtime dependency on LLVM.
-          MLIRCAPIHeaderDeps
+        # LLVMSupport is intentionally removed to avoid introducing an LLVM dependency
+        # for the mlir-python bindings. Do not add new dependencies on the C++ LLVM/MLIR
+        # libraries; use the C++ standard library instead, or wrap LLVM functionality in
+        # the C API first.
           ${sources_target}
           ${ARG_COMMON_CAPI_LINK_LIBS}
       )

>From bcb0fe6feab6af1385d3077e5d46d5ec56cf9489 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Tue, 17 Feb 2026 18:47:07 +0000
Subject: [PATCH 4/5] Clean wrap/unwrap and drop unused namespace llvm

---
 mlir/include/mlir/Bindings/Python/Globals.h      |  1 -
 mlir/lib/Bindings/Python/DialectAMDGPU.cpp       |  1 -
 mlir/lib/Bindings/Python/DialectNVGPU.cpp        |  1 -
 mlir/lib/Bindings/Python/DialectPDL.cpp          |  1 -
 mlir/lib/Bindings/Python/DialectQuant.cpp        |  1 -
 mlir/lib/Bindings/Python/DialectSparseTensor.cpp |  1 -
 mlir/lib/Bindings/Python/DialectTransform.cpp    |  3 ++-
 mlir/lib/Bindings/Python/Globals.cpp             |  6 ++++--
 mlir/lib/Bindings/Python/IRAttributes.cpp        | 14 ++++++++------
 mlir/lib/Bindings/Python/IRCore.cpp              | 11 +++++++----
 mlir/lib/Bindings/Python/IRInterfaces.cpp        |  3 ++-
 mlir/lib/Bindings/Python/Rewrite.cpp             |  6 ++++--
 12 files changed, 27 insertions(+), 22 deletions(-)

diff --git a/mlir/include/mlir/Bindings/Python/Globals.h b/mlir/include/mlir/Bindings/Python/Globals.h
index 33e1130e6979d..8f7085f6024f5 100644
--- a/mlir/include/mlir/Bindings/Python/Globals.h
+++ b/mlir/include/mlir/Bindings/Python/Globals.h
@@ -20,7 +20,6 @@
 #include "mlir-c/IR.h"
 #include "mlir-c/Support.h"
 #include "mlir/Bindings/Python/NanobindUtils.h"
-#include "mlir/CAPI/Support.h"
 
 namespace mlir {
 namespace python {
diff --git a/mlir/lib/Bindings/Python/DialectAMDGPU.cpp b/mlir/lib/Bindings/Python/DialectAMDGPU.cpp
index e5a969fffe940..05418852d466d 100644
--- a/mlir/lib/Bindings/Python/DialectAMDGPU.cpp
+++ b/mlir/lib/Bindings/Python/DialectAMDGPU.cpp
@@ -14,7 +14,6 @@
 #include "nanobind/nanobind.h"
 
 namespace nb = nanobind;
-using namespace llvm;
 using namespace mlir::python::nanobind_adaptors;
 
 namespace mlir {
diff --git a/mlir/lib/Bindings/Python/DialectNVGPU.cpp b/mlir/lib/Bindings/Python/DialectNVGPU.cpp
index 66c3fcd6387c8..00072a2ae9933 100644
--- a/mlir/lib/Bindings/Python/DialectNVGPU.cpp
+++ b/mlir/lib/Bindings/Python/DialectNVGPU.cpp
@@ -13,7 +13,6 @@
 #include "mlir/Bindings/Python/NanobindAdaptors.h"
 
 namespace nb = nanobind;
-using namespace llvm;
 using namespace mlir::python::nanobind_adaptors;
 
 namespace mlir {
diff --git a/mlir/lib/Bindings/Python/DialectPDL.cpp b/mlir/lib/Bindings/Python/DialectPDL.cpp
index fd2fbc1b1b5ee..b4c9bac868829 100644
--- a/mlir/lib/Bindings/Python/DialectPDL.cpp
+++ b/mlir/lib/Bindings/Python/DialectPDL.cpp
@@ -13,7 +13,6 @@
 #include "mlir/Bindings/Python/NanobindAdaptors.h"
 
 namespace nb = nanobind;
-using namespace llvm;
 using namespace mlir::python::nanobind_adaptors;
 
 namespace mlir {
diff --git a/mlir/lib/Bindings/Python/DialectQuant.cpp b/mlir/lib/Bindings/Python/DialectQuant.cpp
index fba6a264ff007..eeb9a4c6dc5e9 100644
--- a/mlir/lib/Bindings/Python/DialectQuant.cpp
+++ b/mlir/lib/Bindings/Python/DialectQuant.cpp
@@ -17,7 +17,6 @@
 #include <mlir/Bindings/Python/IRAttributes.h>
 
 namespace nb = nanobind;
-using namespace llvm;
 using namespace mlir::python::nanobind_adaptors;
 
 namespace mlir {
diff --git a/mlir/lib/Bindings/Python/DialectSparseTensor.cpp b/mlir/lib/Bindings/Python/DialectSparseTensor.cpp
index f93dcd75ef869..87646c58ba4a8 100644
--- a/mlir/lib/Bindings/Python/DialectSparseTensor.cpp
+++ b/mlir/lib/Bindings/Python/DialectSparseTensor.cpp
@@ -17,7 +17,6 @@
 #include "mlir/Bindings/Python/NanobindAdaptors.h"
 
 namespace nb = nanobind;
-using namespace llvm;
 using namespace mlir::python::nanobind_adaptors;
 
 namespace mlir {
diff --git a/mlir/lib/Bindings/Python/DialectTransform.cpp b/mlir/lib/Bindings/Python/DialectTransform.cpp
index 0d9877a1d2a0a..157194f00e3c4 100644
--- a/mlir/lib/Bindings/Python/DialectTransform.cpp
+++ b/mlir/lib/Bindings/Python/DialectTransform.cpp
@@ -227,7 +227,8 @@ class PyTransformOpInterface
 
     // Attach a FallbackModel, which calls into Python, to the named operation.
     mlirTransformOpInterfaceAttachFallbackModel(
-        ctx->get(), wrap(StringRef(opName.c_str())), callbacks);
+        ctx->get(), mlirStringRefCreate(opName.c_str(), opName.size()),
+        callbacks);
   }
 
   static void bindDerived(ClassTy &cls) {
diff --git a/mlir/lib/Bindings/Python/Globals.cpp b/mlir/lib/Bindings/Python/Globals.cpp
index d7f6e1f2fdeb2..411b8a6705f1c 100644
--- a/mlir/lib/Bindings/Python/Globals.cpp
+++ b/mlir/lib/Bindings/Python/Globals.cpp
@@ -176,7 +176,8 @@ PyGlobals::lookupAttributeBuilder(const std::string &attributeKind) {
 std::optional<nb::callable> PyGlobals::lookupTypeCaster(MlirTypeID mlirTypeID,
                                                         MlirDialect dialect) {
   // Try to load dialect module.
-  (void)loadDialectModule(unwrap(mlirDialectGetNamespace(dialect)));
+  MlirStringRef ns = mlirDialectGetNamespace(dialect);
+  (void)loadDialectModule(std::string_view(ns.data, ns.length));
   nb::ft_lock_guard lock(mutex);
   const auto foundIt = typeCasterMap.find(mlirTypeID);
   if (foundIt != typeCasterMap.end()) {
@@ -189,7 +190,8 @@ std::optional<nb::callable> PyGlobals::lookupTypeCaster(MlirTypeID mlirTypeID,
 std::optional<nb::callable> PyGlobals::lookupValueCaster(MlirTypeID mlirTypeID,
                                                          MlirDialect dialect) {
   // Try to load dialect module.
-  (void)loadDialectModule(unwrap(mlirDialectGetNamespace(dialect)));
+  MlirStringRef ns = mlirDialectGetNamespace(dialect);
+  (void)loadDialectModule(std::string_view(ns.data, ns.length));
   nb::ft_lock_guard lock(mutex);
   const auto foundIt = valueCasterMap.find(mlirTypeID);
   if (foundIt != valueCasterMap.end()) {
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 2a13889d773c1..e0e7dc9ec77bd 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -513,13 +513,15 @@ void PySymbolRefAttribute::bindDerived(ClassTy &c) {
   c.def_prop_ro(
       "value",
       [](PySymbolRefAttribute &self) {
+        MlirStringRef rootRef = mlirSymbolRefAttrGetRootReference(self);
         std::vector<std::string> symbols = {
-            unwrap(mlirSymbolRefAttrGetRootReference(self)).str()};
-        for (int i = 0; i < mlirSymbolRefAttrGetNumNestedReferences(self); ++i)
-          symbols.push_back(
-              unwrap(mlirSymbolRefAttrGetRootReference(
-                         mlirSymbolRefAttrGetNestedReference(self, i)))
-                  .str());
+            std::string(rootRef.data, rootRef.length)};
+        for (int i = 0; i < mlirSymbolRefAttrGetNumNestedReferences(self);
+             ++i) {
+          MlirStringRef nestedRef = mlirSymbolRefAttrGetRootReference(
+              mlirSymbolRefAttrGetNestedReference(self, i));
+          symbols.push_back(std::string(nestedRef.data, nestedRef.length));
+        }
         return symbols;
       },
       "Returns the value of the SymbolRef attribute as a list[str]");
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index a33ecc93c2bd4..4aef8a95dcc2f 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -2772,8 +2772,9 @@ MlirLocation tracebackToLocation(MlirContext ctx) {
         nb::cast<std::string>(nb::borrow<nb::str>(code->co_name));
     std::string_view funcName(name);
     int startLine = PyFrame_GetLineNumber(pyFrame);
-    MlirLocation loc =
-        mlirLocationFileLineColGet(ctx, wrap(fileName), startLine, 0);
+    MlirLocation loc = mlirLocationFileLineColGet(
+        ctx, mlirStringRefCreate(fileName.data(), fileName.size()), startLine,
+        0);
 #else
     std::string name =
         nb::cast<std::string>(nb::borrow<nb::str>(code->co_qualname));
@@ -2785,10 +2786,12 @@ MlirLocation tracebackToLocation(MlirContext ctx) {
       throw nb::python_error();
     }
     MlirLocation loc = mlirLocationFileLineColRangeGet(
-        ctx, wrap(fileName), startLine, startCol, endLine, endCol);
+        ctx, mlirStringRefCreate(fileName.data(), fileName.size()), startLine,
+        startCol, endLine, endCol);
 #endif
 
-    frames[count] = mlirLocationNameGet(ctx, wrap(funcName), loc);
+    frames[count] = mlirLocationNameGet(
+        ctx, mlirStringRefCreate(funcName.data(), funcName.size()), loc);
     ++count;
   }
   // When the loop breaks (after the last iter), current frame (if non-null)
diff --git a/mlir/lib/Bindings/Python/IRInterfaces.cpp b/mlir/lib/Bindings/Python/IRInterfaces.cpp
index ea795dde875c1..e7865cfda9d6f 100644
--- a/mlir/lib/Bindings/Python/IRInterfaces.cpp
+++ b/mlir/lib/Bindings/Python/IRInterfaces.cpp
@@ -380,7 +380,8 @@ class PyMemoryEffectsOpInterface
     };
 
     mlirMemoryEffectsOpInterfaceAttachFallbackModel(
-        ctx->get(), wrap(StringRef(opName.c_str())), callbacks);
+        ctx->get(), mlirStringRefCreate(opName.c_str(), opName.size()),
+        callbacks);
   }
 
   static void bindDerived(ClassTy &cls) {
diff --git a/mlir/lib/Bindings/Python/Rewrite.cpp b/mlir/lib/Bindings/Python/Rewrite.cpp
index ca36a5caf0be8..256dd0a78f809 100644
--- a/mlir/lib/Bindings/Python/Rewrite.cpp
+++ b/mlir/lib/Bindings/Python/Rewrite.cpp
@@ -297,8 +297,10 @@ class PyRewritePatternSet {
       std::vector<MlirValue> operandsVec(operands, operands + nOperands);
       nb::object adaptorCls =
           PyGlobals::get()
-              .lookupOpAdaptorClass(
-                  unwrap(mlirIdentifierStr(mlirOperationGetName(op))))
+              .lookupOpAdaptorClass([&] {
+                MlirStringRef ref = mlirIdentifierStr(mlirOperationGetName(op));
+                return std::string_view(ref.data, ref.length);
+              }())
               .value_or(nb::borrow(nb::type<PyOpAdaptor>()));
 
       nb::object res = f(opView, adaptorCls(operandsVec, opView),

>From 3842ccd0fe32963d0abf1b0d9dcfceaf76caec40 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Tue, 17 Feb 2026 19:16:57 +0000
Subject: [PATCH 5/5] Change ssize_t to Py_ssize_t for win os

---
 .../mlir/Bindings/Python/IRAttributes.h        | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/mlir/include/mlir/Bindings/Python/IRAttributes.h b/mlir/include/mlir/Bindings/Python/IRAttributes.h
index 6c272f226e5a9..2a6dbe229044e 100644
--- a/mlir/include/mlir/Bindings/Python/IRAttributes.h
+++ b/mlir/include/mlir/Bindings/Python/IRAttributes.h
@@ -28,17 +28,17 @@ namespace MLIR_BINDINGS_PYTHON_DOMAIN {
 
 struct nb_buffer_info {
   void *ptr = nullptr;
-  ssize_t itemsize = 0;
-  ssize_t size = 0;
+  Py_ssize_t itemsize = 0;
+  Py_ssize_t size = 0;
   const char *format = nullptr;
-  ssize_t ndim = 0;
-  std::vector<ssize_t> shape;
-  std::vector<ssize_t> strides;
+  Py_ssize_t ndim = 0;
+  std::vector<Py_ssize_t> shape;
+  std::vector<Py_ssize_t> strides;
   bool readonly = false;
 
   nb_buffer_info(
-      void *ptr, ssize_t itemsize, const char *format, ssize_t ndim,
-      std::vector<ssize_t> shape_in, std::vector<ssize_t> strides_in,
+      void *ptr, Py_ssize_t itemsize, const char *format, Py_ssize_t ndim,
+      std::vector<Py_ssize_t> shape_in, std::vector<Py_ssize_t> strides_in,
       bool readonly = false,
       std::unique_ptr<Py_buffer, void (*)(Py_buffer *)> owned_view_in =
           std::unique_ptr<Py_buffer, void (*)(Py_buffer *)>(nullptr, nullptr));
@@ -451,11 +451,11 @@ class MLIR_PYTHON_API_EXPORTED PyDenseElementsAttribute
     Type *data = static_cast<Type *>(
         const_cast<void *>(mlirDenseElementsAttrGetRawData(*this)));
     // Prepare the shape for the buffer_info.
-    std::vector<ssize_t> shape;
+    std::vector<Py_ssize_t> shape;
     for (intptr_t i = 0; i < rank; ++i)
       shape.push_back(mlirShapedTypeGetDimSize(shapedType, i));
     // Prepare the strides for the buffer_info.
-    std::vector<ssize_t> strides;
+    std::vector<Py_ssize_t> strides;
     if (mlirDenseElementsAttrIsSplat(*this)) {
       // Splats are special, only the single value is stored.
       strides.assign(rank, 0);



More information about the Mlir-commits mailing list