Commit Graph

2512 Commits

Author SHA1 Message Date
Alex Zinenko 30e9c2fe4f ExecutionEngine: fix after upstream LLVM ORC update
LLVM r368707 updated the APIs in llvm::orc::DynamicLibrarySearchGenerator to
use unique_ptr for holding the instance of the generator.  Update our uses of
DynamicLibrarySearchGenerator in the ExecutionEngine to reflect that.

PiperOrigin-RevId: 263539855
2019-08-15 04:51:16 -07:00
River Riddle 92a7b1080e Add support for Dialect interfaces.
Dialect interfaces are virtual apis registered to a specific dialect instance. Dialect interfaces are generally useful for transformation passes, or analyses, that want to opaquely operate on operations within a given dialect. These interfaces generally involve wide coverage over the entire dialect.

A dialect interface can be defined by inheriting from the CRTP base class DialectInterfaceBase::Base. This class provides the necessary utilities for registering an interface with the dialect so that it can be looked up later. Dialects overriding an interface may register an instance via 'Dialect::addInterfaces'. This API works very similarly to the respective addOperations/addTypes/etc. This will allow for a transformation/utility to later query the interface from an opaque dialect instance via 'getInterface<T>'.

A utility class 'DialectInterfaceCollection' is also provided that will collect all of the dialects that implement a specific interface within a given module. This allows for simplifying the API of interface lookups.

PiperOrigin-RevId: 263489015
2019-08-14 20:49:07 -07:00
River Riddle a481032a33 Refactor ElementsAttr::getValue and DenseElementsAttr::getSplatValue.
All 'getValue' variants now require that the index is valid, queryable via 'isValidIndex'. 'getSplatValue' now requires that the attribute is a proper splat. This allows for querying these methods on DenseElementAttr with all possible value types; e.g. float, int, APInt, etc. This also allows for removing unnecessary conversions to Attribute that really want the underlying value.

PiperOrigin-RevId: 263437337
2019-08-14 15:03:53 -07:00
Nicolas Vasilache f32f291b37 Move remaining linalg ops to ODS - NFC
This CL moves the linalg.load/range/store ops to ODS.
Minor cleanups are performed.
Additional invalid IR tests are added for coverage.

PiperOrigin-RevId: 263432110
2019-08-14 14:40:52 -07:00
Ben Vanik ae9ec43e46 Allow the use of the $cppClass template variable in verifier code blocks.
PiperOrigin-RevId: 263378198
2019-08-14 10:30:59 -07:00
Nicolas Vasilache 4f10c9b1ce Refactor linalg.view lowering to LLVM - NFC
This CL fuses the emission of size and stride information and makes it clearer which indexings are stepped over when querying the positions. This refactor was motivated by an index calculation bug in the stride computation.

PiperOrigin-RevId: 263341610
2019-08-14 07:01:41 -07:00
Nicolas Vasilache 4286c52ae8 Move linalg.slice to ODS
PiperOrigin-RevId: 263334168
2019-08-14 06:03:12 -07:00
River Riddle a9d4015da9 Add a utility script to auto-generate CHECK commands for mlir test cases.
This script is a utility to add FileCheck patterns to an mlir file. The script will heuristically insert CHECK/CHECK-LABEL commands for each line within the file. By default this script will also try to insert string substitution blocks for all SSA value names. The script is designed to make adding checks to a test case fast, it is *not* designed to be authoritative about what constitutes a good test!

Note: Some cases may not be handled well, e.g. operands to operations with regions, but this script is only intended to be a starting point.

Example usage:
$ generate-test-checks.py foo.mlir
$ mlir-opt foo.mlir -transformation | generate-test-checks.py

module {
  func @fold_extract_element(%arg0: index) -> (f32, f16, f16, i32) {
    %cst = constant 4.500000e+00 : f32
    %cst_0 = constant -2.000000e+00 : f16
    %cst_1 = constant 0.000000e+00 : f16
    %c64_i32 = constant 64 : i32
    return %cst, %cst_0, %cst_1, %c64_i32 : f32, f16, f16, i32
  }
}

// CHECK-LABEL:   func @fold_extract_element(
// CHECK-SAME:                               [[VAL_0:%.*]]: index) -> (f32, f16, f16, i32) {
// CHECK:           [[VAL_1:%.*]] = constant 4.500000e+00 : f32
// CHECK:           [[VAL_2:%.*]] = constant -2.000000e+00 : f16
// CHECK:           [[VAL_3:%.*]] = constant 0.000000e+00 : f16
// CHECK:           [[VAL_4:%.*]] = constant 64 : i32
// CHECK:           return [[VAL_1]], [[VAL_2]], [[VAL_3]], [[VAL_4]] : f32, f16, f16, i32
// CHECK:         }

PiperOrigin-RevId: 263242983
2019-08-13 16:43:09 -07:00
jpienaar 12ff145ebf Add unreachable to avoid GCC -Wreturn-type warning
GCC warns of control reaching end of non-void function (-Wreturn-type).

Closes tensorflow/mlir#75

PiperOrigin-RevId: 263214601
2019-08-13 14:23:28 -07:00
Nicolas Vasilache b09dfcb1d7 Fix indexing issue in lowering of linalg.slice
This CL fixes the stepping through operands when emitting the view sizes of linalg.slice to LLVMIR. This is now consistent with the strides emission.

A relevant test is added.

Fix suggested by Alex Zinenko, thanks!

PiperOrigin-RevId: 263150922
2019-08-13 09:20:32 -07:00
Alex Zinenko 5f0a843144 LLVM dialect: introduce fmuladd intrinsic as operation
This operation is important to achieve decent performance in computational
kernels.  In LLVM, it is implemented as an intrinsic (through function
declaration and function call).  Thanks to MLIR's extendable set of operations,
it does not have to differentiate between built-ins and intrinsics, so fmuladd
is introduced as a general type-polymorphic operation.  Custom printing and
parsing will be added later.

PiperOrigin-RevId: 263106305
2019-08-13 03:40:51 -07:00
Alex Zinenko 88de8b2a2b GenerateCubinAccessors: use LLVM dialect constants
The GenerateCubinAccessors was generating functions that fill
dynamically-allocated memory with the binary constant of a CUBIN attached as a
stirng attribute to the GPU kernel.  This approach was taken to circumvent the
missing support for global constants in the LLVM dialect (and MLIR in general).
Global constants were recently added to the LLVM dialect.  Change the
GenerateCubinAccessors pass to emit a global constant array of characters and a
function that returns a pointer to the first character in the array.

PiperOrigin-RevId: 263092052
2019-08-13 01:39:21 -07:00
Mehdi Amini 926fb685de Express ownership transfer in PassManager API through std::unique_ptr (NFC)
Since raw pointers are always passed around for IR construct without
implying any ownership transfer, it can be error prone to have implicit
ownership transferred the same way.
For example this code can seem harmless:

  Pass *pass = ....
  pm.addPass(pass);
  pm.addPass(pass);
  pm.run(module);

PiperOrigin-RevId: 263053082
2019-08-12 19:13:12 -07:00
Jacques Pienaar 532c652d6c Add start of textmate language grammar.
Basic* grammar to start of with, this doesn't handle custom ops and doesn't
handle ops with regions. But useful enough to make reading the .mlir files
easier.

Followed the approach used for emacs & vim and placed in separate directory
under utils.

* I got a little bit carried away trying to handle attributes and tried to do some custom op printing handling, but finally abandoned it. Also first time writing a textmate grammar so I assume a lot can be improved :)

PiperOrigin-RevId: 262985490
2019-08-12 12:53:21 -07:00
Jacques Pienaar e6365f3d02 Use unreachable post switch rather than default case.
Prefer to enumerate all cases in the switch instead of using default to allow
compiler to flag missing cases. This also avoids -Wcovered-switch-default
warning.

PiperOrigin-RevId: 262935972
2019-08-12 09:02:46 -07:00
Jacques Pienaar 77ed5247bf Avoid passing in line/col for files not registered with SourceMgr.
This can result in index expression overflow in "Loc.getPointer() - ColumnNo"
in SourgeMgr.

loc could also be prefixed to the message additionally in this case.

PiperOrigin-RevId: 262935408
2019-08-12 09:00:09 -07:00
Jacques Pienaar fe2ea3003b Update typo
cond_br was accidentally typed as br_cond in a few examples.

PiperOrigin-RevId: 262929398
2019-08-12 08:33:35 -07:00
Alex Zinenko 2dd38b09c1 LLVM dialect: introduce llvm.addressof to access globals
This instruction is a local counterpart of llvm.global that takes a symbol
reference to a global and produces an SSA value containing the pointer to it.
Used in combination, these two operations allow one to use globals with other
operations expecting SSA values.  At a cost of IR indirection, we make sure the
functions don't implicitly capture the surrounding SSA values and remain
suitable for parallel processing.

PiperOrigin-RevId: 262908622
2019-08-12 06:10:54 -07:00
Nicolas Vasilache 252ada4932 Add lowering of vector dialect to LLVM dialect.
This CL is step 3/n towards building a simple, programmable and portable vector abstraction in MLIR that can go all the way down to generating assembly vector code via LLVM's opt and llc tools.

This CL adds support for converting MLIR n-D vector types to (n-1)-D arrays of 1-D LLVM vectors and a conversion VectorToLLVM that lowers the `vector.extractelement` and `vector.outerproduct` instructions to the proper mix of `llvm.vectorshuffle`, `llvm.extractelement` and `llvm.mulf`.

This has been independently verified to produce proper avx2 code.

Input:
```
func @vec_1d(%arg0: vector<4xf32>, %arg1: vector<8xf32>) -> vector<8xf32> {
  %2 = vector.outerproduct %arg0, %arg1 : vector<4xf32>, vector<8xf32>
  %3 = vector.extractelement %2[0 : i32]: vector<4x8xf32>
  return %3 : vector<8xf32>
}
```

Command:
```
mlir-opt vector-to-llvm.mlir -vector-lower-to-llvm-dialect --disable-pass-threading | mlir-opt -lower-to-cfg -lower-to-llvm | mlir-translate --mlir-to-llvmir | opt -O3 | llc -O3 -march=x86-64 -mcpu=haswell -mattr=fma,avx2
```

Output:
```
vec_1d:                                 # @vec_1d
# %bb.0:
        vbroadcastss    %xmm0, %ymm0
        vmulps  %ymm1, %ymm0, %ymm0
        retq
```
PiperOrigin-RevId: 262895929
2019-08-12 04:08:57 -07:00
River Riddle 5290e8c36d NFC: Update pattern rewrite API to pass OwningRewritePatternList by const reference.
The pattern list is not modified by any of these APIs and should thus be passed with const.

PiperOrigin-RevId: 262844002
2019-08-11 18:34:14 -07:00
Chris Lattner 40fc948e55 ODS: Round out the definitions of the common integer attributes sizes, adding
1/8/16 bit attrs.  NFC

PiperOrigin-RevId: 262843016
2019-08-11 18:17:52 -07:00
River Riddle 300a2bda34 Refactor DenseElementAttr::getValues methods to return full ranges for splats.
The current implementation only returns one element for the splat case, which often comes as a surprise; leading to subtle/confusing bugs. The new behavior will include an iterate over the full range of elements, as defined by the shaped type, by providing the splat value for each iterator index.

PiperOrigin-RevId: 262756780
2019-08-11 18:17:28 -07:00
River Riddle 1e42954032 NFC: Standardize the terminology used for parent ops/regions/etc.
There are currently several different terms used to refer to a parent IR unit in 'get' methods: getParent/getEnclosing/getContaining. This cl standardizes all of these methods to use 'getParent*'.

PiperOrigin-RevId: 262680287
2019-08-09 20:07:52 -07:00
Lei Zhang ac68637ba9 NFC: Refactoring PatternSymbolResolver into SymbolInfoMap
In declarative rewrite rules, a symbol can be bound to op arguments or
results in the source pattern, and it can be bound to op results in the
result pattern. This means given a symbol in the pattern, it can stands
for different things: op operand, op attribute, single op result,
op result pack. We need a better way to model this complexity so that
we can handle according to the specific kind a symbol corresponds to.

Created SymbolInfo class for maintaining the information regarding a
symbol. Also created a companion SymbolInfoMap class for a map of
such symbols, providing insertion and querying depending on use cases.

PiperOrigin-RevId: 262675515
2019-08-09 19:04:23 -07:00
River Riddle 41968fb475 NFC: Update usages of OwningRewritePatternList to pass by & instead of &&.
This will allow for reusing the same pattern list, which may be costly to continually reconstruct, on multiple invocations.

PiperOrigin-RevId: 262664599
2019-08-09 17:20:29 -07:00
Alex Zinenko baa1ec22f7 Translation to LLVM IR: use LogicalResult instead of bool
The translation code predates the introduction of LogicalResult and was relying
on the obsolete LLVM convention of returning false on success.  Change it to
use MLIR's LogicalResult abstraction instead. NFC.

PiperOrigin-RevId: 262589432
2019-08-09 10:45:44 -07:00
Alex Zinenko 68451df267 LLVM dialect and translation: support global strings
Unlike regular constant values, strings must be placed in some memory and
referred to through a pointer to that memory.  Until now, they were not
supported in function-local constant declarations with `llvm.constant`.
Introduce support for global strings using `llvm.global`, which would translate
them into global arrays in LLVM IR and thus make sure they have some memory
allocated for storage.

PiperOrigin-RevId: 262569316
2019-08-09 09:00:13 -07:00
Alex Zinenko b9ff2dd87e Translation to LLVM: support llvm.global
Add support for translating recently introduced llvm.global operations to
global variables in the LLVM IR proper.

PiperOrigin-RevId: 262564700
2019-08-09 08:30:42 -07:00
Nicolas Vasilache 59b473c231 External library name mangling support for linalg.
This CL introduces the ability to generate the external library name for Linalg operations.
The problem is that neither mlir or C support overloading and we want a simplified form of name mangling that is still reasonable to read.
This CL creates the name of the external call that Linalg expects from the operation name and the type of its arguments.

The interface library names are updated and use new cases are added for FillOp.

PiperOrigin-RevId: 262556833
2019-08-09 07:33:58 -07:00
Nicolas Vasilache 20f2d3b598 Allow linalg.view to change the underlying elemental type.
This CL adds the ability for linalg.view to act as a bitcast operation.
This will be used when promoting views into faster memory and casting to vector types.

In the process, linalg.view is moved to ODS.

PiperOrigin-RevId: 262556246
2019-08-09 07:29:21 -07:00
Nicolas Vasilache d2aba89f2e Add a higher-order vector.outerproduct operation in MLIR
This CL is step 2/n towards building a simple, programmable and portable vector abstraction in MLIR that can go all the way down to generating assembly vector code via LLVM's opt and llc tools.

This CL adds the vector.outerproduct operation to the MLIR vector dialect as well as the appropriate roundtrip test. Lowering to LLVM will occur in the following CL.

PiperOrigin-RevId: 262552027
2019-08-09 06:55:36 -07:00
Nicolas Vasilache 39f1b9a053 Add a higher-order vector.extractelement operation in MLIR
This CL is step 2/n towards building a simple, programmable and portable vector abstraction in MLIR that can go all the way down to generating assembly vector code via LLVM's opt and llc tools.

This CL adds the vector.extractelement operation to the MLIR vector dialect as well as the appropriate roundtrip test. Lowering to LLVM will occur in the following CL.

PiperOrigin-RevId: 262545089
2019-08-09 05:58:47 -07:00
Nicolas Vasilache 92dc127ab3 Add support for vector ops in the LLVM dialect
This CL is step 1/n towards building a simple, programmable and portable vector abstraction in MLIR that can go all the way down to generating assembly vector code via LLVM's opt and llc tools.

This CL adds the 3 instructions `llvm.extractelement`, `llvm.insertelement` and `llvm.shufflevector` as documented in the LLVM LangRef "Vector Instructions" section.

The "Experimental Vector Reduction Intrinsics" are left out for now and can be added in the future on a per-need basis.

Appropriate roundtrip and LLVM Target tests are added.

PiperOrigin-RevId: 262542095
2019-08-09 05:25:31 -07:00
Alex Zinenko 6d8611b38f LLVM Dialect: introduce llvm.global
Introduce an operation that defines global constants and variables in the LLVM
dialect, to reflect the corresponding LLVM IR capability. This operation is
expected to live in the top-level module and behaves similarly to
llvm.constant.  It currently does not model many of the attributes supported by
the LLVM IR for global values (memory space, alignment, thread-local, linkage)
and will be extended as the relevant use cases appear.

PiperOrigin-RevId: 262539445
2019-08-09 05:01:52 -07:00
Nagy Mostafa 48fdc8d7a3 Add support for floating-point comparison 'fcmp' to the LLVM dialect.
This adds support for fcmp to the LLVM dialect and adds any necessary lowerings, as well as support for EDSCs.

Closes tensorflow/mlir#69

PiperOrigin-RevId: 262475255
2019-08-08 18:29:48 -07:00
Diego Caballero 96371d25c3 Enable TTI for host TargetMachine in JitRunner
This commit improves JitRunner so that it creates a target machine
for the current CPU host which is used to properly initialize LLVM's
TargetTransformInfo for such a target. This will enable optimizations
such as vectorization in LLVM when using JitRunner. Please, note that,
as part of this work, JITTargetMachineBuilder::detectHost() has been
extended to include the host CPU name and sub-target features as part of
the host CPU detection (https://reviews.llvm.org/D65760).

Closes tensorflow/mlir#71

PiperOrigin-RevId: 262452525
2019-08-08 16:03:23 -07:00
Mahesh Ravishankar f525a497ea Build SymbolTable upfront in ModuleOp verification.
Building the symbol table upfront from module op allows for O(1)
lookup of the function while verifying duplicate EntryPointOp within
the module.

PiperOrigin-RevId: 262435697
2019-08-08 14:40:46 -07:00
Mahesh Ravishankar b448266a09 Add SymbolTable trait to spirv::ModuleOp.
Adding the SymbolTable trait allows looking up the name of the
functions using the symbol table while verifying EntryPointOps instead
of manually tracking the function names.

PiperOrigin-RevId: 262431220
2019-08-08 14:20:05 -07:00
Alex Zinenko 466b9f100c Lexer: NFC: sort helper methods alphabetically
Lexer methods were added progressively as implementation advanced. The rest of
MLIR now tends to sort methods alphabetically for better discoverability in
absence of tooling.  Sort the lexer methods as well.

PiperOrigin-RevId: 262406992
2019-08-08 12:18:36 -07:00
Alex Zinenko 44d8637af1 FunctionSupport: wrap around bool to have a more semantic callback type
This changes the type of the function type-building callback from
(ArrayRef<Type>, ArrayRef<Type>, bool, string &) to (ArrayRef<Type>,
ArrayRef<Type>, VariadicFlag, String &) to make the intended use clear from the
callback signature alone.

Also rearrange type definitions in Parser.cpp to make them more sorted
alphabetically.

PiperOrigin-RevId: 262405851
2019-08-08 12:11:54 -07:00
Alex Zinenko 0126dcf1f0 Introduce support for variadic function signatures for the LLVM dialect
LLVM function type has first-class support for variadic functions.  In the
current lowering pipeline, it is emulated using an attribute on functions of
standard function type.  In LLVMFuncOp that has LLVM function type, this can be
modeled directly.  Introduce parsing support for variadic arguments to the
function and use it to support variadic function declarations in LLVMFuncOp.
Function definitions are currently not supported as that would require modeling
va_start/va_end LLVM intrinsics in the dialect and we don't yet have a
consistent story for LLVM intrinsics.

PiperOrigin-RevId: 262372651
2019-08-08 09:42:16 -07:00
Kan Chen 4d6b549339 Command toyc should be toyc-ch2 in this chapter
Closes tensorflow/mlir#70

PiperOrigin-RevId: 262370485
2019-08-08 09:30:35 -07:00
Alex Zinenko 70ca59ac50 Parser: treat implicit top-level module as an SSA name scope
Now that modules are also operations, nothing prevents one from defining SSA
values in the module.  Doing so in an implicit top-level module, i.e. outside
of a `module` operation, was leading to a crash because the implicit module was
not associated with an SSA name scope.  Create a name scope before parsing the
top-level module to fix this.

PiperOrigin-RevId: 262366891
2019-08-08 09:14:46 -07:00
Nicolas Vasilache b0ea33a7c6 Add canonicalization pattern for linalg.dim
This CL introduces canonicalization patterns for linalg.dim.
This allows the dimenions of chains of view, slice and subview operations to simplify.
Down the line, when mixed with cse, this also allows better composition of linalg tiling and fusion by tracking operations that give the same result (not in this CL).

PiperOrigin-RevId: 262365865
2019-08-08 09:09:58 -07:00
Eric Schweitz 881b238d7e Add the LLVM IR unreachable instruction to the LLVMIR dialect.
http://llvm.org/docs/LangRef.html#unreachable-instruction

Closes tensorflow/mlir#64

PiperOrigin-RevId: 262301557
2019-08-08 01:06:00 -07:00
River Riddle f56494f537 NFC: Update FuncOp::addEntryBlock to return the newly inserted block.
The entry block is often used recently after insertion. This removes the need to perform an additional lookup in such cases.

PiperOrigin-RevId: 262265671
2019-08-07 19:24:01 -07:00
Lei Zhang ba35dca4fb Initialize local variables for opcode to fix MSAN failures
PiperOrigin-RevId: 262225919
2019-08-07 15:19:30 -07:00
River Riddle 8089f93746 Add utility 'replaceAllUsesWith' methods to Operation.
These methods will allow replacing the uses of results with an existing operation, with the same number of results, or a range of values. This removes a number of hand-rolled result replacement loops and simplifies replacement for operations with multiple results.

PiperOrigin-RevId: 262206600
2019-08-07 13:48:52 -07:00
Chris Lattner a477fbaf40 Improve support for opaque types in MLIR, allowing dialects to opt into
supporting opaque types, and providing ODS support for matching them.

PiperOrigin-RevId: 262183028
2019-08-07 11:50:26 -07:00
Diego Caballero c6a006d4c7 Fix verification of zero-dim memref in affine.load/affine.store/std.load/std.store
Verification complained when using zero-dimensional memrefs in
affine.load, affine.store, std.load and std.store. This PR extends
verification so that those memrefs can be used.

Closes tensorflow/mlir#58

COPYBARA_INTEGRATE_REVIEW=https://github.com/tensorflow/mlir/pull/58 from dcaballe:dcaballe/zero-dim 49bcdcd45c52c48beca776431328e5ce551dfa9e
PiperOrigin-RevId: 262164916
2019-08-07 10:31:49 -07:00