From ae897863a34f46c86f3419ae4ec30bf44a78332a Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Fri, 19 Mar 2021 17:31:29 -0700 Subject: [PATCH] [JITLink] Remove redundant local variable definitions from a unit test. --- .../llvm/ExecutionEngine/JITLink/JITLink.h | 28 ++++++ .../JITLink/LinkGraphTests.cpp | 95 +++++++++++++++++-- 2 files changed, 117 insertions(+), 6 deletions(-) diff --git a/include/llvm/ExecutionEngine/JITLink/JITLink.h b/include/llvm/ExecutionEngine/JITLink/JITLink.h index 799284d38cb..24c0a75ac53 100644 --- a/include/llvm/ExecutionEngine/JITLink/JITLink.h +++ b/include/llvm/ExecutionEngine/JITLink/JITLink.h @@ -479,6 +479,16 @@ public: /// Returns the size of this symbol. JITTargetAddress getSize() const { return Size; } + /// Set the size of this symbol. + void setSize(JITTargetAddress Size) { + assert(Base && "Cannot set size for null Symbol"); + assert((Size == 0 || Base->isDefined()) && + "Non-zero size can only be set for defined symbols"); + assert((Offset + Size <= static_cast(*Base).getSize()) && + "Symbol size cannot extend past the end of its containing block"); + this->Size = Size; + } + /// Returns true if this symbol is backed by a zero-fill block. /// This method may only be called on defined symbols. bool isSymbolZeroFill() const { return getBlock().isZeroFill(); } @@ -1014,6 +1024,24 @@ public: ExternalSymbols.insert(&Sym); } + /// Turn an external symbol into a defined one by attaching it to a block. + void makeDefined(Symbol &Sym, Block &Content, JITTargetAddress Offset, + JITTargetAddress Size, Linkage L, Scope S, bool IsLive) { + assert(!Sym.isDefined() && !Sym.isAbsolute() && + "Sym is not an external symbol"); + assert(ExternalSymbols.count(&Sym) && "Symbol is not in the externals set"); + ExternalSymbols.erase(&Sym); + Addressable &OldBase = *Sym.Base; + Sym.setBlock(Content); + Sym.setOffset(Offset); + Sym.setSize(Size); + Sym.setLinkage(L); + Sym.setScope(S); + Sym.setLive(IsLive); + Content.getSection().addSymbol(Sym); + destroyAddressable(OldBase); + } + /// Removes an external symbol. Also removes the underlying Addressable. void removeExternalSymbol(Symbol &Sym) { assert(!Sym.isDefined() && !Sym.isAbsolute() && diff --git a/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp b/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp index 810a2fd0e1f..6e00550cf24 100644 --- a/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp +++ b/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp @@ -101,14 +101,97 @@ TEST(LinkGraphTest, BlockAndSymbolIteration) { EXPECT_TRUE(llvm::count(G.defined_symbols(), &S4)); } +TEST(LinkGraphTest, MakeExternal) { + // Check that we can make a defined symbol external. + LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little, + getGenericEdgeKindName); + auto &Sec = G.createSection("__data", RWFlags); + + // Create an initial block. + auto &B1 = G.createContentBlock(Sec, BlockContent, 0x1000, 8, 0); + + // Add a symbol to the block. + auto &S1 = G.addDefinedSymbol(B1, 0, "S1", 4, Linkage::Strong, Scope::Default, + false, false); + + EXPECT_TRUE(S1.isDefined()) << "Symbol should be defined"; + EXPECT_FALSE(S1.isExternal()) << "Symbol should not be external"; + EXPECT_FALSE(S1.isAbsolute()) << "Symbol should not be absolute"; + EXPECT_TRUE(&S1.getBlock()) << "Symbol should have a non-null block"; + EXPECT_EQ(S1.getAddress(), 0x1000U) << "Unexpected symbol address"; + + EXPECT_EQ( + std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 1U) + << "Unexpected number of defined symbols"; + EXPECT_EQ( + std::distance(G.external_symbols().begin(), G.external_symbols().end()), + 0U) + << "Unexpected number of external symbols"; + + // Make S1 external, confirm that the its flags are updated and that it is + // moved from the defined symbols to the externals list. + G.makeExternal(S1); + + EXPECT_FALSE(S1.isDefined()) << "Symbol should not be defined"; + EXPECT_TRUE(S1.isExternal()) << "Symbol should be external"; + EXPECT_FALSE(S1.isAbsolute()) << "Symbol should not be absolute"; + EXPECT_EQ(S1.getAddress(), 0U) << "Unexpected symbol address"; + + EXPECT_EQ( + std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 0U) + << "Unexpected number of defined symbols"; + EXPECT_EQ( + std::distance(G.external_symbols().begin(), G.external_symbols().end()), + 1U) + << "Unexpected number of external symbols"; +} + +TEST(LinkGraphTest, MakeDefined) { + // Check that we can make an external symbol defined. + LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little, + getGenericEdgeKindName); + auto &Sec = G.createSection("__data", RWFlags); + + // Create an initial block. + auto &B1 = G.createContentBlock(Sec, BlockContent, 0x1000, 8, 0); + + // Add an external symbol. + auto &S1 = G.addExternalSymbol("S1", 4, Linkage::Strong); + + EXPECT_FALSE(S1.isDefined()) << "Symbol should not be defined"; + EXPECT_TRUE(S1.isExternal()) << "Symbol should be external"; + EXPECT_FALSE(S1.isAbsolute()) << "Symbol should not be absolute"; + EXPECT_EQ(S1.getAddress(), 0U) << "Unexpected symbol address"; + + EXPECT_EQ( + std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 0U) + << "Unexpected number of defined symbols"; + EXPECT_EQ( + std::distance(G.external_symbols().begin(), G.external_symbols().end()), + 1U) + << "Unexpected number of external symbols"; + + // Make S1 defined, confirm that its flags are updated and that it is + // moved from the defined symbols to the externals list. + G.makeDefined(S1, B1, 0, 4, Linkage::Strong, Scope::Default, false); + + EXPECT_TRUE(S1.isDefined()) << "Symbol should be defined"; + EXPECT_FALSE(S1.isExternal()) << "Symbol should not be external"; + EXPECT_FALSE(S1.isAbsolute()) << "Symbol should not be absolute"; + EXPECT_TRUE(&S1.getBlock()) << "Symbol should have a non-null block"; + EXPECT_EQ(S1.getAddress(), 0x1000U) << "Unexpected symbol address"; + + EXPECT_EQ( + std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 1U) + << "Unexpected number of defined symbols"; + EXPECT_EQ( + std::distance(G.external_symbols().begin(), G.external_symbols().end()), + 0U) + << "Unexpected number of external symbols"; +} + TEST(LinkGraphTest, SplitBlock) { // Check that the LinkGraph::splitBlock test works as expected. - - const char BlockContentBytes[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, - 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, - 0x1C, 0x1D, 0x1E, 0x1F, 0x00}; - StringRef BlockContent(BlockContentBytes); - LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little, getGenericEdgeKindName); auto &Sec = G.createSection("__data", RWFlags);