Skip to content

[MIR] Fix vreg flag vector memory leak #112479

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 4 commits into from
Oct 16, 2024
Merged

Conversation

optimisan
Copy link
Contributor

@optimisan optimisan commented Oct 16, 2024

A fix-it patch for bec839d #110229.

SmallVector will inline 40 values (here) statically on the stack. Although once that is exhausted it'll heap allocate and won't be deallocated.
No need for a container. This allows 8 flags for a register.

The virtual register flags vector had a memory leak because the vector's memory is not freed.
The BumpPtrAllocator handles the deallocation and misses calling the std::vector<uint8_t> Flags destructor.

SmallVector allocates on stack so that works out here.
@optimisan optimisan marked this pull request as ready for review October 16, 2024 05:08
@arsenm
Copy link
Contributor

arsenm commented Oct 16, 2024

The BumpPtrAllocator handles the deallocation and misses calling the std::vector<uint8_t> Flags destructor.

I don't see how this changes that. I would expect this to just move the leak point to cases where the small vector size is exceeded. Whenever VRegInfo is destroyed, the destructor must be called. That's the bug.

But in this case I don't understand why Flags is a vector in the first place. The flags should just be a bitfield, this should be a simple integer field

@optimisan
Copy link
Contributor Author

optimisan commented Oct 16, 2024

Was about to comment to say if this is even a right fix, but bitfield would do it.

VRegInfo *Info = new (Allocator) VRegInfo; is the allocation site, and there doesn't appear a delete for these, nor did I catch the destructor being called for VRegInfo
Checking again.

@@ -703,7 +703,7 @@ bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS,
return error(FlagStringValue.SourceRange.Start,
Twine("use of undefined register flag '") +
FlagStringValue.Value + "'");
Info.Flags.push_back(FlagValue);
Info.Flags |= FlagValue;
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also catch if a flag is set multiple times

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would a warning be better for it?

Copy link
Contributor

Choose a reason for hiding this comment

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

No reason to be permissive

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the check but can't be tested till relanding the WWM_REG flag patch.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can just add the error in a separate PR

@optimisan optimisan merged commit b5cc222 into llvm:main Oct 16, 2024
5 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants