Skip to content

Commit 96c23eb

Browse files
author
Simon Camphausen
authored
[mlir][EmitC] Use declarative assembly format for opaque types and attributes (#76066)
The parser and printer of string attributes were changed to handle escape sequences. Therefore, we no longer require a custom parser and printer. Verification is moved from the parser to the verifier accordingly.
1 parent 901a816 commit 96c23eb

File tree

3 files changed

+11
-44
lines changed

3 files changed

+11
-44
lines changed

mlir/include/mlir/Dialect/EmitC/IR/EmitCAttributes.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ def EmitC_OpaqueAttr : EmitC_Attr<"Opaque", "opaque"> {
5757
}];
5858

5959
let parameters = (ins StringRefParameter<"the opaque value">:$value);
60-
61-
let hasCustomAssemblyFormat = 1;
60+
let assemblyFormat = "`<` $value `>`";
6261
}
6362

6463
def EmitC_OpaqueOrTypedAttr : AnyAttrOf<[EmitC_OpaqueAttr, TypedAttrInterface]>;

mlir/include/mlir/Dialect/EmitC/IR/EmitCTypes.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def EmitC_OpaqueType : EmitC_Type<"Opaque", "opaque"> {
4242
}];
4343

4444
let parameters = (ins StringRefParameter<"the opaque value">:$value);
45-
let hasCustomAssemblyFormat = 1;
45+
let assemblyFormat = "`<` $value `>`";
46+
let genVerifyDecl = 1;
4647
}
4748

4849
def EmitC_PointerType : EmitC_Type<"Pointer", "ptr"> {

mlir/lib/Dialect/EmitC/IR/EmitC.cpp

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -621,27 +621,6 @@ LogicalResult emitc::YieldOp::verify() {
621621
#define GET_ATTRDEF_CLASSES
622622
#include "mlir/Dialect/EmitC/IR/EmitCAttributes.cpp.inc"
623623

624-
Attribute emitc::OpaqueAttr::parse(AsmParser &parser, Type type) {
625-
if (parser.parseLess())
626-
return Attribute();
627-
std::string value;
628-
SMLoc loc = parser.getCurrentLocation();
629-
if (parser.parseOptionalString(&value)) {
630-
parser.emitError(loc) << "expected string";
631-
return Attribute();
632-
}
633-
if (parser.parseGreater())
634-
return Attribute();
635-
636-
return get(parser.getContext(), value);
637-
}
638-
639-
void emitc::OpaqueAttr::print(AsmPrinter &printer) const {
640-
printer << "<\"";
641-
llvm::printEscapedString(getValue(), printer.getStream());
642-
printer << "\">";
643-
}
644-
645624
//===----------------------------------------------------------------------===//
646625
// EmitC Types
647626
//===----------------------------------------------------------------------===//
@@ -653,27 +632,15 @@ void emitc::OpaqueAttr::print(AsmPrinter &printer) const {
653632
// OpaqueType
654633
//===----------------------------------------------------------------------===//
655634

656-
Type emitc::OpaqueType::parse(AsmParser &parser) {
657-
if (parser.parseLess())
658-
return Type();
659-
std::string value;
660-
SMLoc loc = parser.getCurrentLocation();
661-
if (parser.parseOptionalString(&value) || value.empty()) {
662-
parser.emitError(loc) << "expected non empty string in !emitc.opaque type";
663-
return Type();
635+
LogicalResult mlir::emitc::OpaqueType::verify(
636+
llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
637+
llvm::StringRef value) {
638+
if (value.empty()) {
639+
return emitError() << "expected non empty string in !emitc.opaque type";
664640
}
665641
if (value.back() == '*') {
666-
parser.emitError(loc) << "pointer not allowed as outer type with "
667-
"!emitc.opaque, use !emitc.ptr instead";
668-
return Type();
642+
return emitError() << "pointer not allowed as outer type with "
643+
"!emitc.opaque, use !emitc.ptr instead";
669644
}
670-
if (parser.parseGreater())
671-
return Type();
672-
return get(parser.getContext(), value);
673-
}
674-
675-
void emitc::OpaqueType::print(AsmPrinter &printer) const {
676-
printer << "<\"";
677-
llvm::printEscapedString(getValue(), printer.getStream());
678-
printer << "\">";
645+
return success();
679646
}

0 commit comments

Comments
 (0)