2018-10-08 11:10:11 -07:00
|
|
|
//===- ComposeAffineMaps.cpp - MLIR Affine Transform Class-----*- C++ -*-===//
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
// =============================================================================
|
|
|
|
|
//
|
2018-10-15 10:17:14 -07:00
|
|
|
// This file implements a testing pass which composes affine maps from
|
2018-12-28 08:48:09 -08:00
|
|
|
// AffineApplyOps in a Function, by forward subtituting results from an
|
2018-10-15 10:17:14 -07:00
|
|
|
// AffineApplyOp into any of its users which are also AffineApplyOps.
|
2018-10-08 11:10:11 -07:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2019-01-10 21:54:34 -08:00
|
|
|
#include "mlir/Analysis/AffineAnalysis.h"
|
2019-01-26 06:59:23 -08:00
|
|
|
#include "mlir/Analysis/NestedMatcher.h"
|
2018-10-08 11:10:11 -07:00
|
|
|
#include "mlir/IR/AffineMap.h"
|
|
|
|
|
#include "mlir/IR/Attributes.h"
|
|
|
|
|
#include "mlir/IR/Builders.h"
|
2018-10-10 14:23:30 -07:00
|
|
|
#include "mlir/IR/BuiltinOps.h"
|
2018-12-28 16:05:35 -08:00
|
|
|
#include "mlir/IR/InstVisitor.h"
|
Introduce memref bound checking.
Introduce analysis to check memref accesses (in MLFunctions) for out of bound
ones. It works as follows:
$ mlir-opt -memref-bound-check test/Transforms/memref-bound-check.mlir
/tmp/single.mlir:10:12: error: 'load' op memref out of upper bound access along dimension tensorflow/mlir#1
%x = load %A[%idxtensorflow/mlir#0, %idxtensorflow/mlir#1] : memref<9 x 9 x i32>
^
/tmp/single.mlir:10:12: error: 'load' op memref out of lower bound access along dimension tensorflow/mlir#1
%x = load %A[%idxtensorflow/mlir#0, %idxtensorflow/mlir#1] : memref<9 x 9 x i32>
^
/tmp/single.mlir:10:12: error: 'load' op memref out of upper bound access along dimension tensorflow/mlir#2
%x = load %A[%idxtensorflow/mlir#0, %idxtensorflow/mlir#1] : memref<9 x 9 x i32>
^
/tmp/single.mlir:10:12: error: 'load' op memref out of lower bound access along dimension tensorflow/mlir#2
%x = load %A[%idxtensorflow/mlir#0, %idxtensorflow/mlir#1] : memref<9 x 9 x i32>
^
/tmp/single.mlir:12:12: error: 'load' op memref out of upper bound access along dimension tensorflow/mlir#1
%y = load %B[%idy] : memref<128 x i32>
^
/tmp/single.mlir:12:12: error: 'load' op memref out of lower bound access along dimension tensorflow/mlir#1
%y = load %B[%idy] : memref<128 x i32>
^
#map0 = (d0, d1) -> (d0, d1)
#map1 = (d0, d1) -> (d0 * 128 - d1)
mlfunc @test() {
%0 = alloc() : memref<9x9xi32>
%1 = alloc() : memref<128xi32>
for %i0 = -1 to 9 {
for %i1 = -1 to 9 {
%2 = affine_apply #map0(%i0, %i1)
%3 = load %0[%2tensorflow/mlir#0, %2tensorflow/mlir#1] : memref<9x9xi32>
%4 = affine_apply #map1(%i0, %i1)
%5 = load %1[%4] : memref<128xi32>
}
}
return
}
- Improves productivity while manually / semi-automatically developing MLIR for
testing / prototyping; also provides an indirect way to catch errors in
transformations.
- This pass is an easy way to test the underlying affine analysis
machinery including low level routines.
Some code (in getMemoryRegion()) borrowed from @andydavis cl/218263256.
While on this:
- create mlir/Analysis/Passes.h; move Pass.h up from mlir/Transforms/ to mlir/
- fix a bug in AffineAnalysis.cpp::toAffineExpr
TODO: extend to non-constant loop bounds (straightforward). Will transparently
work for all accesses once floordiv, mod, ceildiv are supported in the
AffineMap -> FlatAffineConstraints conversion.
PiperOrigin-RevId: 219397961
2018-10-30 17:43:06 -07:00
|
|
|
#include "mlir/Pass.h"
|
2018-10-10 14:23:30 -07:00
|
|
|
#include "mlir/StandardOps/StandardOps.h"
|
2018-10-08 11:10:11 -07:00
|
|
|
#include "mlir/Transforms/Passes.h"
|
2018-10-12 14:54:54 -07:00
|
|
|
#include "mlir/Transforms/Utils.h"
|
2018-10-08 11:10:11 -07:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2019-01-10 21:54:34 -08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-10-08 11:10:11 -07:00
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2018-12-28 16:05:35 -08:00
|
|
|
// ComposeAffineMaps walks inst blocks in a Function, and for each
|
2018-10-15 10:17:14 -07:00
|
|
|
// AffineApplyOp, forward substitutes its results into any users which are
|
|
|
|
|
// also AffineApplyOps. After forward subtituting its results, AffineApplyOps
|
|
|
|
|
// with no remaining uses are collected and erased after the walk.
|
|
|
|
|
// TODO(andydavis) Remove this when Chris adds instruction combiner pass.
|
2019-01-10 21:54:34 -08:00
|
|
|
struct ComposeAffineMaps : public FunctionPass {
|
2018-11-07 10:24:03 -08:00
|
|
|
explicit ComposeAffineMaps() : FunctionPass(&ComposeAffineMaps::passID) {}
|
2018-12-30 23:10:35 -08:00
|
|
|
PassResult runOnFunction(Function *f) override;
|
2019-01-10 21:54:34 -08:00
|
|
|
|
|
|
|
|
// Thread-safe RAII contexts local to pass, BumpPtrAllocator freed on exit.
|
2019-01-26 06:59:23 -08:00
|
|
|
NestedPatternContext MLContext;
|
2018-11-06 18:34:18 -08:00
|
|
|
|
|
|
|
|
static char passID;
|
2018-10-08 11:10:11 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
2018-11-06 18:34:18 -08:00
|
|
|
char ComposeAffineMaps::passID = 0;
|
|
|
|
|
|
2018-10-25 16:58:08 -07:00
|
|
|
FunctionPass *mlir::createComposeAffineMapsPass() {
|
2018-10-08 11:10:11 -07:00
|
|
|
return new ComposeAffineMaps();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-10 21:54:34 -08:00
|
|
|
static bool affineApplyOp(const Instruction &inst) {
|
|
|
|
|
const auto &opInst = cast<OperationInst>(inst);
|
|
|
|
|
return opInst.isa<AffineApplyOp>();
|
2018-10-15 10:17:14 -07:00
|
|
|
}
|
2018-10-08 11:10:11 -07:00
|
|
|
|
2019-01-10 21:54:34 -08:00
|
|
|
PassResult ComposeAffineMaps::runOnFunction(Function *f) {
|
|
|
|
|
using matcher::Op;
|
|
|
|
|
|
|
|
|
|
auto pattern = Op(affineApplyOp);
|
|
|
|
|
auto apps = pattern.match(f);
|
|
|
|
|
for (auto m : apps) {
|
|
|
|
|
auto app = cast<OperationInst>(m.first)->cast<AffineApplyOp>();
|
2019-01-26 06:59:23 -08:00
|
|
|
SmallVector<Value *, 8> operands(app->getOperands());
|
2019-01-10 21:54:34 -08:00
|
|
|
FuncBuilder b(m.first);
|
|
|
|
|
auto newApp = makeComposedAffineApply(&b, app->getLoc(),
|
|
|
|
|
app->getAffineMap(), operands);
|
|
|
|
|
unsigned idx = 0;
|
|
|
|
|
for (auto *v : app->getResults()) {
|
|
|
|
|
v->replaceAllUsesWith(newApp->getResult(idx++));
|
2018-10-15 10:17:14 -07:00
|
|
|
}
|
2018-10-08 11:10:11 -07:00
|
|
|
}
|
2019-01-10 21:54:34 -08:00
|
|
|
{
|
|
|
|
|
auto pattern = Op(affineApplyOp);
|
|
|
|
|
auto apps = pattern.match(f);
|
|
|
|
|
std::reverse(apps.begin(), apps.end());
|
|
|
|
|
for (auto m : apps) {
|
|
|
|
|
auto app = cast<OperationInst>(m.first)->cast<AffineApplyOp>();
|
|
|
|
|
bool hasNonEmptyUse = llvm::any_of(
|
|
|
|
|
app->getResults(), [](Value *r) { return !r->use_empty(); });
|
|
|
|
|
if (!hasNonEmptyUse) {
|
|
|
|
|
m.first->erase();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-15 10:17:14 -07:00
|
|
|
}
|
2018-10-08 11:10:11 -07:00
|
|
|
return success();
|
|
|
|
|
}
|
2018-11-06 18:34:18 -08:00
|
|
|
|
|
|
|
|
static PassRegistration<ComposeAffineMaps> pass("compose-affine-maps",
|
|
|
|
|
"Compose affine maps");
|