[llvm] 10b5fec - [JITLink][ORC] Add LinkGraph::allocateCString method.
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 11 12:30:37 PST 2023
Author: Lang Hames
Date: 2023-02-11T12:05:28-08:00
New Revision: 10b5fec25638f7cca34d071db2a1d2afcab09a6d
URL: https://github.com/llvm/llvm-project/commit/10b5fec25638f7cca34d071db2a1d2afcab09a6d
DIFF: https://github.com/llvm/llvm-project/commit/10b5fec25638f7cca34d071db2a1d2afcab09a6d.diff
LOG: [JITLink][ORC] Add LinkGraph::allocateCString method.
Renames the existing allocateString method to allocateContent and adds a pair of
allocateCString methods.
The previous allocateString method did not include a null-terminator. It behaved
the same as allocateContent except with a Twine input, rather than an
ArrayRef<char>. Renaming allocateString to allocateBuffer (overloading the
existing method) makes this clearer.
The new allocateCString methods allocate the given content plus a
null-terminator character, and return a buffer covering both the string and
null-terminator. This makes them suitable for creating c-string content for
jitlink::Blocks.
Existing users of the old allocateString method have been updated to use the
new allocateContent overload.
Added:
Modified:
llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp
llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp
llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp
llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp
llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
index 902c5eff5a66..f9d6679ef242 100644
--- a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
@@ -1001,7 +1001,7 @@ class LinkGraph {
/// Note: This Twine-based overload requires an extra string copy and an
/// extra heap allocation for large strings. The ArrayRef<char> overload
/// should be preferred where possible.
- MutableArrayRef<char> allocateString(Twine Source) {
+ MutableArrayRef<char> allocateContent(Twine Source) {
SmallString<256> TmpBuffer;
auto SourceStr = Source.toStringRef(TmpBuffer);
auto *AllocatedBuffer = Allocator.Allocate<char>(SourceStr.size());
@@ -1009,6 +1009,36 @@ class LinkGraph {
return MutableArrayRef<char>(AllocatedBuffer, SourceStr.size());
}
+ /// Allocate a copy of the given string using the LinkGraph's allocator.
+ ///
+ /// The allocated string will be terminated with a null character, and the
+ /// returned MutableArrayRef will include this null character in the last
+ /// position.
+ MutableArrayRef<char> allocateCString(StringRef Source) {
+ char *AllocatedBuffer = Allocator.Allocate<char>(Source.size() + 1);
+ llvm::copy(Source, AllocatedBuffer);
+ AllocatedBuffer[Source.size()] = '\0';
+ return MutableArrayRef<char>(AllocatedBuffer, Source.size() + 1);
+ }
+
+ /// Allocate a copy of the given string using the LinkGraph's allocator.
+ ///
+ /// The allocated string will be terminated with a null character, and the
+ /// returned MutableArrayRef will include this null character in the last
+ /// position.
+ ///
+ /// Note: This Twine-based overload requires an extra string copy and an
+ /// extra heap allocation for large strings. The ArrayRef<char> overload
+ /// should be preferred where possible.
+ MutableArrayRef<char> allocateCString(Twine Source) {
+ SmallString<256> TmpBuffer;
+ auto SourceStr = Source.toStringRef(TmpBuffer);
+ auto *AllocatedBuffer = Allocator.Allocate<char>(SourceStr.size() + 1);
+ llvm::copy(SourceStr, AllocatedBuffer);
+ AllocatedBuffer[SourceStr.size()] = '\0';
+ return MutableArrayRef<char>(AllocatedBuffer, SourceStr.size() + 1);
+ }
+
/// Create a section with the given name, protection flags, and alignment.
Section &createSection(StringRef Name, orc::MemProt Prot) {
assert(llvm::none_of(Sections,
diff --git a/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp b/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp
index 782928c26084..481d6e72b245 100644
--- a/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp
@@ -287,7 +287,7 @@ Error COFFLinkGraphBuilder::handleDirectiveSection(StringRef Str) {
break;
}
case COFF_OPT_incl: {
- auto DataCopy = G->allocateString(S);
+ auto DataCopy = G->allocateContent(S);
StringRef StrCopy(DataCopy.data(), DataCopy.size());
ExternalSymbols[StrCopy] = &G->addExternalSymbol(StrCopy, 0, false);
ExternalSymbols[StrCopy]->setLive(true);
diff --git a/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp b/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp
index fb32c024480d..bdf0eb601289 100644
--- a/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp
@@ -185,7 +185,7 @@ Error MachOLinkGraphBuilder::createNormalizedSections() {
Prot = orc::MemProt::Read | orc::MemProt::Write;
auto FullyQualifiedName =
- G->allocateString(StringRef(NSec.SegName) + "," + NSec.SectName);
+ G->allocateContent(StringRef(NSec.SegName) + "," + NSec.SectName);
NSec.GraphSection = &G->createSection(
StringRef(FullyQualifiedName.data(), FullyQualifiedName.size()), Prot);
diff --git a/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp b/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp
index 3010370f4e40..d0b8762df515 100644
--- a/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp
@@ -126,8 +126,8 @@ class COFFHeaderMaterializationUnit : public MaterializationUnit {
llvm_unreachable("Unrecognized architecture");
}
- auto HeaderContent = G.allocateString(
- StringRef(reinterpret_cast<const char *>(&Hdr), sizeof(Hdr)));
+ auto HeaderContent = G.allocateContent(
+ ArrayRef<char>(reinterpret_cast<const char *>(&Hdr), sizeof(Hdr)));
return G.createContentBlock(HeaderSection, HeaderContent, ExecutorAddr(), 8,
0);
diff --git a/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp b/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
index 377a59993eb0..409174dac3c2 100644
--- a/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
@@ -528,7 +528,7 @@ DLLImportDefinitionGenerator::createStubsGraph(const SymbolMap &Resolved) {
// Create __imp_ symbol
jitlink::Symbol &Ptr =
jitlink::x86_64::createAnonymousPointer(*G, Sec, &Target);
- auto NameCopy = G->allocateString(Twine(getImpPrefix()) + *KV.first);
+ auto NameCopy = G->allocateContent(Twine(getImpPrefix()) + *KV.first);
StringRef NameCopyRef = StringRef(NameCopy.data(), NameCopy.size());
Ptr.setName(NameCopyRef);
Ptr.setLinkage(jitlink::Linkage::Strong);
diff --git a/llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp b/llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp
index 2918d275c092..7e6a71dab9c3 100644
--- a/llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp
@@ -148,8 +148,8 @@ class MachOHeaderMaterializationUnit : public MaterializationUnit {
if (G.getEndianness() != support::endian::system_endianness())
MachO::swapStruct(Hdr);
- auto HeaderContent = G.allocateString(
- StringRef(reinterpret_cast<const char *>(&Hdr), sizeof(Hdr)));
+ auto HeaderContent = G.allocateContent(
+ ArrayRef<char>(reinterpret_cast<const char *>(&Hdr), sizeof(Hdr)));
return G.createContentBlock(HeaderSection, HeaderContent, ExecutorAddr(), 8,
0);
diff --git a/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp b/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp
index 86fc7bddaee3..dbcd285ef0cb 100644
--- a/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp
+++ b/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp
@@ -712,6 +712,26 @@ TEST(LinkGraphTest, SplitBlock) {
}
}
+TEST(LinkGraphTest, GraphAllocationMethods) {
+ LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
+ getGenericEdgeKindName);
+
+ // Test allocation of sized, uninitialized buffer.
+ auto Buf1 = G.allocateBuffer(10);
+ EXPECT_EQ(Buf1.size(), 10U);
+
+ // Test allocation of content-backed buffer.
+ ArrayRef<char> Buf2Src = {1, -1, 0, 42};
+ auto Buf2 = G.allocateContent(Buf2Src);
+ EXPECT_EQ(Buf2, Buf2Src);
+
+ // Test c-string allocation from StringRef.
+ StringRef Buf3Src = "hello";
+ auto Buf3 = G.allocateCString(Buf3Src);
+ EXPECT_TRUE(llvm::equal(Buf3.drop_back(1), Buf3Src));
+ EXPECT_EQ(Buf3.back(), '\0');
+}
+
TEST(LinkGraphTest, IsCStringBlockTest) {
// Check that the LinkGraph::splitBlock test works as expected.
LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
More information about the llvm-commits
mailing list