Skip to content

Commit 6bcb74a

Browse files
committed
Replace CodeExtractor callbacks with subclasses and simplify their creation based on OutlineInfo structures
1 parent d8132f4 commit 6bcb74a

File tree

7 files changed

+309
-206
lines changed

7 files changed

+309
-206
lines changed

llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
namespace llvm {
3333
class CanonicalLoopInfo;
34+
class CodeExtractor;
3435
class ScanInfo;
3536
struct TargetRegionEntryInfo;
3637
class OffloadEntriesInfoManager;
@@ -2260,27 +2261,31 @@ class OpenMPIRBuilder {
22602261
/// during finalization.
22612262
struct OutlineInfo {
22622263
using PostOutlineCBTy = std::function<void(Function &)>;
2263-
using CustomArgAllocatorCBTy = std::function<Instruction *(
2264-
BasicBlock *, BasicBlock::iterator, Type *, const Twine &)>;
2265-
using CustomArgDeallocatorCBTy = std::function<Instruction *(
2266-
BasicBlock *, BasicBlock::iterator, Value *, Type *)>;
22672264
PostOutlineCBTy PostOutlineCB;
2268-
CustomArgAllocatorCBTy CustomArgAllocatorCB;
2269-
CustomArgDeallocatorCBTy CustomArgDeallocatorCB;
22702265
BasicBlock *EntryBB, *ExitBB, *OuterAllocaBB;
22712266
SmallVector<Value *, 2> ExcludeArgsFromAggregate;
22722267

2268+
LLVM_ABI virtual ~OutlineInfo() = default;
2269+
22732270
/// Collect all blocks in between EntryBB and ExitBB in both the given
22742271
/// vector and set.
22752272
LLVM_ABI void collectBlocks(SmallPtrSetImpl<BasicBlock *> &BlockSet,
22762273
SmallVectorImpl<BasicBlock *> &BlockVector);
22772274

2275+
/// Create a CodeExtractor instance based on the information stored in this
2276+
/// structure, the list of collected blocks from a previous call to
2277+
/// \c collectBlocks and a flag stating whether arguments must be passed in
2278+
/// address space 0.
2279+
LLVM_ABI virtual std::unique_ptr<CodeExtractor>
2280+
createCodeExtractor(ArrayRef<BasicBlock *> Blocks,
2281+
bool ArgsInZeroAddressSpace, Twine Suffix = Twine(""));
2282+
22782283
/// Return the function that contains the region to be outlined.
22792284
Function *getFunction() const { return EntryBB->getParent(); }
22802285
};
22812286

22822287
/// Collection of regions that need to be outlined during finalization.
2283-
SmallVector<OutlineInfo, 16> OutlineInfos;
2288+
SmallVector<std::unique_ptr<OutlineInfo>, 16> OutlineInfos;
22842289

22852290
/// A collection of candidate target functions that's constant allocas will
22862291
/// attempt to be raised on a call of finalize after all currently enqueued
@@ -2295,7 +2300,9 @@ class OpenMPIRBuilder {
22952300
std::forward_list<ScanInfo> ScanInfos;
22962301

22972302
/// Add a new region that will be outlined later.
2298-
void addOutlineInfo(OutlineInfo &&OI) { OutlineInfos.emplace_back(OI); }
2303+
void addOutlineInfo(std::unique_ptr<OutlineInfo> &&OI) {
2304+
OutlineInfos.emplace_back(std::move(OI));
2305+
}
22992306

23002307
/// An ordered map of auto-generated variables to their unique names.
23012308
/// It stores variables with the following names: 1) ".gomp_critical_user_" +

llvm/include/llvm/Transforms/Utils/CodeExtractor.h

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
namespace llvm {
2525

2626
template <typename PtrType> class SmallPtrSetImpl;
27+
class AddrSpaceCastInst;
2728
class AllocaInst;
2829
class BlockFrequency;
2930
class BlockFrequencyInfo;
@@ -85,10 +86,6 @@ class CodeExtractorAnalysisCache {
8586
/// 3) Add allocas for any scalar outputs, adding all of the outputs' allocas
8687
/// as arguments, and inserting stores to the arguments for any scalars.
8788
class CodeExtractor {
88-
using CustomArgAllocatorCBTy = std::function<Instruction *(
89-
BasicBlock *, BasicBlock::iterator, Type *, const Twine &)>;
90-
using CustomArgDeallocatorCBTy = std::function<Instruction *(
91-
BasicBlock *, BasicBlock::iterator, Value *, Type *)>;
9289
using ValueSet = SetVector<Value *>;
9390

9491
// Various bits of state computed on construction.
@@ -103,6 +100,14 @@ class CodeExtractorAnalysisCache {
103100
/// will be placed in the entry block of the function.
104101
BasicBlock *AllocationBlock;
105102

103+
/// A block outside of the extraction set where deallocations for
104+
/// intermediate allocations can be placed inside. Not used for
105+
/// automatically deallocated memory (e.g. `alloca`), which is the default.
106+
///
107+
/// If it is null and needed, the end of the replacement basic block will be
108+
/// used to place deallocations.
109+
BasicBlock *DeallocationBlock;
110+
106111
/// If true, varargs functions can be extracted.
107112
bool AllowVarArgs;
108113

@@ -137,25 +142,6 @@ class CodeExtractorAnalysisCache {
137142
/// space.
138143
bool ArgsInZeroAddressSpace;
139144

140-
/// If set, this callback will be used to allocate the arguments in the
141-
/// caller before passing it to the outlined function holding the extracted
142-
/// piece of code.
143-
CustomArgAllocatorCBTy *CustomArgAllocatorCB;
144-
145-
/// A block outside of the extraction set where previously introduced
146-
/// intermediate allocations can be deallocated. This is only used when a
147-
/// custom deallocator is specified.
148-
BasicBlock *DeallocationBlock;
149-
150-
/// If set, this callback will be used to deallocate the arguments in the
151-
/// caller after running the outlined function holding the extracted piece
152-
/// of code. It will not be called if a custom allocator isn't also present.
153-
///
154-
/// By default, this will be done at the end of the basic block containing
155-
/// the call to the outlined function, except if a deallocation block is
156-
/// specified. In that case, that will take precedence.
157-
CustomArgDeallocatorCBTy *CustomArgDeallocatorCB;
158-
159145
public:
160146
/// Create a code extractor for a sequence of blocks.
161147
///
@@ -169,23 +155,23 @@ class CodeExtractorAnalysisCache {
169155
/// however code extractor won't validate whether extraction is legal.
170156
/// Any new allocations will be placed in the AllocationBlock, unless
171157
/// it is null, in which case it will be placed in the entry block of
172-
/// the function from which the code is being extracted.
158+
/// the function from which the code is being extracted. Explicit
159+
/// deallocations for the aforementioned allocations will be placed in the
160+
/// DeallocationBlock or the end of the replacement block, if needed.
173161
/// If ArgsInZeroAddressSpace param is set to true, then the aggregate
174162
/// param pointer of the outlined function is declared in zero address
175-
/// space. If a CustomArgAllocatorCB callback is specified, it will be used
176-
/// to allocate any structures or variable copies needed to pass arguments
177-
/// to the outlined function, rather than using regular allocas.
163+
/// space.
178164
LLVM_ABI
179165
CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT = nullptr,
180166
bool AggregateArgs = false, BlockFrequencyInfo *BFI = nullptr,
181167
BranchProbabilityInfo *BPI = nullptr,
182168
AssumptionCache *AC = nullptr, bool AllowVarArgs = false,
183169
bool AllowAlloca = false,
184170
BasicBlock *AllocationBlock = nullptr,
185-
std::string Suffix = "", bool ArgsInZeroAddressSpace = false,
186-
CustomArgAllocatorCBTy *CustomArgAllocatorCB = nullptr,
187171
BasicBlock *DeallocationBlock = nullptr,
188-
CustomArgDeallocatorCBTy *CustomArgDeallocatorCB = nullptr);
172+
std::string Suffix = "", bool ArgsInZeroAddressSpace = false);
173+
174+
LLVM_ABI virtual ~CodeExtractor() = default;
189175

190176
/// Perform the extraction, returning the new function.
191177
///
@@ -271,6 +257,19 @@ class CodeExtractorAnalysisCache {
271257
/// region, passing it instead as a scalar.
272258
LLVM_ABI void excludeArgFromAggregate(Value *Arg);
273259

260+
protected:
261+
/// Allocate an intermediate variable at the specified point.
262+
LLVM_ABI virtual Instruction *
263+
allocateVar(BasicBlock *BB, BasicBlock::iterator AllocIP, Type *VarType,
264+
const Twine &Name = Twine(""),
265+
AddrSpaceCastInst **CastedAlloc = nullptr);
266+
267+
/// Deallocate a previously-allocated intermediate variable at the specified
268+
/// point.
269+
LLVM_ABI virtual Instruction *deallocateVar(BasicBlock *BB,
270+
BasicBlock::iterator DeallocIP,
271+
Value *Var, Type *VarType);
272+
274273
private:
275274
struct LifetimeMarkerInfo {
276275
bool SinkLifeStart = false;

0 commit comments

Comments
 (0)