Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions mlir/test/mlir-tblgen/op-python-bindings.td
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,34 @@ def DeriveResultTypesVariadicOp : TestOp<"derive_result_types_variadic_op", [Fir
// CHECK: def derive_result_types_variadic_op(res, _gen_res_1, type_, *, loc=None, ip=None)
// CHECK: return _get_op_result_or_op_results(DeriveResultTypesVariadicOp(res=res, _gen_res_1=_gen_res_1, type_=type_, loc=loc, ip=ip))


// CHECK: class DescriptionOp(_ods_ir.OpView):
// CHECK: """
// CHECK: This is a long description.
// CHECK: It has multiple lines.
// CHECK: A code block (to test the indent).
// CHECK: ```mlir
// CHECK: test.loop {
// CHECK: test.yield
// CHECK: }
// CHECK: ```
// CHECK: Add \"\"\" will not terminate the description.
// CHECK: """
def DescriptionOp : TestOp<"description"> {
let description = [{
This is a long description.
It has multiple lines.

A code block (to test the indent).
```mlir
test.loop {
test.yield
}
```
Add """ will not terminate the description.
}];
}

// CHECK: @_ods_cext.register_operation(_Dialect)
// CHECK: class EmptyOp(_ods_ir.OpView):
// CHECK-LABEL: OPERATION_NAME = "test.empty"
Expand Down
6 changes: 3 additions & 3 deletions mlir/test/python/ir/auto_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def testInferLocations():
_cext.globals.register_traceback_file_inclusion(_arith_ops_gen.__file__)
three = arith.constant(IndexType.get(), 3)
# fmt: off
# CHECK: loc(callsite("ConstantOp.__init__"("{{.*}}[[SEP]]mlir[[SEP]]dialects[[SEP]]_arith_ops_gen.py":397:4 to :235) at callsite("testInferLocations"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":52:16 to :50) at callsite("run"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":13:4 to :7) at "<module>"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":26:1 to :4)))))
# CHECK: loc(callsite("ConstantOp.__init__"("{{.*}}[[SEP]]mlir[[SEP]]dialects[[SEP]]_arith_ops_gen.py":649:4 to :235) at callsite("testInferLocations"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":52:16 to :50) at callsite("run"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":13:4 to :7) at "<module>"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":26:1 to :4)))))
# fmt: on
print(three.location)

Expand All @@ -60,14 +60,14 @@ def foo():
print(four.location)

# fmt: off
# CHECK: loc(callsite("ConstantOp.__init__"("{{.*}}[[SEP]]mlir[[SEP]]dialects[[SEP]]_arith_ops_gen.py":397:4 to :235) at callsite("testInferLocations.<locals>.foo"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":59:19 to :53) at callsite("testInferLocations"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":65:8 to :13) at callsite("run"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":13:4 to :7) at "<module>"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":26:1 to :4))))))
# CHECK: loc(callsite("ConstantOp.__init__"("{{.*}}[[SEP]]mlir[[SEP]]dialects[[SEP]]_arith_ops_gen.py":649:4 to :235) at callsite("testInferLocations.<locals>.foo"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":59:19 to :53) at callsite("testInferLocations"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":65:8 to :13) at callsite("run"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":13:4 to :7) at "<module>"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":26:1 to :4))))))
Copy link
Member Author

@PragmaTwice PragmaTwice Sep 16, 2025

Choose a reason for hiding this comment

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

It seems the line numbers are changed frequently as the tblgen code changed, which may introduce code conflicts. Maybe we can make it a regex like {{ \d+ }}? cc @makslevental

Copy link
Contributor

@makslevental makslevental Sep 16, 2025

Choose a reason for hiding this comment

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

i mean yes but then that defeats the purpose of verifying that the line numbers are correct?

Copy link
Contributor

@makslevental makslevental Sep 16, 2025

Choose a reason for hiding this comment

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

actually yea sure why not - for the line numbers in generated code we can do {{ \d+ }} (ain't no one gonna check those anyway lol).

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm yeah you are right. Although practically we may just generate and then paste the line number here. 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

For the generated line number it's fine to ignore - the line numbers that reference the test source file itself are good enough.

# fmt: on
foo()

_cext.globals.register_traceback_file_exclusion(__file__)

# fmt: off
# CHECK: loc("ConstantOp.__init__"("{{.*}}[[SEP]]mlir[[SEP]]dialects[[SEP]]_arith_ops_gen.py":397:4 to :235))
# CHECK: loc("ConstantOp.__init__"("{{.*}}[[SEP]]mlir[[SEP]]dialects[[SEP]]_arith_ops_gen.py":649:4 to :235))
# fmt: on
foo()

Expand Down
31 changes: 28 additions & 3 deletions mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@

#include "OpGenHelpers.h"

#include "mlir/Support/IndentedOstream.h"
#include "mlir/TableGen/GenInfo.h"
#include "mlir/TableGen/Operator.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include <regex>

using namespace mlir;
using namespace mlir::tblgen;
Expand Down Expand Up @@ -62,10 +64,11 @@ from ._{0}_ops_gen import _Dialect

/// Template for operation class:
/// {0} is the Python class name;
/// {1} is the operation name.
/// {1} is the operation name;
/// {2} is the docstring for this operation.
constexpr const char *opClassTemplate = R"Py(
@_ods_cext.register_operation(_Dialect)
class {0}(_ods_ir.OpView):
class {0}(_ods_ir.OpView):{2}
OPERATION_NAME = "{1}"
)Py";

Expand Down Expand Up @@ -1034,9 +1037,31 @@ static void emitValueBuilder(const Operator &op,
}
}

/// Retrieve the description of the given op and generate a docstring for it.
static std::string makeDocStringForOp(const Operator &op) {
if (!op.hasDescription())
return "";

auto desc = op.getDescription().rtrim(" \t").str();
// Replace all """ with \"\"\" to avoid early termination of the literal.
desc = std::regex_replace(desc, std::regex(R"(""")"), R"(\"\"\")");

std::string docString = "\n";
llvm::raw_string_ostream os(docString);
raw_indented_ostream identedOs(os);
os << R"( """)" << "\n";
identedOs.printReindented(desc, " ");
if (!StringRef(desc).ends_with("\n"))
os << "\n";
os << R"( """)" << "\n";

return docString;
}

/// Emits bindings for a specific Op to the given output stream.
static void emitOpBindings(const Operator &op, raw_ostream &os) {
os << formatv(opClassTemplate, op.getCppClassName(), op.getOperationName());
os << formatv(opClassTemplate, op.getCppClassName(), op.getOperationName(),
makeDocStringForOp(op));

// Sized segments.
if (op.getTrait(attrSizedTraitForKind("operand")) != nullptr) {
Expand Down