[PATCH] D72168: [mlir][Linalg] Add a linalg.reshape op

Nicolas Vasilache via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 3 09:28:01 PST 2020


nicolasvasilache created this revision.
nicolasvasilache added reviewers: AlexEichenberger, ftynse, rriddle, asaadaldien, yangjunpro.
Herald added subscribers: llvm-commits, lucyrfox, mgester, arpith-jacob, antiagainst, shauheen, burmako, jpienaar, mehdi_amini.
Herald added a project: LLVM.

This diff adds a new operation to linalg to allow reshaping of an
existing view into a new view in the same buffer at the same offset.

More specifically:
The `linalg.reshape` op produces a new view whose sizes are a reassociation
of the original `view`. Depending on whether or not the reassociated
MemRefType is contiguous, the resulting memref may require explicit alloc
and copies.

A reassociation is defined as a continous grouping of dimensions and is
represented with a affine map array attribute. In the future, non-continous
groupings may be allowed (i.e. permutations, reindexings etc).

For now, it is assumed that either:

1. a reassociation produces and consumes contiguous MemRefType or,
2. the reshape op will be folded into its consumers (by changing the shape of the computations).

All other cases are undefined behavior and a reshape op may not lower to
LLVM if it cannot be proven statically that it does not require alloc+copy.

A reshape may either collapse or expand dimensions, depending on the
relationship between source and target memref ranks. The verification rule
is that the reassociation maps are applied to the memref with the larger
rank to obtain the memref with the smaller rank. In the case of a dimension
expansion, the reassociation maps can be interpreted as inverse maps.

Examples:

  mlir
     // Dimension collapse (i, j) -> i' and k -> k'
     %1 = linalg.reshape %0 [(i, j, k) -> (i, j), 
                             (i, j, k) -> (k)] :
       memref<?x?x?xf32, stride_spec> into memref<?x?xf32, stride_spec_2>



  mlir
     // Dimension expansion i -> (i', j') and (k) -> (k')
     %1 = linalg.reshape %0 [(i, j, k) -> (i, j), 
                             (i, j, k) -> (k)] :
       memref<?x?xf32, stride_spec> into memref<?x?x?xf32, stride_spec_2>

The relevant invalid and roundtripping tests are added.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D72168

Files:
  mlir/include/mlir/Dialect/Linalg/EDSC/Intrinsics.h
  mlir/include/mlir/Dialect/Linalg/IR/LinalgOps.td
  mlir/include/mlir/IR/AffineExpr.h
  mlir/include/mlir/IR/Attributes.h
  mlir/include/mlir/IR/Builders.h
  mlir/include/mlir/IR/StandardTypes.h
  mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
  mlir/lib/IR/Builders.cpp
  mlir/lib/IR/StandardTypes.cpp
  mlir/test/Dialect/Linalg/invalid.mlir
  mlir/test/Dialect/Linalg/roundtrip.mlir

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72168.236081.patch
Type: text/x-patch
Size: 30011 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200103/163e7fe2/attachment.bin>


More information about the llvm-commits mailing list