xref: /freebsd/contrib/llvm-project/llvm/include/llvm/ExecutionEngine/Orc/IRPartitionLayer.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- IRPartitionLayer.h - Partition IR module on lookup -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // JIT layer for breaking up modules into smaller submodules that only contains
10 // looked up symbols.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_IRPARTITIONLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_IRPARTITIONLAYER_H
16 
17 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
18 #include "llvm/ExecutionEngine/Orc/Layer.h"
19 #include "llvm/IR/Attributes.h"
20 #include "llvm/IR/Constant.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/GlobalAlias.h"
25 #include "llvm/IR/GlobalValue.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/Instruction.h"
28 #include "llvm/IR/Mangler.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/Support/Compiler.h"
32 
33 namespace llvm {
34 namespace orc {
35 
36 /// A layer that breaks up IR modules into smaller submodules that only contains
37 /// looked up symbols.
38 class LLVM_ABI IRPartitionLayer : public IRLayer {
39   friend class PartitioningIRMaterializationUnit;
40 
41 public:
42   using GlobalValueSet = std::set<const GlobalValue *>;
43 
44   /// Partitioning function.
45   using PartitionFunction =
46       std::function<std::optional<GlobalValueSet>(GlobalValueSet Requested)>;
47 
48   /// Construct a IRPartitionLayer.
49   IRPartitionLayer(ExecutionSession &ES, IRLayer &BaseLayer);
50 
51   /// Off-the-shelf partitioning which compiles all requested symbols (usually
52   /// a single function at a time).
53   static std::optional<GlobalValueSet>
54   compileRequested(GlobalValueSet Requested);
55 
56   /// Off-the-shelf partitioning which compiles whole modules whenever any
57   /// symbol in them is requested.
58   static std::optional<GlobalValueSet>
59   compileWholeModule(GlobalValueSet Requested);
60 
61   /// Sets the partition function.
62   void setPartitionFunction(PartitionFunction Partition);
63 
64   /// Emits the given module. This should not be called by clients: it will be
65   /// called by the JIT when a definition added via the add method is requested.
66   void emit(std::unique_ptr<MaterializationResponsibility> R,
67             ThreadSafeModule TSM) override;
68 
69 private:
70   void cleanUpModule(Module &M);
71 
72   void expandPartition(GlobalValueSet &Partition);
73 
74   void emitPartition(std::unique_ptr<MaterializationResponsibility> R,
75                      ThreadSafeModule TSM,
76                      IRMaterializationUnit::SymbolNameToDefinitionMap Defs);
77 
78   IRLayer &BaseLayer;
79   PartitionFunction Partition = compileRequested;
80   SymbolLinkagePromoter PromoteSymbols;
81 };
82 
83 } // namespace orc
84 } // namespace llvm
85 
86 #endif
87