Skip to content

Commit

Permalink
[ORC] Introduce IRPartitionLayer for common partition functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunho committed Oct 11, 2024
1 parent 2c01b27 commit 04af63b
Show file tree
Hide file tree
Showing 10 changed files with 417 additions and 322 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/IRPartitionLayer.h"
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
Expand Down Expand Up @@ -48,6 +49,7 @@ class KaleidoscopeJIT {
RTDyldObjectLinkingLayer ObjectLayer;
IRCompileLayer CompileLayer;
IRTransformLayer OptimizeLayer;
IRPartitionLayer IPLayer;
CompileOnDemandLayer CODLayer;

JITDylib &MainJD;
Expand All @@ -68,8 +70,8 @@ class KaleidoscopeJIT {
CompileLayer(*this->ES, ObjectLayer,
std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
OptimizeLayer(*this->ES, CompileLayer, optimizeModule),
CODLayer(*this->ES, OptimizeLayer,
this->EPCIU->getLazyCallThroughManager(),
IPLayer(*this->ES, OptimizeLayer),
CODLayer(*this->ES, IPLayer, this->EPCIU->getLazyCallThroughManager(),
[this] { return this->EPCIU->createIndirectStubsManager(); }),
MainJD(this->ES->createBareJITDylib("<main>")) {
MainJD.addGenerator(
Expand Down
9 changes: 6 additions & 3 deletions llvm/examples/SpeculativeJIT/SpeculativeJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/IRPartitionLayer.h"
#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
Expand Down Expand Up @@ -109,13 +110,14 @@ class SpeculativeJIT {
IndirectStubsManagerBuilderFunction ISMBuilder,
std::unique_ptr<DynamicLibrarySearchGenerator> ProcessSymbolsGenerator)
: ES(std::move(ES)), DL(std::move(DL)),
MainJD(this->ES->createBareJITDylib("<main>")), LCTMgr(std::move(LCTMgr)),
MainJD(this->ES->createBareJITDylib("<main>")),
LCTMgr(std::move(LCTMgr)),
CompileLayer(*this->ES, ObjLayer,
std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
S(Imps, *this->ES),
SpeculateLayer(*this->ES, CompileLayer, S, Mangle, BlockFreqQuery()),
CODLayer(*this->ES, SpeculateLayer, *this->LCTMgr,
std::move(ISMBuilder)) {
IPLayer(*this->ES, SpeculateLayer),
CODLayer(*this->ES, IPLayer, *this->LCTMgr, std::move(ISMBuilder)) {
MainJD.addGenerator(std::move(ProcessSymbolsGenerator));
this->CODLayer.setImplMap(&Imps);
ExitOnErr(S.addSpeculationRuntime(MainJD, Mangle));
Expand All @@ -141,6 +143,7 @@ class SpeculativeJIT {
Speculator S;
RTDyldObjectLinkingLayer ObjLayer{*ES, createMemMgr};
IRSpeculationLayer SpeculateLayer;
IRPartitionLayer IPLayer;
CompileOnDemandLayer CODLayer;
};

Expand Down
36 changes: 2 additions & 34 deletions llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,37 +53,15 @@ namespace llvm {
namespace orc {

class CompileOnDemandLayer : public IRLayer {
friend class PartitioningIRMaterializationUnit;

public:
/// Builder for IndirectStubsManagers.
using IndirectStubsManagerBuilder =
std::function<std::unique_ptr<IndirectStubsManager>()>;

using GlobalValueSet = std::set<const GlobalValue *>;

/// Partitioning function.
using PartitionFunction =
std::function<std::optional<GlobalValueSet>(GlobalValueSet Requested)>;

/// 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);

/// Construct a CompileOnDemandLayer.
CompileOnDemandLayer(ExecutionSession &ES, IRLayer &BaseLayer,
LazyCallThroughManager &LCTMgr,
IndirectStubsManagerBuilder BuildIndirectStubsManager);

/// Sets the partition function.
void setPartitionFunction(PartitionFunction Partition);

LazyCallThroughManager &LCTMgr,
IndirectStubsManagerBuilder BuildIndirectStubsManager);
/// Sets the ImplSymbolMap
void setImplMap(ImplSymbolMap *Imp);

Expand All @@ -110,22 +88,12 @@ class CompileOnDemandLayer : public IRLayer {

PerDylibResources &getPerDylibResources(JITDylib &TargetD);

void cleanUpModule(Module &M);

void expandPartition(GlobalValueSet &Partition);

void emitPartition(std::unique_ptr<MaterializationResponsibility> R,
ThreadSafeModule TSM,
IRMaterializationUnit::SymbolNameToDefinitionMap Defs);

mutable std::mutex CODLayerMutex;

IRLayer &BaseLayer;
LazyCallThroughManager &LCTMgr;
IndirectStubsManagerBuilder BuildIndirectStubsManager;
PerDylibResourcesMap DylibResources;
PartitionFunction Partition = compileRequested;
SymbolLinkagePromoter PromoteSymbols;
ImplSymbolMap *AliaseeImpls = nullptr;
};

Expand Down
85 changes: 85 additions & 0 deletions llvm/include/llvm/ExecutionEngine/Orc/IRPartitionLayer.h
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
7 changes: 4 additions & 3 deletions llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/IRPartitionLayer.h"
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
Expand Down Expand Up @@ -271,9 +272,8 @@ class LLLazyJIT : public LLJIT {
public:

/// Sets the partition function.
void
setPartitionFunction(CompileOnDemandLayer::PartitionFunction Partition) {
CODLayer->setPartitionFunction(std::move(Partition));
void setPartitionFunction(IRPartitionLayer::PartitionFunction Partition) {
IPLayer->setPartitionFunction(std::move(Partition));
}

/// Returns a reference to the on-demand layer.
Expand All @@ -293,6 +293,7 @@ class LLLazyJIT : public LLJIT {
LLLazyJIT(LLLazyJITBuilderState &S, Error &Err);

std::unique_ptr<LazyCallThroughManager> LCTMgr;
std::unique_ptr<IRPartitionLayer> IPLayer;
std::unique_ptr<CompileOnDemandLayer> CODLayer;
};

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_llvm_component_library(LLVMOrcJIT
IndirectionUtils.cpp
IRCompileLayer.cpp
IRTransformLayer.cpp
IRPartitionLayer.cpp
JITTargetMachineBuilder.cpp
LazyReexports.cpp
Layer.cpp
Expand Down
Loading

0 comments on commit 04af63b

Please sign in to comment.