Skip to content

Commit

Permalink
[ODS/AsmParser] Don't pass MLIRContext with DialectAsmParser.
Browse files Browse the repository at this point in the history
The former is redundant because the later carries it as part of
its builder.  Add a getContext() helper method to DialectAsmParser
to make this more convenient, and stop passing the context around
explicitly.  This simplifies ODS generated parser hooks for attrs
and types.

This resolves PR51985

Recommit 4b32f8b after fixing a dependency.

Differential Revision: https://reviews.llvm.org/D110796
  • Loading branch information
lattner authored and joker-eph committed Sep 30, 2021
1 parent 33f4315 commit fb093c8
Show file tree
Hide file tree
Showing 27 changed files with 117 additions and 153 deletions.
70 changes: 25 additions & 45 deletions flang/lib/Optimizer/Dialect/FIRType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TYPE parseIntSingleton(mlir::DialectAsmParser &parser) {
int kind = 0;
if (parser.parseLess() || parser.parseInteger(kind) || parser.parseGreater())
return {};
return TYPE::get(parser.getBuilder().getContext(), kind);
return TYPE::get(parser.getContext(), kind);
}

template <typename TYPE>
Expand Down Expand Up @@ -117,8 +117,7 @@ mlir::Type fir::parseFirType(FIROpsDialect *dialect,
if (parser.parseKeyword(&typeTag))
return {};
mlir::Type genType;
auto parseResult = generatedTypeParser(parser.getBuilder().getContext(),
parser, typeTag, genType);
auto parseResult = generatedTypeParser(parser, typeTag, genType);
if (parseResult.hasValue())
return genType;
parser.emitError(parser.getNameLoc(), "unknown fir type: ") << typeTag;
Expand Down Expand Up @@ -259,12 +258,11 @@ bool fir::isa_unknown_size_box(mlir::Type t) {
//===----------------------------------------------------------------------===//

// `boxproc` `<` return-type `>`
mlir::Type BoxProcType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type BoxProcType::parse(mlir::DialectAsmParser &parser) {
mlir::Type ty;
if (parser.parseLess() || parser.parseType(ty) || parser.parseGreater())
return {};
return get(context, ty);
return get(parser.getContext(), ty);
}

void fir::BoxProcType::print(mlir::DialectAsmPrinter &printer) const {
Expand Down Expand Up @@ -293,8 +291,7 @@ static bool cannotBePointerOrHeapElementType(mlir::Type eleTy) {
//===----------------------------------------------------------------------===//

// `box` `<` type (',' affine-map)? `>`
mlir::Type fir::BoxType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::BoxType::parse(mlir::DialectAsmParser &parser) {
mlir::Type ofTy;
if (parser.parseLess() || parser.parseType(ofTy))
return {};
Expand Down Expand Up @@ -330,8 +327,7 @@ fir::BoxType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
// BoxCharType
//===----------------------------------------------------------------------===//

mlir::Type fir::BoxCharType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::BoxCharType::parse(mlir::DialectAsmParser &parser) {
return parseKindSingleton<fir::BoxCharType>(parser);
}

Expand All @@ -353,8 +349,7 @@ CharacterType fir::BoxCharType::getEleTy() const {
//===----------------------------------------------------------------------===//

// `char` `<` kind [`,` `len`] `>`
mlir::Type fir::CharacterType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::CharacterType::parse(mlir::DialectAsmParser &parser) {
int kind = 0;
if (parser.parseLess() || parser.parseInteger(kind))
return {};
Expand All @@ -368,7 +363,7 @@ mlir::Type fir::CharacterType::parse(mlir::MLIRContext *context,
}
if (parser.parseGreater())
return {};
return get(context, kind, len);
return get(parser.getContext(), kind, len);
}

void fir::CharacterType::print(mlir::DialectAsmPrinter &printer) const {
Expand All @@ -388,8 +383,7 @@ void fir::CharacterType::print(mlir::DialectAsmPrinter &printer) const {
// ComplexType
//===----------------------------------------------------------------------===//

mlir::Type fir::ComplexType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::ComplexType::parse(mlir::DialectAsmParser &parser) {
return parseKindSingleton<fir::ComplexType>(parser);
}

Expand All @@ -406,8 +400,7 @@ mlir::Type fir::ComplexType::getElementType() const {
//===----------------------------------------------------------------------===//

// `heap` `<` type `>`
mlir::Type fir::HeapType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::HeapType::parse(mlir::DialectAsmParser &parser) {
return parseTypeSingleton<HeapType>(parser);
}

Expand All @@ -429,8 +422,7 @@ fir::HeapType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
//===----------------------------------------------------------------------===//

// `int` `<` kind `>`
mlir::Type fir::IntegerType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::IntegerType::parse(mlir::DialectAsmParser &parser) {
return parseKindSingleton<fir::IntegerType>(parser);
}

Expand All @@ -443,8 +435,7 @@ void fir::IntegerType::print(mlir::DialectAsmPrinter &printer) const {
//===----------------------------------------------------------------------===//

// `logical` `<` kind `>`
mlir::Type fir::LogicalType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::LogicalType::parse(mlir::DialectAsmParser &parser) {
return parseKindSingleton<fir::LogicalType>(parser);
}

Expand All @@ -457,8 +448,7 @@ void fir::LogicalType::print(mlir::DialectAsmPrinter &printer) const {
//===----------------------------------------------------------------------===//

// `ptr` `<` type `>`
mlir::Type fir::PointerType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::PointerType::parse(mlir::DialectAsmParser &parser) {
return parseTypeSingleton<fir::PointerType>(parser);
}

Expand All @@ -479,8 +469,7 @@ mlir::LogicalResult fir::PointerType::verify(
//===----------------------------------------------------------------------===//

// `real` `<` kind `>`
mlir::Type fir::RealType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::RealType::parse(mlir::DialectAsmParser &parser) {
return parseKindSingleton<fir::RealType>(parser);
}

Expand All @@ -503,12 +492,11 @@ fir::RealType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
// `type` `<` name
// (`(` id `:` type (`,` id `:` type)* `)`)?
// (`{` id `:` type (`,` id `:` type)* `}`)? '>'
mlir::Type fir::RecordType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::RecordType::parse(mlir::DialectAsmParser &parser) {
llvm::StringRef name;
if (parser.parseLess() || parser.parseKeyword(&name))
return {};
RecordType result = RecordType::get(parser.getBuilder().getContext(), name);
RecordType result = RecordType::get(parser.getContext(), name);

RecordType::TypeList lenParamList;
if (!parser.parseOptionalLParen()) {
Expand Down Expand Up @@ -631,8 +619,7 @@ unsigned fir::RecordType::getFieldIndex(llvm::StringRef ident) {
//===----------------------------------------------------------------------===//

// `ref` `<` type `>`
mlir::Type fir::ReferenceType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::ReferenceType::parse(mlir::DialectAsmParser &parser) {
return parseTypeSingleton<fir::ReferenceType>(parser);
}

Expand All @@ -655,8 +642,7 @@ mlir::LogicalResult fir::ReferenceType::verify(

// `array` `<` `*` | bounds (`x` bounds)* `:` type (',' affine-map)? `>`
// bounds ::= `?` | int-lit
mlir::Type fir::SequenceType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::SequenceType::parse(mlir::DialectAsmParser &parser) {
if (parser.parseLess())
return {};
SequenceType::Shape shape;
Expand All @@ -675,7 +661,7 @@ mlir::Type fir::SequenceType::parse(mlir::MLIRContext *context,
parser.emitError(parser.getNameLoc(), "expecting affine map");
return {};
}
return SequenceType::get(context, shape, eleTy, map);
return SequenceType::get(parser.getContext(), shape, eleTy, map);
}

void fir::SequenceType::print(mlir::DialectAsmPrinter &printer) const {
Expand Down Expand Up @@ -745,8 +731,7 @@ mlir::LogicalResult fir::SequenceType::verify(
// ShapeType
//===----------------------------------------------------------------------===//

mlir::Type fir::ShapeType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::ShapeType::parse(mlir::DialectAsmParser &parser) {
return parseRankSingleton<fir::ShapeType>(parser);
}

Expand All @@ -758,8 +743,7 @@ void fir::ShapeType::print(mlir::DialectAsmPrinter &printer) const {
// ShapeShiftType
//===----------------------------------------------------------------------===//

mlir::Type fir::ShapeShiftType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::ShapeShiftType::parse(mlir::DialectAsmParser &parser) {
return parseRankSingleton<fir::ShapeShiftType>(parser);
}

Expand All @@ -771,8 +755,7 @@ void fir::ShapeShiftType::print(mlir::DialectAsmPrinter &printer) const {
// ShiftType
//===----------------------------------------------------------------------===//

mlir::Type fir::ShiftType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::ShiftType::parse(mlir::DialectAsmParser &parser) {
return parseRankSingleton<fir::ShiftType>(parser);
}

Expand All @@ -785,8 +768,7 @@ void fir::ShiftType::print(mlir::DialectAsmPrinter &printer) const {
//===----------------------------------------------------------------------===//

// `slice` `<` rank `>`
mlir::Type fir::SliceType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::SliceType::parse(mlir::DialectAsmParser &parser) {
return parseRankSingleton<fir::SliceType>(parser);
}

Expand All @@ -799,8 +781,7 @@ void fir::SliceType::print(mlir::DialectAsmPrinter &printer) const {
//===----------------------------------------------------------------------===//

// `tdesc` `<` type `>`
mlir::Type fir::TypeDescType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::TypeDescType::parse(mlir::DialectAsmParser &parser) {
return parseTypeSingleton<fir::TypeDescType>(parser);
}

Expand All @@ -824,8 +805,7 @@ mlir::LogicalResult fir::TypeDescType::verify(
//===----------------------------------------------------------------------===//

// `vector` `<` len `:` type `>`
mlir::Type fir::VectorType::parse(mlir::MLIRContext *context,
mlir::DialectAsmParser &parser) {
mlir::Type fir::VectorType::parse(mlir::DialectAsmParser &parser) {
int64_t len = 0;
mlir::Type eleTy;
if (parser.parseLess() || parser.parseInteger(len) || parser.parseColon() ||
Expand Down
3 changes: 2 additions & 1 deletion mlir/include/mlir/IR/OpImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "mlir/IR/OpDefinition.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/raw_ostream.h"

namespace mlir {

Expand Down Expand Up @@ -323,6 +322,8 @@ class AsmParser {
AsmParser() = default;
virtual ~AsmParser();

MLIRContext *getContext() const;

/// Return the location of the original name token.
virtual llvm::SMLoc getNameLoc() const = 0;

Expand Down
6 changes: 3 additions & 3 deletions mlir/lib/Dialect/Affine/IR/AffineOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3165,8 +3165,8 @@ static void deduplicateAndResolveOperands(
uniqueOperands.push_back(operand);
replacements.push_back(
kind == AffineExprKind::DimId
? getAffineDimExpr(pos, parser.getBuilder().getContext())
: getAffineSymbolExpr(pos, parser.getBuilder().getContext()));
? getAffineDimExpr(pos, parser.getContext())
: getAffineSymbolExpr(pos, parser.getContext()));
}
}
}
Expand Down Expand Up @@ -3276,7 +3276,7 @@ static ParseResult parseAffineMapWithMinMax(OpAsmParser &parser,

Builder &builder = parser.getBuilder();
auto flatMap = AffineMap::get(totalNumDims, totalNumSyms, flatExprs,
parser.getBuilder().getContext());
parser.getContext());
flatMap = flatMap.replaceDimsAndSymbols(
dimRplacements, symRepacements, dimOperands.size(), symOperands.size());

Expand Down
3 changes: 1 addition & 2 deletions mlir/lib/Dialect/ArmSVE/IR/ArmSVEDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ Type ArmSVEDialect::parseType(DialectAsmParser &parser) const {
llvm::SMLoc typeLoc = parser.getCurrentLocation();
{
Type genType;
auto parseResult = generatedTypeParser(parser.getBuilder().getContext(),
parser, "vector", genType);
auto parseResult = generatedTypeParser(parser, "vector", genType);
if (parseResult.hasValue())
return genType;
}
Expand Down
5 changes: 2 additions & 3 deletions mlir/lib/Dialect/Async/IR/Async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ void ValueType::print(DialectAsmPrinter &printer) const {
printer << '>';
}

Type ValueType::parse(mlir::MLIRContext *, mlir::DialectAsmParser &parser) {
Type ValueType::parse(mlir::DialectAsmParser &parser) {
Type ty;
if (parser.parseLess() || parser.parseType(ty) || parser.parseGreater()) {
parser.emitError(parser.getNameLoc(), "failed to parse async value type");
Expand All @@ -366,8 +366,7 @@ Type AsyncDialect::parseType(DialectAsmParser &parser) const {
if (parser.parseKeyword(&typeTag))
return Type();
Type genType;
auto parseResult = generatedTypeParser(parser.getBuilder().getContext(),
parser, typeTag, genType);
auto parseResult = generatedTypeParser(parser, typeTag, genType);
if (parseResult.hasValue())
return genType;
parser.emitError(parser.getNameLoc(), "unknown async type: ") << typeTag;
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/DLTI/DLTI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ DataLayoutSpecAttr DataLayoutSpecAttr::parse(DialectAsmParser &parser) {

// Empty spec.
if (succeeded(parser.parseOptionalGreater()))
return get(parser.getBuilder().getContext(), {});
return get(parser.getContext(), {});

SmallVector<DataLayoutEntryInterface> entries;
do {
Expand All @@ -295,7 +295,7 @@ DataLayoutSpecAttr DataLayoutSpecAttr::parse(DialectAsmParser &parser) {
if (failed(parser.parseGreater()))
return {};
return getChecked([&] { return parser.emitError(parser.getNameLoc()); },
parser.getBuilder().getContext(), entries);
parser.getContext(), entries);
}

void DataLayoutSpecAttr::print(DialectAsmPrinter &os) const {
Expand Down
15 changes: 7 additions & 8 deletions mlir/lib/Dialect/EmitC/IR/EmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ static ParseResult parseIncludeOp(OpAsmParser &parser, OperationState &result) {

if (standardInclude)
result.addAttribute("is_standard_include",
UnitAttr::get(parser.getBuilder().getContext()));
UnitAttr::get(parser.getContext()));

return success();
}
Expand All @@ -166,8 +166,7 @@ static ParseResult parseIncludeOp(OpAsmParser &parser, OperationState &result) {
#define GET_ATTRDEF_CLASSES
#include "mlir/Dialect/EmitC/IR/EmitCAttributes.cpp.inc"

Attribute emitc::OpaqueAttr::parse(MLIRContext *context,
DialectAsmParser &parser, Type type) {
Attribute emitc::OpaqueAttr::parse(DialectAsmParser &parser, Type type) {
if (parser.parseLess())
return Attribute();
std::string value;
Expand All @@ -178,7 +177,7 @@ Attribute emitc::OpaqueAttr::parse(MLIRContext *context,
}
if (parser.parseGreater())
return Attribute();
return get(context, value);
return get(parser.getContext(), value);
}

Attribute EmitCDialect::parseAttribute(DialectAsmParser &parser,
Expand All @@ -189,7 +188,7 @@ Attribute EmitCDialect::parseAttribute(DialectAsmParser &parser,
return Attribute();
Attribute genAttr;
OptionalParseResult parseResult =
generatedAttributeParser(getContext(), parser, mnemonic, type, genAttr);
generatedAttributeParser(parser, mnemonic, type, genAttr);
if (parseResult.hasValue())
return genAttr;
parser.emitError(typeLoc, "unknown attribute in EmitC dialect");
Expand All @@ -214,7 +213,7 @@ void emitc::OpaqueAttr::print(DialectAsmPrinter &printer) const {
#define GET_TYPEDEF_CLASSES
#include "mlir/Dialect/EmitC/IR/EmitCTypes.cpp.inc"

Type emitc::OpaqueType::parse(MLIRContext *context, DialectAsmParser &parser) {
Type emitc::OpaqueType::parse(DialectAsmParser &parser) {
if (parser.parseLess())
return Type();
std::string value;
Expand All @@ -225,7 +224,7 @@ Type emitc::OpaqueType::parse(MLIRContext *context, DialectAsmParser &parser) {
}
if (parser.parseGreater())
return Type();
return get(context, value);
return get(parser.getContext(), value);
}

Type EmitCDialect::parseType(DialectAsmParser &parser) const {
Expand All @@ -235,7 +234,7 @@ Type EmitCDialect::parseType(DialectAsmParser &parser) const {
return Type();
Type genType;
OptionalParseResult parseResult =
generatedTypeParser(getContext(), parser, mnemonic, genType);
generatedTypeParser(parser, mnemonic, genType);
if (parseResult.hasValue())
return genType;
parser.emitError(typeLoc, "unknown type in EmitC dialect");
Expand Down
Loading

0 comments on commit fb093c8

Please sign in to comment.