Skip to content

[X86][SelectionDAG] Fix the Gather's base and index by modifying the Scale value #137813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
May 13, 2025

Conversation

rohitaggarwal007
Copy link
Contributor

Fix the Gather's base and index for one use or multiple uses of Index Node. Using the approach to update the Scale if SHL Opcode and followed by truncate.

PR closed : #134979

Rohit Aggarwal and others added 27 commits April 9, 2025 14:27
… Node. Using the approach to update

the Scale if SHL Opcode and  followed by truncate.
… Node. Using the approach to update

the Scale if SHL Opcode and  followed by truncate.
…7/llvm-project into gatherMultipleOccurrence
…7/llvm-project into gatherMultipleOccurrence
…7/llvm-project into gatherMultipleOccurrence
…7/llvm-project into gatherMultipleOccurrence
…7/llvm-project into gatherMultipleOccurrence
…007/llvm-project into gatherMultipleOccurrence
…7/llvm-project into gatherMultipleOccurrence
@rohitaggarwal007
Copy link
Contributor Author

@RKSimon, This is the new PR as previous one got closed due to merge conflict(#134979 )

@RKSimon RKSimon self-requested a review April 30, 2025 21:26
Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks - almost there!

DCI.AddToWorklist(N);
return SDValue(N, 0);
}
uint64_t ScaleAmt = Scale->getAsZExtVal();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this up and reuse in Log2_32 check - we should probably assert that ScaleAmt is a power of 2 as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
uint64_t ScaleAmt = Scale->getAsZExtVal();
if (auto MinShAmt = DAG.getValidMinimumShiftAmount(Index)) {
if (*MinShAmt >= 1 && (*MinShAmt + Log2_64(ScaleAmt)) < 4 &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge Log2_64 call with the existing Log2_32 call

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

unsigned IndexWidth = Index.getScalarValueSizeInBits();

// Shrink indices if they are larger than 32-bits.
// Only do this before legalize types since v2i64 could become v2i32.
// FIXME: We could check that the type is legal if we're after legalize
// types, but then we would need to construct test cases where that happens.
if (IndexWidth > 32 && DAG.ComputeNumSignBits(Index) > (IndexWidth - 32)) {
unsigned ComputeNumSignBits = DAG.ComputeNumSignBits(Index);
if (Index.getOpcode() == ISD::SHL) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this code needs moving inside the IndexWidth > 32 check - needs commenting as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link

github-actions bot commented May 5, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one minor

// Shrink indices if they are larger than 32-bits.
// Only do this before legalize types since v2i64 could become v2i32.
// FIXME: We could check that the type is legal if we're after legalize
// types, but then we would need to construct test cases where that happens.
if (IndexWidth > 32 && DAG.ComputeNumSignBits(Index) > (IndexWidth - 32)) {
// \ComputeNumSignBits value is recomputed for the shift Index
if (IndexWidth > 32 && ComputeNumSignBits > (IndexWidth - 32)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better if you can move everything inside if (IndexWidth > 32)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RKSimon, Done. Moved the code inside the if (IndexWidth > 32)

@rohitaggarwal007
Copy link
Contributor Author

@RKSimon Can you please merge my changes?
I don't have the write permission.

Thanks

// Attempt to move shifted index into the address scale, allows further
// index truncation below.
if (Index.getOpcode() == ISD::SHL && isa<ConstantSDNode>(Scale)) {
unsigned BitWidth = Index.getScalarValueSizeInBits();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hoist the equivalent IndexWidth above this and remove BitWidth

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

ComputeNumSignBits += *MinShAmt;
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RKSimon, I do not understand the comment fully... Does it mean to move it outside IndexWith > 32 or Does it mean to remove this code snippet?
if (Index.getOpcode() == ISD::SHL) { if (auto MinShAmt = DAG.getValidMinimumShiftAmount(Index)) { if (DAG.ComputeNumSignBits(Index.getOperand(0)) > 1) { ComputeNumSignBits += *MinShAmt; } } }

We can not remove this code as it is required and our test case is failing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which case, please can you pull it out of this patch - I'm happy with the rest of it, but I'm concerned that this will only work under very specific conditions - better to pull it out and you can work on it in a follow up PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incase of SHL have the value of shift 4 or greater... We are recalculating the number of signed bit so that we can truncate.

Sure, i will create a separate PR for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, removed the code and created a separate PR.

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - cheers

@rohitaggarwal007
Copy link
Contributor Author

@RKSimon, Can you please merge this on my behalf?

Thanks

@RKSimon RKSimon merged commit 75d36dc into llvm:main May 13, 2025
9 of 11 checks passed
@rohitaggarwal007 rohitaggarwal007 deleted the gatherMultipleOccurrence branch May 13, 2025 17:49
RKSimon added a commit that referenced this pull request May 19, 2025
…39703)

Fix the Gather's Index for SHL Opcode in which shift amount is 4 or greater.

It is in the continuity of #137813

---------

Co-authored-by: Rohit Aggarwal <[email protected]>
Co-authored-by: Simon Pilgrim <[email protected]>
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
…vm#139703)

Fix the Gather's Index for SHL Opcode in which shift amount is 4 or greater.

It is in the continuity of llvm#137813

---------

Co-authored-by: Rohit Aggarwal <[email protected]>
Co-authored-by: Simon Pilgrim <[email protected]>
ajaden-codes pushed a commit to Jaddyen/llvm-project that referenced this pull request Jun 6, 2025
…vm#139703)

Fix the Gather's Index for SHL Opcode in which shift amount is 4 or greater.

It is in the continuity of llvm#137813

---------

Co-authored-by: Rohit Aggarwal <[email protected]>
Co-authored-by: Simon Pilgrim <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants