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

Billy Zhu llvmlistbot at llvm.org
Sat Nov 25 17:06:52 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");
+    }
----------------
zyx-billy wrote:

Thanks! Looks like the new DIExpressionElemAttr is a neat approach for this. We can continue the discussion there.

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


More information about the Mlir-commits mailing list