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

Justin Wilson llvmlistbot at llvm.org
Tue Nov 28 06:46:35 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) {
----------------
waj334 wrote:

I can use that as long as you are ok with an accumulator (size_t i) sitting outside of the lambda. I need to know the current position in the operands to decide whether not to parse as a keyword for `DW_OP_LLVM_convert`

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


More information about the Mlir-commits mailing list