This is an archive of the discontinued LLVM Phabricator instance.

[NVPTX] remove unnecessary named metadata update that happens to break debug info.
ClosedPublic

Authored by tra on Jul 26 2016, 4:54 PM.

Details

Summary

NVPTXGenericToNVVM pass updates named metadata by dropping all references to its arguments and uses addOperand() to add replacement entries. The problem is that there may bemetadata references to the argument we've attempted to replace and those would still refer to the old argument. When that happens we end up with metadata in inconsistent state which has both old and new metadata.

When following code is compiled with debug info, IR faill verification after NVPTXGenericToNVVM pass, because we end up with duplicate DICompileUnit metadata. Included test case demonstrates this when run with unpatched opt.

__device__ void bar(char* arg);
__global__ void foo() {
  bar("XYZ");
}

As far as I can tell, explicitly updating named metadata is not necessary -- updating metadata nodes for variables we've changed appears to maintain metadata consistency. I didn't manage to find a good way to verify it, though. If anyone has suggestions how to reference variable from named metadata, I can add it to the new test.

Diff Detail

Repository
rL LLVM

Event Timeline

tra updated this revision to Diff 65629.Jul 26 2016, 4:54 PM
tra retitled this revision from to [NVPTX] remove unnecessary named metadata update that happens to break debug info..
tra updated this object.
tra added reviewers: dblaikie, jholewinski.
tra added a subscriber: llvm-commits.
dblaikie edited edge metadata.Aug 1 2016, 4:19 PM

"As far as I can tell, explicitly updating named metadata is not necessary -- updating metadata nodes for variables we've changed appears to maintain metadata consistency. I didn't manage to find a good way to verify it, though. If anyone has suggestions how to reference variable from named metadata, I can add it to the new test."

Not sure I understand what you were and were not able to verify/test. Could you explain that more/differently?

The test case looks fairly reasonable to me - but I also wouldn't mind an explanation of all the features (why the artificial/named metadata node? I would expect to just match the CU/Globals/global metadata nodes, and check that the reference (as you have) uses whatever is necessary (the addrspace casts, etc) to still reference that global)

tra added a comment.Aug 1 2016, 5:11 PM

"As far as I can tell, explicitly updating named metadata is not necessary -- updating metadata nodes for variables we've changed appears to maintain metadata consistency. I didn't manage to find a good way to verify it, though. If anyone has suggestions how to reference variable from named metadata, I can add it to the new test."

Not sure I understand what you were and were not able to verify/test. Could you explain that more/differently?

I've verified that DI metadata is consistent after the pass. I.e. llvm.dbg.cu->DICompileUnit:globals->!3->DIGlobalVariable->correct reference to @static_var. Implicitly IR verification pass also verified that there's no leftover DICompileUnits hanging around which was the case before the patch.

What I'm not sure about is whether named metadata will always be handled correctly.
As far as I can tell, named metadata is pretty much read-only. You can only replace it with a new temporary instance.
Removed code was attempting to manually update named metadata if it happened to reference (directly or not) any IR that the pass changed.

Currently all named metadata is few references away from IR that gets changed. Regular metadata nodes can be modified (all?, some?), so changing a leaf node does not necessarily require replacement of the metadata node that references it. Updating single metadata node appears to be sufficient and does not require to propagate changes further.

However, if there's a way for a global node to refer to IR, that may need explicit replacement of such node.

The test case looks fairly reasonable to me - but I also wouldn't mind an explanation of all the features (why the artificial/named metadata node? I would expect to just match the CU/Globals/global metadata nodes, and check that the reference (as you have) uses whatever is necessary (the addrspace casts, etc) to still reference that global)

Artificial named node is a placeholder for a named metadata node that would have something that is modified by the pass and thus may need to be replaced. So far I've failed to create such metadata.

LLVM language reference says that named metadata can only have node operands, which guarantees at least one level of indirection, so named node can't refer to IR directly (i.e. it can't have DIGlobalVariable operands.). This simplifies things somehow.

What I need to know is -- when a regular metadata node is modified, am I guaranteed that:
a) either modification happend in-place, without replacing the node,
b) or, if the node is replaced, all metadata nodes referencing it (including named metadata nodes) are updated appropriately.

If that is true, then we're good. It appears to be the case, but I can't tell if I'm just missing a test case that would prove me wrong.

I guess I can try to see what happens when I apply ReplaceAllUsesWith() to a node used as an operand of named metadata. If that works correctly (i.e it would update named metadata operand to point to the new node), then I'm in the clear. If it does not, then I'll need to figure out how such case should be handled.

tra updated this revision to Diff 66509.Aug 2 2016, 11:38 AM
tra edited edge metadata.

Removed artificial named metadata node.
Added checks to verify DI consistency.

dblaikie accepted this revision.Aug 2 2016, 11:54 AM
dblaikie edited edge metadata.
dblaikie added inline comments.
test/CodeGen/NVPTX/generic-to-nvvm-ir.ll
49 ↗(On Diff #66509)

Is this not tested anywhere else?

57 ↗(On Diff #66509)

This should probably be CHECK-SAME? & maybe just check the name of the DISubprogram for good measure? (same as you did with the global variable)

This revision is now accepted and ready to land.Aug 2 2016, 11:54 AM
tra updated this revision to Diff 66528.Aug 2 2016, 12:48 PM
tra edited edge metadata.

Added few more metadata checks as dblaikie@ suggested.

tra marked 2 inline comments as done.Aug 2 2016, 12:48 PM
tra updated this revision to Diff 66548.Aug 2 2016, 2:02 PM

Removed mentions of named.md from comments.

This revision was automatically updated to reflect the committed changes.