[Mlir-commits] [mlir] 7d4cd47 - [mlir][python] Handle dashes in op name

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Oct 10 06:39:25 PDT 2023


Author: JoelWee
Date: 2023-10-10T14:39:20+01:00
New Revision: 7d4cd47e242c28c450c1e2a1a9f4bd4b7b5a01ab

URL: https://github.com/llvm/llvm-project/commit/7d4cd47e242c28c450c1e2a1a9f4bd4b7b5a01ab
DIFF: https://github.com/llvm/llvm-project/commit/7d4cd47e242c28c450c1e2a1a9f4bd4b7b5a01ab.diff

LOG: [mlir][python] Handle dashes in op name

The python generated code for 
```
def StableHLO_CrossReplicaSumOp : StableHLO_Op<"cross-replica-sum",
```

fails because the python code 

`def cross-replica-sum...` is generated, which is not a valid python name.

https://github.com/openxla/stablehlo/blob/main/stablehlo/dialect/StablehloOps.td#L2109

Added: 
    

Modified: 
    mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp b/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
index fc094a1829ff755..2c81538b7b40433 100644
--- a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
@@ -301,9 +301,13 @@ static bool isODSReserved(StringRef str) {
 /// (does not change the `name` if it already is suitable) and returns the
 /// modified version.
 static std::string sanitizeName(StringRef name) {
-  if (isPythonReserved(name) || isODSReserved(name))
-    return (name + "_").str();
-  return name.str();
+  std::string processed_str = name.str();
+
+  std::replace(processed_str.begin(), processed_str.end(), '-', '_');
+
+  if (isPythonReserved(processed_str) || isODSReserved(processed_str))
+    return processed_str + "_";
+  return processed_str;
 }
 
 static std::string attrSizedTraitForKind(const char *kind) {


        


More information about the Mlir-commits mailing list