Preserving metadata during lowing to backend

Hi, I’m writing a machine function pass that looks for metadata inserted by an IR function pass by getting the Value objects of MachineMemOperand objects with .getValue() then calling .getMetadata() on the instructions - however, custom metadata that I insert from the IR is not preserved by the time I reach the machine function pass. If from the machine function pass I call .getFunction() on the machine function and then .print() on the IR function, the custom metadata I’ve inserted is no longer there, while other metadata like tbaa still is.

How can I force my metadata to be preserved when lowering from the middle to the backend, as other metadata is?

I’m fairly sure my problem exists more at the IR level than it does the backend, but please correct me if I’m wrong and I’ll remake this post in the backend category.

In general you can’t; metadata is designed such that it is always safe to drop (but may lead to overly conservative code generation). If you require information to persist for correctness that means you cannot use metadata for it.

I think most places that drop (unknown) metadata should use Instruction::dropUnknownNonDebugMetadata LLVM: llvm::Instruction Class Reference in case you want to take a look.