Skip to content

Commit 66abe64

Browse files
committed
[flang][hlfir] add an optimized bufferization pass
This pass is intended to spot cases where we can do better than the default bufferization and to rewrite those specific cases. Then the default bufferization (bufferize-hlfir pass) can handle everything else. The transformation added in this patch rewrites simple element-wise updates to an array to a do-loop modifying the array in place instead of creating and assigning an array temporary. See the RFC at https://discourse.llvm.org/t/rfc-hlfir-optimized-bufferization-for-elemental-array-updates This patch gets the improvement to exchange2 but not the improvement to cam4 described in the RFC. I think the cam4 improvement will require better alias analysis. I aim to follow up to fix this in a later patch. With changes since the RFC, the pass improves polyhedron channel2 by about 52%. Depends on: D156805 D157718 D157626 Differential Revision: https://reviews.llvm.org/D157107
1 parent d2aff81 commit 66abe64

File tree

7 files changed

+1204
-0
lines changed

7 files changed

+1204
-0
lines changed

flang/include/flang/Optimizer/HLFIR/Passes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ std::unique_ptr<mlir::Pass> createLowerHLFIRIntrinsicsPass();
2828
std::unique_ptr<mlir::Pass> createSimplifyHLFIRIntrinsicsPass();
2929
std::unique_ptr<mlir::Pass> createInlineElementalsPass();
3030
std::unique_ptr<mlir::Pass> createLowerHLFIROrderedAssignmentsPass();
31+
std::unique_ptr<mlir::Pass> createOptimizedBufferizationPass();
3132

3233
#define GEN_PASS_REGISTRATION
3334
#include "flang/Optimizer/HLFIR/Passes.h.inc"

flang/include/flang/Optimizer/HLFIR/Passes.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def BufferizeHLFIR : Pass<"bufferize-hlfir", "::mlir::ModuleOp"> {
2020
let constructor = "hlfir::createBufferizeHLFIRPass()";
2121
}
2222

23+
def OptimizedBufferization : Pass<"opt-bufferization", "::mlir::func::FuncOp"> {
24+
let summary = "Special cases for hlfir.expr bufferization where we can avoid a temporary which would be created by the generic bufferization pass";
25+
let constructor = "hlfir::createOptimizedBufferizationPass()";
26+
}
27+
2328
def LowerHLFIRIntrinsics : Pass<"lower-hlfir-intrinsics", "::mlir::ModuleOp"> {
2429
let summary = "Lower HLFIR transformational intrinsic operations";
2530
let constructor = "hlfir::createLowerHLFIRIntrinsicsPass()";

flang/include/flang/Tools/CLOptions.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ inline void createHLFIRToFIRPassPipeline(
243243
pm.addPass(hlfir::createSimplifyHLFIRIntrinsicsPass());
244244
}
245245
pm.addPass(hlfir::createInlineElementalsPass());
246+
if (optLevel.isOptimizingForSpeed()) {
247+
addCanonicalizerPassWithoutRegionSimplification(pm);
248+
pm.addPass(mlir::createCSEPass());
249+
pm.addPass(hlfir::createOptimizedBufferizationPass());
250+
}
246251
pm.addPass(hlfir::createLowerHLFIROrderedAssignmentsPass());
247252
pm.addPass(hlfir::createLowerHLFIRIntrinsicsPass());
248253
pm.addPass(hlfir::createBufferizeHLFIRPass());

flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_flang_library(HLFIRTransforms
88
LowerHLFIROrderedAssignments.cpp
99
ScheduleOrderedAssignments.cpp
1010
SimplifyHLFIRIntrinsics.cpp
11+
OptimizedBufferization.cpp
1112

1213
DEPENDS
1314
FIRDialect

0 commit comments

Comments
 (0)