-
Notifications
You must be signed in to change notification settings - Fork 12.1k
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
Implement Move-assignment for llvm::Module (NFC) #117270
Open
joker-eph
wants to merge
1
commit into
llvm:main
Choose a base branch
from
joker-eph:move-assign-module
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
+118
−3
Conversation
This file contains 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
@llvm/pr-subscribers-llvm-ir Author: Mehdi Amini (joker-eph) ChangesMove-assignment is quite convenient in various situation, and work-around having it available is very convoluted. Full diff: https://github.com/llvm/llvm-project/pull/117270.diff 3 Files Affected:
diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h
index 528e19af5518df..12b50fc506516e 100644
--- a/llvm/include/llvm/IR/Module.h
+++ b/llvm/include/llvm/IR/Module.h
@@ -256,9 +256,12 @@ class LLVM_ABI Module {
/// The module destructor. This will dropAllReferences.
~Module();
-/// @}
-/// @name Module Level Accessors
-/// @{
+ /// Move assignment.
+ Module &operator=(Module &&Other);
+
+ /// @}
+ /// @name Module Level Accessors
+ /// @{
/// Get the module identifier which is, essentially, the name of the module.
/// @returns the module identifier as a string
diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp
index ab48d3e4101b72..57946f21ed97e1 100644
--- a/llvm/lib/IR/Module.cpp
+++ b/llvm/lib/IR/Module.cpp
@@ -78,6 +78,42 @@ Module::Module(StringRef MID, LLVMContext &C)
Context.addModule(this);
}
+Module &Module::operator=(Module &&Other) {
+ assert(&Context == &Other.Context && "Module must be in the same Context");
+
+ dropAllReferences();
+
+
+ ModuleID = std::move(Other.ModuleID);
+ SourceFileName = std::move(Other.SourceFileName);
+ IsNewDbgInfoFormat = std::move(Other.IsNewDbgInfoFormat);
+
+ GlobalList.clear();
+ GlobalList.splice(GlobalList.begin(), Other.GlobalList);
+
+ FunctionList.clear();
+ FunctionList.splice(FunctionList.begin(), Other.FunctionList);
+
+ AliasList.clear();
+ AliasList.splice(AliasList.begin(), Other.AliasList);
+
+ IFuncList.clear();
+ IFuncList.splice(IFuncList.begin(), Other.IFuncList);
+
+ NamedMDList.clear();
+ NamedMDList.splice(NamedMDList.begin(), Other.NamedMDList);
+ GlobalScopeAsm = std::move(Other.GlobalScopeAsm);
+ OwnedMemoryBuffer = std::move(Other.OwnedMemoryBuffer);
+ Materializer = std::move(Other.Materializer);
+ TargetTriple = std::move(Other.TargetTriple);
+ DL = std::move(Other.DL);
+ CurrentIntrinsicIds = std::move(Other.CurrentIntrinsicIds);
+ UniquedIntrinsicNames = std::move(Other.UniquedIntrinsicNames);
+ ModuleFlags = std::move(Other.ModuleFlags);
+ Context.addModule(this);
+ return *this;
+}
+
Module::~Module() {
Context.removeModule(this);
dropAllReferences();
diff --git a/llvm/unittests/IR/ModuleTest.cpp b/llvm/unittests/IR/ModuleTest.cpp
index c18301d5e6d758..5735c6cfe6e4d0 100644
--- a/llvm/unittests/IR/ModuleTest.cpp
+++ b/llvm/unittests/IR/ModuleTest.cpp
@@ -14,6 +14,7 @@
#include "llvm/Pass.h"
#include "llvm/Support/RandomNumberGenerator.h"
#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
#include <random>
@@ -326,4 +327,81 @@ TEST(ModuleTest, GlobalList) {
EXPECT_EQ(M->global_size(), 1u);
}
+TEST(ModuleTest, MoveAssign) {
+ // This tests that we can move-assign modules, we parse two modules and
+ // move assign the second one to the first one, and check that the print
+ // is equal to what we loaded.
+ LLVMContext C;
+ SMDiagnostic Err;
+ LLVMContext Context;
+ std::unique_ptr<Module> M1 = parseAssemblyString(R"(
+; ModuleID = '<string>'
+source_filename = "<string1>"
+
+@GV1 = external global i32
+
+@GA1 = alias void (), ptr @Foo1
+
+define void @Foo1() {
+ ret void
+}
+
+!llvm.module.flags = !{!0}
+!llvm.dbg.cu = !{!1}
+!foo1 = !{!3}
+!bar1 = !{!4}
+
+!0 = !{i32 2, !"Debug Info Version", i32 3}
+!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, producer: "clang1", isOptimized: true, flags: "-O2", runtimeVersion: 0, splitDebugFilename: "abc.debug", emissionKind: LineTablesOnly)
+!2 = !DIFile(filename: "path/to/file1", directory: "/path/to/dir1")
+!3 = !DILocation(line: 12, column: 34, scope: !4)
+!4 = distinct !DISubprogram(name: "foo1", scope: null, spFlags: DISPFlagDefinition, unit: !1)
+)",
+ Err, Context);
+ ASSERT_TRUE(M1.get());
+
+ StringLiteral M2Str = R"(
+; ModuleID = '<string>'
+source_filename = "<string2>"
+
+@GV2 = external global i32
+
+@GA2 = alias void (), ptr @Foo2
+
+define void @Foo2() {
+ ret void
+}
+
+!llvm.module.flags = !{!0}
+!llvm.dbg.cu = !{!1}
+!foo2 = !{!3}
+!bar2 = !{!4}
+
+!0 = !{i32 2, !"Debug Info Version", i32 3}
+!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, producer: "clang2", isOptimized: true, flags: "-O2", runtimeVersion: 0, splitDebugFilename: "abc.debug", emissionKind: LineTablesOnly)
+!2 = !DIFile(filename: "path/to/file2", directory: "/path/to/dir2")
+!3 = !DILocation(line: 1234, column: 56, scope: !4)
+!4 = distinct !DISubprogram(name: "foo2", scope: null, spFlags: DISPFlagDefinition, unit: !1)
+)";
+ {
+ std::unique_ptr<Module> M2 = parseAssemblyString(M2Str,
+ Err, Context);
+ ASSERT_TRUE(M2.get());
+ auto *GV1 = M1->getNamedValue("GV1");
+ ASSERT_TRUE(GV1);
+ auto *GV2 = M2->getNamedValue("GV2");
+ ASSERT_TRUE(GV2);
+ ASSERT_EQ(GV2->getParent(), &*M2);
+ *M1 = std::move(*M2);
+ ASSERT_EQ(GV2->getParent(), &*M1);
+ }
+
+ std::string M1Print;
+ {
+ llvm::raw_string_ostream Os(M1Print);
+ Os << "\n" << *M1;
+ }
+ ASSERT_EQ(M2Str, M1Print);
+}
+
} // end namespace
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Move-assignment is quite convenient in various situation, and work-around having it available is very convoluted.
joker-eph
force-pushed
the
move-assign-module
branch
from
November 22, 2024 01:04
1a7db96
to
1c5be45
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Move-assignment is quite convenient in various situation, and work-around having it available is very convoluted.