Skip to content

[mlir][python] fix value-builder generation for snake_case ops #135302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

makslevental
Copy link
Contributor

@makslevental makslevental commented Apr 11, 2025

Ops that are already snake case (like ROCDL_wmma_* ops) produce python "value-builders" that collide with the class names:

class wmma_bf16_16x16x16_bf16(_ods_ir.OpView):
  OPERATION_NAME = "rocdl.wmma.bf16.16x16x16.bf16"
  ...

def wmma_bf16_16x16x16_bf16(res, args, *, loc=None, ip=None) -> _ods_ir.Value:
  return wmma_bf16_16x16x16_bf16(res=res, args=args, loc=loc, ip=ip).result

and thus cannot be emitted (because of recursive self-calls).

This PR fixes that by affixing _ to the value builder names.

I would've preferred to just rename the ops but that would be a breaking change 🤷.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Apr 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 11, 2025

@llvm/pr-subscribers-mlir-core

@llvm/pr-subscribers-mlir

Author: Maksim Levental (makslevental)

Changes

Ops that are already snake case (like ROCDL_wmma_* ops) produce python "value-builders" that collide with the class names;

class wmma_bf16_16x16x16_bf16(_ods_ir.OpView):
  OPERATION_NAME = "rocdl.wmma.bf16.16x16x16.bf16"
  ...

def wmma_bf16_16x16x16_bf16(res, args, *, loc=None, ip=None) -> _ods_ir.Value:
  return wmma_bf16_16x16x16_bf16(res=res, args=args, loc=loc, ip=ip).result

and thus cannot be emited (because recursive self-calls).

This PR fixes that by affixing _ to the value builders. I would've preferred to just rename the ops but that would be a breaking change 🤷.


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

2 Files Affected:

  • (modified) mlir/test/mlir-tblgen/op-python-bindings.td (+3)
  • (modified) mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp (+5-2)
diff --git a/mlir/test/mlir-tblgen/op-python-bindings.td b/mlir/test/mlir-tblgen/op-python-bindings.td
index 72963cac64d54..280c745262428 100644
--- a/mlir/test/mlir-tblgen/op-python-bindings.td
+++ b/mlir/test/mlir-tblgen/op-python-bindings.td
@@ -654,3 +654,6 @@ def WithSuccessorsOp : TestOp<"with_successors"> {
 
 // CHECK: def with_successors(successor, successors, *, loc=None, ip=None)
 // CHECK:   return WithSuccessorsOp(successor=successor, successors=successors, loc=loc, ip=ip)
+
+def already_snake_case : TestOp<"already_snake_case"> {}
+// CHECK: def already_snake_case_(*, loc=None, ip=None)
diff --git a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp b/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
index 604d2376052a8..14d50cd71047b 100644
--- a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
@@ -998,8 +998,11 @@ static void emitValueBuilder(const Operator &op,
         auto lhs = *llvm::split(arg, "=").begin();
         return (lhs + "=" + llvm::convertToSnakeFromCamelCase(lhs)).str();
       });
-  std::string nameWithoutDialect = sanitizeName(
-      op.getOperationName().substr(op.getOperationName().find('.') + 1));
+  StringRef opClassName =
+      op.getOperationName().substr(op.getOperationName().find('.') + 1);
+  std::string nameWithoutDialect = sanitizeName(opClassName);
+  if (nameWithoutDialect == opClassName)
+    nameWithoutDialect += "_";
   std::string params = llvm::join(valueBuilderParams, ", ");
   std::string args = llvm::join(opBuilderArgs, ", ");
   const char *type =

@makslevental makslevental force-pushed the makslevental/fix-snake-case-python-ops branch 3 times, most recently from 65d4951 to 54992b9 Compare April 11, 2025 03:56
@makslevental makslevental force-pushed the makslevental/fix-snake-case-python-ops branch from 54992b9 to 5b250ac Compare April 11, 2025 03:59
@makslevental makslevental merged commit c12cb0c into llvm:main Apr 11, 2025
11 checks passed
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…135302)

Ops that are already snake case (like [`ROCDL_wmma_*`
ops](https://github.com/makslevental/llvm-project/blob/66b0b0466bbd995146aadaf2cd18de5476c19941/mlir/include/mlir/Dialect/LLVMIR/ROCDLOps.td#L411))
produce python "value-builders" that collide with the class names:

```python
class wmma_bf16_16x16x16_bf16(_ods_ir.OpView):
  OPERATION_NAME = "rocdl.wmma.bf16.16x16x16.bf16"
  ...

def wmma_bf16_16x16x16_bf16(res, args, *, loc=None, ip=None) -> _ods_ir.Value:
  return wmma_bf16_16x16x16_bf16(res=res, args=args, loc=loc, ip=ip).result
```

and thus cannot be emitted (because of recursive self-calls).

This PR fixes that by affixing `_` to the value builder names. 

I would've preferred to just rename the ops but that would be a breaking
change 🤷.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants