[Mlir-commits] [mlir] 52a479d - [mlir][ods] Store the pointer to the anchor element (NFC)

Jeff Niu llvmlistbot at llvm.org
Tue Sep 20 11:08:06 PDT 2022


Author: Jeff Niu
Date: 2022-09-20T11:07:47-07:00
New Revision: 52a479de607d2e03ccdd221ac8d4c6422a3f0703

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

LOG: [mlir][ods] Store the pointer to the anchor element (NFC)

Instead of its index. There is no benefit to storing the index instead
of the pointer.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D133805

Added: 
    

Modified: 
    mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp
    mlir/tools/mlir-tblgen/FormatGen.cpp
    mlir/tools/mlir-tblgen/FormatGen.h
    mlir/tools/mlir-tblgen/OpFormatGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp
index 7449b108fa4d2..7ccaccef8a5c8 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp
@@ -917,9 +917,9 @@ class DefFormatParser : public FormatParser {
   verifyCustomDirectiveArguments(SMLoc loc,
                                  ArrayRef<FormatElement *> arguments) override;
   /// Verify the elements of an optional group.
-  LogicalResult
-  verifyOptionalGroupElements(SMLoc loc, ArrayRef<FormatElement *> elements,
-                              Optional<unsigned> anchorIndex) override;
+  LogicalResult verifyOptionalGroupElements(SMLoc loc,
+                                            ArrayRef<FormatElement *> elements,
+                                            FormatElement *anchor) override;
 
   /// Parse an attribute or type variable.
   FailureOr<FormatElement *> parseVariableImpl(SMLoc loc, StringRef name,
@@ -989,7 +989,7 @@ LogicalResult DefFormatParser::verifyCustomDirectiveArguments(
 LogicalResult
 DefFormatParser::verifyOptionalGroupElements(llvm::SMLoc loc,
                                              ArrayRef<FormatElement *> elements,
-                                             Optional<unsigned> anchorIndex) {
+                                             FormatElement *anchor) {
   // `params` and `struct` directives are allowed only if all the contained
   // parameters are optional.
   for (FormatElement *el : elements) {
@@ -1011,8 +1011,8 @@ DefFormatParser::verifyOptionalGroupElements(llvm::SMLoc loc,
     }
   }
   // The anchor must be a parameter or one of the aforementioned directives.
-  if (anchorIndex && !isa<ParameterElement, ParamsDirective, StructDirective>(
-                         elements[*anchorIndex])) {
+  if (anchor &&
+      !isa<ParameterElement, ParamsDirective, StructDirective>(anchor)) {
     return emitError(loc,
                      "optional group anchor must be a parameter or directive");
   }

diff  --git a/mlir/tools/mlir-tblgen/FormatGen.cpp b/mlir/tools/mlir-tblgen/FormatGen.cpp
index 029293265a9eb..84dd03d28ac9f 100644
--- a/mlir/tools/mlir-tblgen/FormatGen.cpp
+++ b/mlir/tools/mlir-tblgen/FormatGen.cpp
@@ -320,17 +320,17 @@ FailureOr<FormatElement *> FormatParser::parseOptionalGroup(Context ctx) {
 
   // Parse the child elements for this optional group.
   std::vector<FormatElement *> thenElements, elseElements;
-  Optional<unsigned> anchorIndex;
+  FormatElement *anchor = nullptr;
   do {
     FailureOr<FormatElement *> element = parseElement(TopLevelContext);
     if (failed(element))
       return failure();
     // Check for an anchor.
     if (curToken.is(FormatToken::caret)) {
-      if (anchorIndex)
+      if (anchor)
         return emitError(curToken.getLoc(), "only one element can be marked as "
                                             "the anchor of an optional group");
-      anchorIndex = thenElements.size();
+      anchor = *element;
       consumeToken();
     }
     thenElements.push_back(*element);
@@ -357,12 +357,12 @@ FailureOr<FormatElement *> FormatParser::parseOptionalGroup(Context ctx) {
     return failure();
 
   // The optional group is required to have an anchor.
-  if (!anchorIndex)
+  if (!anchor)
     return emitError(loc, "optional group has no anchor element");
 
   // Verify the child elements.
-  if (failed(verifyOptionalGroupElements(loc, thenElements, anchorIndex)) ||
-      failed(verifyOptionalGroupElements(loc, elseElements, llvm::None)))
+  if (failed(verifyOptionalGroupElements(loc, thenElements, anchor)) ||
+      failed(verifyOptionalGroupElements(loc, elseElements, nullptr)))
     return failure();
 
   // Get the first parsable element. It must be an element that can be
@@ -377,8 +377,7 @@ FailureOr<FormatElement *> FormatParser::parseOptionalGroup(Context ctx) {
 
   unsigned parseStart = std::distance(thenElements.begin(), parseBegin);
   return create<OptionalElement>(std::move(thenElements),
-                                 std::move(elseElements), *anchorIndex,
-                                 parseStart);
+                                 std::move(elseElements), anchor, parseStart);
 }
 
 FailureOr<FormatElement *> FormatParser::parseCustomDirective(SMLoc loc,

diff  --git a/mlir/tools/mlir-tblgen/FormatGen.h b/mlir/tools/mlir-tblgen/FormatGen.h
index cc57ff9ee8719..3230a591a0206 100644
--- a/mlir/tools/mlir-tblgen/FormatGen.h
+++ b/mlir/tools/mlir-tblgen/FormatGen.h
@@ -378,9 +378,9 @@ class OptionalElement : public FormatElementBase<FormatElement::Optional> {
   /// Create an optional group with the given child elements.
   OptionalElement(std::vector<FormatElement *> &&thenElements,
                   std::vector<FormatElement *> &&elseElements,
-                  unsigned anchorIndex, unsigned parseStart)
+                  FormatElement *anchor, unsigned parseStart)
       : thenElements(std::move(thenElements)),
-        elseElements(std::move(elseElements)), anchorIndex(anchorIndex),
+        elseElements(std::move(elseElements)), anchor(anchor),
         parseStart(parseStart) {}
 
   /// Return the `then` elements of the optional group.
@@ -390,7 +390,7 @@ class OptionalElement : public FormatElementBase<FormatElement::Optional> {
   ArrayRef<FormatElement *> getElseElements() const { return elseElements; }
 
   /// Return the anchor of the optional group.
-  FormatElement *getAnchor() const { return thenElements[anchorIndex]; }
+  FormatElement *getAnchor() const { return anchor; }
 
   /// Return the index of the first element to be parsed.
   unsigned getParseStart() const { return parseStart; }
@@ -400,9 +400,8 @@ class OptionalElement : public FormatElementBase<FormatElement::Optional> {
   std::vector<FormatElement *> thenElements;
   /// The child elements emitted when the anchor is not present.
   std::vector<FormatElement *> elseElements;
-  /// The index of the anchor element of the optional group within
-  /// `thenElements`.
-  unsigned anchorIndex;
+  /// The anchor element of the optional group.
+  FormatElement *anchor;
   /// The index of the first element that is parsed in `thenElements`. That is,
   /// the first non-whitespace element.
   unsigned parseStart;
@@ -496,7 +495,7 @@ class FormatParser {
   virtual LogicalResult
   verifyOptionalGroupElements(llvm::SMLoc loc,
                               ArrayRef<FormatElement *> elements,
-                              Optional<unsigned> anchorIndex) = 0;
+                              FormatElement *anchor) = 0;
 
   //===--------------------------------------------------------------------===//
   // Lexer Utilities

diff  --git a/mlir/tools/mlir-tblgen/OpFormatGen.cpp b/mlir/tools/mlir-tblgen/OpFormatGen.cpp
index d20901d52ede6..38a1ed35baf36 100644
--- a/mlir/tools/mlir-tblgen/OpFormatGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpFormatGen.cpp
@@ -2170,9 +2170,9 @@ class OpFormatParser : public FormatParser {
   verifyCustomDirectiveArguments(SMLoc loc,
                                  ArrayRef<FormatElement *> arguments) override;
   /// Verify the elements of an optional group.
-  LogicalResult
-  verifyOptionalGroupElements(SMLoc loc, ArrayRef<FormatElement *> elements,
-                              Optional<unsigned> anchorIndex) override;
+  LogicalResult verifyOptionalGroupElements(SMLoc loc,
+                                            ArrayRef<FormatElement *> elements,
+                                            FormatElement *anchor) override;
   LogicalResult verifyOptionalGroupElement(SMLoc loc, FormatElement *element,
                                            bool isAnchor);
 
@@ -3150,13 +3150,10 @@ OpFormatParser::parseTypeDirectiveOperand(SMLoc loc, bool isRefChild) {
   return element;
 }
 
-LogicalResult
-OpFormatParser::verifyOptionalGroupElements(SMLoc loc,
-                                            ArrayRef<FormatElement *> elements,
-                                            Optional<unsigned> anchorIndex) {
-  for (auto &it : llvm::enumerate(elements)) {
-    if (failed(verifyOptionalGroupElement(
-            loc, it.value(), anchorIndex && *anchorIndex == it.index())))
+LogicalResult OpFormatParser::verifyOptionalGroupElements(
+    SMLoc loc, ArrayRef<FormatElement *> elements, FormatElement *anchor) {
+  for (FormatElement *element : elements) {
+    if (failed(verifyOptionalGroupElement(loc, element, element == anchor)))
       return failure();
   }
   return success();


        


More information about the Mlir-commits mailing list