Commit Graph

1379 Commits

Author SHA1 Message Date
River Riddle 42c19e8272 Add a utility function to OperationName for extracting the dialect name.
PiperOrigin-RevId: 251333376
2019-06-03 19:27:35 -07:00
Geoffrey Martin-Noble e2032c7d4e Replace comments referring to "vector or tensor" with "shaped" where appropriate
PiperOrigin-RevId: 251306752
2019-06-03 19:27:19 -07:00
River Riddle 95eaca3e0f Refactor the dialect conversion framework to support multi-level conversions. Multi-level conversions are those that require multiple patterns to be applied before an operation is completely legalized. This essentially means that conversion patterns do not have to directly generate legal operations, and may be chained together to produce legal code.
To accomplish this, moving forward users will need to provide a legalization target that defines what operations are legal for the conversion. A target can mark an operation as legal by providing a specific legalization action. The initial actions are:
* Legal
  - This action signals that every instance of the given operation is legal,
    i.e. any combination of attributes, operands, types, etc. is valid.
* Dynamic
  - This action signals that only some instances of a given operation are legal. This
    allows for defining fine-tune constraints, like say std.add is only legal when
    operating on 32-bit integers.

An example target is shown below:
struct MyTarget : public ConversionTarget {
  MyTarget(MLIRContext &ctx) : ConversionTarget(ctx) {
    // All operations in the LLVM dialect are legal.
    addLegalDialect<LLVMDialect>();

    // std.constant op is always legal on this target.
    addLegalOp<ConstantOp>();

    // std.return op has dynamic legality constraints.
    addDynamicallyLegalOp<ReturnOp>();
  }

  /// Implement the custom legalization handler to handle
  /// std.return.
  bool isLegal(Operation *op) override {
    // Process the dynamic handling for a std.return op (and any others that were
    // marked "dynamic").
    ...
  }
};

PiperOrigin-RevId: 251289374
2019-06-03 19:27:02 -07:00
River Riddle 62cbdd51fa Start defining a new operation 'FuncOp' that replicates all of the functionality of 'Function', but with an operation. The pretty syntax for the operation is exactly the same as that of Function. This operation is currently builtin, but should hopefully be moved to a different dialect when it has been completely decoupled from IR/. This is the first patch in a large series that refactors Functions to be represented as operations.
PiperOrigin-RevId: 251281612
2019-06-03 19:26:46 -07:00
Alex Zinenko f5e736c448 SDBM: fix the order of variables in the stripe-induced inequalities
The initial implementation of SDBM mistakenly swapped the order of variables in
the inequalities induced by a stripe equality: y = x # B actually implies
y - x <= 0 and x - y <= B - 1 rather than x - y <= 0 and y - x <= B - 1 as
implemented.  Textual comments in the test files were correct but did not
correspond to the emitted IR.  Round-tripping between SDBM and expression lists
was not affected because the wrong order was used in both directions of the
conversion.  Use the correct order.

PiperOrigin-RevId: 251252980
2019-06-03 19:26:29 -07:00
River Riddle c263ae9104 Restructure the parser to support nested name scopes. This allows for regions at the same level of nesting to have values with the same SSA name. This is a necessary step for representing functions as operations.
The following is now valid IR:
  foo.op ... {
    %val = ...
  }, {
    %val = ...
  }

PiperOrigin-RevId: 251249875
2019-06-03 19:26:20 -07:00
Alex Zinenko 252de8eca0 Introduce OpOperandAdaptors and emit them from ODS
When manipulating generic operations, such as in dialect conversion /
rewriting, it is often necessary to view a list of Values as operands to an
operation without creating the operation itself.  The absence of such view
makes dialect conversion patterns, among others, to use magic numbers to obtain
specific operands from a list of rewritten values when converting an operation.
Introduce XOpOperandAdaptor classes that wrap an ArrayRef<Value *> and provide
accessor functions identical to those available in XOp.  This makes it possible
for conversions to use these adaptors to address the operands with names rather
than rely on their position in the list.  The adaptors are generated from ODS
together with the actual operation definitions.

This is another step towards making dialect conversion patterns specific for a
given operation.

Illustrate the approach on conversion patterns in the standard to LLVM dialect
conversion.

PiperOrigin-RevId: 251232899
2019-06-03 19:26:12 -07:00
River Riddle b1393c2cd0 NFC: Split up Parser::parseAttribute into multiple smaller functions to improve readability.
PiperOrigin-RevId: 251158192
2019-06-03 19:25:55 -07:00
Jacques Pienaar c914976c72 Allow constant of unit type.
--

PiperOrigin-RevId: 251053682
2019-06-01 20:14:48 -07:00
Jacques Pienaar 482d39eb98 Avoid failure due to incomplete type specification.
--

PiperOrigin-RevId: 251048081
2019-06-01 20:14:39 -07:00
Mehdi Amini 05b0527ab7 Move NamedAttributeList::get() method out-of-line (fix CMake build due to missing include)
--

PiperOrigin-RevId: 251000296
2019-06-01 20:14:19 -07:00
Geoffrey Martin-Noble 8c4ae76b27 Use size_t for tuple type size
This is more consistent with standard containers.

--

PiperOrigin-RevId: 250985851
2019-06-01 20:14:09 -07:00
Geoffrey Martin-Noble daefaded4a Consistently use int64_t for shape-related values in shaped types
We want to support 64-bit shapes (even when the compiler is on a 32-bit architecture). Using int64_t consistently allows us to sidestep the bugginess of unsigned arithmetic.

    Still unsigned: kind, memory space, and bit width. The first two are basically enums. We could have a discussion about the last one, but it's basically just a very large enum as well and we're not doing any math on it, I think.

--

PiperOrigin-RevId: 250985791
2019-06-01 20:13:58 -07:00
Geoffrey Martin-Noble c912981bbd Static cast size_t -> int64_t instead of vice versa for equals comparisons
These were just introduced by a previous CL moving MemRef getRank to return int64_t. size_t could be smaller than 64 bits and in equals comparisons, signed vs unsigned doesn't matter. In these cases, we know right now that the particular int64_t is not larger than max size_t (because it currently comes directly from a size() call), the alternative cast plus equals comparison is always safe, so we might as well do it that way and no longer require reasoning deeper into the callstack.

    We are already assuming that size() calls fit into int64_t in a number of other cases like the aforementioned getRank() (since exabytes of RAM are rare). If we want to avoid this assumption we will have to come up with a principled way to do it throughout.

--

PiperOrigin-RevId: 250980297
2019-06-01 20:13:49 -07:00
River Riddle e7b337acf8 Add support for providing an output stream to the SourceMgrDiagnosticHandlers.
--

PiperOrigin-RevId: 250974331
2019-06-01 20:13:39 -07:00
Amit Sabne 7a43da6060 Loop invariant code motion - remove reliance on getForwardSlice. Add more tests.
--

PiperOrigin-RevId: 250950703
2019-06-01 20:13:30 -07:00
River Riddle 05bb27fac2 NFC: Cleanup method definitions within Parser and add header blocks to improve readability.
--

PiperOrigin-RevId: 250949195
2019-06-01 20:13:19 -07:00
Geoffrey Martin-Noble 65eae3f75f Get rid of separate getRank() on MemRef now that it subclasses ShapedType
We are moving towards int64_t for these shape/dimension -related values to avoid buginess with unsigned arithmetic

--

PiperOrigin-RevId: 250945322
2019-06-01 20:13:00 -07:00
Geoffrey Martin-Noble ac4b0a1e7b Some cleanup of ShapedType now that MemRef subclasses it.
Extract common methods into ShapedType.
    Simplify methods.
    Remove some extraneous asserts.
    Replace sentinel value with a helper method to check the same.

--

PiperOrigin-RevId: 250945261
2019-06-01 20:12:51 -07:00
Geoffrey Martin-Noble 32de860a09 Make MemRefType subclass ShapedType
MemRefs have the same notion of shape, rank, and fixed element type. This allows us to reuse utilities based on shape for memref.

    All dyn_cast and isa calls for ShapedType have been checked and either modified to explicitly check for vector or tensor, or confirmed to not depend on the result being a vector or tensor.

    Discussion in https://groups.google.com/a/tensorflow.org/forum/#!topic/mlir/cHLoyfGu8y8

--

PiperOrigin-RevId: 250945184
2019-06-01 20:12:40 -07:00
Geoffrey Martin-Noble 8eca4cc30d Don't use ShapedType to indicate vector or tensor type
MemRefType may soon subclass ShapedType. ShapedType only guarantees that something has a shape (possibly dynamic), rank (or explicitly unranked), and fixed element type.

--

PiperOrigin-RevId: 250940537
2019-06-01 20:12:31 -07:00
River Riddle 37732b1759 Fix an msan warning for use of an uninitialized variable.
--

PiperOrigin-RevId: 250939301
2019-06-01 20:12:21 -07:00
River Riddle 94541563dc Abstract the internal storage of the NamedAttributeList into a new attribute, DictionaryAttr. This attribute maintains a sorted list of NamedAttributes. This will allow for operations/functions to maintain sub dictionaries of attributes.
The syntax is the same as top level attribute dictionaries:
       {sub_dictionary: {fn: @someFn, boolAttr: true}}

--

PiperOrigin-RevId: 250898950
2019-06-01 20:12:01 -07:00
Lei Zhang 3650df50dd [ODS] Support region names and constraints
Similar to arguments and results, now we require region definition in ops to
    be specified as a DAG expression with the 'region' operator. This way we can
    specify the constraints for each region and optionally give the region a name.

    Two kinds of region constraints are added, one allowing any region, and the
    other requires a certain number of blocks.

--

PiperOrigin-RevId: 250790211
2019-06-01 20:11:42 -07:00
Geoffrey Martin-Noble 60d6249fbd Replace checks against numDynamicDims with hasStaticShape
--

PiperOrigin-RevId: 250782165
2019-06-01 20:11:31 -07:00
Geoffrey Martin-Noble 97505013c6 Make getRank abort for unranked type
This better matches the other methods in ShapedType which only make sense for ranked types. There's now an explicit hasRank for checking the rank. Actual call sites rarely used the "-1" sentinel to combine checking for rankedness and checking that rank is a certain value. And in most cases they should actually be checking for rankedness at a higher level using type predicates. Using an explicit method is clearer than a sentinel anyway.

--

PiperOrigin-RevId: 250720853
2019-06-01 20:11:12 -07:00
Jacques Pienaar 4a697a91de Fix 5 ClangTidy - Readability findings.
* the 'empty' method should be used to check for emptiness instead of 'size'
    * using decl 'CapturableHandle' is unused
    * redundant get() call on smart pointer
    * using decl 'apply' is unused
    * using decl 'ScopeGuard' is unused

--

PiperOrigin-RevId: 250623863
2019-06-01 20:10:22 -07:00
River Riddle 9abdbb3189 NFC: Inline toString as operations can be streamed directly into raw_ostream.
--

PiperOrigin-RevId: 250619765
2019-06-01 20:10:12 -07:00
Geoffrey Martin-Noble d407a85169 Use hasRank instead of comparing rank to -1
--

PiperOrigin-RevId: 250598252
2019-06-01 20:10:03 -07:00
River Riddle e2af847a2e Move CondBranchOp to the ODG framework.
--

PiperOrigin-RevId: 250593367
2019-06-01 20:09:52 -07:00
Geoffrey Martin-Noble 1c681a7caf Exclude all ShapedType subclasses other than TensorType subclasses from having non-scalar elements.
The current logic assumes that ShapedType indicates a vector or tensor, which will not be true soon when MemRef subclasses ShapedType

--

PiperOrigin-RevId: 250586364
2019-06-01 20:09:42 -07:00
Geoffrey Martin-Noble 9ebab7bc30 Avoid dyn_cast to ShapedType
ShapedType just indicates shape/rank/element semantics. It's generally not useful for other type checking. This check already checks for vector or tensor type, so we can use a direct cast if we check those first.

    Related to making MemRefType a subclass of ShapedType

--

PiperOrigin-RevId: 250583231
2019-06-01 20:09:22 -07:00
Geoffrey Martin-Noble 66e84bf88c Make it clear that ElementsAttr is only for static shaped vectors or tensors.
This is in preparation for making MemRef a subclass of ShapedType, but also UnrankedTensor should already be excluded.

--

PiperOrigin-RevId: 250580197
2019-06-01 20:09:12 -07:00
MLIR Team 5a91b9896c Remove "size" property of affine maps.
--

PiperOrigin-RevId: 250572818
2019-06-01 20:09:02 -07:00
Andy Davis 1de0f97fff LoopFusionUtils CL 2/n: Factor out and generalize slice union computation.
*) Factors slice union computation out of LoopFusion into Analysis/Utils (where other iteration slice utilities exist).
    *) Generalizes slice union computation to take the union of slices computed on all loads/stores pairs between source and destination loop nests.
    *) Fixes a bug in FlatAffineConstraints::addSliceBounds where redundant constraints were added.
    *) Takes care of a TODO to expose FlatAffineConstraints::mergeAndAlignIds as a public method.

--

PiperOrigin-RevId: 250561529
2019-06-01 20:08:52 -07:00
Geoffrey Martin-Noble 16ebc48c9d Change elements literal parsing to not rely on shaped type being a vector or tensor.
This is in preparation for making MemRef a ShapedType. In general, a shaped type should be anything with shape, rank, and element type properties, so use sites shouldn't assume more than that.

    I also pulled the trailing comma parsing out the parseElementsLiteralType (new name) method. It seems weird to have the method parse the type + a trailing comma, even if all call sites currently need that. It's surprising behavior without looking at the implementation.

--

PiperOrigin-RevId: 250558363
2019-06-01 20:08:22 -07:00
River Riddle 1fd1c7a8bb Replace the Function reference methods from the OpAsmParser/OpAsmPrinter with usages of FunctionAttr.
--

PiperOrigin-RevId: 250555680
2019-06-01 20:08:03 -07:00
River Riddle 661f062f6b NFC: Switch std::vector to SmallVector. This fixes a compiler error on MSVC as the StringMap key dereference method is non-const.
--

PiperOrigin-RevId: 250538085
2019-06-01 20:07:31 -07:00
Lei Zhang 22e3aa7594 [spirv] Add ModuleOp
This op defines a SPIR-V module using a MLIR region. The region contains
    one block. Module-level operations, including functions definitions,
    are all placed in this block.

    This CL extracts common definitions from SPIRVOps.td into SPIRVBase.td.
    The new op is placed in SPIRVStructureOps.td.

--

PiperOrigin-RevId: 250522320
2019-06-01 20:07:10 -07:00
Rasmus Munk Larsen e5a6904914 Get rid of redundant verifier, which is implied by the type traits.
--

PiperOrigin-RevId: 250511694
2019-06-01 20:07:01 -07:00
Rasmus Munk Larsen 861c55e150 Add a rank op to MLIR. Example:
%1 = rank %0 : index

--

PiperOrigin-RevId: 250505411
2019-06-01 20:06:51 -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
Alex Zinenko 33dc956647 EDSC: use llvm::function_ref instead of std::function
Region body constructors in EDSC now take a callback to the function that fills
    in the body.  This callback is called immediately and not stored, so it is
    sufficient to pass a reference to it and avoid a potentially expensive copy.

--

PiperOrigin-RevId: 250473793
2019-06-01 20:06:30 -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
River Riddle c3b8ef2e66 Tidy up a few parser functions in the ModuleParser. This also adds a missing error for attribute aliases that contain '.' characters.
--

PiperOrigin-RevId: 250291646
2019-06-01 20:05:42 -07:00
Lei Zhang d4c8c8de42 [ODS] Support numRegions in Op definition
--

PiperOrigin-RevId: 250282024
2019-06-01 20:05:31 -07:00
Alex Zinenko c2d105811a Do not assume Blocks belong to Functions
Fix Block::splitBlock and Block::eraseFromFunction that erronously assume
    blocks belong to functions.  They now belong to regions.  When splitting, new
    blocks should be created in the same region as the existing block.  When
    erasing a block, it should be removed from the region rather than from the
    function body that transitively contains the region.

    Also rename Block::eraseFromFunction to Block::erase for consistency with other
    IR containers.

--

PiperOrigin-RevId: 250278272
2019-06-01 20:05:21 -07:00
Stephan Herhut 8d703af2f8 Fix translation of NVVM special registers to intrinsics.
The original implementation did not map the return value of the intrinsics
    call to the result value of the special register op. Uses of the result
    value hence hit a nullpointer.

--

PiperOrigin-RevId: 250255436
2019-06-01 20:05:12 -07:00
Alex Zinenko d4c071cc69 Decouple affine->standard lowering from the pass
The lowering from the Affine dialect to the Standard dialect was originally
    implemented as a standalone pass.  However, it may be used by other passes
    willing to lower away some of the affine constructs as a part of their
    operation.  Decouple the transformation functions from the pass infrastructure
    and expose the entry point for the lowering.

    Also update the lowering functions to use `LogicalResult` instead of bool for
    return values.

--

PiperOrigin-RevId: 250229198
2019-06-01 20:05:01 -07:00
River Riddle be9b20ff57 NFC: Add a missing include for std::isalnum and std::digit.
--

PiperOrigin-RevId: 250085298
2019-06-01 20:04:03 -07:00