Commit Graph

1929 Commits

Author SHA1 Message Date
Stephan Herhut cf74e41277 Verification for gpu.launch_func should rely on function type and not on the
actual body of a kernel function. This enables using gpu.launch_func with
external kernel declarations.

PiperOrigin-RevId: 253639770
2019-06-19 23:04:29 -07:00
Lei Zhang 31e2a6efe5 Also consider attributes when getting context for Operation
This CL also updates to use containing region as a fallback way to find
context since functions will eventually become ops with regions.

PiperOrigin-RevId: 253627322
2019-06-19 23:04:19 -07:00
Lei Zhang 2091ed5e30 [spirv] Add doc regarding the SPIR-V dialect
* Add basic design philosophy
* Add type syntax to both the doc and type parser comments

PiperOrigin-RevId: 253625782
2019-06-19 23:04:10 -07:00
Alex Zinenko f218519cc2 Introduce std.index_cast and its lowering+translation to LLVM
Index types integers of platform-specific bit width.  They are used to index
memrefs and as loop induction variables, however they could not be obtained
from an integer until now, making it virtually impossible to express indirect
accesses (given that memrefs of indices are not allowed) or data-dependent
loops.  Introduce `std.index_cast` to transform indices into integers and vice
versa.  The semantics of this cast is to sign-extend when casting to a wider
integer, and to truncate when casting to a narrower integer.  It belongs to
StandardOps because both types it operates on are standard types, and because
its results are likely to be used in std.load and std.store.

Introduce llvm.sext, llvm.zext and llvm.trunc operations to the LLVM dialect.
Provide the conversion of `std.index_cast` to llvm.sext or llvm.trunc,
depending on the actual bitwidth of `index` known during the conversion.

PiperOrigin-RevId: 253624100
2019-06-19 23:04:01 -07:00
Alex Zinenko 4291ae7431 Factor Region::getUsedValuesDefinedAbove into Transforms/RegionUtils
Arguably, this function is only useful for transformations and should not
pollute the main IR.  Also make sure it accepts a the resulting container
by-reference instead of returning it.

PiperOrigin-RevId: 253622981
2019-06-19 23:03:51 -07:00
Andy Davis 898cf0e968 LoopFusion: adds support for computing forward computation slices, which will enable fusion of consumer loop nests into their producers in subsequent CLs.
PiperOrigin-RevId: 253601994
2019-06-19 23:03:42 -07:00
Stephan Herhut a14eeacf2c Add lowering pass from GPU dialect operations to LLVM/NVVM intrinsics.
PiperOrigin-RevId: 253551452
2019-06-19 23:03:30 -07:00
Chris Lattner 05cf32162e Move the Region type out to its own .h/.cpp file instead of putting it into
Block.h/cpp.

This doesn't change much but makes it easier to find.

PiperOrigin-RevId: 253423041
2019-06-19 23:03:20 -07:00
River Riddle 06eb2fc06a Use braces when building an 'llvm_return' via EDSC intrinsics. This fixes a build error on MSVC where it is unable to properly handled template type aliases.
PiperOrigin-RevId: 253339204
2019-06-19 23:03:10 -07:00
Jing Pu 580a77fef5 Remove dead code.
PiperOrigin-RevId: 253314416
2019-06-19 23:03:00 -07:00
Alex Zinenko ebea5767fb Start moving conversions to {lib,include/mlir}/Conversion
Conversions from dialect A to dialect B depend on both A and B.  Therefore, it
is reasonable for them to live in a separate library that depends on both
DialectA and DialectB library, and does not forces dependees of DialectA or
DialectB to also link in the conversion.  Create the directory layout for the
conversions and move the Standard to LLVM dialect conversion as the first
example.

PiperOrigin-RevId: 253312252
2019-06-19 23:02:50 -07:00
River Riddle 3682936982 Disallow using NOperands/NResults when N < 2. We have special traits for the case of 0/1 that we explicitly check for throughout the codebase. This also fixes weird build failures in MSVC where it doesn't properly handle template type aliases.
PiperOrigin-RevId: 253269936
2019-06-19 23:02:40 -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
River Riddle 5624bc289e Add an overloaded 'get' method to DenseElementsAttr that accepts an initializer_list.
PiperOrigin-RevId: 253234385
2019-06-19 23:02:21 -07:00
Alex Zinenko b582338f62 Explicitly construct ArrayRef in AttributeTest.cpp
Some compilers find initializer list constructors from boolean literals
ambiguous between ArrayRef<bool> and ArrayRef<Attribute>.  Call the
ArrayRef<bool> constructor explicitly to disambiguate.

PiperOrigin-RevId: 253224859
2019-06-19 23:02:12 -07:00
Alex Zinenko ee6f84aebd Convert a nest affine loops to a GPU kernel
This converts entire loops into threads/blocks.  No check on the size of the
block or grid, or on the validity of parallelization is performed, it is under
the responsibility of the caller to strip-mine the loops and to perform the
dependence analysis before calling the conversion.

PiperOrigin-RevId: 253189268
2019-06-19 23:02:02 -07:00
River Riddle 6a0555a875 Refactor SplatElementsAttr to inherit from DenseElementsAttr as opposed to being a separate Attribute type. DenseElementsAttr provides a better internal representation for splat values as well as better API for accessing elements.
PiperOrigin-RevId: 253138287
2019-06-19 23:01:52 -07:00
River Riddle 2c9269124a NFC: Fix a narrowing conversion from size_t to int64_t when constructing a VectorType.
PiperOrigin-RevId: 253125435
2019-06-19 23:01:42 -07:00
River Riddle a0037a0473 NFC: Fix a warning for casting away const qualifiers.
PiperOrigin-RevId: 253124057
2019-06-19 23:01:31 -07:00
Geoffrey Martin-Noble d156b83060 Add ability to verify type matching between operands/results
This extends and generalizes the functionality for checking that element types match

PiperOrigin-RevId: 253110512
2019-06-19 23:01:21 -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
River Riddle 13461f5c33 Add several utility 'getValues<T>' functions to DenseElementsAttr that return ranges as opposed to filling a SmallVector. This is much more efficient for the general case and allows for avoiding constructing APInt/APFloat/Attribute when possible.
PiperOrigin-RevId: 253092550
2019-06-19 23:01:03 -07:00
Alex Zinenko 5de1152e2a Disallow non-index operands and results in affine.apply
`affine.apply` is supposed to operate on values of index types in context of
affine loops.  It is possible to programmatically constuct an `affine.apply`
that takes values of other types as operands or returns them, but it would not
be parseable.  Disallow such cases in the verifier.

PiperOrigin-RevId: 253021704
2019-06-19 23:00:53 -07:00
Alex Zinenko a566dd9270 GPU Dialect: introduce gpu.return
This terminator operation should appear at the end of the blocks in the body
region of `gpu.launch` when the control flow needs to be returned from the
kernel.  Using `std.return` in this place is ambiguous: it may exit the body
region or the enclosing function.  Furthermore, this allows the GPU dialect to
impose the absence of return values as required by the underlying kernel
execution models.

Update outlining transformation from `gpu.launch` to `gpu.launch_func` so that
it replaces `gpu.return` with `std.return`.

PiperOrigin-RevId: 252985992
2019-06-19 23:00:43 -07:00
Lei Zhang d13f670311 [spirv] Add SPV_StorageClassAttr and PointerType
Pointer types need to specify the storage class. We use the utility functions
generated from SPV_StorageClassAttr to parse and print the storage classes.

Also improved the case that no element type is provided for (runtime) array.

PiperOrigin-RevId: 252935599
2019-06-19 23:00:33 -07:00
River Riddle b5a34406a8 Update the Parser to support parsing/printing DenseElementAttrs with a splat value. The syntax for this is the same as 0-D tensors:
dense<tensor<100x100x100xi32>, 10>
  dense<tensor<1x1x1xi64>, -5>

PiperOrigin-RevId: 252907880
2019-06-19 23:00:24 -07:00
Lei Zhang 8c6f188143 [spirv] Use mlir::parseType in type parsers and add more checks
PiperOrigin-RevId: 252874386
2019-06-19 23:00:14 -07:00
Lei Zhang a3e6f102ca [ODG] Fix value indices in verification error messages
we should use the dynamic index for the specific value instead
of the static one for ODS-declared values.

PiperOrigin-RevId: 252873052
2019-06-19 23:00:04 -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
Alex Zinenko 867867a44d Fix static assertion in AttributeDetail.h
llvm::maskTrailingOnes<char> runs into a static assertion on the type not being
unsigned.  Use `unsigned char` instead of `char`.
PiperOrigin-RevId: 252827214
2019-06-19 22:59:45 -07:00
Nicolas Vasilache d43b8923e0 Update 2 instances of isa<BlockArgument>
PiperOrigin-RevId: 252739405
2019-06-19 22:59:35 -07:00
Nicolas Vasilache 705b2b5ea4 Fix OSS build
Missing a spot with std::make_pair causes a compiler error in OSS.
Also fixes the warning:
```
warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
            it->getSecond()->getType().isa<BufferType>() &&
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
                "Buffer or block argument expected");
```
PiperOrigin-RevId: 252738323
2019-06-19 22:59:25 -07:00
River Riddle d8cd96bc8b Refactor DenseElementsAttr to support auto-splatting the dense data on construction. This essentially means that we always auto-detect splat data and only store the minimum amount of data necessary. Support for parsing dense splats, and removing SplatElementsAttr(now that it is redundant) will come in followup cls
PiperOrigin-RevId: 252720561
2019-06-19 22:59:15 -07:00
River Riddle 5da741f671 Add basic cost modeling to the dialect conversion infrastructure. This initial cost model favors specific patterns based upon two criteria:
1) Lowest minimum pattern stack depth when legalizing.
  - This leads the system to favor patterns that have lower legalization stacks, i.e. represent a more direct mapping to the target.

2)  Pattern benefit.
  - When considering multiple patterns with the same legalization depth, this favors patterns with a larger specified benefit.

PiperOrigin-RevId: 252713470
2019-06-19 22:59:06 -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 a8a4d35d3f Add a lowering for Linalg matmul to LLVM
This CL adds a lowering to LLVM for MamulOp and a corresponding integration test.

View descriptor manipulation is moved from MLIR's LLVM dialect to C++ code compiled on the side. To this end a separation is introduced between `cblas.cpp` and `cblas_interface.cpp`, the latter operating on view types whose ABI correspond to the LLVM signature generated by MLIR.

An intermediary step is introduced that allocates a new descriptor on the MLIR side for the purpose of passing it to LLVM. The reason for this extra step is that the ABI for by-value ViewType objects wants aligned descriptors, e.g.:
```
extern "C" void linalg_dot_impl(ViewType<float, 1> X, ViewType<float, 1> Y,
                                BaseViewType<float> Z) {
   ...
}
```
produces LLVM IR with the signature:
```
%struct.ViewType = type { %struct.BaseViewType, [1 x i64], [1 x i64] }
%struct.BaseViewType = type { float*, i64 }

define void @linalg_dot_impl(%struct.ViewType* byval align 8, %struct.ViewType* byval align 8, float*, i64) tensorflow/mlir#0 {
...
}
```

We don't seem to be able to make such aligned  allocations in the MLIR -> LLVM converter atm.
Going through a level of indirection allows the test to pass.
The temporary tradeoff is that the MLIR shims have to be written by hand.
They will disappear in the future.

PiperOrigin-RevId: 252670672
2019-06-19 22:58:46 -07:00
Jacques Pienaar f3ececd6b3 Simplify trait naming for verifying argument/result constraints.
Improve the naming to something more intuitive.

PiperOrigin-RevId: 252662347
2019-06-19 22:58:36 -07:00
Mahesh Ravishankar d3a601ce33 [spirv] Add a skeleton to translate standard ops into SPIR-V dialect
PiperOrigin-RevId: 252651994
2019-06-19 22:58:26 -07:00
River Riddle 420c1f383a Add a utility to OpAsmPrinter for printing an optional trailing arrow type list. This is useful for any operation that wants to print a set of types in the same format as a FunctionType/Operation signature.
PiperOrigin-RevId: 252647152
2019-06-19 22:58:16 -07:00
River Riddle eb28b30940 NFC: Cleanup the naming scheme for registering legalization actions to be consistent, and move a file functions to the source file.
PiperOrigin-RevId: 252639629
2019-06-11 10:14:35 -07:00
Alex Zinenko 8ad35b90ec Use DialectConversion to lower the Affine dialect to the Standard dialect
This introduces the support for region-containing operations to the dialect
conversion framework in order to support the conversion of affine control-flow
operations into the standard control flow with branches.  Regions that belong
to an operation are converted before the operation itself.  The
DialectConversionPattern can therefore access the converted regions of the
original operation and process them further if necessary.  In particular, the
conversion is allowed to move the blocks from the original region to other
regions and to split blocks into multiple blocks.  All block manipulations must
be performed through the PatternRewriter to ensure they will be undone if the
conversion fails.

Port the pass converting from the affine dialect (loops and ifs with bodies as
regions) to the standard dialect (branch-based cfg) to use DialectConversion in
order to exercise this new functionality.  The modification to the lowering
functions are minor and are focused on using the PatterRewriter instead of
directly modifying the IR.

PiperOrigin-RevId: 252625169
2019-06-11 10:14:27 -07:00
Lei Zhang c680d7d063 [spirv] Include SPIRVStructureOps.td in SPIRVOps.td
This allows us to have SPIRVOps.td as the single entry point for
all SPIR-V ops, which simplifies downstream users and build rules.

PiperOrigin-RevId: 252609258
2019-06-11 10:14:19 -07:00
Lei Zhang 6553b90c82 [ODG] Add support for private methods in class writers
PiperOrigin-RevId: 252602093
2019-06-11 10:14:11 -07:00
Lei Zhang fd6542c12b [spirv] Add missing CMake rules for enum utility generation
PiperOrigin-RevId: 252601308
2019-06-11 10:14:02 -07:00
Jacques Pienaar 765734c6ab Add bool constant attributes.
PiperOrigin-RevId: 252551030
2019-06-11 10:13:54 -07:00
Mehdi Amini dfd6b349ea Fix MSVC 2019 missing <string> include (NFC)
Fix tensorflow/mlir#31.

PiperOrigin-RevId: 252547010
2019-06-11 10:13:46 -07:00
River Riddle d4491084ae Change a call to FloatAttr::getChecked to FloatAttr::get inside of 'parseFloatAttr'. The invariants of FloatAttr are already checked before construction. This also removes an unnecessary materialization of a mlir::Location which becomes expensive when parsing dense element literals.
PiperOrigin-RevId: 252545776
2019-06-11 10:13:37 -07:00
River Riddle 65c94470ed Add a general Operation::verify that verifies an operation instance and the dominance of operations in any nested regions.
PiperOrigin-RevId: 252529850
2019-06-11 10:13:28 -07:00
Lei Zhang eb3ed07cd1 [spirv] Add values for enum cases and generate the enum utilities
PiperOrigin-RevId: 252494957
2019-06-11 10:13:20 -07:00
Lei Zhang 5392d3badf [spirv] NFC: use two spaces for indentation in gen_spirv_dialect.py
PiperOrigin-RevId: 252469663
2019-06-11 10:13:11 -07:00