-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[CodeGen] Fix MachineModuleInfo
's move constructor to be more safe with MCContext
ownership.
#104834
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
Open
weiweichen
wants to merge
9
commits into
llvm:main
Choose a base branch
from
weiweichen:weiweic/make-mmiwp-mmi-unique-ptr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[CodeGen] Fix MachineModuleInfo
's move constructor to be more safe with MCContext
ownership.
#104834
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f6d4993
Make MMI in MachineModuleInfoWrapperPass a unique_ptr.
weiweichen 6186520
Make MCContext in MachineModuleInfo unique_ptr instead.
weiweichen 9deb7bf
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen 9b6e6a0
Redo format.
weiweichen 160845f
Add a unittest for the move constructor change.
weiweichen 395fcdf
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen e194088
Update comment.
weiweichen 6736ec4
Trim down header includes.
weiweichen b9f7185
Format MachineModuleInfoTest.cpp.
weiweichen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//===- llvm/unittest/CodeGen/PassManager.cpp - PassManager tests ----------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// Test MachineModuleInfo. | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/CodeGen/MachineModuleInfo.h" | ||
#include "llvm/AsmParser/Parser.h" | ||
#include "llvm/IR/LLVMContext.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/MC/TargetRegistry.h" | ||
#include "llvm/Support/SourceMgr.h" | ||
#include "llvm/Support/TargetSelect.h" | ||
#include "llvm/Target/TargetMachine.h" | ||
#include "llvm/TargetParser/Host.h" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
|
||
std::unique_ptr<Module> parseIR(LLVMContext &Context, const char *IR) { | ||
SMDiagnostic Err; | ||
return parseAssemblyString(IR, Err, Context); | ||
} | ||
|
||
class MachineModuleInfoTest : public ::testing::Test { | ||
protected: | ||
LLVMContext Context; | ||
std::unique_ptr<Module> M; | ||
std::unique_ptr<TargetMachine> TM; | ||
|
||
public: | ||
MachineModuleInfoTest() | ||
: M(parseIR(Context, "define void @f() {\n" | ||
"entry:\n" | ||
" call void @g()\n" | ||
" ret void\n" | ||
"}\n" | ||
"define void @g() {\n" | ||
" ret void\n" | ||
"}\n")) { | ||
// MachineModuleInfo needs a TargetMachine instance. | ||
llvm::InitializeAllTargets(); | ||
|
||
std::string TripleName = Triple::normalize(sys::getDefaultTargetTriple()); | ||
std::string Error; | ||
const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); | ||
if (!TheTarget) | ||
return; | ||
|
||
TargetOptions Options; | ||
TM.reset(TheTarget->createTargetMachine(TripleName, "", "", Options, | ||
std::nullopt)); | ||
} | ||
}; | ||
|
||
TEST_F(MachineModuleInfoTest, MachineModuleInfoMoveConstructorMovesMCContext) { | ||
if (!TM) | ||
GTEST_SKIP(); | ||
|
||
LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(TM.get()); | ||
M->setDataLayout(TM->createDataLayout()); | ||
|
||
MachineModuleInfo MMI(LLVMTM); | ||
|
||
MCContext *OriginalCtx = &MMI.getContext(); | ||
|
||
MachineModuleInfo MovedMMI(std::move(MMI)); | ||
MCContext *MovedCtx = &MovedMMI.getContext(); | ||
|
||
EXPECT_EQ(OriginalCtx, MovedCtx); | ||
} | ||
|
||
} // end namespace |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we get rid of this ExternalContext business? I don't understand why it's here. If it's necessary to construct with an external context, I would expect that to be the only way to construct MMI
Uh oh!
There was an error while loading. Please reload this page.
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.
Maybe? I don't understand why it's here either (and my PR is not doing anything with it 😛). Any chance we can decide the fate of
ExternalContext
in a separate PR since it's out of the scope of this one?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'd say it's the same scope, for what should own the MCContext
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.
Well, it explicitly said
This is an external context
which means someone else is owning it instead ofthis
here, so not quite the same scope, no?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.
The question that needs to be answered is "who owns the MCContext". I think the answer should be definitely MachineModuleInfo, or definitively an external owner. Having 2 modes here is a bad API
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 agree; See my comments below. IMO it's better to have an external owner. It will bring MMI closer to how
llvm::Module
is managed.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 personally agree with this in principal, but again, I don't have much context on why these two are here originally, so I'd prefer not having to decide the fate of this API while not understanding the original intension of the design. If anyone else has more context, I'd really love to hear and happy to make another PR to improve this.