Skip to content

Commit 241a2ba

Browse files
Simon Camphausenmgehre-amd
authored andcommitted
[mlir][EmitC] Use declarative assembly format for opaque types and attributes (llvm#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 864aa1d commit 241a2ba

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
@@ -90,7 +90,8 @@ def EmitC_OpaqueType : EmitC_Type<"Opaque", "opaque"> {
9090
}];
9191

9292
let parameters = (ins StringRefParameter<"the opaque value">:$value);
93-
let hasCustomAssemblyFormat = 1;
93+
let assemblyFormat = "`<` $value `>`";
94+
let genVerifyDecl = 1;
9495
}
9596

9697
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
@@ -636,27 +636,6 @@ LogicalResult emitc::YieldOp::verify() {
636636
#define GET_ATTRDEF_CLASSES
637637
#include "mlir/Dialect/EmitC/IR/EmitCAttributes.cpp.inc"
638638

639-
Attribute emitc::OpaqueAttr::parse(AsmParser &parser, Type type) {
640-
if (parser.parseLess())
641-
return Attribute();
642-
std::string value;
643-
SMLoc loc = parser.getCurrentLocation();
644-
if (parser.parseOptionalString(&value)) {
645-
parser.emitError(loc) << "expected string";
646-
return Attribute();
647-
}
648-
if (parser.parseGreater())
649-
return Attribute();
650-
651-
return get(parser.getContext(), value);
652-
}
653-
654-
void emitc::OpaqueAttr::print(AsmPrinter &printer) const {
655-
printer << "<\"";
656-
llvm::printEscapedString(getValue(), printer.getStream());
657-
printer << "\">";
658-
}
659-
660639
//===----------------------------------------------------------------------===//
661640
// EmitC Types
662641
//===----------------------------------------------------------------------===//
@@ -731,27 +710,15 @@ emitc::ArrayType::cloneWith(std::optional<ArrayRef<int64_t>> shape,
731710
// OpaqueType
732711
//===----------------------------------------------------------------------===//
733712

734-
Type emitc::OpaqueType::parse(AsmParser &parser) {
735-
if (parser.parseLess())
736-
return Type();
737-
std::string value;
738-
SMLoc loc = parser.getCurrentLocation();
739-
if (parser.parseOptionalString(&value) || value.empty()) {
740-
parser.emitError(loc) << "expected non empty string in !emitc.opaque type";
741-
return Type();
713+
LogicalResult mlir::emitc::OpaqueType::verify(
714+
llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
715+
llvm::StringRef value) {
716+
if (value.empty()) {
717+
return emitError() << "expected non empty string in !emitc.opaque type";
742718
}
743719
if (value.back() == '*') {
744-
parser.emitError(loc) << "pointer not allowed as outer type with "
745-
"!emitc.opaque, use !emitc.ptr instead";
746-
return Type();
720+
return emitError() << "pointer not allowed as outer type with "
721+
"!emitc.opaque, use !emitc.ptr instead";
747722
}
748-
if (parser.parseGreater())
749-
return Type();
750-
return get(parser.getContext(), value);
751-
}
752-
753-
void emitc::OpaqueType::print(AsmPrinter &printer) const {
754-
printer << "<\"";
755-
llvm::printEscapedString(getValue(), printer.getStream());
756-
printer << "\">";
723+
return success();
757724
}

0 commit comments

Comments
 (0)