[MLIR] Improve support for 0-dimensional Affine Maps.

Summary:
Modified AffineMap::get to remove support for the overload which allowed
an ArrayRef of AffineExpr but no context (and gathered the context from a
presumed first entry, resulting in bugs when there were 0 results).

Instead, we support only a ArrayRef and a context, and a version which
takes a single AffineExpr.

Additionally, removed some now needless case logic which previously
special cased which call to AffineMap::get to use.

Reviewers: flaub, bondhugula, rriddle!, nicolasvasilache, ftynse, ulysseB, mravishankar, antiagainst, aartbik

Subscribers: mehdi_amini, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, bader, grosul1, frgossen, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D78226
This commit is contained in:
Jeremy Bruestle 2020-04-15 11:12:47 -07:00
parent 639b8da8dc
commit 9f3ab92ec8
25 changed files with 124 additions and 115 deletions

View File

@ -188,9 +188,9 @@ def DotOp : LinalgStructured_Op<"dot", [NInputs<2>, NOutputs<1>]> {
MLIRContext *context = getContext(); MLIRContext *context = getContext();
auto r_i = getAffineDimExpr(0, context); auto r_i = getAffineDimExpr(0, context);
return SmallVector<AffineMap, 8>{ return SmallVector<AffineMap, 8>{
AffineMap::get(1, 0, {r_i}), AffineMap::get(1, 0, {r_i}, context),
AffineMap::get(1, 0, {r_i}), AffineMap::get(1, 0, {r_i}, context),
AffineMap::get(1, 0, context)}; AffineMap::get(1, 0, {}, context)};
} }
}]; }];
@ -215,8 +215,10 @@ def MatvecOp : LinalgStructured_Op<"matvec", [NInputs<2>, NOutputs<1>]> {
AffineExpr i, r_j; AffineExpr i, r_j;
bindDims(context, i, r_j); bindDims(context, i, r_j);
return SmallVector<AffineMap, 8>{ return SmallVector<AffineMap, 8>{
AffineMap::get(2, 0, {i, r_j}), AffineMap::get(2, 0, {r_j}), AffineMap::get(2, 0, {i, r_j}, context),
AffineMap::get(2, 0, {i})}; AffineMap::get(2, 0, {r_j}, context),
AffineMap::get(2, 0, {i}, context)
};
} }
}]; }];
@ -242,9 +244,11 @@ def MatmulOp : LinalgStructured_Op<"matmul", [NInputs<2>, NOutputs<1>]> {
MLIRContext *context = getContext(); MLIRContext *context = getContext();
AffineExpr i, j, r_k; AffineExpr i, j, r_k;
bindDims(context, i, j, r_k); bindDims(context, i, j, r_k);
return SmallVector<AffineMap, 8>{AffineMap::get(3, 0, {i, r_k}), return SmallVector<AffineMap, 8>{
AffineMap::get(3, 0, {r_k, j}), AffineMap::get(3, 0, {i, r_k}, context),
AffineMap::get(3, 0, {i, j})}; AffineMap::get(3, 0, {r_k, j},context),
AffineMap::get(3, 0, {i, j}, context)
};
} }
}]; }];
@ -403,15 +407,15 @@ def ConvOp : PoolingBase_Op<"conv", [NInputs<2>, NOutputs<1>]> {
auto ws = weightedPoolingInputIndex(*this, xs, zs); auto ws = weightedPoolingInputIndex(*this, xs, zs);
return SmallVector<AffineMap, 8>{ return SmallVector<AffineMap, 8>{
// filter[z[0], ..., z[N-1], q, k] // filter[z[0], ..., z[N-1], q, k]
AffineMap::get(idx, 0, concat(concat(zs, qs), ks)), AffineMap::get(idx, 0, concat(concat(zs, qs), ks), context),
// input[b, // input[b,
// x[0]*s[0] + d[0]*z[0] - pad_low[0], // x[0]*s[0] + d[0]*z[0] - pad_low[0],
// ... // ...
// x[N-1]*s[N-1] + d[N-1]*z[N-1] - pad_low[N-1], // x[N-1]*s[N-1] + d[N-1]*z[N-1] - pad_low[N-1],
// q] // q]
AffineMap::get(idx, 0, concat(concat(bs, ws), qs)), AffineMap::get(idx, 0, concat(concat(bs, ws), qs), context),
// output[b, x[0], ..., x[N-1], k] // output[b, x[0], ..., x[N-1], k]
AffineMap::get(idx, 0, concat(concat(bs, xs), ks))}; AffineMap::get(idx, 0, concat(concat(bs, xs), ks), context)};
} }
}]; }];
@ -465,11 +469,11 @@ class SingleInputPoolingBase_Op<string mnemonic>
weightedPoolingInputIndex(*this, outputDims, windowDims); weightedPoolingInputIndex(*this, outputDims, windowDims);
return SmallVector<AffineMap, 8>{ return SmallVector<AffineMap, 8>{
// input // input
AffineMap::get(idx, 0, inputDims), AffineMap::get(idx, 0, inputDims, context),
// windowDims // windowDims
AffineMap::get(idx, 0, windowDims), AffineMap::get(idx, 0, windowDims, context),
// output // output
AffineMap::get(idx, 0, outputDims) AffineMap::get(idx, 0, outputDims, context)
}; };
} }
}]; }];

View File

@ -25,22 +25,24 @@
namespace mlir { namespace mlir {
inline bool isRowMajorMatmul(ArrayAttr indexingMaps) { inline bool isRowMajorMatmul(ArrayAttr indexingMaps) {
auto context = indexingMaps.getContext();
AffineExpr m, n, k; AffineExpr m, n, k;
bindDims(indexingMaps.getContext(), m, n, k); bindDims(context, m, n, k);
auto mapA = AffineMapAttr::get(AffineMap::get(3, 0, {m, k})); auto mapA = AffineMapAttr::get(AffineMap::get(3, 0, {m, k}, context));
auto mapB = AffineMapAttr::get(AffineMap::get(3, 0, {k, n})); auto mapB = AffineMapAttr::get(AffineMap::get(3, 0, {k, n}, context));
auto mapC = AffineMapAttr::get(AffineMap::get(3, 0, {m, n})); auto mapC = AffineMapAttr::get(AffineMap::get(3, 0, {m, n}, context));
auto maps = ArrayAttr::get({mapA, mapB, mapC}, indexingMaps.getContext()); auto maps = ArrayAttr::get({mapA, mapB, mapC}, context);
return indexingMaps == maps; return indexingMaps == maps;
} }
inline bool isColumnMajorMatmul(ArrayAttr indexingMaps) { inline bool isColumnMajorMatmul(ArrayAttr indexingMaps) {
auto context = indexingMaps.getContext();
AffineExpr m, n, k; AffineExpr m, n, k;
bindDims(indexingMaps.getContext(), m, n, k); bindDims(context, m, n, k);
auto mapA = AffineMapAttr::get(AffineMap::get(3, 0, {k, n})); auto mapA = AffineMapAttr::get(AffineMap::get(3, 0, {k, n}, context));
auto mapB = AffineMapAttr::get(AffineMap::get(3, 0, {m, k})); auto mapB = AffineMapAttr::get(AffineMap::get(3, 0, {m, k}, context));
auto mapC = AffineMapAttr::get(AffineMap::get(3, 0, {n, m})); auto mapC = AffineMapAttr::get(AffineMap::get(3, 0, {n, m}, context));
auto maps = ArrayAttr::get({mapA, mapB, mapC}, indexingMaps.getContext()); auto maps = ArrayAttr::get({mapA, mapB, mapC}, context);
return indexingMaps == maps; return indexingMaps == maps;
} }

View File

@ -49,13 +49,13 @@ public:
static AffineMap get(unsigned dimCount, unsigned symbolCount, static AffineMap get(unsigned dimCount, unsigned symbolCount,
MLIRContext *context); MLIRContext *context);
/// Returns an affine map with `dimCount` dimensions and `symbolCount` symbols /// Returns an affine map with `dimCount` dimensions and `symbolCount` mapping
/// mapping to the given results. The array of results cannot be empty. /// to a single output dimension
static AffineMap get(unsigned dimCount, unsigned symbolCount, static AffineMap get(unsigned dimCount, unsigned symbolCount,
ArrayRef<AffineExpr> results); AffineExpr result);
/// Returns an affine map with `dimCount` dimensions and `symbolCount` mapping /// Returns an affine map with `dimCount` dimensions and `symbolCount` mapping
/// to the given results, where the number of results can be zero. /// to the given results.
static AffineMap get(unsigned dimCount, unsigned symbolCount, static AffineMap get(unsigned dimCount, unsigned symbolCount,
ArrayRef<AffineExpr> results, MLIRContext *context); ArrayRef<AffineExpr> results, MLIRContext *context);

View File

@ -1464,11 +1464,8 @@ std::pair<AffineMap, AffineMap> FlatAffineConstraints::getLowerAndUpperBound(
lbExprs.push_back(expr); lbExprs.push_back(expr);
} }
auto lbMap = lbExprs.empty() ? AffineMap() auto lbMap = AffineMap::get(dimCount, symCount, lbExprs, context);
: AffineMap::get(dimCount, symCount, lbExprs); auto ubMap = AffineMap::get(dimCount, symCount, ubExprs, context);
auto ubMap = ubExprs.empty() ? AffineMap()
: AffineMap::get(dimCount, symCount, ubExprs);
return {lbMap, ubMap}; return {lbMap, ubMap};
} }

View File

@ -62,8 +62,8 @@ void mlir::buildTripCountMapAndOperands(
SmallVector<AffineExpr, 4> lbSplatExpr(ubValueMap.getNumResults(), SmallVector<AffineExpr, 4> lbSplatExpr(ubValueMap.getNumResults(),
lbMap.getResult(0)); lbMap.getResult(0));
auto lbMapSplat = auto lbMapSplat = AffineMap::get(lbMap.getNumDims(), lbMap.getNumSymbols(),
AffineMap::get(lbMap.getNumDims(), lbMap.getNumSymbols(), lbSplatExpr); lbSplatExpr, b.getContext());
AffineValueMap lbSplatValueMap(lbMapSplat, forOp.getLowerBoundOperands()); AffineValueMap lbSplatValueMap(lbMapSplat, forOp.getLowerBoundOperands());
AffineValueMap tripCountValueMap; AffineValueMap tripCountValueMap;

View File

@ -99,11 +99,11 @@ SingleWorkgroupReduction::matchAsPerformingReduction(
genericOp.indexing_maps().getValue()[1].cast<AffineMapAttr>(); genericOp.indexing_maps().getValue()[1].cast<AffineMapAttr>();
// The indexing map for the input should be `(i) -> (i)`. // The indexing map for the input should be `(i) -> (i)`.
if (inputMap.getValue() != if (inputMap.getValue() !=
AffineMap::get(1, 0, {getAffineDimExpr(0, op->getContext())})) AffineMap::get(1, 0, getAffineDimExpr(0, op->getContext())))
return llvm::None; return llvm::None;
// The indexing map for the input should be `(i) -> (0)`. // The indexing map for the input should be `(i) -> (0)`.
if (outputMap.getValue() != if (outputMap.getValue() !=
AffineMap::get(1, 0, {getAffineConstantExpr(0, op->getContext())})) AffineMap::get(1, 0, getAffineConstantExpr(0, op->getContext())))
return llvm::None; return llvm::None;
return linalg::RegionMatcher::matchAsScalarBinaryOp(genericOp); return linalg::RegionMatcher::matchAsScalarBinaryOp(genericOp);

View File

@ -129,7 +129,7 @@ static ValueHandle createBinaryIndexHandle(
if (v1) { if (v1) {
operands.push_back(v1); operands.push_back(v1);
} }
auto map = AffineMap::get(numDims, numSymbols, {affCombiner(d0, d1)}); auto map = AffineMap::get(numDims, numSymbols, affCombiner(d0, d1));
// TODO: createOrFold when available. // TODO: createOrFold when available.
Operation *op = Operation *op =
makeComposedAffineApply(ScopedContext::getBuilder(), makeComposedAffineApply(ScopedContext::getBuilder(),

View File

@ -522,7 +522,8 @@ AffineApplyNormalizer::AffineApplyNormalizer(AffineMap map,
"Unexpected number of concatenated symbols"); "Unexpected number of concatenated symbols");
auto numDims = dimValueToPosition.size(); auto numDims = dimValueToPosition.size();
auto numSymbols = concatenatedSymbols.size() - map.getNumSymbols(); auto numSymbols = concatenatedSymbols.size() - map.getNumSymbols();
auto auxiliaryMap = AffineMap::get(numDims, numSymbols, auxiliaryExprs); auto auxiliaryMap =
AffineMap::get(numDims, numSymbols, auxiliaryExprs, map.getContext());
LLVM_DEBUG(map.print(dbgs() << "\nCompose map: ")); LLVM_DEBUG(map.print(dbgs() << "\nCompose map: "));
LLVM_DEBUG(auxiliaryMap.print(dbgs() << "\nWith map: ")); LLVM_DEBUG(auxiliaryMap.print(dbgs() << "\nWith map: "));
@ -2163,19 +2164,13 @@ LogicalResult AffinePrefetchOp::fold(ArrayRef<Attribute> cstOperands,
void AffineParallelOp::build(Builder *builder, OperationState &result, void AffineParallelOp::build(Builder *builder, OperationState &result,
ArrayRef<int64_t> ranges) { ArrayRef<int64_t> ranges) {
// Default initialize empty maps.
auto lbMap = AffineMap::get(builder->getContext());
auto ubMap = AffineMap::get(builder->getContext());
// If there are ranges, set each to [0, N).
if (ranges.size()) {
SmallVector<AffineExpr, 8> lbExprs(ranges.size(), SmallVector<AffineExpr, 8> lbExprs(ranges.size(),
builder->getAffineConstantExpr(0)); builder->getAffineConstantExpr(0));
lbMap = AffineMap::get(0, 0, lbExprs); auto lbMap = AffineMap::get(0, 0, lbExprs, builder->getContext());
SmallVector<AffineExpr, 8> ubExprs; SmallVector<AffineExpr, 8> ubExprs;
for (int64_t range : ranges) for (int64_t range : ranges)
ubExprs.push_back(builder->getAffineConstantExpr(range)); ubExprs.push_back(builder->getAffineConstantExpr(range));
ubMap = AffineMap::get(0, 0, ubExprs); auto ubMap = AffineMap::get(0, 0, ubExprs, builder->getContext());
}
build(builder, result, lbMap, {}, ubMap, {}); build(builder, result, lbMap, {}, ubMap, {});
} }

View File

@ -51,8 +51,9 @@ void AffineValueMap::difference(const AffineValueMap &a,
diffExprs.push_back(normalizer.getAffineMap().getResult(i) - diffExprs.push_back(normalizer.getAffineMap().getResult(i) -
bMap.getResult(i)); bMap.getResult(i));
auto diffMap = AffineMap::get(normalizer.getNumDims(), auto diffMap =
normalizer.getNumSymbols(), diffExprs); AffineMap::get(normalizer.getNumDims(), normalizer.getNumSymbols(),
diffExprs, aMap.getContext());
canonicalizeMapAndOperands(&diffMap, &bOperands); canonicalizeMapAndOperands(&diffMap, &bOperands);
diffMap = simplifyAffineMap(diffMap); diffMap = simplifyAffineMap(diffMap);
res->reset(diffMap, bOperands); res->reset(diffMap, bOperands);

View File

@ -143,8 +143,9 @@ constructTiledIndexSetHyperRect(MutableArrayRef<AffineForOp> origLoops,
boundExprs.push_back(dim + tileSizes[i]); boundExprs.push_back(dim + tileSizes[i]);
boundExprs.append(origUbMap.getResults().begin(), boundExprs.append(origUbMap.getResults().begin(),
origUbMap.getResults().end()); origUbMap.getResults().end());
auto ubMap = AffineMap::get(origUbMap.getNumDims() + 1, auto ubMap =
origUbMap.getNumSymbols(), boundExprs); AffineMap::get(origUbMap.getNumDims() + 1, origUbMap.getNumSymbols(),
boundExprs, b.getContext());
newLoops[width + i].setUpperBound(/*operands=*/ubOperands, ubMap); newLoops[width + i].setUpperBound(/*operands=*/ubOperands, ubMap);
} else { } else {
// No need of the min expression. // No need of the min expression.

View File

@ -523,8 +523,10 @@ getSymbolLessAffineMaps(ArrayRef<ArrayRef<AffineExpr>> reassociation) {
"Expected symbol-less expressions"); "Expected symbol-less expressions");
SmallVector<AffineMap, 4> maps; SmallVector<AffineMap, 4> maps;
maps.reserve(reassociation.size()); maps.reserve(reassociation.size());
for (auto exprs : reassociation) for (auto exprs : reassociation) {
maps.push_back(AffineMap::get(maxDim + 1, 0, exprs)); assert(exprs.size() != 0);
maps.push_back(AffineMap::get(maxDim + 1, 0, exprs, exprs[0].getContext()));
}
return maps; return maps;
} }

View File

@ -252,7 +252,8 @@ public:
// so having a max op is enough. // so having a max op is enough.
auto maxMap = AffineMap::get(/*dimCount=*/1, 0, auto maxMap = AffineMap::get(/*dimCount=*/1, 0,
{getAffineDimExpr(/*position=*/0, context), {getAffineDimExpr(/*position=*/0, context),
getAffineConstantExpr(0, context)}); getAffineConstantExpr(0, context)},
context);
clampedImIdx.push_back( clampedImIdx.push_back(
affine_max(dim.getType(), maxMap, ValueRange{dim})); affine_max(dim.getType(), maxMap, ValueRange{dim}));
} }

View File

@ -294,7 +294,8 @@ makeTiledViews(OpBuilder &b, Location loc, LinalgOp linalgOp,
/*dimCount=*/3, /*symbolCount=*/0, /*dimCount=*/3, /*symbolCount=*/0,
{getAffineDimExpr(/*position=*/0, b.getContext()), {getAffineDimExpr(/*position=*/0, b.getContext()),
getAffineDimExpr(/*position=*/1, b.getContext()) - getAffineDimExpr(/*position=*/1, b.getContext()) -
getAffineDimExpr(/*position=*/2, b.getContext())}); getAffineDimExpr(/*position=*/2, b.getContext())},
b.getContext());
auto d = folded_std_dim(folder, view, r); auto d = folded_std_dim(folder, view, r);
size = folded_affine_min(folder, b.getIndexType(), minMap, size = folded_affine_min(folder, b.getIndexType(), minMap,
ValueRange{size, d, offset}); ValueRange{size, d, offset});

View File

@ -66,7 +66,8 @@ void mlir::loop::tileParallelLoop(ParallelOp op, ArrayRef<int64_t> tileSizes) {
/*dimCount=*/3, /*symbolCount=*/0, /*dimCount=*/3, /*symbolCount=*/0,
{getAffineDimExpr(/*position=*/0, b.getContext()), {getAffineDimExpr(/*position=*/0, b.getContext()),
getAffineDimExpr(/*position=*/1, b.getContext()) - getAffineDimExpr(/*position=*/1, b.getContext()) -
getAffineDimExpr(/*position=*/2, b.getContext())}); getAffineDimExpr(/*position=*/2, b.getContext())},
b.getContext());
// Create the inner loop with adjusted bounds. // Create the inner loop with adjusted bounds.
SmallVector<Value, 2> newBounds; SmallVector<Value, 2> newBounds;

View File

@ -1348,9 +1348,7 @@ private:
auto targetExpr = getAffineDimExpr(idx < index ? idx : idx - 1, ctx); auto targetExpr = getAffineDimExpr(idx < index ? idx : idx - 1, ctx);
results.push_back(targetExpr); results.push_back(targetExpr);
} }
// The (...) -> () affine map has its own factory method. return AffineMap::get(map.getNumDims() - 1, 0, results, ctx);
return results.empty() ? AffineMap::get(map.getNumDims() - 1, 0, ctx)
: AffineMap::get(map.getNumDims() - 1, 0, results);
} }
// Helper to drop dimension from vector type. // Helper to drop dimension from vector type.

View File

@ -176,7 +176,7 @@ static AffineMap makePermutationMap(
"Vectorization prerequisite violated: at most 1 index may be " "Vectorization prerequisite violated: at most 1 index may be "
"invariant wrt a vectorized loop"); "invariant wrt a vectorized loop");
} }
return AffineMap::get(indices.size(), 0, perm); return AffineMap::get(indices.size(), 0, perm, context);
} }
/// Implementation detail that walks up the parents and records the ones with /// Implementation detail that walks up the parents and records the ones with

View File

@ -104,7 +104,7 @@ AffineMap AffineMap::getPermutationMap(ArrayRef<unsigned> permutation,
for (auto index : permutation) for (auto index : permutation)
affExprs.push_back(getAffineDimExpr(index, context)); affExprs.push_back(getAffineDimExpr(index, context));
auto m = std::max_element(permutation.begin(), permutation.end()); auto m = std::max_element(permutation.begin(), permutation.end());
auto permutationMap = AffineMap::get(*m + 1, 0, affExprs); auto permutationMap = AffineMap::get(*m + 1, 0, affExprs, context);
assert(permutationMap.isPermutation() && "Invalid permutation vector"); assert(permutationMap.isPermutation() && "Invalid permutation vector");
return permutationMap; return permutationMap;
} }
@ -127,13 +127,16 @@ static void getMaxDimAndSymbol(ArrayRef<AffineExprContainer> exprsList,
template <typename AffineExprContainer> template <typename AffineExprContainer>
static SmallVector<AffineMap, 4> static SmallVector<AffineMap, 4>
inferFromExprList(ArrayRef<AffineExprContainer> exprsList) { inferFromExprList(ArrayRef<AffineExprContainer> exprsList) {
assert(!exprsList.empty());
assert(!exprsList[0].empty());
auto context = exprsList[0][0].getContext();
int64_t maxDim = -1, maxSym = -1; int64_t maxDim = -1, maxSym = -1;
getMaxDimAndSymbol(exprsList, maxDim, maxSym); getMaxDimAndSymbol(exprsList, maxDim, maxSym);
SmallVector<AffineMap, 4> maps; SmallVector<AffineMap, 4> maps;
maps.reserve(exprsList.size()); maps.reserve(exprsList.size());
for (const auto &exprs : exprsList) for (const auto &exprs : exprsList)
maps.push_back(AffineMap::get(/*dimCount=*/maxDim + 1, maps.push_back(AffineMap::get(/*dimCount=*/maxDim + 1,
/*symbolCount=*/maxSym + 1, exprs)); /*symbolCount=*/maxSym + 1, exprs, context));
return maps; return maps;
} }
@ -153,7 +156,7 @@ AffineMap AffineMap::getMultiDimIdentityMap(unsigned numDims,
dimExprs.reserve(numDims); dimExprs.reserve(numDims);
for (unsigned i = 0; i < numDims; ++i) for (unsigned i = 0; i < numDims; ++i)
dimExprs.push_back(mlir::getAffineDimExpr(i, context)); dimExprs.push_back(mlir::getAffineDimExpr(i, context));
return get(/*dimCount=*/numDims, /*symbolCount=*/0, dimExprs); return get(/*dimCount=*/numDims, /*symbolCount=*/0, dimExprs, context);
} }
MLIRContext *AffineMap::getContext() const { return map->context; } MLIRContext *AffineMap::getContext() const { return map->context; }
@ -255,8 +258,7 @@ AffineMap AffineMap::replaceDimsAndSymbols(ArrayRef<AffineExpr> dimReplacements,
results.push_back( results.push_back(
expr.replaceDimsAndSymbols(dimReplacements, symReplacements)); expr.replaceDimsAndSymbols(dimReplacements, symReplacements));
return results.empty() ? get(numResultDims, 0, getContext()) return get(numResultDims, numResultSyms, results, getContext());
: get(numResultDims, numResultSyms, results);
} }
AffineMap AffineMap::compose(AffineMap map) { AffineMap AffineMap::compose(AffineMap map) {
@ -280,8 +282,7 @@ AffineMap AffineMap::compose(AffineMap map) {
exprs.reserve(getResults().size()); exprs.reserve(getResults().size());
for (auto expr : getResults()) for (auto expr : getResults())
exprs.push_back(expr.compose(newMap)); exprs.push_back(expr.compose(newMap));
return exprs.empty() ? AffineMap::get(numDims, 0, map.getContext()) return AffineMap::get(numDims, numSymbols, exprs, map.getContext());
: AffineMap::get(numDims, numSymbols, exprs);
} }
bool AffineMap::isProjectedPermutation() { bool AffineMap::isProjectedPermutation() {
@ -312,7 +313,7 @@ AffineMap AffineMap::getSubMap(ArrayRef<unsigned> resultPos) {
for (auto idx : resultPos) { for (auto idx : resultPos) {
exprs.push_back(getResult(idx)); exprs.push_back(getResult(idx));
} }
return AffineMap::get(getNumDims(), getNumSymbols(), exprs); return AffineMap::get(getNumDims(), getNumSymbols(), exprs, getContext());
} }
AffineMap mlir::simplifyAffineMap(AffineMap map) { AffineMap mlir::simplifyAffineMap(AffineMap map) {
@ -321,7 +322,8 @@ AffineMap mlir::simplifyAffineMap(AffineMap map) {
exprs.push_back( exprs.push_back(
simplifyAffineExpr(e, map.getNumDims(), map.getNumSymbols())); simplifyAffineExpr(e, map.getNumDims(), map.getNumSymbols()));
} }
return AffineMap::get(map.getNumDims(), map.getNumSymbols(), exprs); return AffineMap::get(map.getNumDims(), map.getNumSymbols(), exprs,
map.getContext());
} }
AffineMap mlir::removeDuplicateExprs(AffineMap map) { AffineMap mlir::removeDuplicateExprs(AffineMap map) {
@ -354,7 +356,7 @@ AffineMap mlir::inversePermutation(AffineMap map) {
seenExprs.push_back(expr); seenExprs.push_back(expr);
if (seenExprs.size() != map.getNumInputs()) if (seenExprs.size() != map.getNumInputs())
return AffineMap(); return AffineMap();
return AffineMap::get(map.getNumResults(), 0, seenExprs); return AffineMap::get(map.getNumResults(), 0, seenExprs, map.getContext());
} }
AffineMap mlir::concatAffineMaps(ArrayRef<AffineMap> maps) { AffineMap mlir::concatAffineMaps(ArrayRef<AffineMap> maps) {
@ -369,9 +371,8 @@ AffineMap mlir::concatAffineMaps(ArrayRef<AffineMap> maps) {
results.append(m.getResults().begin(), m.getResults().end()); results.append(m.getResults().begin(), m.getResults().end());
numDims = std::max(m.getNumDims(), numDims); numDims = std::max(m.getNumDims(), numDims);
} }
return results.empty() ? AffineMap::get(numDims, /*numSymbols=*/0, return AffineMap::get(numDims, /*numSymbols=*/0, results,
maps.front().getContext()) maps.front().getContext());
: AffineMap::get(numDims, /*numSymbols=*/0, results);
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -380,8 +381,7 @@ AffineMap mlir::concatAffineMaps(ArrayRef<AffineMap> maps) {
MutableAffineMap::MutableAffineMap(AffineMap map) MutableAffineMap::MutableAffineMap(AffineMap map)
: numDims(map.getNumDims()), numSymbols(map.getNumSymbols()), : numDims(map.getNumDims()), numSymbols(map.getNumSymbols()),
// A map always has at least 1 result by construction context(map.getContext()) {
context(map.getResult(0).getContext()) {
for (auto result : map.getResults()) for (auto result : map.getResults())
results.push_back(result); results.push_back(result);
} }
@ -390,8 +390,7 @@ void MutableAffineMap::reset(AffineMap map) {
results.clear(); results.clear();
numDims = map.getNumDims(); numDims = map.getNumDims();
numSymbols = map.getNumSymbols(); numSymbols = map.getNumSymbols();
// A map always has at least 1 result by construction context = map.getContext();
context = map.getResult(0).getContext();
for (auto result : map.getResults()) for (auto result : map.getResults())
results.push_back(result); results.push_back(result);
} }
@ -416,5 +415,5 @@ void MutableAffineMap::simplify() {
} }
AffineMap MutableAffineMap::getAffineMap() const { AffineMap MutableAffineMap::getAffineMap() const {
return AffineMap::get(numDims, numSymbols, results); return AffineMap::get(numDims, numSymbols, results, context);
} }

View File

@ -293,12 +293,11 @@ AffineMap Builder::getEmptyAffineMap() { return AffineMap::get(context); }
AffineMap Builder::getConstantAffineMap(int64_t val) { AffineMap Builder::getConstantAffineMap(int64_t val) {
return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/0, return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/0,
{getAffineConstantExpr(val)}); getAffineConstantExpr(val));
} }
AffineMap Builder::getDimIdentityMap() { AffineMap Builder::getDimIdentityMap() {
return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, getAffineDimExpr(0));
{getAffineDimExpr(0)});
} }
AffineMap Builder::getMultiDimIdentityMap(unsigned rank) { AffineMap Builder::getMultiDimIdentityMap(unsigned rank) {
@ -306,18 +305,19 @@ AffineMap Builder::getMultiDimIdentityMap(unsigned rank) {
dimExprs.reserve(rank); dimExprs.reserve(rank);
for (unsigned i = 0; i < rank; ++i) for (unsigned i = 0; i < rank; ++i)
dimExprs.push_back(getAffineDimExpr(i)); dimExprs.push_back(getAffineDimExpr(i));
return AffineMap::get(/*dimCount=*/rank, /*symbolCount=*/0, dimExprs); return AffineMap::get(/*dimCount=*/rank, /*symbolCount=*/0, dimExprs,
context);
} }
AffineMap Builder::getSymbolIdentityMap() { AffineMap Builder::getSymbolIdentityMap() {
return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/1, return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/1,
{getAffineSymbolExpr(0)}); getAffineSymbolExpr(0));
} }
AffineMap Builder::getSingleDimShiftAffineMap(int64_t shift) { AffineMap Builder::getSingleDimShiftAffineMap(int64_t shift) {
// expr = d0 + shift. // expr = d0 + shift.
auto expr = getAffineDimExpr(0) + shift; auto expr = getAffineDimExpr(0) + shift;
return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, {expr}); return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, expr);
} }
AffineMap Builder::getShiftedAffineMap(AffineMap map, int64_t shift) { AffineMap Builder::getShiftedAffineMap(AffineMap map, int64_t shift) {
@ -325,7 +325,8 @@ AffineMap Builder::getShiftedAffineMap(AffineMap map, int64_t shift) {
shiftedResults.reserve(map.getNumResults()); shiftedResults.reserve(map.getNumResults());
for (auto resultExpr : map.getResults()) for (auto resultExpr : map.getResults())
shiftedResults.push_back(resultExpr + shift); shiftedResults.push_back(resultExpr + shift);
return AffineMap::get(map.getNumDims(), map.getNumSymbols(), shiftedResults); return AffineMap::get(map.getNumDims(), map.getNumSymbols(), shiftedResults,
context);
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

View File

@ -717,10 +717,8 @@ AffineMap AffineMap::get(unsigned dimCount, unsigned symbolCount,
} }
AffineMap AffineMap::get(unsigned dimCount, unsigned symbolCount, AffineMap AffineMap::get(unsigned dimCount, unsigned symbolCount,
ArrayRef<AffineExpr> results) { AffineExpr result) {
// The number of results can't be zero. return getImpl(dimCount, symbolCount, {result}, result.getContext());
assert(!results.empty());
return getImpl(dimCount, symbolCount, results, results[0].getContext());
} }
AffineMap AffineMap::get(unsigned dimCount, unsigned symbolCount, AffineMap AffineMap::get(unsigned dimCount, unsigned symbolCount,

View File

@ -723,7 +723,7 @@ MemRefType mlir::canonicalizeStridedLayout(MemRefType t) {
simplifyAffineExpr(m.getResult(0), m.getNumDims(), m.getNumSymbols()); simplifyAffineExpr(m.getResult(0), m.getNumDims(), m.getNumSymbols());
if (expr != simplifiedLayoutExpr) if (expr != simplifiedLayoutExpr)
return MemRefType::Builder(t).setAffineMaps({AffineMap::get( return MemRefType::Builder(t).setAffineMaps({AffineMap::get(
m.getNumDims(), m.getNumSymbols(), {simplifiedLayoutExpr})}); m.getNumDims(), m.getNumSymbols(), simplifiedLayoutExpr)});
return MemRefType::Builder(t).setAffineMaps({}); return MemRefType::Builder(t).setAffineMaps({});
} }

View File

@ -3132,12 +3132,8 @@ AffineParser::parseAffineMapOfSSAIds(AffineMap &map,
/*allowEmptyList=*/true)) /*allowEmptyList=*/true))
return failure(); return failure();
// Parsed a valid affine map. // Parsed a valid affine map.
if (exprs.empty())
map = AffineMap::get(numDimOperands, dimsAndSymbols.size() - numDimOperands, map = AffineMap::get(numDimOperands, dimsAndSymbols.size() - numDimOperands,
getContext()); exprs, getContext());
else
map = AffineMap::get(numDimOperands, dimsAndSymbols.size() - numDimOperands,
exprs);
return success(); return success();
} }
@ -3166,11 +3162,8 @@ AffineMap AffineParser::parseAffineMapRange(unsigned numDims,
if (parseCommaSeparatedListUntil(Token::r_paren, parseElt, true)) if (parseCommaSeparatedListUntil(Token::r_paren, parseElt, true))
return AffineMap(); return AffineMap();
if (exprs.empty())
return AffineMap::get(numDims, numSymbols, getContext());
// Parsed a valid affine map. // Parsed a valid affine map.
return AffineMap::get(numDims, numSymbols, exprs); return AffineMap::get(numDims, numSymbols, exprs, getContext());
} }
/// Parse an affine constraint. /// Parse an affine constraint.

View File

@ -942,7 +942,8 @@ static Value createPrivateMemRef(AffineForOp forOp, Operation *srcStoreOpInst,
} }
auto indexRemap = zeroOffsetCount == rank auto indexRemap = zeroOffsetCount == rank
? AffineMap() ? AffineMap()
: AffineMap::get(outerIVs.size() + rank, 0, remapExprs); : AffineMap::get(outerIVs.size() + rank, 0, remapExprs,
forOp.getContext());
// Replace all users of 'oldMemRef' with 'newMemRef'. // Replace all users of 'oldMemRef' with 'newMemRef'.
LogicalResult res = LogicalResult res =
replaceAllMemRefUsesWith(oldMemRef, newMemRef, {}, indexRemap, replaceAllMemRefUsesWith(oldMemRef, newMemRef, {}, indexRemap,

View File

@ -98,8 +98,8 @@ static bool doubleBuffer(Value oldMemRef, AffineForOp forOp) {
// Create 'iv mod 2' value to index the leading dimension. // Create 'iv mod 2' value to index the leading dimension.
auto d0 = bInner.getAffineDimExpr(0); auto d0 = bInner.getAffineDimExpr(0);
int64_t step = forOp.getStep(); int64_t step = forOp.getStep();
auto modTwoMap = AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, auto modTwoMap =
{d0.floorDiv(step) % 2}); AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, d0.floorDiv(step) % 2);
auto ivModTwoOp = bInner.create<AffineApplyOp>(forOp.getLoc(), modTwoMap, auto ivModTwoOp = bInner.create<AffineApplyOp>(forOp.getLoc(), modTwoMap,
forOp.getInductionVar()); forOp.getInductionVar());

View File

@ -103,7 +103,8 @@ static void getCleanupLoopLowerBound(AffineForOp forOp, unsigned unrollFactor,
operands.clear(); operands.clear();
operands.push_back(lb); operands.push_back(lb);
operands.append(bumpValues.begin(), bumpValues.end()); operands.append(bumpValues.begin(), bumpValues.end());
map = AffineMap::get(1 + tripCountMap.getNumResults(), 0, newUbExprs); map = AffineMap::get(1 + tripCountMap.getNumResults(), 0, newUbExprs,
b.getContext());
// Simplify the map + operands. // Simplify the map + operands.
fullyComposeAffineMapAndOperands(&map, &operands); fullyComposeAffineMapAndOperands(&map, &operands);
map = simplifyAffineMap(map); map = simplifyAffineMap(map);
@ -485,7 +486,7 @@ LogicalResult mlir::loopUnrollByFactor(AffineForOp forOp,
if (!forOpIV.use_empty()) { if (!forOpIV.use_empty()) {
// iv' = iv + 1/2/3...unrollFactor-1; // iv' = iv + 1/2/3...unrollFactor-1;
auto d0 = builder.getAffineDimExpr(0); auto d0 = builder.getAffineDimExpr(0);
auto bumpMap = AffineMap::get(1, 0, {d0 + i * step}); auto bumpMap = AffineMap::get(1, 0, d0 + i * step);
auto ivUnroll = auto ivUnroll =
builder.create<AffineApplyOp>(forOp.getLoc(), bumpMap, forOpIV); builder.create<AffineApplyOp>(forOp.getLoc(), bumpMap, forOpIV);
operandMap.map(forOpIV, ivUnroll); operandMap.map(forOpIV, ivUnroll);
@ -616,7 +617,7 @@ LogicalResult mlir::loopUnrollJamByFactor(AffineForOp forOp,
if (!forOpIV.use_empty()) { if (!forOpIV.use_empty()) {
// iv' = iv + i, i = 1 to unrollJamFactor-1. // iv' = iv + i, i = 1 to unrollJamFactor-1.
auto d0 = builder.getAffineDimExpr(0); auto d0 = builder.getAffineDimExpr(0);
auto bumpMap = AffineMap::get(1, 0, {d0 + i * step}); auto bumpMap = AffineMap::get(1, 0, d0 + i * step);
auto ivUnroll = auto ivUnroll =
builder.create<AffineApplyOp>(forOp.getLoc(), bumpMap, forOpIV); builder.create<AffineApplyOp>(forOp.getLoc(), bumpMap, forOpIV);
operandMap.map(forOpIV, ivUnroll); operandMap.map(forOpIV, ivUnroll);
@ -859,7 +860,8 @@ static void augmentMapAndBounds(OpBuilder &b, Value iv, AffineMap *map,
auto bounds = llvm::to_vector<4>(map->getResults()); auto bounds = llvm::to_vector<4>(map->getResults());
bounds.push_back(b.getAffineDimExpr(map->getNumDims()) + offset); bounds.push_back(b.getAffineDimExpr(map->getNumDims()) + offset);
operands->insert(operands->begin() + map->getNumDims(), iv); operands->insert(operands->begin() + map->getNumDims(), iv);
*map = AffineMap::get(map->getNumDims() + 1, map->getNumSymbols(), bounds); *map = AffineMap::get(map->getNumDims() + 1, map->getNumSymbols(), bounds,
b.getContext());
canonicalizeMapAndOperands(map, operands); canonicalizeMapAndOperands(map, operands);
} }
@ -1514,7 +1516,7 @@ generatePointWiseCopy(Location loc, Value memref, Value fastMemRef,
b = forOp.getBodyBuilder(); b = forOp.getBodyBuilder();
auto fastBufOffsetMap = auto fastBufOffsetMap =
AffineMap::get(lbOperands.size(), 0, {fastBufOffsets[d]}); AffineMap::get(lbOperands.size(), 0, fastBufOffsets[d]);
auto offset = b.create<AffineApplyOp>(loc, fastBufOffsetMap, lbOperands); auto offset = b.create<AffineApplyOp>(loc, fastBufOffsetMap, lbOperands);
// Construct the subscript for the fast memref being copied into/from: // Construct the subscript for the fast memref being copied into/from:
@ -1529,7 +1531,8 @@ generatePointWiseCopy(Location loc, Value memref, Value fastMemRef,
memIndices.push_back(forOp.getInductionVar()); memIndices.push_back(forOp.getInductionVar());
} }
auto fastBufMap = AffineMap::get(2 * rank, /*symbolCount=*/0, fastBufExprs); auto fastBufMap =
AffineMap::get(2 * rank, /*symbolCount=*/0, fastBufExprs, b.getContext());
fullyComposeAffineMapAndOperands(&fastBufMap, &fastBufMapOperands); fullyComposeAffineMapAndOperands(&fastBufMap, &fastBufMapOperands);
fastBufMap = simplifyAffineMap(fastBufMap); fastBufMap = simplifyAffineMap(fastBufMap);
canonicalizeMapAndOperands(&fastBufMap, &fastBufMapOperands); canonicalizeMapAndOperands(&fastBufMap, &fastBufMapOperands);
@ -1837,7 +1840,8 @@ static LogicalResult generateCopy(
auto dimExpr = b.getAffineDimExpr(regionSymbols.size() + i); auto dimExpr = b.getAffineDimExpr(regionSymbols.size() + i);
remapExprs.push_back(dimExpr - fastBufOffsets[i]); remapExprs.push_back(dimExpr - fastBufOffsets[i]);
} }
auto indexRemap = AffineMap::get(regionSymbols.size() + rank, 0, remapExprs); auto indexRemap = AffineMap::get(regionSymbols.size() + rank, 0, remapExprs,
b.getContext());
// Record the begin since it may be invalidated by memref replacement. // Record the begin since it may be invalidated by memref replacement.
Block::iterator prevOfBegin; Block::iterator prevOfBegin;

View File

@ -271,3 +271,13 @@ func @affine.apply(%N : index) {
// CHECK-NEXT: addi // CHECK-NEXT: addi
return return
} }
// -----
// CHECK-DAG: #[[MAP_0D:.*]] = affine_map<() -> ()>
// CHECK-LABEL: func @simplify_zero_dim_map
func @simplify_zero_dim_map(%in : memref<f32>) -> f32 {
%out = affine.load %in[] : memref<f32>
return %out : f32
}