2019-05-11 11:50:29 -07:00
|
|
|
//===- SimplifyAffineStructures.cpp ---------------------------------------===//
|
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
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2019-02-22 16:51:08 -08:00
|
|
|
#include "mlir/Analysis/AffineStructures.h"
|
2019-02-01 16:42:18 -08:00
|
|
|
#include "mlir/IR/IntegerSet.h"
|
2019-02-19 17:17:46 -08:00
|
|
|
#include "mlir/Pass/Pass.h"
|
2018-08-30 17:35:15 -07:00
|
|
|
#include "mlir/Transforms/Passes.h"
|
2019-09-03 12:13:59 -07:00
|
|
|
#include "mlir/Transforms/Utils.h"
|
2018-08-30 17:35:15 -07:00
|
|
|
|
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 {
|
|
|
|
|
|
2019-09-03 12:13:59 -07:00
|
|
|
/// Simplifies affine maps and sets appearing in the operations of the Function.
|
|
|
|
|
/// This part is mainly to test the simplifyAffineExpr method. In addition,
|
|
|
|
|
/// all memrefs with non-trivial layout maps are converted to ones with trivial
|
|
|
|
|
/// identity layout ones.
|
2019-02-27 10:59:29 -08:00
|
|
|
struct SimplifyAffineStructures
|
|
|
|
|
: public FunctionPass<SimplifyAffineStructures> {
|
2019-02-28 14:50:42 -08:00
|
|
|
void runOnFunction() override;
|
2019-03-15 09:24:05 -07:00
|
|
|
|
|
|
|
|
/// Utility to simplify an affine attribute and update its entry in the parent
|
2019-03-27 14:02:02 -07:00
|
|
|
/// operation if necessary.
|
2019-03-15 09:24:05 -07:00
|
|
|
template <typename AttributeT>
|
2019-03-27 14:02:02 -07:00
|
|
|
void simplifyAndUpdateAttribute(Operation *op, Identifier name,
|
2019-03-15 09:24:05 -07:00
|
|
|
AttributeT attr) {
|
|
|
|
|
auto &simplified = simplifiedAttributes[attr];
|
|
|
|
|
if (simplified == attr)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// This is a newly encountered attribute.
|
|
|
|
|
if (!simplified) {
|
|
|
|
|
// Try to simplify the value of the attribute.
|
|
|
|
|
auto value = attr.getValue();
|
|
|
|
|
auto simplifiedValue = simplify(value);
|
|
|
|
|
if (simplifiedValue == value) {
|
|
|
|
|
simplified = attr;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
simplified = AttributeT::get(simplifiedValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Simplification was successful, so update the attribute.
|
2019-03-27 14:02:02 -07:00
|
|
|
op->setAttr(name, simplified);
|
2019-03-15 09:24:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Performs basic integer set simplifications. Checks if it's empty, and
|
|
|
|
|
/// replaces it with the canonical empty set if it is.
|
|
|
|
|
IntegerSet simplify(IntegerSet set) {
|
|
|
|
|
FlatAffineConstraints fac(set);
|
|
|
|
|
if (fac.isEmpty())
|
|
|
|
|
return IntegerSet::getEmptySet(set.getNumDims(), set.getNumSymbols(),
|
2019-03-27 13:57:02 -07:00
|
|
|
&getContext());
|
2019-03-15 09:24:05 -07:00
|
|
|
return set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Performs basic affine map simplifications.
|
|
|
|
|
AffineMap simplify(AffineMap map) {
|
|
|
|
|
MutableAffineMap mMap(map);
|
|
|
|
|
mMap.simplify();
|
|
|
|
|
return mMap.getAffineMap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DenseMap<Attribute, Attribute> simplifiedAttributes;
|
2018-08-30 17:35:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
2019-09-13 13:33:46 -07:00
|
|
|
std::unique_ptr<OpPassBase<FuncOp>> mlir::createSimplifyAffineStructuresPass() {
|
2019-08-17 11:05:35 -07:00
|
|
|
return std::make_unique<SimplifyAffineStructures>();
|
2018-10-24 11:30:06 -07:00
|
|
|
}
|
|
|
|
|
|
2019-02-28 14:50:42 -08:00
|
|
|
void SimplifyAffineStructures::runOnFunction() {
|
2019-09-03 12:13:59 -07:00
|
|
|
auto func = getFunction();
|
2019-03-15 09:24:05 -07:00
|
|
|
simplifiedAttributes.clear();
|
2019-09-03 12:13:59 -07:00
|
|
|
func.walk([&](Operation *opInst) {
|
2019-01-28 21:23:53 -08:00
|
|
|
for (auto attr : opInst->getAttrs()) {
|
2019-03-15 09:24:05 -07:00
|
|
|
if (auto mapAttr = attr.second.dyn_cast<AffineMapAttr>())
|
|
|
|
|
simplifyAndUpdateAttribute(opInst, attr.first, mapAttr);
|
|
|
|
|
else if (auto setAttr = attr.second.dyn_cast<IntegerSetAttr>())
|
|
|
|
|
simplifyAndUpdateAttribute(opInst, attr.first, setAttr);
|
2019-01-28 21:23:53 -08:00
|
|
|
}
|
2018-12-30 23:10:35 -08:00
|
|
|
});
|
2019-09-03 12:13:59 -07:00
|
|
|
|
2019-10-07 10:03:38 -07:00
|
|
|
// Turn memrefs' non-identity layouts maps into ones with identity. Collect
|
|
|
|
|
// alloc ops first and then process since normalizeMemRef replaces/erases ops
|
|
|
|
|
// during memref rewriting.
|
|
|
|
|
SmallVector<AllocOp, 4> allocOps;
|
|
|
|
|
func.walk([&](AllocOp op) { allocOps.push_back(op); });
|
|
|
|
|
for (auto allocOp : allocOps) {
|
|
|
|
|
normalizeMemRef(allocOp);
|
|
|
|
|
}
|
2018-08-30 17:35:15 -07:00
|
|
|
}
|
2018-11-06 18:34:18 -08:00
|
|
|
|
|
|
|
|
static PassRegistration<SimplifyAffineStructures>
|
2019-10-07 10:03:38 -07:00
|
|
|
pass("simplify-affine-structures",
|
|
|
|
|
"Simplify affine expressions in maps/sets and normalize memrefs");
|