-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
[ORC] Implement basic reoptimization. #67050
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 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,85 @@ | ||
//===- IRPartitionLayer.h - Partition IR module on lookup -------*- C++ -*-===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// JIT layer for breaking up modules into smaller submodules that only contains | ||
// looked up symbols. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_EXECUTIONENGINE_ORC_IRPARTITIONLAYER_H | ||
#define LLVM_EXECUTIONENGINE_ORC_IRPARTITIONLAYER_H | ||
|
||
#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h" | ||
#include "llvm/ExecutionEngine/Orc/Layer.h" | ||
#include "llvm/IR/Attributes.h" | ||
#include "llvm/IR/Constant.h" | ||
#include "llvm/IR/Constants.h" | ||
#include "llvm/IR/DataLayout.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/GlobalAlias.h" | ||
#include "llvm/IR/GlobalValue.h" | ||
#include "llvm/IR/GlobalVariable.h" | ||
#include "llvm/IR/Instruction.h" | ||
#include "llvm/IR/Mangler.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/IR/Type.h" | ||
|
||
namespace llvm { | ||
namespace orc { | ||
|
||
/// A layer that breaks up IR modules into smaller submodules that only contains | ||
/// looked up symbols. | ||
class IRPartitionLayer : public IRLayer { | ||
friend class PartitioningIRMaterializationUnit; | ||
|
||
public: | ||
using GlobalValueSet = std::set<const GlobalValue *>; | ||
|
||
/// Partitioning function. | ||
using PartitionFunction = | ||
std::function<std::optional<GlobalValueSet>(GlobalValueSet Requested)>; | ||
|
||
/// Construct a IRPartitionLayer. | ||
IRPartitionLayer(ExecutionSession &ES, IRLayer &BaseLayer); | ||
|
||
/// Off-the-shelf partitioning which compiles all requested symbols (usually | ||
/// a single function at a time). | ||
static std::optional<GlobalValueSet> | ||
compileRequested(GlobalValueSet Requested); | ||
|
||
/// Off-the-shelf partitioning which compiles whole modules whenever any | ||
/// symbol in them is requested. | ||
static std::optional<GlobalValueSet> | ||
compileWholeModule(GlobalValueSet Requested); | ||
|
||
/// Sets the partition function. | ||
void setPartitionFunction(PartitionFunction Partition); | ||
|
||
/// Emits the given module. This should not be called by clients: it will be | ||
/// called by the JIT when a definition added via the add method is requested. | ||
void emit(std::unique_ptr<MaterializationResponsibility> R, | ||
ThreadSafeModule TSM) override; | ||
|
||
private: | ||
void cleanUpModule(Module &M); | ||
|
||
void expandPartition(GlobalValueSet &Partition); | ||
|
||
void emitPartition(std::unique_ptr<MaterializationResponsibility> R, | ||
ThreadSafeModule TSM, | ||
IRMaterializationUnit::SymbolNameToDefinitionMap Defs); | ||
|
||
IRLayer &BaseLayer; | ||
PartitionFunction Partition = compileRequested; | ||
SymbolLinkagePromoter PromoteSymbols; | ||
}; | ||
|
||
} // namespace orc | ||
} // namespace llvm | ||
|
||
#endif |
107 changes: 107 additions & 0 deletions
107
llvm/include/llvm/ExecutionEngine/Orc/JITLinkRedirectableSymbolManager.h
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//===- JITLinkRedirectableSymbolManager.h - JITLink redirection -*- C++ -*-===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Redirectable Symbol Manager implementation using JITLink | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_EXECUTIONENGINE_ORC_JITLINKREDIRECABLEMANAGER_H | ||
#define LLVM_EXECUTIONENGINE_ORC_JITLINKREDIRECABLEMANAGER_H | ||
Comment on lines
+13
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include guard should be |
||
|
||
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" | ||
#include "llvm/ExecutionEngine/Orc/RedirectionManager.h" | ||
#include "llvm/Support/StringSaver.h" | ||
|
||
namespace llvm { | ||
namespace orc { | ||
|
||
class JITLinkRedirectableSymbolManager : public RedirectableSymbolManager, | ||
public ResourceManager { | ||
public: | ||
/// Create redirection manager that uses JITLink based implementaion. | ||
static Expected<std::unique_ptr<RedirectableSymbolManager>> | ||
Create(ObjectLinkingLayer &ObjLinkingLayer, JITDylib &JD) { | ||
Error Err = Error::success(); | ||
auto RM = std::unique_ptr<RedirectableSymbolManager>( | ||
new JITLinkRedirectableSymbolManager(ObjLinkingLayer, JD, Err)); | ||
if (Err) | ||
return Err; | ||
return std::move(RM); | ||
} | ||
|
||
void emitRedirectableSymbols(std::unique_ptr<MaterializationResponsibility> R, | ||
const SymbolAddrMap &InitialDests) override; | ||
|
||
Error redirect(JITDylib &TargetJD, const SymbolAddrMap &NewDests) override; | ||
|
||
Error handleRemoveResources(JITDylib &TargetJD, ResourceKey K) override; | ||
|
||
void handleTransferResources(JITDylib &TargetJD, ResourceKey DstK, | ||
ResourceKey SrcK) override; | ||
|
||
private: | ||
using StubHandle = unsigned; | ||
constexpr static unsigned StubBlockSize = 256; | ||
constexpr static StringRef JumpStubPrefix = "$__IND_JUMP_STUBS"; | ||
constexpr static StringRef StubPtrPrefix = "$IND_JUMP_PTR_"; | ||
constexpr static StringRef JumpStubTableName = "$IND_JUMP_"; | ||
constexpr static StringRef StubPtrTableName = "$__IND_JUMP_PTRS"; | ||
|
||
JITLinkRedirectableSymbolManager(ObjectLinkingLayer &ObjLinkingLayer, | ||
JITDylib &JD, Error &Err) | ||
: ObjLinkingLayer(ObjLinkingLayer), JD(JD), | ||
AnonymousPtrCreator(jitlink::getAnonymousPointerCreator( | ||
ObjLinkingLayer.getExecutionSession().getTargetTriple())), | ||
PtrJumpStubCreator(jitlink::getPointerJumpStubCreator( | ||
ObjLinkingLayer.getExecutionSession().getTargetTriple())) { | ||
if (!AnonymousPtrCreator || !PtrJumpStubCreator) | ||
Err = make_error<StringError>("Architecture not supported", | ||
inconvertibleErrorCode()); | ||
if (Err) | ||
return; | ||
ObjLinkingLayer.getExecutionSession().registerResourceManager(*this); | ||
} | ||
|
||
~JITLinkRedirectableSymbolManager() { | ||
ObjLinkingLayer.getExecutionSession().deregisterResourceManager(*this); | ||
} | ||
|
||
StringRef JumpStubSymbolName(unsigned I) { | ||
return *ObjLinkingLayer.getExecutionSession().intern( | ||
(JumpStubPrefix + Twine(I)).str()); | ||
} | ||
|
||
StringRef StubPtrSymbolName(unsigned I) { | ||
return *ObjLinkingLayer.getExecutionSession().intern( | ||
(StubPtrPrefix + Twine(I)).str()); | ||
} | ||
|
||
unsigned GetNumAvailableStubs() const { return AvailableStubs.size(); } | ||
|
||
Error redirectInner(JITDylib &TargetJD, const SymbolAddrMap &NewDests); | ||
Error grow(unsigned Need); | ||
|
||
ObjectLinkingLayer &ObjLinkingLayer; | ||
JITDylib &JD; | ||
jitlink::AnonymousPointerCreator AnonymousPtrCreator; | ||
jitlink::PointerJumpStubCreator PtrJumpStubCreator; | ||
|
||
std::vector<StubHandle> AvailableStubs; | ||
using SymbolToStubMap = DenseMap<SymbolStringPtr, StubHandle>; | ||
DenseMap<JITDylib *, SymbolToStubMap> SymbolToStubs; | ||
std::vector<ExecutorSymbolDef> JumpStubs; | ||
std::vector<ExecutorSymbolDef> StubPointers; | ||
DenseMap<ResourceKey, std::vector<SymbolStringPtr>> TrackedResources; | ||
|
||
std::mutex Mutex; | ||
}; | ||
|
||
} // namespace orc | ||
} // namespace llvm | ||
|
||
#endif |
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
Oops, something went wrong.
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.
It'd be good to add a class comment explaining the layer's behavior. (I know the file comment covers this, but people looking up the type docs may not see that).