llvmlite icon indicating copy to clipboard operation
llvmlite copied to clipboard

Allow addition/change of fields after creation of debug info

Open varun-r-mallya opened this issue 2 months ago • 2 comments

Feature request

When we generate debug info for a module using .add_debug_info, updating it later, for example, to add a scope field to a DILocalVariable that was initially created with partial data, isn’t straightforward. The current workaround involves manually fetching the underlying tuple for that field, modifying it, and replacing it. It would be much cleaner if native functions existed to update such fields directly.

For example:

# Current workaround
var = module.add_debug_info("DILocalVariable", {
    "name": "ctx",
    "arg": 1,
    "file": module._file_metadata,
    "type": some_type
})

# Later we realize we need to add a scope
existing_operands = list(var.operands)
existing_operands.append(("scope", scope_value))
var.operands = tuple(existing_operands)

Ideally, this could instead be done through a helper like:

var.add_debug_info_field("scope", scope_value)

This would make the API safer and more maintainable.

varun-r-mallya avatar Nov 01 '25 02:11 varun-r-mallya

This can get tricky and need careful planning. In LLVM C++, MDNode can be unique or non-unique. IIRC, Unique MDNode are immutable. That's what llvmlite implemented only. All metadata node goes into Module.._metadatacache which uses the operands as a key.

The feature request here is essentially asking for a non-unique MDNode such that it doesn't get stored in _metadatacache.

Contributions are welcomed.

sklam avatar Nov 04 '25 18:11 sklam

Additional references:

  • https://llvm.org/docs/TransformMetadata.html#metadata-on-loops

    Because metadata nodes are immutable (with the exception of MDNode::replaceOperandWith which is dangerous to use on uniqued metadata)

  • https://github.com/llvm-mirror/llvm/blob/2c4ca6832fa6b306ee6a7010bfb80a3f2596f824/lib/IR/Metadata.cpp#L858-L868

sklam avatar Nov 04 '25 18:11 sklam