Commit Graph

5917 Commits

Author SHA1 Message Date
Fangrui Song bf146857bb [mlir] Change deprecated -Werror-implicit-function-declaration to -Werror=implicit-function-declaration
This -Werror- form has been deprecated since 2007 (GCC r122017).

And only enable it for CMAKE_C_FLAGS. C++ does not need it.
2020-11-04 18:33:51 -08:00
Rahul Joshi 8e466f69cf [MLIR][NFC] Update syntax of global_memref in ODS description.
- The ODS description was using an old syntax that was updated during the review.
  This fixes the ODS description to match the current syntax.

Differential Revision: https://reviews.llvm.org/D90797
2020-11-04 15:58:46 -08:00
Alexandre Eichenberger 0795715616 [mlir][std] Add SignedCeilDivIOp and SignedFloorDivIOp with std to std lowering triggered by -std-expand-divs option. The new operations support positive/negative nominator/denominator numbers.
Differential Revision: https://reviews.llvm.org/D89726

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>
2020-11-04 14:16:23 -05:00
Mehdi Amini 5b29d26b21 Refactor PyPrintAccumulatorm, PyFileAccumulator, and PySinglePartStringAccumulator from IRModules.cpp to PybindUtils.h
These are reusable utilities across bindings.

Differential Revision: https://reviews.llvm.org/D90737
2020-11-04 18:46:36 +00:00
Mehdi Amini bf5c8625c4 Move MlirStringCallback declaration from mlir-c/IR.h to mlir-c/Support.h (NFC)
This is a generic utility that can be reused beyond the IR bindings.

Differential Revision: https://reviews.llvm.org/D90736
2020-11-04 18:46:36 +00:00
Rahul Joshi 8c2025cc61 [MLIR] Refactor memref type -> LLVM Type conversion
- Eliminate duplicated information about mapping from memref -> its descriptor fields
  by consolidating that mapping in two functions:  getMemRefDescriptorFields and
  getUnrankedMemRefDescriptorFields.
- Change convertMemRefType() and convertUnrankedMemRefType() to use these
  functions.
- Remove convertMemrefSignature and convertUnrankedMemrefSignature.

Differential Revision: https://reviews.llvm.org/D90707
2020-11-04 10:32:56 -08:00
Rahul Joshi 63e72aa4f5 [MLIR] Remove NoSideEffect from std.global_memref op.
- Also spell "isUninitialized" correctly.

Differential Revision: https://reviews.llvm.org/D90768
2020-11-04 10:31:19 -08:00
Sean Silva eb8d386d51 [mlir] Make linalg-bufferize a composable bufferization pass
Previously, linalg-bufferize was a "finalizing" bufferization pass (it
did a "full" conversion). This wasn't great because it couldn't be used
composably with other bufferization passes like std-bufferize and
scf-bufferize.

This patch makes linalg-bufferize a composable bufferization pass.
Notice that the integration tests are switched over to using a pipeline
of std-bufferize, linalg-bufferize, and (to finalize the conversion)
func-bufferize. It all "just works" together.

While doing this transition, I ran into a nasty bug in the 1-use special
case logic for forwarding init tensors. That logic, while
well-intentioned, was fundamentally flawed, because it assumed that if
the original tensor value had one use, then the converted memref could
be mutated in place. That assumption is wrong in many cases. For
example:

```
  %0 = some_tensor : tensor<4xf32>
  br ^bb0(%0, %0: tensor<4xf32>, tensor<4xf32>)
^bb0(%bbarg0: tensor<4xf32>, %bbarg1: tensor<4xf32>)
  // %bbarg0 is an alias of %bbarg1. We cannot safely write
  // to it without analyzing uses of %bbarg1.
  linalg.generic ... init(%bbarg0) {...}
```

A similar example can happen in many scenarios with function arguments.
Even more sinister, if the converted memref is produced by a
`std.get_global_memref` of a constant global memref, then we might
attempt to write into read-only statically allocated storage! Not all
memrefs are writable!

Clearly, this 1-use check is not a local transformation that we can do
on the fly in this pattern, so I removed it.

The test is now drastically shorter and I basically rewrote the CHECK
lines from scratch because:
- the new composable linalg-bufferize just doesn't do as much, so there
is less to test
- a lot of the tests were related to the 1-use check, which is now gone,
so there is less to test
- the `-buffer-hoisting -buffer-deallocation` is no longer mixed in, so
the checks related to that had to be rewritten

Differential Revision: https://reviews.llvm.org/D90657
2020-11-04 10:16:55 -08:00
Sean Silva f556af965f [mlir] Fix materializations for unranked tensors.
Differential Revision: https://reviews.llvm.org/D90656
2020-11-04 10:16:55 -08:00
Mehdi Amini c7994bd939 Switch from C-style comments `/* ... */` to C++ style `//` (NFC)
This is mostly a scripted update, it may not be perfect.

function replace() {
  FROM=$1
  TO=$2
  git grep "$FROM" $REPO_PATH |cut -f 1 -d : | sort -u | \
    while read file; do
      sed -i "s#$FROM#$TO#" $file ;
    done
}

replace '|\*===----------------------------------------------------------------------===\*|$' '//===----------------------------------------------------------------------===//'
replace '^/\* =' '//=='
replace '^/\*=' '//='
replace '^\\\*=' '//='
replace '^|\*' '//'
replace ' \*|$' ''
replace '=\*\\$' '=//'
replace '== \*/$' '===//'
replace '==\*/$' '==//'
replace '^/\*\*\(.*\)\*/$' '///\1'
replace '^/\*\(.*\)\*/$' '//\1'
replace '//============================================================================//' '//===----------------------------------------------------------------------===//'

Differential Revision: https://reviews.llvm.org/D90732
2020-11-04 18:11:13 +00:00
Mehdi Amini aeb4b1a9d8 Add facilities to print/parse a pass pipeline through the C API
This also includes and exercise a register function for individual
passes.

Differential Revision: https://reviews.llvm.org/D90728
2020-11-04 17:29:49 +00:00
Paul C. Anagnostopoulos d56cd4291e [TableGen] Add !interleave operator to concatenate a list of values with delimiters
Add a test. Use it in some TableGen files.

Differential Revision: https://reviews.llvm.org/D90469
2020-11-04 09:23:54 -05:00
Nicolas Vasilache 85ff2705cd [mlir][std] Add DimOp folding for dim(tensor_load(m)) -> dim(m).
Differential Revision: https://reviews.llvm.org/D90755
2020-11-04 13:06:22 +00:00
Frederik Gossen 1664462d70 [MLIR] Support walks over regions and blocks
Relands
- [MLIR] Support walks over regions and blocks
         (dbae3d50f1)
- [MLIR] Use llvm::is_one_of in walk templates
         (56299b1e58)

Differential Revision: https://reviews.llvm.org/D90753
2020-11-04 12:50:05 +00:00
Nicolas Vasilache f202d32216 [mlir][SCF] Add canonicalization pattern for scf::For to eliminate yields that just forward.
For instance:
```
func @for_yields_3(%lb : index, %ub : index, %step : index) -> (i32, i32, i32) {
  %a = call @make_i32() : () -> (i32)
  %b = call @make_i32() : () -> (i32)
  %r:3 = scf.for %i = %lb to %ub step %step iter_args(%0 = %a, %1 = %a, %2 = %b) -> (i32, i32, i32) {
    %c = call @make_i32() : () -> (i32)
    scf.yield %0, %c, %2 : i32, i32, i32
  }
  return %r#0, %r#1, %r#2 : i32, i32, i32
}
```

Canonicalizes as:
```
  func @for_yields_3(%arg0: index, %arg1: index, %arg2: index) -> (i32, i32, i32) {
    %0 = call @make_i32() : () -> i32
    %1 = call @make_i32() : () -> i32
    %2 = scf.for %arg3 = %arg0 to %arg1 step %arg2 iter_args(%arg4 = %0) -> (i32) {
      %3 = call @make_i32() : () -> i32
      scf.yield %3 : i32
    }
    return %0, %2, %1 : i32, i32, i32
  }
```

Differential Revision: https://reviews.llvm.org/D90745
2020-11-04 11:36:27 +00:00
Andrzej Warzynski d007bbd986 [mlir] Fix failing shared libraries build
Failing buildbot: http://lab.llvm.org:8011/#/builders/33/builds/478

Patch that introduced the breaking change: https://reviews.llvm.org/D90667
2020-11-04 10:35:17 +00:00
Alex Zinenko 8475fa6ed6 [mlir] Add a simpler lowering pattern for WhileOp representing a do-while loop
When the "after" region of a WhileOp is merely forwarding its arguments back to
the "before" region, i.e. WhileOp is a canonical do-while loop, a simpler CFG
subgraph that omits the "after" region with its extra branch operation can be
produced. Loop rotation from general "while" to "if { do-while }" is left for a
future canonicalization pattern when it becomes necessary.

Differential Revision: https://reviews.llvm.org/D90604
2020-11-04 09:43:13 +01:00
Alex Zinenko 4c0e255c98 [mlir] Add lowering to CFG for WhileOp
The lowering is a straightforward inlining of the "before" and "after" regions
connected by (conditional) branches. This plugs the WhileOp into the
progressive lowering scheme. Future commits may choose to target WhileOp
instead of CFG when lowering ForOp.

Differential Revision: https://reviews.llvm.org/D90603
2020-11-04 09:43:13 +01:00
Alex Zinenko 79716559b5 [mlir] Add a generic while/do-while loop to the SCF dialect
The new construct represents a generic loop with two regions: one executed
before the loop condition is verifier and another after that. This construct
can be used to express both a "while" loop and a "do-while" loop, depending on
where the main payload is located. It is intended as an intermediate
abstraction for lowering, which will be added later. This form is relatively
easy to target from higher-level abstractions and supports transformations such
as loop rotation and LICM.

Differential Revision: https://reviews.llvm.org/D90255
2020-11-04 09:43:13 +01:00
Stella Laurenzo 8260db752c [mlir][Python] Return and accept OpView for all functions.
* All functions that return an Operation now return an OpView.
* All functions that accept an Operation now accept an _OperationBase, which both Operation and OpView extend and can resolve to the backing Operation.
* Moves user-facing instance methods from Operation -> _OperationBase so that both can have the same API.
* Concretely, this means that if there are custom op classes defined (i.e. in Python), any iteration or creation will return the appropriate instance (i.e. if you get/create an std.addf, you will get an instance of the mlir.dialects.std.AddFOp class, getting full access to any custom API it exposes).
* Refactors all __eq__ methods after realizing the proper way to do this for _OperationBase.

Differential Revision: https://reviews.llvm.org/D90584
2020-11-03 22:48:34 -08:00
Stella Laurenzo ebe12df896 Fix linkage error on mlirLogicalResultIsFailure.
* For C, this needs to be inline static like the others.

Differential Revision: https://reviews.llvm.org/D90740
2020-11-03 22:47:07 -08:00
Mehdi Amini 065047ab01 Enable -Werror-implicit-function-declaration by default (NFC)
This is useful in C source files where it is easy for a typo to be
silently assumed by the compiler to be an implicit declaration.

Differential Revision: https://reviews.llvm.org/D90727
2020-11-04 06:36:32 +00:00
Mehdi Amini b4fa6d3e13 Switch the CallbackOstream wrapper in the MLIR C API to an Unbuffered stream
This delegate the control of the buffering to the user of the API. This
seems like a safer option as messages are immediately propagated to the
user, which may lead to less surprising behavior during debugging for
instance.
In terms of performance, a user can add a buffered stream on the other
side of the callback.

Differential Revision: https://reviews.llvm.org/D90726
2020-11-04 06:36:32 +00:00
Mehdi Amini f61d1028fa Add a basic C API for the MLIR PassManager as well as a basic TableGen backend for creating passes
This is exposing the basic functionalities (create, nest, addPass, run) of
the PassManager through the C API in the new header: `include/mlir-c/Pass.h`.

In order to exercise it in the unit-test, a basic TableGen backend is
also provided to generate a simple C wrapper around the pass
constructor. It is used to expose the libTransforms passes to the C API.

Reviewed By: stellaraccident, ftynse

Differential Revision: https://reviews.llvm.org/D90667
2020-11-04 06:36:31 +00:00
Rahul Joshi c298824f9c [MLIR] Check for duplicate entries in attribute dictionary during custom parsing
- Verify that attributes parsed using a custom parser do not have duplicates.
- If there are duplicated in the attribute dictionary in the input, they get caught during the
  dictionary parsing.
- This check verifies that there is no duplication between the parsed dictionary and any
  attributes that might be added by the custom parser (or when the custom parsing code
  adds duplicate attributes).
- Fixes https://bugs.llvm.org/show_bug.cgi?id=48025

Differential Revision: https://reviews.llvm.org/D90502
2020-11-03 16:40:46 -08:00
mikeurbach 2e36e0dad5 [MLIR] Move eraseArguments and eraseResults to FunctionLike
Previously, they were only defined for `FuncOp`.

To support this, `FunctionLike` needs a way to get an updated type
from the concrete operation. This adds a new hook for that purpose,
called `getTypeWithoutArgsAndResults`.

For now, `FunctionLike` continues to assume the type is
`FunctionType`, and concrete operations that use another type can hide
the `getType`, `setType`, and `getTypeWithoutArgsAndResults` methods.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D90363
2020-11-03 16:53:46 -07:00
Thomas Raoux 29d1fba7b5 [mlir][vector] Make linalg FillOp vectorization use Transfer op
Differential Revision: https://reviews.llvm.org/D90474
2020-11-03 14:35:26 -08:00
Mehdi Amini bd156fee05 Remove extra comma after macro, fix GCC warning (NFC) 2020-11-03 22:22:13 +00:00
Kiran Chandramohan ab8a4cec55 [MLIR] NFC : Move OpenMP dialect include to translation
The OpenMP dialect include is only needed for translation
and is not required in LLVM dialect.

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D90510
2020-11-03 22:12:10 +00:00
River Riddle 3e1390090f [mlir][Parser] Small optimization to parsing
* Use function_ref instead of std::function in several methods
* Use ::get instead of ::getChecked for IntegerType.
  - It is already fully verified and constructing a mlir::Location can be extremely costly during parsing.
2020-11-03 13:10:26 -08:00
Thomas Raoux 36480657d8 [mlir][vector] Add canonicalization patterns for ExtractStride/ShapeCast + Splat constant
Differential Revision: https://reviews.llvm.org/D90567
2020-11-03 11:29:54 -08:00
Lei Zhang d5bf727bcd [mlir][spirv] Support for a few more decorations in (de)serialization
Reviewed By: mravishankar

Differential Revision: https://reviews.llvm.org/D90655
2020-11-03 08:11:19 -05:00
Alexander Bosch 5452fa6a59 [MLIR] Added test operations to replace linalg dependency for
BufferizeTests.

Summary:
Added test operations to replace the LinalgDialect dependency in tests
which use the buffer-deallocation, buffer-hoisting,
buffer-loop-hoisting, promote-buffers-to-stack,
buffer-placement-preparation-allowed-memref-resutls and
buffer-placement-preparation pass. Adapted the corresponding tests cases
and TestBufferPlacement.cpp.

Differential Revision: https://reviews.llvm.org/D90037
2020-11-03 12:18:49 +01:00
Mehdi Amini 008b9d97cb Make the implicit nesting behavior of the PassManager user-controllable and default to false
This is an error prone behavior, I frequently have ~20 min debugging sessions when I hit
an unexpected implicit nesting. This default makes the C++ API safer for users.

Depends On D90669

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D90671
2020-11-03 11:17:44 +00:00
Mehdi Amini cd7107a62b Handle the verifier at run() time in the PassManager instead of build time
This simplifies a few parts of the pass manager, but in particular we don't add as many
verifierpass as there are passes in the pipeline, and we can now enable/disable the
verifier after the fact on an already built PassManager.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D90669
2020-11-03 11:17:14 +00:00
Mehdi Amini bf523186fb Change the PrintOpStatsPass to operate on any operation instead of just ModuleOp
This allows to use it on other operation, like a GPUModule for example.
2020-11-03 11:15:32 +00:00
Mehdi Amini 0aaa2a4cb1 Remove mlir-c/Core.h which is superseded by the new API in mlir-c/IR.h
This header was an initial early attempt at a crude C API for bindings,
but it isn't used and redundant with the new API. At this point it only
contributes to more confusion.

Differential Revision: https://reviews.llvm.org/D90643
2020-11-03 11:15:32 +00:00
Alexander Belyaev 9925168576 [mlir] Convert `memref_reshape` to LLVM.
https://llvm.discourse.group/t/rfc-standard-memref-cast-ops/1454/15

Differential Revision: https://reviews.llvm.org/D90377
2020-11-03 11:39:08 +01:00
Tres Popp ca1bcdff4b [mlir] Add to shape.is_broadcastable description 2020-11-03 10:23:55 +01:00
Tres Popp d05d42199f [mlir] Add partial lowering of shape.cstr_broadcastable.
Because cstr operations allow more instruction reordering than asserts, we only
lower cstr_broadcastable to std ops with cstr_require. This ensures that the
more drastic lowering to asserts can happen specifically with the user's desire.

Differential Revision: https://reviews.llvm.org/D89325
2020-11-03 09:57:23 +01:00
Diego Caballero f82d307c98 [mlir][Affine] Remove single iteration affine.for ops in AffineLoopNormalize
This patch renames AffineParallelNormalize to AffineLoopNormalize to make it
more generic and be able to hold more loop normalization transformations in
the future for affine.for and affine.parallel ops. Eventually, it could also be
extended to support scf.for and scf.parallel. As a starting point for affine.for,
the patch also adds support for removing single iteration affine.for ops to the
the pass.

Differential Revision: https://reviews.llvm.org/D90267
2020-11-02 16:44:04 -08:00
MaheshRavishankar 04776bd0ed [mlir][Linalg] Add more utility functions to LinalgDependenceGraph.
Reviewed By: ThomasRaoux

Differential Revision: https://reviews.llvm.org/D90582
2020-11-02 16:35:20 -08:00
Rahul Joshi 549eac9d87 [MLIR] Remove unnecessary CHECK's from tests for which we do not run FileCheck.
Differential Revision: https://reviews.llvm.org/D90651
2020-11-02 15:21:33 -08:00
River Riddle b870d9ec83 [mlir] Optimize Op definitions and registration to optimize for code size
This revision refactors the base Op/AbstractOperation classes to reduce the amount of generated code size when defining a new operation. The current scheme involves taking the address of functions defined directly on Op and Trait classes. This is problematic because even when these functions are empty/unused we still result in these functions being defined in the main executable. In this revision, we switch to using SFINAE and template type filtering to remove remove functions that are not needed/used. For example, if an operation does not define a custom `print` method we shouldn't define a templated `printAssembly` method for it. The same applies to parsing/folding/verification/etc. This dropped MLIR code size for a large downstream library by ~10%(~1 mb in an opt build).

Differential Revision: https://reviews.llvm.org/D90196
2020-11-02 14:39:43 -08:00
Benjamin Kramer 5ece2115d8 [MLIR] Work around an ICE in GCC 7.
Looks like we have a blind spot in the testing matrix.

AsyncRegionRewriter.cpp: In member function ‘virtual void {anonymous}::GpuAsyncRegionPass::runOnFunction()’:
AsyncRegionRewriter.cpp:113:16: internal compiler error: in replace_placeholders_r, at cp/tree.c:2804
   if (getFunction()
       ~~~~~~~~~~~~~
           .getRegion()
           ~~~~~~~~~~~~
           .walk(Callback{OpBuilder{&getContext()}})
           ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2020-11-02 22:46:19 +01:00
Rahul Joshi c254b0bb69 [MLIR] Introduce std.global_memref and std.get_global_memref operations.
- Add standard dialect operations to define global variables with memref types and to
  retrieve the memref for to a named global variable
- Extend unit tests to test verification for these operations.

Differential Revision: https://reviews.llvm.org/D90337
2020-11-02 13:43:04 -08:00
Sean Silva 773ad135a3 [mlir][Bufferize] Rename TestBufferPlacement to TestFinalizingBufferize
BufferPlacement is no longer part of bufferization. However, this test
is an important test of "finalizing" bufferize passes.
A "finalizing" bufferize conversion is one that performs a "full"
conversion and expects all tensors to be gone from the program. This in
particular involves rewriting funcs (including block arguments of the
contained region), calls, and returns. The unique property of finalizing
bufferization passes is that they cannot be done via a local
transformation with suitable materializations to ensure composability
(as other bufferization passes do). For example, if a call is
rewritten, the callee needs to be rewritten otherwise the IR will end up
invalid. Thus, finalizing bufferization passes require an atomic change
to the entire program (e.g. the whole module).

This new designation makes it clear also that it shouldn't be testing
bufferization of linalg ops, so the tests have been updated to not use
linalg.generic ops. (linalg.copy is still used as the "copy" op for
copying into out-params)

Differential Revision: https://reviews.llvm.org/D89979
2020-11-02 12:42:32 -08:00
Sean Silva 52b0fe6404 [mlir] Add func-bufferize pass.
This is the most basic possible finalizing bufferization pass, which I
also think is sufficient for most new use cases. The more concentrated
nature of this pass also greatly clarifies the invariants that it
requires on its input to safely transform the program (see the
pass description in Passes.td).

With this pass, I have now upstreamed practically all of the
bufferizations from npcomp (the exception being std.constant, which can
be upstreamed when std.global_memref lands:
https://llvm.discourse.group/t/rfc-global-variables-in-mlir/2076/16 )

Differential Revision: https://reviews.llvm.org/D90205
2020-11-02 12:42:32 -08:00
Mehdi Amini 9be3c01eb9 Undef the `DEFINE_C_API_STRUCT` macro after using it in the MLIR C API header (NFC)
Leaking macros isn't a good practice when defining headers. This
requires to duplicate the macro definition in every header though, but
that seems like a better tradeoff right now.

Differential Revision: https://reviews.llvm.org/D90633
2020-11-02 19:18:32 +00:00
Thomas Raoux 9081e7594d [mlir][vector] Address post-commit review comments on vector ops folding patterns
Differential Revision: https://reviews.llvm.org/D90183
2020-11-02 10:57:32 -08:00