Skip to content

Commit c60b897

Browse files
committed
[mlir] Refactor the Parser library in preparation for an MLIR binary format
The current Parser library is solely focused on providing API for the textual MLIR format, but MLIR will soon also provide a binary format. This commit renames the current Parser library to AsmParser to better correspond to what the library is actually intended for. A new Parser library is added which will act as a unified parser interface between both text and binary formats. Most parser clients are unaffected, given that the unified interface is essentially the same as the current interface. Only clients that rely on utilizing the AsmParserState, or those that want to parse Attributes/Types need to be updated to point to the AsmParser library. Differential Revision: https://reviews.llvm.org/D129605
1 parent 5ae0472 commit c60b897

33 files changed

+2779
-2705
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//===- AsmParser.h - MLIR AsmParser Library Interface -----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file is contains the interface to the MLIR assembly parser library.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_ASMPARSER_ASMPARSER_H
14+
#define MLIR_ASMPARSER_ASMPARSER_H
15+
16+
#include "mlir/IR/AsmState.h"
17+
#include <cstddef>
18+
19+
namespace llvm {
20+
class SourceMgr;
21+
class StringRef;
22+
} // namespace llvm
23+
24+
namespace mlir {
25+
class AsmParserState;
26+
class AsmParserCodeCompleteContext;
27+
28+
/// This parses the file specified by the indicated SourceMgr and appends parsed
29+
/// operations to the given block. If the block is non-empty, the operations are
30+
/// placed before the current terminator. If parsing is successful, success is
31+
/// returned. Otherwise, an error message is emitted through the error handler
32+
/// registered in the context, and failure is returned. If `sourceFileLoc` is
33+
/// non-null, it is populated with a file location representing the start of the
34+
/// source file that is being parsed. If `asmState` is non-null, it is populated
35+
/// with detailed information about the parsed IR (including exact locations for
36+
/// SSA uses and definitions). `asmState` should only be provided if this
37+
/// detailed information is desired. If `codeCompleteContext` is non-null, it is
38+
/// used to signal tracking of a code completion event (generally only ever
39+
/// useful for LSP or other high level language tooling).
40+
LogicalResult
41+
parseAsmSourceFile(const llvm::SourceMgr &sourceMgr, Block *block,
42+
const ParserConfig &config,
43+
AsmParserState *asmState = nullptr,
44+
AsmParserCodeCompleteContext *codeCompleteContext = nullptr);
45+
46+
/// This parses a single MLIR attribute to an MLIR context if it was valid. If
47+
/// not, an error message is emitted through a new SourceMgrDiagnosticHandler
48+
/// constructed from a new SourceMgr with a single a MemoryBuffer wrapping
49+
/// `attrStr`. If the passed `attrStr` has additional tokens that were not part
50+
/// of the type, an error is emitted.
51+
// TODO: Improve diagnostic reporting.
52+
Attribute parseAttribute(llvm::StringRef attrStr, MLIRContext *context);
53+
Attribute parseAttribute(llvm::StringRef attrStr, Type type);
54+
55+
/// This parses a single MLIR attribute to an MLIR context if it was valid. If
56+
/// not, an error message is emitted through a new SourceMgrDiagnosticHandler
57+
/// constructed from a new SourceMgr with a single a MemoryBuffer wrapping
58+
/// `attrStr`. The number of characters of `attrStr` parsed in the process is
59+
/// returned in `numRead`.
60+
Attribute parseAttribute(llvm::StringRef attrStr, MLIRContext *context,
61+
size_t &numRead);
62+
Attribute parseAttribute(llvm::StringRef attrStr, Type type, size_t &numRead);
63+
64+
/// This parses a single MLIR type to an MLIR context if it was valid. If not,
65+
/// an error message is emitted through a new SourceMgrDiagnosticHandler
66+
/// constructed from a new SourceMgr with a single a MemoryBuffer wrapping
67+
/// `typeStr`. If the passed `typeStr` has additional tokens that were not part
68+
/// of the type, an error is emitted.
69+
// TODO: Improve diagnostic reporting.
70+
Type parseType(llvm::StringRef typeStr, MLIRContext *context);
71+
72+
/// This parses a single MLIR type to an MLIR context if it was valid. If not,
73+
/// an error message is emitted through a new SourceMgrDiagnosticHandler
74+
/// constructed from a new SourceMgr with a single a MemoryBuffer wrapping
75+
/// `typeStr`. The number of characters of `typeStr` parsed in the process is
76+
/// returned in `numRead`.
77+
Type parseType(llvm::StringRef typeStr, MLIRContext *context, size_t &numRead);
78+
79+
/// This parses a single IntegerSet to an MLIR context if it was valid. If not,
80+
/// an error message is emitted through a new SourceMgrDiagnosticHandler
81+
/// constructed from a new SourceMgr with a single MemoryBuffer wrapping
82+
/// `str`. If the passed `str` has additional tokens that were not part of the
83+
/// IntegerSet, a failure is returned. Diagnostics are printed on failure if
84+
/// `printDiagnosticInfo` is true.
85+
IntegerSet parseIntegerSet(llvm::StringRef str, MLIRContext *context,
86+
bool printDiagnosticInfo = true);
87+
88+
} // namespace mlir
89+
90+
#endif // MLIR_ASMPARSER_ASMPARSER_H

mlir/include/mlir/Parser/AsmParserState.h renamed to mlir/include/mlir/AsmParser/AsmParserState.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef MLIR_PARSER_ASMPARSERSTATE_H
10-
#define MLIR_PARSER_ASMPARSERSTATE_H
9+
#ifndef MLIR_ASMPARSER_ASMPARSERSTATE_H
10+
#define MLIR_ASMPARSER_ASMPARSERSTATE_H
1111

1212
#include "mlir/Support/LLVM.h"
1313
#include "llvm/ADT/ArrayRef.h"
@@ -179,4 +179,4 @@ class AsmParserState {
179179

180180
} // namespace mlir
181181

182-
#endif // MLIR_PARSER_ASMPARSERSTATE_H
182+
#endif // MLIR_ASMPARSER_ASMPARSERSTATE_H

mlir/include/mlir/Parser/CodeComplete.h renamed to mlir/include/mlir/AsmParser/CodeComplete.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef MLIR_PARSER_CODECOMPLETE_H
10-
#define MLIR_PARSER_CODECOMPLETE_H
9+
#ifndef MLIR_ASMPARSER_CODECOMPLETE_H
10+
#define MLIR_ASMPARSER_CODECOMPLETE_H
1111

1212
#include "mlir/Support/LLVM.h"
1313
#include "llvm/ADT/StringMap.h"
@@ -75,4 +75,4 @@ class AsmParserCodeCompleteContext {
7575
};
7676
} // namespace mlir
7777

78-
#endif // MLIR_PARSER_CODECOMPLETE_H
78+
#endif // MLIR_ASMPARSER_CODECOMPLETE_H

mlir/include/mlir/IR/AsmState.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ class AsmResourcePrinter {
290290
//===----------------------------------------------------------------------===//
291291

292292
/// This class represents a configuration for the MLIR assembly parser. It
293-
/// contains all of the necessary state to parse a textual MLIR source file.
293+
/// contains all of the necessary state to parse a MLIR source file.
294294
class ParserConfig {
295295
public:
296296
ParserConfig(MLIRContext *context) : context(context) {

mlir/include/mlir/Parser/Parser.h

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file is contains the interface to the MLIR parser library.
9+
// This file is contains a unified interface for parsing serialized MLIR.
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -15,7 +15,7 @@
1515

1616
#include "mlir/IR/AsmState.h"
1717
#include "mlir/IR/Builders.h"
18-
#include "mlir/IR/BuiltinOps.h"
18+
#include "mlir/IR/OwningOpRef.h"
1919
#include <cstddef>
2020

2121
namespace llvm {
@@ -25,9 +25,6 @@ class StringRef;
2525
} // namespace llvm
2626

2727
namespace mlir {
28-
class AsmParserState;
29-
class AsmParserCodeCompleteContext;
30-
3128
namespace detail {
3229

3330
/// Given a block containing operations that have just been parsed, if the block
@@ -81,16 +78,10 @@ inline OwningOpRef<ContainerOpT> constructContainerOpForParserIfNecessary(
8178
/// returned. Otherwise, an error message is emitted through the error handler
8279
/// registered in the context, and failure is returned. If `sourceFileLoc` is
8380
/// non-null, it is populated with a file location representing the start of the
84-
/// source file that is being parsed. If `asmState` is non-null, it is populated
85-
/// with detailed information about the parsed IR (including exact locations for
86-
/// SSA uses and definitions). `asmState` should only be provided if this
87-
/// detailed information is desired. If `codeCompleteContext` is non-null, it is
88-
/// used to signal tracking of a code completion event (generally only ever
89-
/// useful for LSP or other high level language tooling).
90-
LogicalResult parseSourceFile(
91-
const llvm::SourceMgr &sourceMgr, Block *block, const ParserConfig &config,
92-
LocationAttr *sourceFileLoc = nullptr, AsmParserState *asmState = nullptr,
93-
AsmParserCodeCompleteContext *codeCompleteContext = nullptr);
81+
/// source file that is being parsed.
82+
LogicalResult parseSourceFile(const llvm::SourceMgr &sourceMgr, Block *block,
83+
const ParserConfig &config,
84+
LocationAttr *sourceFileLoc = nullptr);
9485

9586
/// This parses the file specified by the indicated filename and appends parsed
9687
/// operations to the given block. If the block is non-empty, the operations are
@@ -109,15 +100,11 @@ LogicalResult parseSourceFile(llvm::StringRef filename, Block *block,
109100
/// parsing is successful, success is returned. Otherwise, an error message is
110101
/// emitted through the error handler registered in the context, and failure is
111102
/// returned. If `sourceFileLoc` is non-null, it is populated with a file
112-
/// location representing the start of the source file that is being parsed. If
113-
/// `asmState` is non-null, it is populated with detailed information about the
114-
/// parsed IR (including exact locations for SSA uses and definitions).
115-
/// `asmState` should only be provided if this detailed information is desired.
103+
/// location representing the start of the source file that is being parsed.
116104
LogicalResult parseSourceFile(llvm::StringRef filename,
117105
llvm::SourceMgr &sourceMgr, Block *block,
118106
const ParserConfig &config,
119-
LocationAttr *sourceFileLoc = nullptr,
120-
AsmParserState *asmState = nullptr);
107+
LocationAttr *sourceFileLoc = nullptr);
121108

122109
/// This parses the IR string and appends parsed operations to the given block.
123110
/// If the block is non-empty, the operations are placed before the current
@@ -208,48 +195,6 @@ inline OwningOpRef<ContainerOpT> parseSourceString(llvm::StringRef sourceStr,
208195
&block, config.getContext(), sourceFileLoc);
209196
}
210197

211-
/// This parses a single MLIR attribute to an MLIR context if it was valid. If
212-
/// not, an error message is emitted through a new SourceMgrDiagnosticHandler
213-
/// constructed from a new SourceMgr with a single a MemoryBuffer wrapping
214-
/// `attrStr`. If the passed `attrStr` has additional tokens that were not part
215-
/// of the type, an error is emitted.
216-
// TODO: Improve diagnostic reporting.
217-
Attribute parseAttribute(llvm::StringRef attrStr, MLIRContext *context);
218-
Attribute parseAttribute(llvm::StringRef attrStr, Type type);
219-
220-
/// This parses a single MLIR attribute to an MLIR context if it was valid. If
221-
/// not, an error message is emitted through a new SourceMgrDiagnosticHandler
222-
/// constructed from a new SourceMgr with a single a MemoryBuffer wrapping
223-
/// `attrStr`. The number of characters of `attrStr` parsed in the process is
224-
/// returned in `numRead`.
225-
Attribute parseAttribute(llvm::StringRef attrStr, MLIRContext *context,
226-
size_t &numRead);
227-
Attribute parseAttribute(llvm::StringRef attrStr, Type type, size_t &numRead);
228-
229-
/// This parses a single MLIR type to an MLIR context if it was valid. If not,
230-
/// an error message is emitted through a new SourceMgrDiagnosticHandler
231-
/// constructed from a new SourceMgr with a single a MemoryBuffer wrapping
232-
/// `typeStr`. If the passed `typeStr` has additional tokens that were not part
233-
/// of the type, an error is emitted.
234-
// TODO: Improve diagnostic reporting.
235-
Type parseType(llvm::StringRef typeStr, MLIRContext *context);
236-
237-
/// This parses a single MLIR type to an MLIR context if it was valid. If not,
238-
/// an error message is emitted through a new SourceMgrDiagnosticHandler
239-
/// constructed from a new SourceMgr with a single a MemoryBuffer wrapping
240-
/// `typeStr`. The number of characters of `typeStr` parsed in the process is
241-
/// returned in `numRead`.
242-
Type parseType(llvm::StringRef typeStr, MLIRContext *context, size_t &numRead);
243-
244-
/// This parses a single IntegerSet to an MLIR context if it was valid. If not,
245-
/// an error message is emitted through a new SourceMgrDiagnosticHandler
246-
/// constructed from a new SourceMgr with a single MemoryBuffer wrapping
247-
/// `str`. If the passed `str` has additional tokens that were not part of the
248-
/// IntegerSet, a failure is returned. Diagnostics are printed on failure if
249-
/// `printDiagnosticInfo` is true.
250-
IntegerSet parseIntegerSet(llvm::StringRef str, MLIRContext *context,
251-
bool printDiagnosticInfo = true);
252-
253198
} // namespace mlir
254199

255200
#endif // MLIR_PARSER_PARSER_H

mlir/lib/Parser/AsmParserImpl.h renamed to mlir/lib/AsmParser/AsmParserImpl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef MLIR_LIB_PARSER_ASMPARSERIMPL_H
10-
#define MLIR_LIB_PARSER_ASMPARSERIMPL_H
9+
#ifndef MLIR_LIB_ASMPARSER_ASMPARSERIMPL_H
10+
#define MLIR_LIB_ASMPARSER_ASMPARSERIMPL_H
1111

1212
#include "Parser.h"
13+
#include "mlir/AsmParser/AsmParserState.h"
1314
#include "mlir/IR/Builders.h"
1415
#include "mlir/IR/OpImplementation.h"
15-
#include "mlir/Parser/AsmParserState.h"
1616

1717
namespace mlir {
1818
namespace detail {
@@ -566,4 +566,4 @@ class AsmParserImpl : public BaseT {
566566
} // namespace detail
567567
} // namespace mlir
568568

569-
#endif // MLIR_LIB_PARSER_ASMPARSERIMPL_H
569+
#endif // MLIR_LIB_ASMPARSER_ASMPARSERIMPL_H

mlir/lib/Parser/AsmParserState.cpp renamed to mlir/lib/AsmParser/AsmParserState.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "mlir/Parser/AsmParserState.h"
9+
#include "mlir/AsmParser/AsmParserState.h"
1010
#include "mlir/IR/Operation.h"
1111
#include "mlir/IR/SymbolTable.h"
1212
#include "llvm/ADT/StringExtras.h"

mlir/lib/Parser/AttributeParser.cpp renamed to mlir/lib/AsmParser/AttributeParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
#include "Parser.h"
1414

1515
#include "AsmParserImpl.h"
16+
#include "mlir/AsmParser/AsmParserState.h"
1617
#include "mlir/IR/AffineMap.h"
1718
#include "mlir/IR/BuiltinTypes.h"
1819
#include "mlir/IR/Dialect.h"
1920
#include "mlir/IR/DialectImplementation.h"
2021
#include "mlir/IR/IntegerSet.h"
21-
#include "mlir/Parser/AsmParserState.h"
2222
#include "llvm/ADT/StringExtras.h"
2323
#include "llvm/Support/Endian.h"
2424

mlir/lib/AsmParser/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
add_mlir_library(MLIRAsmParser
2+
AffineParser.cpp
3+
AsmParserState.cpp
4+
AttributeParser.cpp
5+
DialectSymbolParser.cpp
6+
Lexer.cpp
7+
LocationParser.cpp
8+
Parser.cpp
9+
Token.cpp
10+
TypeParser.cpp
11+
12+
ADDITIONAL_HEADER_DIRS
13+
${MLIR_MAIN_INCLUDE_DIR}/mlir/Parser
14+
15+
LINK_LIBS PUBLIC
16+
MLIRIR
17+
)

0 commit comments

Comments
 (0)