Convert expr - c * (expr floordiv c) to expr mod c in AffineExpr
- Detect 'mod' to replace the combination of floordiv, mul, and subtract when
possible at construction time; when 'c' is a power of two, this reduces the number of
operations; also more compact and readable. Update simplifyAdd for this.
On a side note:
- with the affine expr flattening we have, a mod expression like d0 mod c
would be flattened into d0 - c * q, c * q <= d0 <= c*q + c - 1, with 'q'
being added as the local variable (q = d0 floordiv c); as a result, a mod
was turned into a floordiv whenever the expression was reconstructed back,
i.e., as d0 - c * (d0 floordiv c); as a result of this change, we recover
the mod back.
- rename SimplifyAffineExpr -> SimplifyAffineStructures (pass had been renamed but
the file hadn't been).
PiperOrigin-RevId: 228258120
2019-01-07 16:35:06 -08:00
|
|
|
//===- SimplifyAffineStructures.cpp - ---------------------------*- C++ -*-===//
|
2018-08-30 17:35:15 -07:00
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
// =============================================================================
|
|
|
|
|
//
|
Convert expr - c * (expr floordiv c) to expr mod c in AffineExpr
- Detect 'mod' to replace the combination of floordiv, mul, and subtract when
possible at construction time; when 'c' is a power of two, this reduces the number of
operations; also more compact and readable. Update simplifyAdd for this.
On a side note:
- with the affine expr flattening we have, a mod expression like d0 mod c
would be flattened into d0 - c * q, c * q <= d0 <= c*q + c - 1, with 'q'
being added as the local variable (q = d0 floordiv c); as a result, a mod
was turned into a floordiv whenever the expression was reconstructed back,
i.e., as d0 - c * (d0 floordiv c); as a result of this change, we recover
the mod back.
- rename SimplifyAffineExpr -> SimplifyAffineStructures (pass had been renamed but
the file hadn't been).
PiperOrigin-RevId: 228258120
2019-01-07 16:35:06 -08:00
|
|
|
// This file implements a pass to simplify affine structures.
|
2018-08-30 17:35:15 -07:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "mlir/Analysis/AffineStructures.h"
|
2018-12-27 11:07:34 -08:00
|
|
|
#include "mlir/IR/Function.h"
|
2019-02-03 09:49:39 -08:00
|
|
|
#include "mlir/IR/Instruction.h"
|
2019-02-01 16:42:18 -08:00
|
|
|
#include "mlir/IR/IntegerSet.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-08-30 17:35:15 -07:00
|
|
|
#include "mlir/Transforms/Passes.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
|
|
|
#define DEBUG_TYPE "simplify-affine-structure"
|
|
|
|
|
|
2018-08-30 17:35:15 -07:00
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2018-12-28 16:05:35 -08:00
|
|
|
/// Simplifies all affine expressions appearing in the operation instructions of
|
2018-12-28 08:48:09 -08:00
|
|
|
/// the Function. This is mainly to test the simplifyAffineExpr method.
|
2018-12-30 23:10:35 -08:00
|
|
|
/// TODO(someone): This should just be defined as a canonicalization pattern
|
|
|
|
|
/// on AffineMap and driven from the existing canonicalization pass.
|
|
|
|
|
struct SimplifyAffineStructures : public FunctionPass {
|
2018-11-07 10:24:03 -08:00
|
|
|
explicit SimplifyAffineStructures()
|
|
|
|
|
: FunctionPass(&SimplifyAffineStructures::passID) {}
|
2018-08-30 17:35:15 -07:00
|
|
|
|
2018-12-30 23:10:35 -08:00
|
|
|
PassResult runOnFunction(Function *f) override;
|
2018-10-24 11:30:06 -07:00
|
|
|
|
2018-11-06 18:34:18 -08:00
|
|
|
static char passID;
|
2018-08-30 17:35:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
2018-11-06 18:34:18 -08:00
|
|
|
char SimplifyAffineStructures::passID = 0;
|
|
|
|
|
|
2018-10-25 08:33:02 -07:00
|
|
|
FunctionPass *mlir::createSimplifyAffineStructuresPass() {
|
2018-10-24 11:30:06 -07:00
|
|
|
return new SimplifyAffineStructures();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-10 12:59:53 -08:00
|
|
|
/// Performs basic integer set simplifications. Checks if it's empty, and
|
|
|
|
|
/// replaces it with the canonical empty set if it is.
|
2018-10-24 11:30:06 -07:00
|
|
|
static IntegerSet simplifyIntegerSet(IntegerSet set) {
|
|
|
|
|
FlatAffineConstraints fac(set);
|
|
|
|
|
if (fac.isEmpty())
|
|
|
|
|
return IntegerSet::getEmptySet(set.getNumDims(), set.getNumSymbols(),
|
|
|
|
|
set.getContext());
|
|
|
|
|
return set;
|
2018-08-30 17:35:15 -07:00
|
|
|
}
|
|
|
|
|
|
2019-01-28 18:28:43 -08:00
|
|
|
PassResult SimplifyAffineStructures::runOnFunction(Function *f) {
|
2019-02-04 10:30:45 -08:00
|
|
|
f->walk([&](OperationInst *opInst) {
|
2019-01-28 21:23:53 -08:00
|
|
|
for (auto attr : opInst->getAttrs()) {
|
|
|
|
|
if (auto mapAttr = attr.second.dyn_cast<AffineMapAttr>()) {
|
|
|
|
|
MutableAffineMap mMap(mapAttr.getValue());
|
|
|
|
|
mMap.simplify();
|
|
|
|
|
auto map = mMap.getAffineMap();
|
|
|
|
|
opInst->setAttr(attr.first, AffineMapAttr::get(map));
|
|
|
|
|
} else if (auto setAttr = attr.second.dyn_cast<IntegerSetAttr>()) {
|
|
|
|
|
auto simplified = simplifyIntegerSet(setAttr.getValue());
|
|
|
|
|
opInst->setAttr(attr.first, IntegerSetAttr::get(simplified));
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-30 23:10:35 -08:00
|
|
|
});
|
|
|
|
|
|
2018-09-14 15:59:13 -07:00
|
|
|
return success();
|
2018-08-30 17:35:15 -07:00
|
|
|
}
|
2018-11-06 18:34:18 -08:00
|
|
|
|
|
|
|
|
static PassRegistration<SimplifyAffineStructures>
|
|
|
|
|
pass("simplify-affine-structures", "Simplify affine expressions");
|