This is an archive of the discontinued LLVM Phabricator instance.

Fix possible self assign issue for DIEValue
ClosedPublic

Authored by XinWang10 on May 6 2023, 12:04 AM.

Details

Summary

In DIEValue's operator assignment constructor, it didn't identify if
the two obj is the same.
I add code to identify them so that it will work correctly when we do
self assign here.

Diff Detail

Event Timeline

XinWang10 created this revision.May 6 2023, 12:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 6 2023, 12:04 AM
XinWang10 requested review of this revision.May 6 2023, 12:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 6 2023, 12:04 AM
XinWang10 retitled this revision from [draft]Fix possible self assign for DIEValue to Fix possible self assign issue for DIEValue.May 6 2023, 12:55 AM
XinWang10 edited the summary of this revision. (Show Details)
XinWang10 added reviewers: skan, pengfei.
skan added inline comments.May 6 2023, 1:02 AM
llvm/include/llvm/CodeGen/DIE.h
467

Shouldn't we compare this with &X instead?

XinWang10 updated this revision to Diff 520045.May 6 2023, 1:41 AM
  • resolve comment
XinWang10 marked an inline comment as done.May 6 2023, 1:42 AM
skan added inline comments.May 6 2023, 1:56 AM
llvm/include/llvm/CodeGen/DIE.h
469

https://stackoverflow.com/questions/12015156/what-is-wrong-with-checking-for-self-assignment-and-what-does-it-mean

Use copy-and-swap method?

DIEValue &operator=(DIEValue X) {
  swap(*this, X);
  return *this;
}
XinWang10 added inline comments.May 6 2023, 3:00 AM
llvm/include/llvm/CodeGen/DIE.h
469

This reported a lot of errors to me, I think swap will also call operator assign?
From my point of view, the logic here for assign is correct if we just remove destroyVal(), since the Val is on stack, we don't need to destroy it and it will be overwrite by X. But I'm not sure if there is some concern here.

When I read this code, another question occured to me, why do we need the explicit copy and assign constructor, this class don't maintain any dynamic memory.
I think if I remove the copy and assign constructor, it will also work for me.
But I don't want to make that big change :)

XinWang10 added inline comments.May 6 2023, 3:08 AM
llvm/include/llvm/CodeGen/DIE.h
469

Seems can pass check-all locally without copy and assign construction function.

skan added inline comments.May 6 2023, 6:33 AM
llvm/include/llvm/CodeGen/DIE.h
469

swap does not call assignment operator, and it swaps the members of two classes.
It seems copyVal will dynamically allocate memory.

What's the error?

XinWang10 added inline comments.May 7 2023, 6:59 PM
llvm/include/llvm/CodeGen/DIE.h
469

swap can not get a rvalue as parm.
copyVal calls construct which just calls it's constructor.

skan accepted this revision.May 8 2023, 2:20 AM

LGTM

This revision is now accepted and ready to land.May 8 2023, 2:20 AM
This revision was automatically updated to reflect the committed changes.