forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows us to reuse it in XLA Next, in addition to kernel_gen. PiperOrigin-RevId: 448935170
- Loading branch information
1 parent
fcc0207
commit 23529a7
Showing
9 changed files
with
210 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
tensorflow/compiler/mlir/hlo/lib/Transforms/tile_loops_pass.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
// This files implements the logic for converting `scf.parallel` loops into | ||
// tiled loops. | ||
|
||
#include "mlir-hlo/Transforms/PassDetail.h" | ||
#include "mlir-hlo/Transforms/passes.h" | ||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/Dialect/MemRef/IR/MemRef.h" | ||
#include "mlir/Dialect/SCF/SCF.h" | ||
#include "mlir/Dialect/SCF/Transforms.h" | ||
#include "mlir/Dialect/SCF/Utils/Utils.h" | ||
|
||
namespace mlir { | ||
|
||
using ::llvm::to_vector; | ||
using ::mlir::scf::ParallelOp; | ||
|
||
namespace { | ||
|
||
// This is the implementation of the TileLoops pass declared in | ||
// include/mlir-hlo/Transforms/passes.td | ||
class TileLoopsPass : public TileLoopsPassBase<TileLoopsPass> { | ||
public: | ||
// Creates a TileLoopsPass with tiles sizes provided through `tile_sizes` | ||
// and unroll factors provided through `unroll_factors`. | ||
explicit TileLoopsPass(ArrayRef<int64_t> tile_sizes, | ||
ArrayRef<int64_t> unroll_factors) { | ||
tile_sizes_ = tile_sizes; | ||
unroll_factors_ = unroll_factors; | ||
} | ||
|
||
void runOnOperation() override; | ||
}; | ||
|
||
} // namespace | ||
|
||
// Checks if the access pattern in the `scf.parallel` loop `ploop` is "complex". | ||
// I.e., its memory load patterns include more than just scalar accesses, and | ||
// accesses with offsets corresponding to loop inductions variables. | ||
static bool IsComplexAccessPattern(ParallelOp ploop) { | ||
for (Operation& nested : ploop.getBody()->without_terminator()) { | ||
if (auto load_op = llvm::dyn_cast<memref::LoadOp>(nested)) { | ||
if (!load_op.getMemRefType().getLayout().isIdentity() || | ||
(!load_op.getIndices().empty() && | ||
load_op.getIndices() != ploop.getInductionVars())) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
void TileLoopsPass::runOnOperation() { | ||
auto unrolled_tile = [&]() -> SmallVector<int64_t, 4> { | ||
if (tile_sizes_.size() != unroll_factors_.size()) return {}; | ||
auto multiply = [](std::tuple<int64_t, int64_t> tuple) { | ||
return std::get<0>(tuple) * std::get<1>(tuple); | ||
}; | ||
return to_vector<4>( | ||
llvm::map_range(llvm::zip(tile_sizes_, unroll_factors_), multiply)); | ||
}(); | ||
|
||
SmallVector<ParallelOp, 2> innermostPloops; | ||
getInnermostParallelLoops(this->getOperation().getOperation(), | ||
innermostPloops); | ||
|
||
for (ParallelOp ploop : innermostPloops) { | ||
// Do not unroll if the multiplier has the wrong rank, or if we have complex | ||
// memory access patterns. | ||
if (unrolled_tile.empty() || IsComplexAccessPattern(ploop)) { | ||
tileParallelLoop(ploop, tile_sizes_, /*noMinMaxBounds=*/false); | ||
continue; | ||
} | ||
auto tiled_loops = | ||
tileParallelLoop(ploop, unrolled_tile, /*noMinMaxBounds=*/false); | ||
tileParallelLoop(tiled_loops.second, unroll_factors_, | ||
/*noMinMaxBounds=*/false); | ||
} | ||
} | ||
|
||
std::unique_ptr<OperationPass<func::FuncOp>> CreateTileLoopsPass( | ||
ArrayRef<int64_t> tile_sizes, ArrayRef<int64_t> unroll_factors) { | ||
return std::make_unique<TileLoopsPass>(tile_sizes, unroll_factors); | ||
} | ||
|
||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// RUN: mlir-hlo-opt --tile-loops="tile-sizes=2 unroll-factors=4" %s | \ | ||
// RUN: FileCheck %s | ||
|
||
// CHECK-LABEL: func @parallel_loop | ||
func.func @parallel_loop(%arg0: memref<16xf32>, %arg1: memref<16xf32>) { | ||
%c0 = arith.constant 0 : index | ||
%c1 = arith.constant 1 : index | ||
%c16 = arith.constant 16 : index | ||
%0 = memref.alloc() {alignment = 128 : i64} : memref<16xf32> | ||
scf.parallel (%arg2) = (%c0) to (%c16) step (%c1) { | ||
// CHECK: %[[C8:.*]] = arith.constant 8 | ||
// CHECK: %[[TILE:.*]] = arith.muli {{.*}} %[[C8]] | ||
// CHECK: scf.parallel {{.*}} step (%[[TILE]]) | ||
// CHECK: %[[C4:.*]] = arith.constant 4 | ||
// CHECK: %[[UNROLL:.*]] = arith.muli {{.*}} %[[C4]] | ||
// CHECK: scf.parallel {{.*}} to (%[[TILE]]) step (%[[UNROLL]]) | ||
// CHECK: scf.parallel | ||
%2 = memref.load %arg0[%arg2] : memref<16xf32> | ||
%3 = math.log %2 : f32 | ||
memref.store %3, %0[%arg2] : memref<16xf32> | ||
scf.yield | ||
} | ||
%1 = bufferization.to_tensor %0 : memref<16xf32> | ||
memref.tensor_store %1, %arg1 : memref<16xf32> | ||
"lmhlo.terminator"() : () -> () | ||
} | ||
|
||
// CHECK-LABEL: func @complex_access | ||
func.func @complex_access(%arg0: memref<16xf32>, %arg1: memref<4xf32>) { | ||
%c0 = arith.constant 0 : index | ||
%c1 = arith.constant 1 : index | ||
%c4 = arith.constant 4 : index | ||
%0 = memref.alloc() {alignment = 128 : i64} : memref<4xf32> | ||
scf.parallel (%arg2) = (%c0) to (%c4) step (%c1) { | ||
// CHECK: %[[C2:.*]] = arith.constant 2 | ||
// CHECK: %[[TILE:.*]] = arith.muli {{.*}} %[[C2]] | ||
// CHECK: scf.parallel {{.*}} step (%[[TILE]]) | ||
// CHECK: scf.parallel | ||
// We should see only 2 loops for complex access patterns | ||
// CHECK-NOT: scf.parallel | ||
%idx = arith.muli %arg2, %c4 : index | ||
%2 = memref.load %arg0[%idx] : memref<16xf32> | ||
%3 = math.log %2 : f32 | ||
memref.store %3, %0[%arg2] : memref<4xf32> | ||
scf.yield | ||
} | ||
%1 = bufferization.to_tensor %0 : memref<4xf32> | ||
memref.tensor_store %1, %arg1 : memref<4xf32> | ||
"lmhlo.terminator"() : () -> () | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.