-
Notifications
You must be signed in to change notification settings - Fork 396
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
[NDMatrix] Resolved Dangling Reference Warning #2827
[NDMatrix] Resolved Dangling Reference Warning #2827
Conversation
GCC13 introduced a warning for potentially dangling references. The NDMatrix class was creating temporary objects to store the offseted pointer and then returning refrences to these pointers. Although this is correct, since the pointer is owned by the base NDMatrix object, it was confusing the compiler which is not something you want to do in general. Changed how the NDMatrix proxy class works by storing the offset into the pointer directly and passing a reference to the base pointer instead. This makes it clear to the compiler that this pointer belongs to another class and we are returning a reference to a portion of that memory, which is allowed.
@soheilshahrouz In case you were curious, this resolves the NDMatrix issues I was talking about. This should get us through the next few Ubuntu versions without issue. We may not even need to go to MDSpan. |
@vaughnbetz This PR fixes a whole mess of warnings from the Ubuntu 24.04 build. The issue was that the NDMatrix class was creating a temporary sub-matrix objects per dimension during accesses (each of which were given a pointer which was offset from the base pointer of the NDMatrix class). This caused the compiler to get confused about who owned the pointer and it thought that we were returning a reference to a temporary object in memory when we accessed this pointer. To fix this I changed the NDMatrix class to pass the base pointer by reference and only had the offset get passed to each sub-matrix. This makes it obvious to the compiler that the pointer in the sub-matrix objects do not belong to them. This resolved the warnings and I think it may have a slightly positive affect on performance; I am running the VTR benchmarks now. This will allow us to move to the next version of Ubuntu for the CI and remain warning clean. There are still a couple of warnings left, but this one was definitely the worst to fix. Please review when you have a moment. |
VTR Benchmarks results. Seems to be a wash in runtime; which is good.
The QoR did not change at all which is also good. Let me know what you think about this change @vaughnbetz |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good -- I suggest a few more comments though.
@@ -62,7 +64,8 @@ class NdMatrixProxy { | |||
private: | |||
const size_t* dim_sizes_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment these data members if you know what they are. Since offset was just added it at least should be commentable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume going from a T* to a unique_ptr reference has some advantage, and if there is that would be a good thing to mention in the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment. The main benefit is safety. This makes it clear that the owner of the memory is someone else (the base NDMatrix object). This also allows the base pointer to be reallocated without invalidating all of the NDMatrixProxy objects (since they would be pointing to the stale data). This was the change that helped the compiler better understand what we were doing here. What we were doing was not wrong, just a bit hard to analyze.
Added comments based on Vaughn's suggestions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks!
GCC13 introduced a warning for potentially dangling references. The NDMatrix class was creating temporary objects to store the offseted pointer and then returning refrences to these pointers. Although this is correct, since the pointer is owned by the base NDMatrix object, it was confusing the compiler which is not something you want to do in general.
Changed how the NDMatrix proxy class works by storing the offset into the pointer directly and passing a reference to the base pointer instead. This makes it clear to the compiler that this pointer belongs to another class and we are returning a reference to a portion of that memory, which is allowed.