13 #ifndef MLIR_PARSER_PARSER_H
14 #define MLIR_PARSER_PARSER_H
37 template <
typename ContainerOpT>
42 if (llvm::hasSingleElement(*parsedBlock)) {
43 if (ContainerOpT op = dyn_cast<ContainerOpT>(&parsedBlock->
front())) {
51 if constexpr (std::is_same_v<ContainerOpT, Operation *>) {
54 <<
"source must contain a single top-level operation, found: "
59 ContainerOpT::template hasTrait<OpTrait::OneRegion>() &&
60 (ContainerOpT::template hasTrait<OpTrait::NoTerminator>() ||
61 OpTrait::template hasSingleBlockImplicitTerminator<
62 ContainerOpT>::value),
63 "Expected `ContainerOpT` to have a single region with a single "
64 "block that has an implicit terminator or does not require one");
67 ContainerOpT op = builder.
create<ContainerOpT>(sourceFileLoc);
69 assert(op->getNumRegions() == 1 &&
70 llvm::hasSingleElement(op->getRegion(0)) &&
71 "expected generated operation to have a single region with a single "
79 if (failed(op.verifyInvariants()))
93 LogicalResult
parseSourceFile(
const llvm::SourceMgr &sourceMgr, Block *block,
94 const ParserConfig &
config,
95 LocationAttr *sourceFileLoc =
nullptr);
101 LogicalResult
parseSourceFile(
const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
102 Block *block,
const ParserConfig &
config,
103 LocationAttr *sourceFileLoc =
nullptr);
113 const ParserConfig &
config,
114 LocationAttr *sourceFileLoc =
nullptr);
124 llvm::SourceMgr &sourceMgr, Block *block,
125 const ParserConfig &
config,
126 LocationAttr *sourceFileLoc =
nullptr);
133 const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
134 Block *block,
const ParserConfig &
config,
135 LocationAttr *sourceFileLoc =
nullptr);
147 const ParserConfig &
config,
148 StringRef sourceName =
"",
149 LocationAttr *sourceFileLoc =
nullptr);
154 template <
typename ContainerOpT,
typename... ParserArgs>
156 ParserArgs &&...args) {
162 return detail::constructContainerOpForParserIfNecessary<ContainerOpT>(
163 &block,
config.getContext(), sourceFileLoc);
175 template <
typename ContainerOpT = Operation *>
176 inline OwningOpRef<ContainerOpT>
178 return detail::parseSourceFile<ContainerOpT>(
config, sourceMgr);
185 template <
typename ContainerOpT = Operation *>
186 inline OwningOpRef<ContainerOpT>
189 return detail::parseSourceFile<ContainerOpT>(
config, sourceMgr);
200 template <
typename ContainerOpT = Operation *>
203 return detail::parseSourceFile<ContainerOpT>(
config, filename);
214 template <
typename ContainerOpT = Operation *>
216 llvm::SourceMgr &sourceMgr,
218 return detail::parseSourceFile<ContainerOpT>(
config, filename, sourceMgr);
225 template <
typename ContainerOpT = Operation *>
226 inline OwningOpRef<ContainerOpT>
228 const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
230 return detail::parseSourceFile<ContainerOpT>(
config, filename, sourceMgr);
244 template <
typename ContainerOpT = Operation *>
247 StringRef sourceName =
"") {
253 return detail::constructContainerOpForParserIfNecessary<ContainerOpT>(
254 &block,
config.getContext(), sourceFileLoc);
Block represents an ordered list of Operations.
OpListType & getOperations()
Location objects represent source locations information in MLIR.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
This class helps build Operations.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
This class acts as an owning reference to an op, and will automatically destroy the held op on destru...
This class represents a configuration for the MLIR assembly parser.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
OwningOpRef< ContainerOpT > constructContainerOpForParserIfNecessary(Block *parsedBlock, MLIRContext *context, Location sourceFileLoc)
Given a block containing operations that have just been parsed, if the block contains a single operat...
OwningOpRef< ContainerOpT > parseSourceFile(const ParserConfig &config, ParserArgs &&...args)
The internal implementation of the templated parseSourceFile methods below, that simply forwards to t...
Include the generated interface declarations.
const FrozenRewritePatternSet GreedyRewriteConfig config
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
LogicalResult parseSourceString(llvm::StringRef sourceStr, Block *block, const ParserConfig &config, StringRef sourceName="", LocationAttr *sourceFileLoc=nullptr)
This parses the IR string and appends parsed operations to the given block.
LogicalResult parseSourceFile(const llvm::SourceMgr &sourceMgr, Block *block, const ParserConfig &config, LocationAttr *sourceFileLoc=nullptr)
This parses the file specified by the indicated SourceMgr and appends parsed operations to the given ...