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

Christian Ulmann llvmlistbot at llvm.org
Mon Nov 27 23:03:12 PST 2023


================
@@ -109,3 +125,94 @@ bool MemoryEffectsAttr::isReadWrite() {
     return false;
   return true;
 }
+
+//===----------------------------------------------------------------------===//
+// DIExpression
+//===----------------------------------------------------------------------===//
+
+DIExpressionAttr DIExpressionAttr::get(MLIRContext *context) {
+  return get(context, ArrayRef<DIExpressionElemAttr>({}));
+}
+
+/// Parse DWARF expression arguments with respect to the DWARF operation
+/// opcode. Some DWARF expression operations have a specific number of operands
+/// and may appear in a textual form.
+LogicalResult parseExpressionArg(AsmParser &parser, uint64_t opcode,
+                                 SmallVector<uint64_t> &args) {
+  // The number of operands must be known for each specific operations
+  // in order to parse operations in their respective textual forms.
+  const llvm::DIExpression::ExprOperand opInfo(&opcode);
+  size_t numOperands = opInfo.getNumArgs();
+
+  for (size_t i = 0; i < numOperands; ++i) {
+    uint64_t operand = 0;
+
+    // Parse an operand.
+    if (i > 0 && opcode == llvm::dwarf::DW_OP_LLVM_convert) {
+      // Attempt to parse an keyword.
+      StringRef keyword;
+      if (succeeded(parser.parseOptionalKeyword(&keyword))) {
+        operand = llvm::dwarf::getAttributeEncoding(keyword);
+        if (operand == 0) {
+          // The keyword is invalid.
+          return parser.emitError(parser.getCurrentLocation())
+                 << "encountered unknown attribute encoding \"" << keyword
+                 << "\"";
+        }
+      }
+    }
+
+    // operand should be non-zero if a keyword was parsed. Otherwise, the
+    // operand MUST be an integer.
+    if (operand == 0) {
+      // Parse the next operand as an integer
+      if (parser.parseInteger(operand)) {
+        return parser.emitError(parser.getCurrentLocation())
+               << "expected operand #" << i;
+      }
+    }
+
+    // NOTE: A few operations are variadic.
+    if (i == 0 && (opcode == llvm::dwarf::DW_OP_LLVM_entry_value ||
+                   opcode == llvm::dwarf::DW_OP_LLVM_arg)) {
+      // This operand represents the number of follwing operands. Add to the
+      // current number of operands.
+      numOperands += operand;
+    }
+    args.push_back(operand);
+
+    if (i + 1 != numOperands) {
+      if (parser.parseComma()) {
+        return parser.emitError(parser.getCurrentLocation())
+               << "expected `,` following operand #" << i;
+      }
+    }
+  }
+  return success();
+}
+
+/// Print DWARF expression arguments with respect to the specific DWARF
+/// operation. Some operands are printed in their textual form.
+LogicalResult printExpressionArg(AsmPrinter &printer, uint64_t opcode,
+                                 ArrayRef<uint64_t> args) {
+  for (size_t i = 0; i < args.size(); i++) {
+    const auto &operand = args[i];
+    if (i > 0 && opcode == llvm::dwarf::DW_OP_LLVM_convert) {
+      if (auto keyword = llvm::dwarf::AttributeEncodingString(operand);
+          !keyword.empty()) {
+        printer << keyword;
+      } else {
+        // Fallback to printing the operand as an integer.
+        printer << operand;
+      }
+    } else {
+      // All operands are expected to be printed as integers.
+      printer << operand;
+    }
+
+    if (i + 1 != args.size()) {
----------------
Dinistro wrote:

`llvm::interleveComma` can be of help here.

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


More information about the Mlir-commits mailing list