[Mlir-commits] [mlir] [mlir] Add support for DIGlobalVariable and DIGlobalVariableExpression (PR #73367)

Justin Wilson llvmlistbot at llvm.org
Fri Nov 24 18:08:28 PST 2023


================
@@ -109,3 +123,93 @@ bool MemoryEffectsAttr::isReadWrite() {
     return false;
   return true;
 }
+
+//===----------------------------------------------------------------------===//
+// DIExpression
+//===----------------------------------------------------------------------===//
+
+::mlir::Attribute DIExpressionAttr::parse(AsmParser &parser, Type type) {
+  SmallVector<uint64_t, 1> operations;
+
+  // Begin parsing the attribute
+  if (parser.parseLess()) {
+    parser.emitError(parser.getNameLoc()) << "expected `<`";
+    return {};
+  }
+
+  if (parser.parseLSquare()) {
+    parser.emitError(parser.getNameLoc()) << "expected `[`";
+    return {};
+  }
+
+  // Begin parsing the operations list.
+  while (true) {
+    uint64_t op = 0;
+
+    // Attempt to parse the operation as an integer first.
+    if (OptionalParseResult parsedInteger = parser.parseOptionalInteger(op);
+        parsedInteger.has_value() && !failed(*parsedInteger)) {
+      operations.push_back(op);
+    } else {
+      // Fallback to parsing an operation in its textual form
+      std::string opName;
+      if (OptionalParseResult parsedName = parser.parseOptionalString(&opName);
+          parsedName.has_value() && !failed(*parsedName)) {
+        // Translate the string into the respective operation integer value.
+        op = StringSwitch<uint64_t>(opName)
+#define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) .Case(#NAME, ID)
+#include "llvm/BinaryFormat/Dwarf.def"
+                 .Default(0);
+      }
+    }
+
+    // Was an operation value parsed?
+    if (op == 0) {
+      parser.emitError(parser.getNameLoc())
+          << "expected valid expression operator";
+      return {};
+    }
+
+    // Values are expected to comma-separated.
+    if (succeeded(parser.parseOptionalComma())) {
+      // Begin parsing the next operation.
+      continue;
+    }
+
+    // If no comma is parsed, then `]` is expected to end the operations list.
+    if (failed(parser.parseOptionalRSquare())) {
+      parser.emitError(parser.getNameLoc()) << "expected `]`";
+      return {};
+    }
+
+    // Stop parsing the operation list.
+    break;
+  }
+
+  // Finish parsing the attribute.
+  if (parser.parseGreater()) {
+    parser.emitError(parser.getNameLoc()) << "expected `>`";
+    return {};
+  }
+  return DIExpressionAttr::get(parser.getContext(), operations);
+}
+
+void DIExpressionAttr::print(AsmPrinter &printer) const {
+  const auto operations = getOperations();
+  printer << "<[";
+  for (size_t i = 0; i < operations.size(); i++) {
+    switch (operations[i]) {
+#define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR)                                \
+  case ID:                                                                     \
+    printer << #NAME;                                                          \
+    break;
+#include "llvm/BinaryFormat/Dwarf.def"
+    default:
+      assert(false && "unhandled expression operation");
+    }
----------------
waj334 wrote:

I'll add more smarts here. I just need to add a method to query how many args specific ops have (assuming none are variadic). Then it'll be easy to do verification and such.

https://github.com/llvm/llvm-project/pull/73367


More information about the Mlir-commits mailing list