Commit Graph

39 Commits

Author SHA1 Message Date
Nicolas Vasilache 4bbfb749bb Propagate linalg op attributes in transformations
Also fix the tile_conv test.

PiperOrigin-RevId: 257602321
2019-07-12 08:43:22 -07:00
Nicolas Vasilache cb3f0d6f9b Fix BufferSizeOp type lowering to LLVM.
This fixes a bug where the result type was incorrect when lowering to LLVM.

PiperOrigin-RevId: 257449384
2019-07-12 08:42:49 -07:00
River Riddle 89bc449cee Standardize the value numbering in the AsmPrinter.
Change the AsmPrinter to number values breadth-first so that values in adjacent regions can have the same name. This allows for ModuleOp to contain operations that produce results. This also standardizes the special name of region entry arguments to "arg[0-9+]" now that Functions are also operations.

PiperOrigin-RevId: 257225069
2019-07-09 10:41:00 -07:00
Nicolas Vasilache 28fb743798 More general subview calculation in tiling
This CL refactors tiling to enable tiling of views that are not just specified by a simple permutation. This allows the tiling of convolutions for which a new example is added.

PiperOrigin-RevId: 256346028
2019-07-03 14:36:42 -07:00
Nicolas Vasilache 516188bf1c Add support for promoting Linalg views into new buffers.
This CL uses the generic CopyOp to promote a subview (constructed during tiling) into a new buffer + copy by:
  1. Creating a new buffer for the subview.
  2. Taking a view into the buffer and copying into it.
  3. Adapting the linalg op to operating on the view from point 2.
Tiling is extended with a boolean flag to enable promoting views (all or nothing for now).

More specifically, the current implementation creates a buffer that is always of the full size of the ranges of the subview. This produces a buffer whose size may be bigger
than the actual size of the `subView` at the boundaries and is related to the full/partial tile problem.

In practice, we introduce a `buffer`, a `fullLocalView` and a `partialLocalView` such that:
  * `buffer` is always the size of the subview in the full tile case.
  * `fullLocalView` is a dense contiguous view into that buffer.
  * `partialLocalView` is a dense non-contiguous slice of `fullLocalView`
     that corresponds to the size of `subView` and accounting for boundary
     effects.
The point of the full tile buffer is that constant static tile sizes are
folded and result in a buffer type with statically known size and alignment
properties.

Padding is introduced on the boundary tiles with a `fill` op followed by a partial `copy` op.

These behaviors will be refined later, on a per-need basis.

PiperOrigin-RevId: 256237319
2019-07-02 16:43:36 -07:00
Mahesh Ravishankar 25094e90bd Resolving buffer operand of linalg.view doesnt have the information
about the buffer size. This is needed to resolve the operand
correctly. Add that information to view op
serialization/deserialization

Also modify the parsing of buffer type by splitting at 'x' to
side-step issues with StringRef number parsing.

PiperOrigin-RevId: 256188319
2019-07-02 10:28:59 -07:00
Nicolas Vasilache 6a7a1ca25d Move BufferAllocOp and BufferDeallocOp to ODS
This CL also fixes a parsing issue in the BufferType, adds LLVM lowering support for handling the static constant buffer size and a roundtrip test.

PiperOrigin-RevId: 255834356
2019-07-01 09:54:27 -07:00
Mahesh Ravishankar 266841751f Add buffer size information to Linalg::BufferType. If the size is
constant then it is represented as <size x elementType>. If the size
is not a compile time constant, then it is represented as
<? x elementType>.

PiperOrigin-RevId: 255619400
2019-06-28 10:10:17 -07:00
River Riddle 679a3b4191 Change the attribute dictionary syntax to separate name and value with '='.
The current syntax separates the name and value with ':', but ':' is already overloaded by several other things(e.g. trailing types). This makes the syntax difficult to parse in some situtations:

Old:
  "foo: 10 : i32"

New:
  "foo = 10 : i32"
PiperOrigin-RevId: 255097928
2019-06-25 19:06:34 -07:00
Nicolas Vasilache 7e7ed9104a Cleanup test following bad merge
PiperOrigin-RevId: 254770395
2019-06-24 13:46:22 -07:00
Nicolas Vasilache 46b755d405 Use linalg.view_slice in tiling and fusion
This CL makes use of view_slice in tiling and fusion.
Using a higher level IR element greatly simplifies the IR produced during tiling and fusion.
Lowering to LLVM is updated to first translate view_slice into a sequence of dim, range and cmpi.
This level will also be useful when lowering to affine.

PiperOrigin-RevId: 254767814
2019-06-24 13:46:08 -07:00
Nicolas Vasilache 2ff1c01063 Add higher-level linalg.view_slice operation.
This will be useful to simplify the IR emitted during transformations as well as lowering to affine.

PiperOrigin-RevId: 254757641
2019-06-24 13:45:24 -07:00
Nicolas Vasilache 4a1df48f44 Add a Linalg convolution op.
This CL adds a conv op that corresponds to the TF description along with its lowering to loops (https://www.tensorflow.org/api_docs/python/tf/nn/convolution).

The dimension of the convolution is inferred from the rank of the views. The other logical
dimensions correspond to the TF description.

The computation of tiled views need to be updated to work for the input tensor. This is left for a future CL.

PiperOrigin-RevId: 254505644
2019-06-22 09:18:06 -07:00
Nicolas Vasilache 235e2fe030 Support for 0-D case in Linalg ops
This CL adds support for O-D ops in Linalg ops by:
1. making the CopyOp maps optional instead of default valued
2. allowing certain map operations to accept and return empty maps
3. making linalg::LowerToLoops aware of these changes
4. providing a proper 0-D impl for CopyOp and FillOp
5. adding the relevant tests

PiperOrigin-RevId: 254381908
2019-06-22 09:15:36 -07:00
Geoffrey Martin-Noble fd99b6ce97 Remove unnecessary -verify-diagnostics
These were likely added in error because of confusion about the flag when it was just called "-verify". The extra flag doesn't cause much harm, but it does make mlir-opt do more work and clutter the RUN line

PiperOrigin-RevId: 254037016
2019-06-19 23:08:13 -07:00
Geoffrey Martin-Noble d7d69569e7 Rename -verify mlir-opt flag to -verify-expected-diagnostics
This name has caused some confusion because it suggests that it's running op verification (and that this verification isn't getting run by default).

PiperOrigin-RevId: 254035268
2019-06-19 23:08:03 -07:00
Nicolas Vasilache e7e03cee1f Add Linalg CopyOp
This CL adds a generic CopyOp to Linalg and its lowering to loops.
The CopyOp supports input and output permutation maps.
When combined with tiling and allocating a new local buffer, this should provide basic support for implementing simple memory transfers with coalescing.

At the moment, lowering copies to a library call is not supported.

PiperOrigin-RevId: 253250497
2019-06-19 23:02:31 -07:00
Mahesh Ravishankar 54b35cec08 Add a definition of the library function to use when Linalg ops are
lowered to LLVM, instead of expecting one to exist in the Module

PiperOrigin-RevId: 253097382
2019-06-19 23:01:12 -07:00
Nicolas Vasilache de32c03ebe Add Linalg FillOp
This CL adds a generic FillOp to Linalg and its lowering to loops.
This is achieved by avoiding to specify the static NLoopTypes and ViewRanks type traits but instead defines the relevant methods as `extraClassDeclaration`.
The relevant AffineMap and scalar emission code are added, with relevant tests.

This gives us a first rank-agnostic Linalg op with its generic lowering to loops that should compose with view-based tiling and fusion.

PiperOrigin-RevId: 252869205
2019-06-19 22:59:54 -07:00
Nicolas Vasilache bab53a9484 Add a Linalg fusion pass.
This CL adds a fusion pass for the Linalg dialect.
Fusion is backed by a simple analysis on SSA values and proceeds as follows:
1. A dependence and alias analyses are performed on views.
2. A Linalg op is tiled by a particular tile size. This creates a new Linalg op operating on tiled loops and tiled views.
3. The dependence analysis is used to obtain ops that produce views that are consumed by the original Linalg op.
4. Dependence analysis is used to determine whether op-level fusion would violate any dependence.
5. If fusion is safe, matching tiled views are sliced for the producing op.
6. A tiled clone of the producer op is written before the tiled consumer op.

If a producer is fused, its entire output view has been computed in tiled form.
The original producer op is then erased.

PiperOrigin-RevId: 252695194
2019-06-19 22:58:56 -07:00
Nicolas Vasilache 3148d60e60 Expose a minimal type parser to dialects.
This CL exposes a parseType method which allows standalone reuse of the MLIR type parsing mechanism. This is a free function for now because the underlying MLIR parser is not guaranteed to receive a StringRef which lives in the proper MemBuffer. This requires building a new MemBuffer/SourceMgr and modifying the Parser constructor to not require an mlir::Module.

The error diagnostic emitted by parseType has context limited to the local string.
For now the dialect has the additional option to emit its own extra error that has the FileLineColLoc context.

In the future, both error messages should be combined into a single error.

PiperOrigin-RevId: 252468911
2019-06-11 10:13:02 -07:00
Nicolas Vasilache 0eac031fac Add lowering linalg.for to LLVM IR
This CL adds lowering of linalg.for to LLVM IR and adds an IR test.
    This also replaces the usage of affine.for with linalg.for and enables the LLVM IR path in the integration test.

--

PiperOrigin-RevId: 250503798
2019-06-01 20:06:40 -07:00
Nicolas Vasilache 3ad0fa95d1 Add a linalg.for operation to support non-affine loop constructs
The affine.for operation has restrictions that make it suitable for dependence analysis. The Linalg dialect aims at being more general.
    This CL introduces linalg.for, and its associated terminator, along with a simple roundtripping test.
    A `linalg.for` only takes one value of index type for lower bound, upper bound and step.

    Example usage:
    ```
    linalg.for %iv = %lb to %ub step %step {
      ... // body
    }
    ```

--

PiperOrigin-RevId: 250369722
2019-06-01 20:06:21 -07:00
Nicolas Vasilache 997c7c4a8d Fix Linalg tiling for the partial tile case.
This CL prepares for mixing lowering of tiled linalg operations to loops with load and store operations. In particular it is necessary to capture partial tile information in views. This CL makes slice ops during Linalg tiling properly stop at partial tile boundaries by implementing `min` with a `cmpi` and `select` over values of index type.

    To be consistent with lowering to loops, the implementation of tiling also drops specifics of accessing values via ranges and instead uses ranges of the form
    `[0, dim(view), 1]` for creating view slices. This simplifies the code for the implementation of tiling and utils.

    This also allows removing restrictions around needing a View or SliceOp defined in the current function context (as well as all it RangeOps). The restriction removal is tested by making the dot test operate directly on views.

    The above is still subject to folding of the linalg.dim operation left for a future CL.

    At this time, mixing tiling and lowering to loops all the way to execution is not yet functional because affine.for does not allow arbitrarily defined values of index type as its operands.

    The previously introduced linalg.range_intersection was not sufficient to capture the necessary information and still required dealing with max quantities.
    A followup CL will remove linalg.range_intersection.

--

PiperOrigin-RevId: 249698823
2019-06-01 19:58:43 -07:00
Nicolas Vasilache c0f41e5bb3 Fix Linalg lowering to loops
This CL makes lowering to loops always be a:
    ```
    %D = linalg.dim %view, constant : !linalg.view<...>
    affine.for %ix = %c0 to %D {
      ...
    }
    ```

    This form composes correctly with tiling and is also the proper way to emit loops from views that across function boundaries.
    The previous version that would extract the range_min/max/step was composing incorrectly with tiling (i.e. would shift by range_min both in the loop bounds and in the slice) and would not work across function boundaries.

    The relevant tests are updated and a new test `dot_view`---which lowers to loops from views passed as function parameters---is added.

    When additional context is available, the linalg.dim operations should be folded away but this is left for a future CL.

--

PiperOrigin-RevId: 249634712
2019-06-01 19:57:54 -07:00
Nicolas Vasilache aabb44f66d Fix Linalg/llvm_ir test
--

PiperOrigin-RevId: 249057043
2019-05-20 13:49:19 -07:00
Nicolas Vasilache 05df9d121a Fix stride computation bug when lowering linalg.view to llvm
--

PiperOrigin-RevId: 249053115
2019-05-20 13:49:10 -07:00
Nicolas Vasilache 9b58691e74 Add lowering of LinalgLibraryOps to linalg.load + linalg.store.
This CL adds a pass to lower out of dot,matvec,matmul etc and into a combination of affine.for, linalg.load and linalg.store operations.
    Such operations can then later lowered to LLVM.
    This CL essentially performs op expansion using EDSCs and factors out a few common utils from Tiling.cpp.

--

PiperOrigin-RevId: 249049518
2019-05-20 13:49:02 -07:00
Nicolas Vasilache 13dbad87f6 Add linalg.range_intersect conversion to LLVM.
This CL adds lowering for linalg.range_intersect into LLVM by computing:
      * new_min <- max (range1.min, range2.min)
      * new_max <- min (range1.max, range2.max)
      * new_step <- range1.step * range2.step

--

PiperOrigin-RevId: 248571810
2019-05-20 13:44:15 -07:00
Nicolas Vasilache a4317d1a59 Add a linalg.range_intersect op.
This CL adds an operation whose purpose is to encode boundary conditions directly in the view type. In particular, full/partial tile distinction can
    occur at the level of metadata only.
    This CL also adopts a Linalg_Op pattern that is similar to Std_Op.

--

PiperOrigin-RevId: 248529469
2019-05-20 13:44:07 -07:00
Nicolas Vasilache cf3959f49d Add a linalg.dim
A linalg.dim operation is used to extract size information from !linalg.view objects passed
    through function call boundaries.

--

PiperOrigin-RevId: 248017488
2019-05-20 13:41:02 -07:00
Nicolas Vasilache 8adc3f0ec7 Add a primitive linalg-lower-to-llvm-dialect pass
This CL builds upon ftynse@'s Linalg dialect conversion (in examples/Linalg/Linalg1) and updates it to support buffers and the fully composed form of view and slice operations.
    A new BufferSizeOp is introduced for the purpose of extracting the size information from a buffer.
    This will be useful in a followup CL for an end-to-end LLVM execution path where mlir-cpu-runner will allocate a buffer.

--

PiperOrigin-RevId: 246358593
2019-05-06 08:24:59 -07:00
Nicolas Vasilache 21d9dc4f29 [Linalg] Add a primitive tiling pass
This CL adds a primitive tiling pass for Linalg.
    The tiling pass uses the loopToOperandRangesMaps property which should be ideally Tablegen'd and in-class.

    The tiling specification uses 0 as a convention to skip loops that should not be tiled.

    Tiling proceeds in 3 steps, for each op:
    1. Pad tile sizes with 0 to match the number of loops, this simplifies the implementation and avoids affine map manipulations to align dimensions.
    2. Create loop ranges that represent the min/max/step by which to iterate. This should be later complemented by a range intersection to avoid the out-of-bounds case.
    3. Map the loop ranges to view ranges in order to create subviews on which the op can be called.

    Relevant utility and helper functions are added separately that support writing the transformation in a declarative fashion.
    Simplifying assumptions are made for now on the views and the ranges that are constructed
    in the function and are not passed as function arguments. This restriction will be lifted
    in the future.

--

PiperOrigin-RevId: 246124419
2019-05-06 08:23:43 -07:00
River Riddle 5f801366d1 Fix flaky Linalg roundtrip test. This removes an invalid "%s" from the second mlir-opt command.
--

PiperOrigin-RevId: 244953230
2019-04-23 22:03:31 -07:00
Nicolas Vasilache a2e7775441 [Linalg] Add basic linalg ops
This CL adds linalg.dot, linalg.matvec and linalg.matmul ops with the proper roundtripping test. These are the first LinalgOp that operate on views and that will lower to library calls.
    Linalg ops exhibit some common properties and behavior that are modeled with Traits.

    A LinalgOp is defined as a generic Op that operates on input and output views (passed as operands) and has the following properties:
    1. a number of input and outputs captured by the `NInputsAndOutputs` trait.
    2. a list of ranks for each operand captured by the `ViewRanks` trait.
    3. a set of parallel, reduction and windowing loops captured by `NLoopTypes` trait.

    These represent are a first set of generic properties that will enable the definition of generic linear algebra operations and the properties necessary for upcoming transformations.

--

PiperOrigin-RevId: 244912754
2019-04-23 22:03:06 -07:00
Nicolas Vasilache 0b47f74037 [Linalg] Add a slice op
This CL adds a linalg.slice op with the proper roundtripping test.
    A slice op allows taking subviews that may be rank-reducing (if some indexing is of index type) or not (if all indexings are of linalg.range type).

    A slice must be constructed directly from a base view (no chains of slices may exist in the IR). Helper functions that fold will be provided for construction if/when necessary.

    This also renames base_view to view.

--

PiperOrigin-RevId: 244406827
2019-04-23 22:01:10 -07:00
Nicolas Vasilache 1d5dc840e7 [Linalg] Add a view type with base_view op
This CL adds a linalg.view<?x?xf32> type and base_view op with the proper roundtripping test. The parser will be improved in a subsequent CL once portions of the mlir::Parser are exposed.

    For now this only supports dynamic views, static views will be introduced at a later time when they are needed.

--

PiperOrigin-RevId: 244374180
2019-04-23 22:01:02 -07:00
Nicolas Vasilache 75be1fe82b [Linalg] Add a simple buffer type with alloc/dealloc ops
This CL adds a linalg.buffer<f32> type and buffer_alloc / buffer_dealloc ops with the proper roundtripping test.

--

PiperOrigin-RevId: 244252306
2019-04-23 22:00:36 -07:00
Nicolas Vasilache 8370cc7492 Start a Linalg dialect
This CL starts implementing a Linalg dialect with the objective of supporting
    optimizing compilation of loops and library calls for a subset of common linear
    algebra operations.

    This CL starts by simply adding a linalg.range type and an operation with the
    proper roundtripping test.

--

PiperOrigin-RevId: 244189468
2019-04-18 11:50:27 -07:00