Skip to content

Conversation

@makslevental
Copy link
Contributor

@makslevental makslevental commented Jan 8, 2026

If you set MLIR_PYTHON_BINDINGS_DOMAIN=mlir, you get namespace nesting like mlir::python::mlir and then mlir::Twine shadows llvm::Twine (but only on MSVC). So prefix with ::llvm to have the correct root namespace.

Co-authored-by: Abhishek Varma <abhvarma@amd.com>
@makslevental makslevental force-pushed the users/makslevental/namespace-shadowing branch from 3a1a1b8 to 3938502 Compare January 8, 2026 22:23
@makslevental makslevental changed the title [mlir][Python] fix namespace shadowing [mlir][Python] fix namespace shadowing on MSVC Jan 8, 2026
@makslevental makslevental marked this pull request as ready for review January 8, 2026 22:33
@llvmbot llvmbot added mlir:python MLIR Python bindings mlir labels Jan 8, 2026
@llvmbot
Copy link
Member

llvmbot commented Jan 8, 2026

@llvm/pr-subscribers-mlir

Author: Maksim Levental (makslevental)

Changes

If you set MLIR_PYTHON_BINDINGS_DOMAIN=mlir, you get namespace nesting like mlir::python::mlir and then mlir::Twine shadows llvm::Twine (but only on MSVC). So prefix with ::llvm to have the correct root namespace.


Full diff: https://github.com/llvm/llvm-project/pull/175077.diff

1 Files Affected:

  • (modified) mlir/include/mlir/Bindings/Python/IRCore.h (+11-11)
diff --git a/mlir/include/mlir/Bindings/Python/IRCore.h b/mlir/include/mlir/Bindings/Python/IRCore.h
index 59dc496c9e206..330318683c15e 100644
--- a/mlir/include/mlir/Bindings/Python/IRCore.h
+++ b/mlir/include/mlir/Bindings/Python/IRCore.h
@@ -947,7 +947,7 @@ class MLIR_PYTHON_API_EXPORTED PyConcreteType : public BaseTy {
     if (!DerivedTy::isaFunction(orig)) {
       auto origRepr =
           nanobind::cast<std::string>(nanobind::repr(nanobind::cast(orig)));
-      throw nanobind::value_error((llvm::Twine("Cannot cast type to ") +
+      throw nanobind::value_error((::llvm::Twine("Cannot cast type to ") +
                                    DerivedTy::pyClassName + " (from " +
                                    origRepr + ")")
                                       .str()
@@ -966,7 +966,7 @@ class MLIR_PYTHON_API_EXPORTED PyConcreteType : public BaseTy {
           if (DerivedTy::getTypeIdFunction)
             return PyTypeID(DerivedTy::getTypeIdFunction());
           throw nanobind::attribute_error(
-              (DerivedTy::pyClassName + llvm::Twine(" has no typeid."))
+              (DerivedTy::pyClassName + ::llvm::Twine(" has no typeid."))
                   .str()
                   .c_str());
         },
@@ -1080,7 +1080,7 @@ class MLIR_PYTHON_API_EXPORTED PyConcreteAttribute : public BaseTy {
     if (!DerivedTy::isaFunction(orig)) {
       auto origRepr =
           nanobind::cast<std::string>(nanobind::repr(nanobind::cast(orig)));
-      throw nanobind::value_error((llvm::Twine("Cannot cast attribute to ") +
+      throw nanobind::value_error((::llvm::Twine("Cannot cast attribute to ") +
                                    DerivedTy::pyClassName + " (from " +
                                    origRepr + ")")
                                       .str()
@@ -1110,7 +1110,7 @@ class MLIR_PYTHON_API_EXPORTED PyConcreteAttribute : public BaseTy {
           if (DerivedTy::getTypeIdFunction)
             return PyTypeID(DerivedTy::getTypeIdFunction());
           throw nanobind::attribute_error(
-              (DerivedTy::pyClassName + llvm::Twine(" has no typeid."))
+              (DerivedTy::pyClassName + ::llvm::Twine(" has no typeid."))
                   .str()
                   .c_str());
         },
@@ -1324,7 +1324,7 @@ class MLIR_PYTHON_API_EXPORTED PySymbolTable {
 /// Custom exception that allows access to error diagnostic information. This is
 /// converted to the `ir.MLIRError` python exception when thrown.
 struct MLIR_PYTHON_API_EXPORTED MLIRError {
-  MLIRError(llvm::Twine message,
+  MLIRError(::llvm::Twine message,
             std::vector<PyDiagnostic::DiagnosticInfo> &&errorDiagnostics = {})
       : message(message.str()), errorDiagnostics(std::move(errorDiagnostics)) {}
   std::string message;
@@ -1544,7 +1544,7 @@ class MLIR_PYTHON_API_EXPORTED PyConcreteValue : public PyValue {
     if (!DerivedTy::isaFunction(orig.get())) {
       auto origRepr =
           nanobind::cast<std::string>(nanobind::repr(nanobind::cast(orig)));
-      throw nanobind::value_error((Twine("Cannot cast value to ") +
+      throw nanobind::value_error((::llvm::Twine("Cannot cast value to ") +
                                    DerivedTy::pyClassName + " (from " +
                                    origRepr + ")")
                                       .str()
@@ -1555,11 +1555,11 @@ class MLIR_PYTHON_API_EXPORTED PyConcreteValue : public PyValue {
 
   /// Binds the Python module objects to functions of this class.
   static void bind(nanobind::module_ &m) {
-    auto cls = ClassTy(
-        m, DerivedTy::pyClassName, nanobind::is_generic(),
-        nanobind::sig((Twine("class ") + DerivedTy::pyClassName + "(Value[_T])")
-                          .str()
-                          .c_str()));
+    auto cls = ClassTy(m, DerivedTy::pyClassName, nanobind::is_generic(),
+                       nanobind::sig((::llvm::Twine("class ") +
+                                      DerivedTy::pyClassName + "(Value[_T])")
+                                         .str()
+                                         .c_str()));
     cls.def(nanobind::init<PyValue &>(), nanobind::keep_alive<0, 1>(),
             nanobind::arg("value"));
     cls.def(

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@makslevental makslevental merged commit 51f6c58 into main Jan 8, 2026
14 checks passed
@makslevental makslevental deleted the users/makslevental/namespace-shadowing branch January 8, 2026 22:35
@boomanaiden154
Copy link
Contributor

We saw this in a downstream too. It doesn’t seem like we have upstream CI to cover this configuration? For what it’s worth, we saw this with clang-cl too.

@makslevental
Copy link
Contributor Author

It doesn’t seem like we have upstream CI to cover this configuration

TBH I don't really understand which configuration this fails on - both IREE and substrate (@ingomueller-net) reported Windows issues but we do have Windows pre-commit right and I have basically "exhaustive" Windows (among others) tests in eudsl (where I do use clang-cl) and neither caught this. So @boomanaiden154 if you can tell me what environment/configuration produced this I'd be happy to add a test somewhere for it (probably eudsl).

@boomanaiden154
Copy link
Contributor

So boomanaiden154 if you can tell me what environment/configuration produced this I'd be happy to add a test somewhere for it (probably eudsl).

I was somewhat hoping you would have the answer there despite me having no idea. 😅

I also don't have a good idea. I tried to reproduce this issue (which manifest in Jax/XLA for us) locally and could not. I sent a patch to patch in this change and someone else tested it on the CI and that worked. It was an old clang-cl version (v18), but I tried locally with that too and also could not reproduce. So not really sure what's happening.

kshitijvp pushed a commit to kshitijvp/llvm-project that referenced this pull request Jan 9, 2026
If you set `MLIR_PYTHON_BINDINGS_DOMAIN=mlir`, you get namespace nesting
like `mlir::python::mlir` and then `mlir::Twine` shadows `llvm::Twine`
(but only on MSVC). So prefix with `::llvm` to have the correct root
namespace.

Co-authored-by: Abhishek Varma <abhvarma@amd.com>
Abhishek-Varma added a commit to Abhishek-Varma/iree that referenced this pull request Jan 9, 2026
Existing local reverts carried forward:
* Local revert of llvm/llvm-project#169614 due to llvm/llvm-project#172932.
* llvm/llvm-project#174084 -- causes linker errors related to rtti in stablehlo code
* llvm/llvm-project@2b903df -- follow up fix to the above (doesn't fix it for us)

-- Along with the above, IREE header fixes were done to
   accomodate changes introduced via llvm/llvm-project#82190
   for `Affine/Transforms/Passes.h`.

NOTE: [Twine fix commit](iree-org/llvm-project@2eaa29c) was dropped from our fork since llvm/llvm-project#175077 got merged upstream.

Signed-off-by: Abhishek Varma <abhvarma@amd.com>
kuhar pushed a commit to iree-org/iree that referenced this pull request Jan 9, 2026
Existing local reverts carried forward:
* Local revert of llvm/llvm-project#169614 due
to llvm/llvm-project#172932.
* llvm/llvm-project#174084 -- causes linker
errors related to rtti in stablehlo code
*
llvm/llvm-project@2b903df
-- follow up fix to the above (doesn't fix it for us)

-- Along with the above, IREE header fixes were done to
accomodate changes introduced via
llvm/llvm-project#82190
   for `Affine/Transforms/Passes.h`.

NOTE: [Twine fix
commit](iree-org/llvm-project@2eaa29c)
was dropped from our fork since
llvm/llvm-project#175077 got merged upstream.

Signed-off-by: Abhishek Varma <abhvarma@amd.com>

Signed-off-by: Abhishek Varma <abhvarma@amd.com>
Priyanshu3820 pushed a commit to Priyanshu3820/llvm-project that referenced this pull request Jan 18, 2026
If you set `MLIR_PYTHON_BINDINGS_DOMAIN=mlir`, you get namespace nesting
like `mlir::python::mlir` and then `mlir::Twine` shadows `llvm::Twine`
(but only on MSVC). So prefix with `::llvm` to have the correct root
namespace.

Co-authored-by: Abhishek Varma <abhvarma@amd.com>
keshavvinayak01 pushed a commit to iree-org/iree that referenced this pull request Jan 27, 2026
Existing local reverts carried forward:
* Local revert of llvm/llvm-project#169614 due
to llvm/llvm-project#172932.
* llvm/llvm-project#174084 -- causes linker
errors related to rtti in stablehlo code
*
llvm/llvm-project@2b903df
-- follow up fix to the above (doesn't fix it for us)

-- Along with the above, IREE header fixes were done to
accomodate changes introduced via
llvm/llvm-project#82190
   for `Affine/Transforms/Passes.h`.

NOTE: [Twine fix
commit](iree-org/llvm-project@2eaa29c)
was dropped from our fork since
llvm/llvm-project#175077 got merged upstream.

Signed-off-by: Abhishek Varma <abhvarma@amd.com>

Signed-off-by: Abhishek Varma <abhvarma@amd.com>
Signed-off-by: Keshav Vinayak Jha <keshavvinayakjha@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mlir:python MLIR Python bindings mlir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants