[Mlir-commits] [mlir] ee1cf1f - [mlir][NFC] Simplify the various `parseSourceFile<T>` overloads

River Riddle llvmlistbot at llvm.org
Thu Jun 2 19:25:09 PDT 2022


Author: River Riddle
Date: 2022-06-02T19:18:55-07:00
New Revision: ee1cf1f64519c815025d962bdf9c9bb3d09d7699

URL: https://github.com/llvm/llvm-project/commit/ee1cf1f64519c815025d962bdf9c9bb3d09d7699
DIFF: https://github.com/llvm/llvm-project/commit/ee1cf1f64519c815025d962bdf9c9bb3d09d7699.diff

LOG: [mlir][NFC] Simplify the various `parseSourceFile<T>` overloads

These effectively all share the same implementation, i.e. forward
to the non-templated overload and then construct the container op.

Added: 
    

Modified: 
    mlir/include/mlir/Parser/Parser.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Parser/Parser.h b/mlir/include/mlir/Parser/Parser.h
index ae59ea2896c72..52b7b49e2a7b8 100644
--- a/mlir/include/mlir/Parser/Parser.h
+++ b/mlir/include/mlir/Parser/Parser.h
@@ -126,6 +126,22 @@ LogicalResult parseSourceString(llvm::StringRef sourceStr, Block *block,
                                 MLIRContext *context,
                                 LocationAttr *sourceFileLoc = nullptr);
 
+namespace detail {
+/// The internal implementation of the templated `parseSourceFile` methods
+/// below, that simply forwards to the non-templated version.
+template <typename ContainerOpT, typename... ParserArgs>
+inline OwningOpRef<ContainerOpT> parseSourceFile(MLIRContext *ctx,
+                                                 ParserArgs &&...args) {
+  LocationAttr sourceFileLoc;
+  Block block;
+  if (failed(parseSourceFile(std::forward<ParserArgs>(args)..., &block, ctx,
+                             &sourceFileLoc)))
+    return OwningOpRef<ContainerOpT>();
+  return detail::constructContainerOpForParserIfNecessary<ContainerOpT>(
+      &block, ctx, sourceFileLoc);
+}
+} // namespace detail
+
 /// This parses the file specified by the indicated SourceMgr. If the source IR
 /// contained a single instance of `ContainerOpT`, it is returned. Otherwise, a
 /// new instance of `ContainerOpT` is constructed containing all of the parsed
@@ -137,12 +153,7 @@ LogicalResult parseSourceString(llvm::StringRef sourceStr, Block *block,
 template <typename ContainerOpT>
 inline OwningOpRef<ContainerOpT>
 parseSourceFile(const llvm::SourceMgr &sourceMgr, MLIRContext *context) {
-  LocationAttr sourceFileLoc;
-  Block block;
-  if (failed(parseSourceFile(sourceMgr, &block, context, &sourceFileLoc)))
-    return OwningOpRef<ContainerOpT>();
-  return detail::constructContainerOpForParserIfNecessary<ContainerOpT>(
-      &block, context, sourceFileLoc);
+  return detail::parseSourceFile<ContainerOpT>(context, sourceMgr);
 }
 
 /// This parses the file specified by the indicated filename. If the source IR
@@ -154,14 +165,9 @@ parseSourceFile(const llvm::SourceMgr &sourceMgr, MLIRContext *context) {
 /// containing a single block, and must implement the
 /// `SingleBlockImplicitTerminator` trait.
 template <typename ContainerOpT>
-inline OwningOpRef<ContainerOpT> parseSourceFile(llvm::StringRef filename,
+inline OwningOpRef<ContainerOpT> parseSourceFile(StringRef filename,
                                                  MLIRContext *context) {
-  LocationAttr sourceFileLoc;
-  Block block;
-  if (failed(parseSourceFile(filename, &block, context, &sourceFileLoc)))
-    return OwningOpRef<ContainerOpT>();
-  return detail::constructContainerOpForParserIfNecessary<ContainerOpT>(
-      &block, context, sourceFileLoc);
+  return detail::parseSourceFile<ContainerOpT>(context, filename);
 }
 
 /// This parses the file specified by the indicated filename using the provided
@@ -176,13 +182,7 @@ template <typename ContainerOpT>
 inline OwningOpRef<ContainerOpT> parseSourceFile(llvm::StringRef filename,
                                                  llvm::SourceMgr &sourceMgr,
                                                  MLIRContext *context) {
-  LocationAttr sourceFileLoc;
-  Block block;
-  if (failed(parseSourceFile(filename, sourceMgr, &block, context,
-                             &sourceFileLoc)))
-    return OwningOpRef<ContainerOpT>();
-  return detail::constructContainerOpForParserIfNecessary<ContainerOpT>(
-      &block, context, sourceFileLoc);
+  return detail::parseSourceFile<ContainerOpT>(context, filename, sourceMgr);
 }
 
 /// This parses the provided string containing MLIR. If the source IR contained


        


More information about the Mlir-commits mailing list