Skip to content

Conversation

@michaelmaitland
Copy link
Contributor

@michaelmaitland michaelmaitland commented Jan 29, 2025

MBBFrameObjsReachingDefs was not being built correctly since we were not inserting into a reference of Frame2InstrIdx. If there was multiple stack slot defs in the same basic block, then the bug would occur. This PR fixes this problem while simplifying the insertion logic.

Additionally, when lookup into MBBFrameObjsReachingDefs was occurring, there was a chance that there was no entry in the map, in the case that there was no reaching def. This was causing us to return a default value, which may or may not have been correct. This patch returns the correct value now.

@michaelmaitland
Copy link
Contributor Author

michaelmaitland commented Jan 29, 2025

@mgudim I found that !isFIDef is never true so nothing ever gets added to the map in test0.

@michaelmaitland michaelmaitland changed the title [ReachingDefAnalysis][NFC] Fix management of MBBFrameObjsReachingDefs [ReachingDefAnalysis] Fix management of MBBFrameObjsReachingDefs Jan 29, 2025
@mgudim
Copy link
Contributor

mgudim commented Jan 29, 2025

Since there is nothing using this in llvm right now these changes shouldn't break anything.
We can either:
(1) merge this and hope it's all good.
(2) I can rebase my shrink-wrapping patch on top of this and I can run all of spec - that should be a good test.

What do you think @michaelmaitland ?

@michaelmaitland
Copy link
Contributor Author

michaelmaitland commented Jan 29, 2025

I had some trouble writing a test for the lookup part, since we were sort of getting lucky. What I mean by that is lookup was returning a empty list of defs as a default value which was exactly what would happen when there is no reaching defs. I think that's why it did not impact the tests negatively. But the changes here are definitely more robust. That makes that part of the patch "NFC-ish" i think, although I don't want to call it NFC because it may be fixing a tricky to find bug.

WRT to the Frame2InstrIdx bug, I would be surprised if that was NFC. I will try and write a test for it.

@michaelmaitland
Copy link
Contributor Author

@michaelmaitland michaelmaitland requested a review from nikic January 29, 2025 19:05
@nikic
Copy link
Contributor

nikic commented Jan 29, 2025

Here is the memory usage compared to before your patch went in:

https://llvm-compile-time-tracker.com/compare.php?from=8baa0d9d545f9daf0d82596cb90f35456efb1153&to=4ef33af3bb780b9f7ef13dea8c15479dfd30f28f&stat=max-rss

@nikic is this reasonable?

It's probably okay, max-rss is pretty noisy.

@michaelmaitland
Copy link
Contributor Author

I've added a test case. I pre-committed it in this patch so you can see the diff.

{FrameIndex - ObjectIndexBegin, {CurInstr}}};
}

int Key = FrameIndex - ObjectIndexBegin;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need to normalize the FrameIndex. Can't we use it as the key directly?

}

int Key = FrameIndex - ObjectIndexBegin;
MBBFrameObjsReachingDefs[MBBNumber][Key].push_back(CurInstr);
Copy link
Collaborator

Choose a reason for hiding this comment

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

A map of maps is a kind of silly data structure. You can make the key a std::pair and use a single level map. Unless we need to be able to easily access the inner map for a single basic block.

auto &InnerMap = Lookup1->second;
auto Lookup2 = InnerMap.find(Key);
if (Lookup2 == InnerMap.end())
auto Lookup = MBBFrameObjsReachingDefs.find({MBBNumber, Key});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't this need to use FrameIndex instead of Key?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, forgot to update. thanks

Copy link
Collaborator

@topperc topperc Jan 31, 2025

Choose a reason for hiding this comment

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

So there's no testing? Or is it because object begin was 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no testing for a non-zero ObjectIndexBegin, so it ended up behaving as if Key == FrameIndex. Do you want me to add a test for a non-zero ObjectIndexBegin?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@topperc test added. It doesn't matter anymore though since we no longer care about ObjectIndexBegin.

Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

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

LGTM

@michaelmaitland michaelmaitland merged commit 93b90a5 into llvm:main Feb 4, 2025
8 checks passed
@michaelmaitland michaelmaitland deleted the cleanup-rda-stack branch February 4, 2025 15:04
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…m#124943)

MBBFrameObjsReachingDefs was not being built correctly since we were not
inserting into a reference of Frame2InstrIdx. If there was multiple
stack slot defs in the same basic block, then the bug would occur. This
PR fixes this problem while simplifying the insertion logic.

Additionally, when lookup into MBBFrameObjsReachingDefs was occurring,
there was a chance that there was no entry in the map, in the case that
there was no reaching def. This was causing us to return a default
value, which may or may not have been correct. This patch returns the
correct value now.
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.

4 participants