This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyCFG] Preserve metadata when unconditionalizing branches (constant condition).
ClosedPublic

Authored by Meinersbur on Apr 23 2021, 12:14 AM.

Details

Summary

When replacing a conditional branch by an unconditional one because the condition is a constant, transfer the metadata to the new branch instruction.

Part of fix for llvm.org/PR50060

Diff Detail

Event Timeline

Meinersbur created this revision.Apr 23 2021, 12:14 AM
Meinersbur requested review of this revision.Apr 23 2021, 12:14 AM
lebedev.ri added a subscriber: lebedev.ri.

The diff seems to be reverse from what you intended?

llvm/test/Transforms/SimplifyCFG/pr50060-constantfold-loopid.ll
1

Did you mean to update the test instead of deleting it

Accidentally uploaded a reversed patch

lebedev.ri added inline comments.Apr 23 2021, 12:23 AM
llvm/lib/Transforms/Utils/Local.cpp
175

What about prof md?

llvm/lib/Transforms/Utils/Local.cpp
151

Shouldn't we also take care of the metadata here ?

166

Can't we just copy the metadata from the BI directly ? Or will removing predecessors modify the attached metadata ?

Meinersbur added inline comments.Apr 23 2021, 4:10 PM
llvm/lib/Transforms/Utils/Local.cpp
151

Yes, this patch is about the fixing PR50060 and I don't have a test case for this one.

166

Removing code bears the risk that instructions are invalidated and should not be accessed anymore. I am not sure enough about removePredecessor since it causes an orphaned block. However, NewBr could be created before removePredecessor.
However, BI->eraseFromParent() is also only called afterwards, so it should be fine.

175

Edge weights are not in the scope of this patch.

  • Use copyMetadata.
Meinersbur retitled this revision from [SimplifyCFG] Preserve metadata when unconditionalizing branches. to [SimplifyCFG] Preserve metadata when unconditionalizing branches (constant condition)..Apr 23 2021, 11:58 PM
Meinersbur added inline comments.
llvm/lib/Transforms/Utils/Local.cpp
151
lebedev.ri added inline comments.Apr 24 2021, 12:19 AM
llvm/lib/Transforms/Utils/Local.cpp
175

Indeed.
But my question is, this copies *all* metadata.
What happens if the cond br had prof md?
They won't really make sense on uncond br.
Will verifier be ok with that, or should this specify which md to copy?

Meinersbur added inline comments.Apr 25 2021, 7:20 PM
llvm/lib/Transforms/Utils/Local.cpp
175

Thanks for the hint. Looking at the pre-merge fails, the sanitizer check indeed complains about "Wrong number of operands". I did not think it was related, all the LLVM checks succeed. I could not find a common handling of metadata for branches such as llvm::copyMetadataForLoad for loads.

  • Transfer only selected metadata kinds.
lebedev.ri accepted this revision.Apr 26 2021, 12:20 AM

This LGTM, thank you.

llvm/lib/Transforms/Utils/Local.cpp
175

I'm guessing you are intentionally not copying mustprocess?

This revision is now accepted and ready to land.Apr 26 2021, 12:20 AM
llvm/lib/Transforms/Utils/Local.cpp
175

I'm guessing you are intentionally not copying mustprocess?

mustprogress is part of MD_loop.

This revision was landed with ongoing or failed builds.Apr 26 2021, 8:57 AM
This revision was automatically updated to reflect the committed changes.