Skip to content

Commit 7d9518c

Browse files
[mlir][Linalg] Add an option to use Alloca instead of malloc/free pairs.
Summary: A relevant test is also added. Subscribers: mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes Tags: #mlir Differential Revision: https://reviews.llvm.org/D82959
1 parent a03dc8c commit 7d9518c

5 files changed

Lines changed: 61 additions & 25 deletions

File tree

‎mlir/include/mlir/Dialect/Linalg/Passes.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ std::unique_ptr<OperationPass<FuncOp>>
3636
createLinalgTilingToParallelLoopsPass(ArrayRef<int64_t> tileSizes = {});
3737

3838
std::unique_ptr<OperationPass<FuncOp>>
39-
createLinalgPromotionPass(bool dynamicBuffers);
39+
createLinalgPromotionPass(bool dynamicBuffers, bool useAlloca);
4040
std::unique_ptr<OperationPass<FuncOp>> createLinalgPromotionPass();
4141

4242
/// Create a pass to convert Linalg operations to scf.for loops and

‎mlir/include/mlir/Dialect/Linalg/Passes.td‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ def LinalgPromotion : FunctionPass<"linalg-promote-subviews"> {
6161
let constructor = "mlir::createLinalgPromotionPass()";
6262
let options = [
6363
Option<"dynamicBuffers", "test-promote-dynamic", "bool",
64-
/*default=*/"false", "Test generation of dynamic promoted buffers">
64+
/*default=*/"false", "Test generation of dynamic promoted buffers">,
65+
Option<"useAlloca", "test-use-alloca", "bool",
66+
/*default=*/"false", "Test generation of alloca'ed buffers.">
6567
];
6668
}
6769

‎mlir/include/mlir/Dialect/Linalg/Transforms/Transforms.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ struct LinalgPromotionOptions {
122122
alignment = align;
123123
return *this;
124124
}
125+
/// Use alloca with the default allocation scheme.
126+
bool useAlloca = false;
127+
LinalgPromotionOptions &setUseAlloca(bool use) {
128+
useAlloca = use;
129+
return *this;
130+
}
125131
/// Callback function to do the allocation of the promoted buffer. If None,
126132
/// then the default allocation scheme of allocating a memref<?xi8> buffer
127133
/// followed by a view operation is used.
@@ -134,7 +140,6 @@ struct LinalgPromotionOptions {
134140
deallocationFn = deallocFn;
135141
return *this;
136142
}
137-
138143
/// Callback function to do the copy of data to and from the promoted
139144
/// subview. If None then a linalg.copy is used.
140145
Optional<CopyCallbackFn> copyInFn = None;

‎mlir/lib/Dialect/Linalg/Transforms/Promotion.cpp‎

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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,
383402
namespace {
384403
struct 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.
405427
std::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
}
409431
std::unique_ptr<OperationPass<FuncOp>> mlir::createLinalgPromotionPass() {
410432
return std::make_unique<LinalgPromotionPass>();

‎mlir/test/Dialect/Linalg/promote.mlir‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: mlir-opt %s -linalg-promote-subviews | FileCheck %s
22
// RUN: mlir-opt %s -linalg-promote-subviews="test-promote-dynamic" | FileCheck %s --check-prefix=DYNAMIC
3+
// RUN: mlir-opt %s -linalg-promote-subviews="test-use-alloca" | FileCheck %s --check-prefix=ALLOCA
34

45
#map1 = affine_map<(d0) -> (d0 + 2)>
56
#map2 = affine_map<(d0) -> (d0 + 4)>
@@ -45,16 +46,19 @@ func @matmul_f32(%A: memref<?xi8>, %M: index, %N: index, %K: index) {
4546
// CHECK: %[[vC:.*]] = subview {{.*}} : memref<?x?xf32>
4647
///
4748
// CHECK: %[[tmpA:.*]] = alloc() : memref<32xi8>
49+
// ALLOCA: %[[tmpA:.*]] = alloca() : memref<32xi8>
4850
// CHECK: %[[fullA:.*]] = std.view %[[tmpA]][{{.*}}][{{.*}}] : memref<32xi8> to memref<?x?xf32>
4951
// DYNAMIC: std.view %{{.*}}[{{.*}}][{{.*}}] : memref<?xi8> to memref<?x?xf32>
5052
// CHECK: %[[partialA:.*]] = subview %[[fullA]]{{.*}} : memref<?x?xf32> to memref<?x?xf32, #[[$strided2D_dynamic]]>
5153
///
5254
// CHECK: %[[tmpB:.*]] = alloc() : memref<48xi8>
55+
// ALLOCA: %[[tmpB:.*]] = alloca() : memref<48xi8>
5356
// CHECK: %[[fullB:.*]] = std.view %[[tmpB]][{{.*}}][{{.*}}] : memref<48xi8> to memref<?x?xf32>
5457
// DYNAMIC: std.view %{{.*}}[{{.*}}][{{.*}}] : memref<?xi8> to memref<?x?xf32>
5558
// CHECK: %[[partialB:.*]] = subview %[[fullB]]{{.*}} : memref<?x?xf32> to memref<?x?xf32, #[[$strided2D_dynamic]]>
5659
///
5760
// CHECK: %[[tmpC:.*]] = alloc() : memref<24xi8>
61+
// ALLOCA: %[[tmpC:.*]] = alloca() : memref<24xi8>
5862
// CHECK: %[[fullC:.*]] = std.view %[[tmpC]][{{.*}}][{{.*}}] : memref<24xi8> to memref<?x?xf32>
5963
// DYNAMIC: std.view %{{.*}}[{{.*}}][{{.*}}] : memref<?xi8> to memref<?x?xf32>
6064
// CHECK: %[[partialC:.*]] = subview %[[fullC]]{{.*}} : memref<?x?xf32> to memref<?x?xf32, #[[$strided2D_dynamic]]>
@@ -75,6 +79,9 @@ func @matmul_f32(%A: memref<?xi8>, %M: index, %N: index, %K: index) {
7579
// CHECK: dealloc %[[tmpA]] : memref<32xi8>
7680
// CHECK: dealloc %[[tmpB]] : memref<48xi8>
7781
// CHECK: dealloc %[[tmpC]] : memref<24xi8>
82+
// ALLOCA-NOT: dealloc %[[tmpA]] : memref<32xi8>
83+
// ALLOCA-NOT: dealloc %[[tmpB]] : memref<48xi8>
84+
// ALLOCA-NOT: dealloc %[[tmpC]] : memref<24xi8>
7885

7986
// -----
8087

0 commit comments

Comments
 (0)