This is an archive of the discontinued LLVM Phabricator instance.

[NFC] [TargetRegisterInfo] add one use check to lookThruCopyLike.
ClosedPublic

Authored by shchenz on Nov 24 2020, 7:44 PM.

Details

Summary

add one use check to lookThruCopyLike.

The root node is safe to be deleted if we are sure that every definition in the copy chain only has one use.

https://reviews.llvm.org/D92071 is a user of this modified function.

Diff Detail

Event Timeline

shchenz created this revision.Nov 24 2020, 7:44 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 24 2020, 7:44 PM
shchenz requested review of this revision.Nov 24 2020, 7:44 PM
shchenz edited the summary of this revision. (Show Details)Nov 24 2020, 8:04 PM
shchenz updated this revision to Diff 309196.Dec 3 2020, 2:12 AM

lint warning fix

gentle ping...

gentle ping

gentle ping

jsji accepted this revision as: jsji.Jan 14 2021, 8:05 PM

LGTM.

This revision is now accepted and ready to land.Jan 14 2021, 8:05 PM

Sorry to point this out now, but taking a bool in/out pointer is some
fairly awkward API. Can we do something else here?

Thanks!

-eric

Sorry to point this out now, but taking a bool in/out pointer is some
fairly awkward API. Can we do something else here?

Thanks!

-eric

Yes, I am happy to change this to another version. Do you think returning std::pair<Register, bool> is an acceptable way?

I'd like to avoid the bool completely if possible. Passing the register
might work as well if you wanted to do that, but I think it needs to be
clear API wise why and how you're doing computation where you are.

Do you have the follow on patch?

-eric

shchenz added a comment.EditedJan 17 2021, 6:01 PM

I'd like to avoid the bool completely if possible. Passing the register
might work as well if you wanted to do that, but I think it needs to be
clear API wise why and how you're doing computation where you are.

Do you have the follow on patch?

-eric

I don't have the follow on yet. Sorry, I am not very clear about the passing the register method to avoid the bool type. Could you please give me more details? Thanks.

User patch of this API is in patch D92071

jsji added a comment.Jan 17 2021, 7:45 PM

I'd like to avoid the bool completely if possible.

Thanks @echristo for bring up this. We are happy to change it for better code.

However, can you help us to understand the reason, and any advice about how to design this API?
Also is this PowerPC specific or general to LLVM coding style?

As we are seeing quite decent similar usages in the code base,
eg:

$ grep "bool \*" llvm/include/llvm -R |wc
    53     332    5185

If this is general, do you think it worth mentioning in ProgrammersManual?
If so, we can follow up a patch to update that as well.

I don't see an issue in providing two functions to achieve the two different goals:

lookThruCopyLike()           // Look through a chain of copies.
lookThruSingleUseCopyChain() // Look through a chain of copies, checking that each def has a single use.

Also, I am a little curious about what this change is meant to accomplish. The name of the introduced bool parameter is AllDefHaveOneUser yet we don't actually check all defs - we only check the final one that we are returning. So it is presumably possible to have something like this (all regs virtual):

%1 = ...
%2 = SUBREG_TO_REG %1, ...
%3 = COPY %2
%4 = SUBREG_TO_REG %2
%5 = COPY %4
%6 = COPY %4
%7 = <some use of %4>
%8 = <some use of %5>
%9 = <some use of %6>

Running lookThruCopyLike() on %9 will presumably correctly return %1 and if a bool argument is passed, it will be set to true. I would argue that this is a surprising result since I would expect AllDefHaveOneUser to mean that all the definitions traversed (%9, %6, %4, %2, %1) have a single use. But this is clearly not the case. Am I just missing something?

shchenz updated this revision to Diff 317459.Jan 18 2021, 9:08 PM

address comments

shchenz reopened this revision.Jan 18 2021, 9:12 PM

@nemanjai Hi, thanks for your comments. You are right. The first version is not what I intend to implement. I have updated the patch according to your comments.

The original revision was reverted. Could you please help to do another review? Thanks. @echristo @nemanjai @jsji

This revision is now accepted and ready to land.Jan 18 2021, 9:12 PM
shchenz requested review of this revision.Jan 18 2021, 9:13 PM
nemanjai accepted this revision.Jan 19 2021, 2:34 AM

LGTM now. Maybe give @echristo a bit of time to see if this adequately addresses his original concern.

llvm/lib/CodeGen/TargetRegisterInfo.cpp
541–547

Minor nit: Stylistically, it is probably a bit neater to write this as:

// Found the real definition, return it if it has a single use.
if (!MI->isCopyLike())
  return MRI->hasOneNonDBGUse(SrcReg) ? SrcReg : Register();
557

Maybe it is clearer as:

// Continue only if the next definition in the chain is for a virtual
// register that has a single use.
This revision is now accepted and ready to land.Jan 19 2021, 2:34 AM
shchenz updated this revision to Diff 317520.Jan 19 2021, 4:15 AM

1: address @nemanjai comments

echristo added inline comments.Jan 21 2021, 5:16 PM
llvm/lib/CodeGen/TargetRegisterInfo.cpp
536

You should document what it's doing ahead of time as well. It's not clear when and how you'd use this function.

shchenz added inline comments.Jan 21 2021, 5:41 PM
llvm/lib/CodeGen/TargetRegisterInfo.cpp
536

Thank you, @echristo . Do you think is it ok to add

This function is called when try to delete the root non-copy virtual register. We must make sure the root non-copy virtual register and all the copy like definitions in the chain have only one user.

In the header?