Specifying the namespace of Ops from StandardOps on use in a tablegen file


many of the Ops from Standard do not specify a C++ namespace in their ODS in include/mlir/Dialect/StandardOps/IR/Ops.td. As a result, the associated C++ classes are not put into a namespace directly in the generated header file. However, the wrapping header file include/mlir/Dialect/StandardOps/IR/Ops.h puts all definitions into the mlir namespace by putting the include directive into a namespace.

While this works fine when using Ops.h in C++ code, this scheme requires some workaround when using the ODS in another tablegen file, e.g., in a DRR. The generated code then does not seem to reference the class from the mlir namespace, but from the default namespace.

For example:

include "mlir/Dialect/StandardOps/IR/Ops.td"

def TestPattern : Pat<
  (SomeOp:$res $arg), (AddIOp $arg, $arg),

fails at compilation, since the generated code references AddIOp instead of mlir::AddIOp. What is the correct way to specify the namespace for AddIOp here? As far as I understand, this wouldn’t be an issue at all if the ODS for all Ops would specify a cppNamespace field.


Yes I think std ops should be specifying the namespace they’re in. There was some cleanup to make the generated code itself specify its namespace rather than relying on the place where it was included. Not sure why this one would have been missed

(Make namespace handling uniform across dialect backends. · llvm/llvm-project@7d1ed69 · GitHub)

Thanks for pointing to the commit. I just submitted D103234 for review attempting to fix the issue.