@@ -67,7 +67,8 @@ static Value extractSmallestConstantBoundingSize(OpBuilder &b, Location loc,
6767
6868// / Alloc a new buffer of `size`. If `dynamicBuffers` is true allocate exactly
6969// / the size needed, otherwise try to allocate a static bounding box.
70- static Value allocBuffer (Type elementType, Value size, bool dynamicBuffers,
70+ static Value allocBuffer (const LinalgPromotionOptions &options,
71+ Type elementType, Value size, bool dynamicBuffers,
7172 OperationFolder *folder,
7273 Optional<unsigned > alignment = None) {
7374 auto *ctx = size.getContext ();
@@ -78,23 +79,34 @@ static Value allocBuffer(Type elementType, Value size, bool dynamicBuffers,
7879 IntegerAttr::get (IntegerType::get (64 , ctx), alignment.getValue ());
7980 if (!dynamicBuffers)
8081 if (auto cst = size.getDefiningOp <ConstantIndexOp>())
81- return std_alloc (
82- MemRefType::get (width * cst.getValue (), IntegerType::get (8 , ctx)),
83- ValueRange{}, alignment_attr);
82+ return options.useAlloca
83+ ? std_alloca (MemRefType::get (width * cst.getValue (),
84+ IntegerType::get (8 , ctx)),
85+ ValueRange{}, alignment_attr)
86+ .value
87+ : std_alloc (MemRefType::get (width * cst.getValue (),
88+ IntegerType::get (8 , ctx)),
89+ ValueRange{}, alignment_attr)
90+ .value ;
8491 Value mul =
8592 folded_std_muli (folder, folded_std_constant_index (folder, width), size);
86- return std_alloc (MemRefType::get (-1 , IntegerType::get (8 , ctx)), mul,
87- alignment_attr);
93+ return options.useAlloca
94+ ? std_alloca (MemRefType::get (-1 , IntegerType::get (8 , ctx)), mul,
95+ alignment_attr)
96+ .value
97+ : std_alloc (MemRefType::get (-1 , IntegerType::get (8 , ctx)), mul,
98+ alignment_attr)
99+ .value ;
88100}
89101
90102// / Default allocation callback function. This allocates a promoted buffer when
91103// / no call back to do so is provided. The default is to allocate a
92104// / memref<..xi8> and return a view to get a memref type of shape
93105// / boundingSubViewSize.
94- static Optional<Value>
95- allocBufferCallBack (OpBuilder &builder, SubViewOp subView ,
96- ArrayRef<Value> boundingSubViewSize, bool dynamicBuffers,
97- Optional<unsigned > alignment, OperationFolder *folder) {
106+ static Optional<Value> defaultAllocBufferCallBack (
107+ const LinalgPromotionOptions &options, OpBuilder &builder ,
108+ SubViewOp subView, ArrayRef<Value> boundingSubViewSize, bool dynamicBuffers,
109+ Optional<unsigned > alignment, OperationFolder *folder) {
98110 ShapedType viewType = subView.getType ();
99111 int64_t rank = viewType.getRank ();
100112 (void )rank;
@@ -105,7 +117,7 @@ allocBufferCallBack(OpBuilder &builder, SubViewOp subView,
105117 Value allocSize = one;
106118 for (auto size : llvm::enumerate (boundingSubViewSize))
107119 allocSize = folded_std_muli (folder, allocSize, size.value ());
108- Value buffer = allocBuffer (viewType.getElementType (), allocSize,
120+ Value buffer = allocBuffer (options, viewType.getElementType (), allocSize,
109121 dynamicBuffers, folder, alignment);
110122 SmallVector<int64_t , 4 > dynSizes (boundingSubViewSize.size (),
111123 ShapedType::kDynamicSize );
@@ -118,10 +130,13 @@ allocBufferCallBack(OpBuilder &builder, SubViewOp subView,
118130// / Default implementation of deallocation of the buffer use for promotion. It
119131// / expects to get the same value that the default allocation method returned,
120132// / i.e. result of a ViewOp.
121- static LogicalResult deallocCallBack (OpBuilder &b, Value fullLocalView) {
133+ static LogicalResult
134+ defaultDeallocBufferCallBack (const LinalgPromotionOptions &options,
135+ OpBuilder &b, Value fullLocalView) {
122136 auto viewOp = fullLocalView.getDefiningOp <ViewOp>();
123137 assert (viewOp && " expected full local view to be a ViewOp" );
124- std_dealloc (viewOp.source ());
138+ if (!options.useAlloca )
139+ std_dealloc (viewOp.source ());
125140 return success ();
126141}
127142
@@ -182,11 +197,16 @@ LinalgOpInstancePromotionOptions::LinalgOpInstancePromotionOptions(
182197 : [&](OpBuilder &builder, SubViewOp subViewOp,
183198 ArrayRef<Value> boundingSubViewSize,
184199 OperationFolder *folder) -> Optional<Value> {
185- return allocBufferCallBack (builder, subViewOp, boundingSubViewSize,
186- dynamicBuffers, alignment, folder);
200+ return defaultAllocBufferCallBack (options, builder, subViewOp,
201+ boundingSubViewSize, dynamicBuffers,
202+ alignment, folder);
187203 });
188204 deallocationFn =
189- (options.deallocationFn ? *(options.deallocationFn ) : deallocCallBack);
205+ (options.deallocationFn
206+ ? *(options.deallocationFn )
207+ : [&](OpBuilder &b, Value buffer) {
208+ return defaultDeallocBufferCallBack (options, b, buffer);
209+ });
190210 auto defaultCopyCallBack = [&](OpBuilder &builder, Value src,
191211 Value dst) -> LogicalResult {
192212 linalg_copy (src, dst);
@@ -344,9 +364,8 @@ promoteSubViews(OpBuilder &b, LinalgOp op,
344364 }
345365
346366 // 4. Dealloc all local buffers.
347- for (const auto &pi : *promotedBuffersAndViews) {
367+ for (const auto &pi : *promotedBuffersAndViews)
348368 options.deallocationFn (b, pi.second .fullLocalView );
349- }
350369 return op;
351370}
352371
@@ -383,14 +402,17 @@ Optional<LinalgOp> mlir::linalg::promoteSubViews(OpBuilder &b,
383402namespace {
384403struct LinalgPromotionPass : public LinalgPromotionBase <LinalgPromotionPass> {
385404 LinalgPromotionPass () = default ;
386- LinalgPromotionPass (bool dynamicBuffers) {
405+ LinalgPromotionPass (bool dynamicBuffers, bool useAlloca ) {
387406 this ->dynamicBuffers = dynamicBuffers;
407+ this ->useAlloca = useAlloca;
388408 }
389409
390410 void runOnFunction () override {
391411 OperationFolder folder (&getContext ());
392412 getFunction ().walk ([this , &folder](LinalgOp op) {
393- auto options = LinalgPromotionOptions ().setDynamicBuffers (dynamicBuffers);
413+ auto options = LinalgPromotionOptions ()
414+ .setDynamicBuffers (dynamicBuffers)
415+ .setUseAlloca (useAlloca);
394416 if (failed (promoteSubviewsPrecondition (op, options)))
395417 return ;
396418 LLVM_DEBUG (llvm::dbgs () << " Promote: " << *(op.getOperation ()) << " \n " );
@@ -403,8 +425,8 @@ struct LinalgPromotionPass : public LinalgPromotionBase<LinalgPromotionPass> {
403425
404426// TODO: support more transformation options in the pass.
405427std::unique_ptr<OperationPass<FuncOp>>
406- mlir::createLinalgPromotionPass (bool dynamicBuffers) {
407- return std::make_unique<LinalgPromotionPass>(dynamicBuffers);
428+ mlir::createLinalgPromotionPass (bool dynamicBuffers, bool useAlloca ) {
429+ return std::make_unique<LinalgPromotionPass>(dynamicBuffers, useAlloca );
408430}
409431std::unique_ptr<OperationPass<FuncOp>> mlir::createLinalgPromotionPass () {
410432 return std::make_unique<LinalgPromotionPass>();
0 commit comments