llvm-project/mlir/lib/Analysis/SliceAnalysis.cpp
Nicolas Vasilache a5782f0d40 [MLIR][MaterializeVectors] Add a MaterializeVector pass via unrolling.
This CL adds an MLIR-MLIR pass which materializes super-vectors to
hardware-dependent sized vectors.

While the physical vector size is target-dependent, the pass is written in
a target-independent way: the target vector size is specified as a parameter
to the pass. This pass is thus a partial lowering that opens the "greybox"
that is the super-vector abstraction.

This first CL adds a first materilization pass iterates over vector_transfer_write operations and:
1. computes the program slice including the current vector_transfer_write;
2. computes the multi-dimensional ratio of super-vector shape to hardware
vector shape;
3. for each possible multi-dimensional value within the bounds of ratio, a new slice is
instantiated (i.e. cloned and rewritten) so that all operations in this instance operate on
the hardware vector type.

As a simple example, given:
```mlir
mlfunc @vector_add_2d(%M : index, %N : index) -> memref<?x?xf32> {
  %A = alloc (%M, %N) : memref<?x?xf32>
  %B = alloc (%M, %N) : memref<?x?xf32>
  %C = alloc (%M, %N) : memref<?x?xf32>
  for %i0 = 0 to %M {
    for %i1 = 0 to %N {
      %a1 = load %A[%i0, %i1] : memref<?x?xf32>
      %b1 = load %B[%i0, %i1] : memref<?x?xf32>
      %s1 = addf %a1, %b1 : f32
      store %s1, %C[%i0, %i1] : memref<?x?xf32>
    }
  }
  return %C : memref<?x?xf32>
}
```

and the following options:
```
-vectorize -virtual-vector-size 32 --test-fastest-varying=0 -materialize-vectors -vector-size=8
```

materialization emits:
```mlir
#map0 = (d0, d1) -> (d0, d1)
#map1 = (d0, d1) -> (d0, d1 + 8)
#map2 = (d0, d1) -> (d0, d1 + 16)
#map3 = (d0, d1) -> (d0, d1 + 24)
mlfunc @vector_add_2d(%arg0 : index, %arg1 : index) -> memref<?x?xf32> {
  %0 = alloc(%arg0, %arg1) : memref<?x?xf32>
  %1 = alloc(%arg0, %arg1) : memref<?x?xf32>
  %2 = alloc(%arg0, %arg1) : memref<?x?xf32>
  for %i0 = 0 to %arg0 {
    for %i1 = 0 to %arg1 step 32 {
      %3 = affine_apply #map0(%i0, %i1)
      %4 = "vector_transfer_read"(%0, %3tensorflow/mlir#0, %3tensorflow/mlir#1) : (memref<?x?xf32>, index, index) -> vector<8xf32>
      %5 = affine_apply #map1(%i0, %i1)
      %6 = "vector_transfer_read"(%0, %5tensorflow/mlir#0, %5tensorflow/mlir#1) : (memref<?x?xf32>, index, index) -> vector<8xf32>
      %7 = affine_apply #map2(%i0, %i1)
      %8 = "vector_transfer_read"(%0, %7tensorflow/mlir#0, %7tensorflow/mlir#1) : (memref<?x?xf32>, index, index) -> vector<8xf32>
      %9 = affine_apply #map3(%i0, %i1)
      %10 = "vector_transfer_read"(%0, %9tensorflow/mlir#0, %9tensorflow/mlir#1) : (memref<?x?xf32>, index, index) -> vector<8xf32>
      %11 = affine_apply #map0(%i0, %i1)
      %12 = "vector_transfer_read"(%1, %11tensorflow/mlir#0, %11tensorflow/mlir#1) : (memref<?x?xf32>, index, index) -> vector<8xf32>
      %13 = affine_apply #map1(%i0, %i1)
      %14 = "vector_transfer_read"(%1, %13tensorflow/mlir#0, %13tensorflow/mlir#1) : (memref<?x?xf32>, index, index) -> vector<8xf32>
      %15 = affine_apply #map2(%i0, %i1)
      %16 = "vector_transfer_read"(%1, %15tensorflow/mlir#0, %15tensorflow/mlir#1) : (memref<?x?xf32>, index, index) -> vector<8xf32>
      %17 = affine_apply #map3(%i0, %i1)
      %18 = "vector_transfer_read"(%1, %17tensorflow/mlir#0, %17tensorflow/mlir#1) : (memref<?x?xf32>, index, index) -> vector<8xf32>
      %19 = addf %4, %12 : vector<8xf32>
      %20 = addf %6, %14 : vector<8xf32>
      %21 = addf %8, %16 : vector<8xf32>
      %22 = addf %10, %18 : vector<8xf32>
      %23 = affine_apply #map0(%i0, %i1)
      "vector_transfer_write"(%19, %2, %23tensorflow/mlir#0, %23tensorflow/mlir#1) : (vector<8xf32>, memref<?x?xf32>, index, index) -> ()
      %24 = affine_apply #map1(%i0, %i1)
      "vector_transfer_write"(%20, %2, %24tensorflow/mlir#0, %24tensorflow/mlir#1) : (vector<8xf32>, memref<?x?xf32>, index, index) -> ()
      %25 = affine_apply #map2(%i0, %i1)
      "vector_transfer_write"(%21, %2, %25tensorflow/mlir#0, %25tensorflow/mlir#1) : (vector<8xf32>, memref<?x?xf32>, index, index) -> ()
      %26 = affine_apply #map3(%i0, %i1)
      "vector_transfer_write"(%22, %2, %26tensorflow/mlir#0, %26tensorflow/mlir#1) : (vector<8xf32>, memref<?x?xf32>, index, index) -> ()
    }
  }
  return %2 : memref<?x?xf32>
}
```

PiperOrigin-RevId: 222455351
2019-03-29 14:08:31 -07:00

200 lines
6.4 KiB
C++

//===- UseDefAnalysis.cpp - Analysis for Transitive UseDef chains ---------===//
//
// Copyright 2019 The MLIR Authors.
//
// 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 file implements Analysis functions specific to slicing in MLFunction.
//
//===----------------------------------------------------------------------===//
#include "mlir/Analysis/SliceAnalysis.h"
#include "mlir/Analysis/VectorAnalysis.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Statements.h"
#include "mlir/Support/Functional.h"
#include "mlir/Support/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include <type_traits>
///
/// Implements Analysis functions specific to slicing in MLFunction.
///
using namespace mlir;
using llvm::DenseSet;
using llvm::SetVector;
void mlir::getForwardSlice(Statement *stmt,
SetVector<Statement *> *forwardSlice,
TransitiveFilter filter, bool topLevel) {
if (!stmt) {
return;
}
// Evaluate whether we should keep this use.
// This is useful in particular to implement scoping; i.e. return the
// transitive forwardSlice in the current scope.
if (!filter(stmt)) {
return;
}
if (auto *opStmt = dyn_cast<OperationStmt>(stmt)) {
assert(opStmt->getNumResults() <= 1 && "NYI: multiple results");
if (opStmt->getNumResults() > 0) {
for (auto &u : opStmt->getResult(0)->getUses()) {
auto *ownerStmt = u.getOwner();
if (forwardSlice->count(ownerStmt) == 0) {
getForwardSlice(ownerStmt, forwardSlice, filter,
/*topLevel=*/false);
}
}
}
} else if (auto *forStmt = dyn_cast<ForStmt>(stmt)) {
for (auto &u : forStmt->getUses()) {
auto *ownerStmt = u.getOwner();
if (forwardSlice->count(ownerStmt) == 0) {
getForwardSlice(ownerStmt, forwardSlice, filter,
/*topLevel=*/false);
}
}
} else {
assert(false && "NYI slicing case");
}
// At the top level we reverse to get back the actual topological order.
if (topLevel) {
// std::reverse does not work out of the box on SetVector and I want an
// in-place swap based thing (the real std::reverse, not the LLVM adapter).
// TODO(clattner): Consider adding an extra method?
std::vector<Statement *> v(forwardSlice->takeVector());
forwardSlice->insert(v.rbegin(), v.rend());
} else {
forwardSlice->insert(stmt);
}
}
void mlir::getBackwardSlice(Statement *stmt,
SetVector<Statement *> *backwardSlice,
TransitiveFilter filter, bool topLevel) {
if (!stmt) {
return;
}
// Evaluate whether we should keep this def.
// This is useful in particular to implement scoping; i.e. return the
// transitive forwardSlice in the current scope.
if (!filter(stmt)) {
return;
}
for (auto *operand : stmt->getOperands()) {
auto *stmt = operand->getDefiningStmt();
if (backwardSlice->count(stmt) == 0) {
getBackwardSlice(stmt, backwardSlice, filter,
/*topLevel=*/false);
}
}
// Don't insert the top level statement, we just queried on it and don't
// want it in the results.
if (!topLevel) {
backwardSlice->insert(stmt);
}
}
SetVector<Statement *> mlir::getSlice(Statement *stmt,
TransitiveFilter backwardFilter,
TransitiveFilter forwardFilter) {
SetVector<Statement *> slice;
slice.insert(stmt);
unsigned currentIndex = 0;
SetVector<Statement *> backwardSlice;
SetVector<Statement *> forwardSlice;
while (currentIndex != slice.size()) {
auto *currentStmt = (slice)[currentIndex];
// Compute and insert the backwardSlice starting from currentStmt.
backwardSlice.clear();
getBackwardSlice(currentStmt, &backwardSlice, backwardFilter);
slice.insert(backwardSlice.begin(), backwardSlice.end());
// Compute and insert the forwardSlice starting from currentStmt.
forwardSlice.clear();
getForwardSlice(currentStmt, &forwardSlice, forwardFilter);
slice.insert(forwardSlice.begin(), forwardSlice.end());
++currentIndex;
}
return topologicalSort(slice);
}
namespace {
/// DFS post-order implementation that maintains a global count to work across
/// multiple invocations, to help implement topological sort on multi-root DAGs.
/// We traverse all statements but only record the ones that appear in `toSort`
/// for the final result.
struct DFSState {
DFSState(const SetVector<Statement *> &set)
: toSort(set), topologicalCounts(), seen() {}
const SetVector<Statement *> &toSort;
SmallVector<Statement *, 16> topologicalCounts;
DenseSet<Statement *> seen;
};
} // namespace
static void DFSPostorder(Statement *current, DFSState *state) {
auto *opStmt = cast<OperationStmt>(current);
assert(opStmt->getNumResults() <= 1 && "NYI: multi-result");
if (opStmt->getNumResults() > 0) {
for (auto &u : opStmt->getResult(0)->getUses()) {
auto *stmt = u.getOwner();
DFSPostorder(stmt, state);
}
}
bool inserted;
using IterTy = decltype(state->seen.begin());
IterTy iter;
std::tie(iter, inserted) = state->seen.insert(current);
if (inserted) {
if (state->toSort.count(current) > 0) {
state->topologicalCounts.push_back(current);
}
}
}
SetVector<Statement *>
mlir::topologicalSort(const SetVector<Statement *> &toSort) {
if (toSort.empty()) {
return toSort;
}
// Run from each root with global count and `seen` set.
DFSState state(toSort);
for (auto *s : toSort) {
assert(toSort.count(s) == 1 && "NYI: multi-sets not supported");
DFSPostorder(s, &state);
}
// Reorder and return.
SetVector<Statement *> res;
for (auto it = state.topologicalCounts.rbegin(),
eit = state.topologicalCounts.rend();
it != eit; ++it) {
res.insert(*it);
}
return res;
}