Skip to content

Commit 3fc2f91

Browse files
committed
[mlir] Add dialect hooks for registering custom type and attribute alias printers
This patch introduces a mechanism for dialects to register custom alias printers for types and attributes via the `OpAsmDialectInterface`. This allows dialects to provide alternative printed representations for types and attributes based on their TypeID, including types/attributes from other dialects. The new `registerAttrAliasPrinter` and `registerTypeAliasPrinter` virtual methods accept callbacks that register printers for specific TypeIDs. When printing, these custom printers are invoked in registration order, and the first one to produce output is used. The precedence for alias resolution is: 1. Explicit type/attribute aliases returned by `getAlias` 2. Dialect-specific alias printers registered via the new hooks 3. Default type/attribute printers Signed-off-by: Fabian Mora <[email protected]>
1 parent b184f00 commit 3fc2f91

File tree

4 files changed

+310
-4
lines changed

4 files changed

+310
-4
lines changed

mlir/include/mlir/IR/OpImplementation.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "mlir/IR/DialectInterface.h"
1818
#include "mlir/IR/OpAsmSupport.h"
1919
#include "mlir/IR/OpDefinition.h"
20+
#include "mlir/Support/LLVM.h"
21+
#include "mlir/Support/TypeID.h"
2022
#include "llvm/ADT/Twine.h"
2123
#include "llvm/Support/SMLoc.h"
2224
#include <optional>
@@ -169,6 +171,9 @@ class AsmPrinter {
169171
if (succeeded(printAlias(attrOrType)))
170172
return;
171173

174+
if (succeeded(printDialectAlias(attrOrType, /*printStripped=*/true)))
175+
return;
176+
172177
raw_ostream &os = getStream();
173178
uint64_t posPrior = os.tell();
174179
attrOrType.print(*this);
@@ -218,6 +223,14 @@ class AsmPrinter {
218223
/// be printed.
219224
virtual LogicalResult printAlias(Type type);
220225

226+
/// Print the alias for the given attribute, return failure if no alias could
227+
/// be printed.
228+
virtual LogicalResult printDialectAlias(Attribute attr, bool printStripped);
229+
230+
/// Print the alias for the given type, return failure if no alias could
231+
/// be printed.
232+
virtual LogicalResult printDialectAlias(Type type, bool printStripped);
233+
221234
/// Print the given string as a keyword, or a quoted and escaped string if it
222235
/// has any special or non-printable characters in it.
223236
virtual void printKeywordOrString(StringRef keyword);
@@ -1799,6 +1812,30 @@ class OpAsmDialectInterface
17991812
return AliasResult::NoAlias;
18001813
}
18011814

1815+
/// Hooks for registering alias printers for types and attributes. These
1816+
/// printers are invoked when printing types or attributes of the given
1817+
/// TypeID. Printers are invoked in the order they are registered, and the
1818+
/// first one to print an alias is used.
1819+
/// The precedence of these printers is as follow:
1820+
/// 1. The type and attribute aliases returned by `getAlias`.
1821+
/// 2. Dialect-specific alias printers registered here.
1822+
/// 3. The type and attribute printers.
1823+
/// The boolean argument to the printer indicates whether the stripped form
1824+
/// of the type or attribute is being printed.
1825+
/// NOTE: This mechanism caches the printed object, therefore the printer
1826+
/// must always produce the same output for the same input.
1827+
using AttributeAliasPrinter =
1828+
llvm::function_ref<void(Attribute, AsmPrinter &, bool)>;
1829+
using InsertAttrAliasPrinter =
1830+
llvm::function_ref<void(TypeID, AttributeAliasPrinter)>;
1831+
virtual void registerAttrAliasPrinter(InsertAttrAliasPrinter insertFn) const {
1832+
}
1833+
using TypeAliasPrinter = llvm::function_ref<void(Type, AsmPrinter &, bool)>;
1834+
using InsertTypeAliasPrinter =
1835+
llvm::function_ref<void(TypeID, TypeAliasPrinter)>;
1836+
virtual void registerTypeAliasPrinter(InsertTypeAliasPrinter insertFn) const {
1837+
}
1838+
18021839
//===--------------------------------------------------------------------===//
18031840
// Resources
18041841
//===--------------------------------------------------------------------===//

0 commit comments

Comments
 (0)