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

Billy Zhu llvmlistbot at llvm.org
Fri Nov 24 17:36:45 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:

It looks like this only tries to print elements as ops not arguments? Is more op-awareness in the plans? That way args can be printed as numbers and the entire expression can also be verified.
For now, should this default case print the number instead of asserting, since non-op numbers may appear?

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


More information about the Mlir-commits mailing list