[llvm] r251658 - [Orc] Teach IndirectStubsManager to manage an expandable pool of stubs, rather

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 29 15:56:27 PDT 2015


This change is essentially unrelated to that. This is really about allowing
you to request additional stubs at any point in your execution, rather than
having to request all stubs up front.

The lightweight stub creation patch that went in a week or so back was an
emphatic success: It reduced the cost for creating a stub by several orders
of magnitude.

- Lang.

On Thu, Oct 29, 2015 at 3:15 PM, David Blaikie <dblaikie at gmail.com> wrote:

>
>
> On Thu, Oct 29, 2015 at 3:04 PM, Lang Hames via llvm-commits <
> llvm-commits at lists.llvm.org> wrote:
>
>> Author: lhames
>> Date: Thu Oct 29 17:04:22 2015
>> New Revision: 251658
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=251658&view=rev
>> Log:
>> [Orc] Teach IndirectStubsManager to manage an expandable pool of stubs,
>> rather
>> than a pre-allocated slab of stubs. Also add a convenience method for
>> creating a
>> single stub, rather than a whole block a time.
>>
>
> Even with the lighter weight stub construction this still seems to be a
> hot point & worth removing the upfront O(sizeof initial module) cost with
> the smaller factor? (I know we chatted about it a week or two ago & you
> weren't sure if that'd be the case)
>
>
>>
>>
>> Modified:
>>     llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
>>     llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h
>>     llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp
>>     llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp
>>
>> Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
>> URL:
>> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h?rev=251658&r1=251657&r2=251658&view=diff
>>
>> ==============================================================================
>> --- llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
>> (original)
>> +++ llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h Thu
>> Oct 29 17:04:22 2015
>> @@ -234,6 +234,10 @@ public:
>>
>>    virtual ~IndirectStubsManagerBase() {}
>>
>> +  /// @brief Create a single stub with the given name, target address
>> and flags.
>> +  virtual std::error_code createStub(StringRef StubName, TargetAddress
>> StubAddr,
>> +                                     JITSymbolFlags StubFlags) = 0;
>> +
>>    /// @brief Create StubInits.size() stubs with the given names, target
>>    ///        addresses, and flags.
>>    virtual std::error_code createStubs(const StubInitsMap &StubInits) = 0;
>> @@ -252,25 +256,29 @@ private:
>>    virtual void anchor();
>>  };
>>
>> -/// @brief IndirectStubsManager implementation for a concrete target,
>> e.g. OrcX86_64.
>> -///        (See OrcTargetSupport.h).
>> +/// @brief IndirectStubsManager implementation for a concrete target,
>> e.g.
>> +///        OrcX86_64. (See OrcTargetSupport.h).
>>  template <typename TargetT>
>>  class IndirectStubsManager : public IndirectStubsManagerBase {
>>  public:
>>
>> -  std::error_code
>> -  createStubs(const StubInitsMap &StubInits) override {
>> -    if (auto EC = TargetT::emitIndirectStubsBlock(IndirectStubsInfo,
>> -                                                  StubInits.size(),
>> -                                                  nullptr))
>> +  std::error_code createStub(StringRef StubName, TargetAddress StubAddr,
>> +                             JITSymbolFlags StubFlags) override {
>> +    if (auto EC = reserveStubs(1))
>>        return EC;
>>
>> -    unsigned I = 0;
>> -    for (auto &Entry : StubInits) {
>> -      *IndirectStubsInfo.getPtr(I) =
>> -
>> reinterpret_cast<void*>(static_cast<uintptr_t>(Entry.second.first));
>> -      StubIndexes[Entry.first()] = std::make_pair(I++,
>> Entry.second.second);
>> -    }
>> +    createStubInternal(StubName, StubAddr, StubFlags);
>> +
>> +    return std::error_code();
>> +  }
>> +
>> +  std::error_code createStubs(const StubInitsMap &StubInits) override {
>> +    if (auto EC = reserveStubs(StubInits.size()))
>> +      return EC;
>> +
>> +    for (auto &Entry : StubInits)
>> +      createStubInternal(Entry.first(), Entry.second.first,
>> +                         Entry.second.second);
>>
>>      return std::error_code();
>>    }
>> @@ -279,7 +287,8 @@ public:
>>      auto I = StubIndexes.find(Name);
>>      if (I == StubIndexes.end())
>>        return nullptr;
>> -    void *StubAddr = IndirectStubsInfo.getStub(I->second.first);
>> +    auto Key = I->second.first;
>> +    void *StubAddr = IndirectStubsInfos[Key.first].getStub(Key.second);
>>      assert(StubAddr && "Missing stub address");
>>      auto StubTargetAddr =
>>        static_cast<TargetAddress>(reinterpret_cast<uintptr_t>(StubAddr));
>> @@ -293,23 +302,54 @@ public:
>>      auto I = StubIndexes.find(Name);
>>      if (I == StubIndexes.end())
>>        return nullptr;
>> -    void *PtrAddr = IndirectStubsInfo.getPtr(StubIndexes[Name].first);
>> +    auto Key = I->second.first;
>> +    void *PtrAddr = IndirectStubsInfos[Key.first].getPtr(Key.second);
>>      assert(PtrAddr && "Missing pointer address");
>>      auto PtrTargetAddr =
>>        static_cast<TargetAddress>(reinterpret_cast<uintptr_t>(PtrAddr));
>> -    return JITSymbol(PtrTargetAddr, JITSymbolFlags::None);
>> +    return JITSymbol(PtrTargetAddr, I->second.second);
>>    }
>>
>>    std::error_code updatePointer(StringRef Name, TargetAddress NewAddr)
>> override {
>> -    assert(StubIndexes.count(Name) && "No stub pointer for symbol");
>> -    *IndirectStubsInfo.getPtr(StubIndexes[Name].first) =
>> +    auto I = StubIndexes.find(Name);
>> +    assert(I != StubIndexes.end() && "No stub pointer for symbol");
>> +    auto Key = I->second.first;
>> +    *IndirectStubsInfos[Key.first].getPtr(Key.second) =
>>        reinterpret_cast<void*>(static_cast<uintptr_t>(NewAddr));
>>      return std::error_code();
>>    }
>>
>>  private:
>> -  typename TargetT::IndirectStubsInfo IndirectStubsInfo;
>> -  StringMap<std::pair<unsigned, JITSymbolFlags>> StubIndexes;
>> +
>> +  std::error_code reserveStubs(unsigned NumStubs) {
>> +    if (NumStubs <= FreeStubs.size())
>> +      return std::error_code();
>> +
>> +    unsigned NewStubsRequired = NumStubs - FreeStubs.size();
>> +    unsigned NewBlockId = IndirectStubsInfos.size();
>> +    typename TargetT::IndirectStubsInfo ISI;
>> +    if (auto EC = TargetT::emitIndirectStubsBlock(ISI, NewStubsRequired,
>> +                                                  nullptr))
>> +      return EC;
>> +    for (unsigned I = 0; I < ISI.getNumStubs(); ++I)
>> +      FreeStubs.push_back(std::make_pair(NewBlockId, I));
>> +    IndirectStubsInfos.push_back(std::move(ISI));
>> +    return std::error_code();
>> +  }
>> +
>> +  void createStubInternal(StringRef StubName, TargetAddress InitAddr,
>> +                          JITSymbolFlags StubFlags) {
>> +    auto Key = FreeStubs.back();
>> +    FreeStubs.pop_back();
>> +    *IndirectStubsInfos[Key.first].getPtr(Key.second) =
>> +      reinterpret_cast<void*>(static_cast<uintptr_t>(InitAddr));
>> +    StubIndexes[StubName] = std::make_pair(Key, StubFlags);
>> +  }
>> +
>> +  std::vector<typename TargetT::IndirectStubsInfo> IndirectStubsInfos;
>> +  typedef std::pair<uint16_t, uint16_t> StubKey;
>> +  std::vector<StubKey> FreeStubs;
>> +  StringMap<std::pair<StubKey, JITSymbolFlags>> StubIndexes;
>>  };
>>
>>  /// @brief Build a function pointer of FunctionType with the given
>> constant
>>
>> Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h
>> URL:
>> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h?rev=251658&r1=251657&r2=251658&view=diff
>>
>> ==============================================================================
>> --- llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h
>> (original)
>> +++ llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h Thu
>> Oct 29 17:04:22 2015
>> @@ -59,6 +59,8 @@ public:
>>      const static unsigned PtrSize = 8;
>>
>>      IndirectStubsInfo() : NumStubs(0) {}
>> +    IndirectStubsInfo(IndirectStubsInfo&&);
>> +    IndirectStubsInfo& operator=(IndirectStubsInfo&&);
>>      ~IndirectStubsInfo();
>>
>>      /// @brief Number of stubs in this block.
>>
>> Modified: llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp?rev=251658&r1=251657&r2=251658&view=diff
>>
>> ==============================================================================
>> --- llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp (original)
>> +++ llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp Thu Oct 29
>> 17:04:22 2015
>> @@ -144,6 +144,22 @@ OrcX86_64::insertCompileCallbackTrampoli
>>    return GetLabelName;
>>  }
>>
>> +OrcX86_64::IndirectStubsInfo::IndirectStubsInfo(IndirectStubsInfo
>> &&Other) {
>> +  StubsBlock = std::move(Other.StubsBlock);
>> +  PtrsBlock = std::move(Other.PtrsBlock);
>> +  Other.StubsBlock = sys::MemoryBlock();
>> +  Other.PtrsBlock = sys::MemoryBlock();
>> +}
>> +
>> +OrcX86_64::IndirectStubsInfo&
>> +OrcX86_64::IndirectStubsInfo::operator=(IndirectStubsInfo &&Other) {
>> +  StubsBlock = std::move(Other.StubsBlock);
>> +  PtrsBlock = std::move(Other.PtrsBlock);
>> +  Other.StubsBlock = sys::MemoryBlock();
>> +  Other.PtrsBlock = sys::MemoryBlock();
>> +  return *this;
>> +}
>> +
>>  OrcX86_64::IndirectStubsInfo::~IndirectStubsInfo() {
>>    sys::Memory::releaseMappedMemory(StubsBlock);
>>    sys::Memory::releaseMappedMemory(PtrsBlock);
>>
>> Modified:
>> llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp?rev=251658&r1=251657&r2=251658&view=diff
>>
>> ==============================================================================
>> --- llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp
>> (original)
>> +++ llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp
>> Thu Oct 29 17:04:22 2015
>> @@ -33,6 +33,11 @@ public:
>>
>>  class DummyStubsManager : public orc::IndirectStubsManagerBase {
>>  public:
>> +  std::error_code createStub(StringRef StubName, TargetAddress InitAddr,
>> +                             JITSymbolFlags Flags) override {
>> +    llvm_unreachable("Not implemented");
>> +  }
>> +
>>    std::error_code createStubs(const StubInitsMap &StubInits) override {
>>      llvm_unreachable("Not implemented");
>>    }
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151029/55a0670f/attachment.html>


More information about the llvm-commits mailing list