1563 Commits

Author SHA1 Message Date
Kazu Hirata
caa99a01f5 Use llvm::popcount instead of llvm::countPopulation(NFC) 2023-01-22 12:48:51 -08:00
Markus Böck
715b025852 [mlir][ods] Simplify signature of custom printers and parsers of Attributes and Types in presence of default constructible parameters
The vast majority of parameters of C++ types used as parameters for Attributes and Types are likely to be default constructible. Nevertheless, TableGen conservatively generates code for the custom directive, expecting signatures using FailureOr<T> for all parameter types T to accomodate them possibly not being default constructible. This however reduces the ergonomics of the likely case of default constructible parameters.

This patch fixes that issue, while barely changing the generated TableGen code, by using a helper function that is used to pass any parameters into custom parser methods. If the type is default constructible, as deemed by the C++ compiler, a default constructible instance is created and passed into the parser method by reference. In all other cases it is a Noop and a FailureOr is passed as before.

Documentation was also updated to document the new behaviour.

Fixes https://github.com/llvm/llvm-project/issues/60178

Differential Revision: https://reviews.llvm.org/D142301
2023-01-22 16:18:44 +01:00
River Riddle
83a635c0d4 [mlir] Add support for interface inheritance
This allows for interfaces to define a set of "base classes",
which are interfaces whose methods/extra class decls/etc.
should be inherited by the derived interface. This more
easily enables combining interfaces and their dependencies,
without lots of awkard casting. Additional implicit conversion
operators also greatly simplify the conversion process.

One other aspect of this "inheritance" is that we also implicitly
add the base interfaces to the attr/op/type. The user can still
add them manually if desired, but this should help remove some
of the boiler plate when an interface has dependencies.

See https://discourse.llvm.org/t/interface-inheritance-and-dependencies-interface-method-visibility-interface-composition

Differential Revision: https://reviews.llvm.org/D140198
2023-01-18 19:16:30 -08:00
River Riddle
5cdc2bbc75 [mlir] Move SymbolOpInterfaces "classof" check to a proper "extraClassOf" interface field
SymbolOpInterface overrides the base classof to provide support
for optionally implementing the interface. This is currently placed
in the extraClassDeclarations, but that is kind of awkard given that
it requires underlying knowledge of how the base classof is implemented.
This commit adds a proper "extraClassOf" field to allow interfaces to
implement this, which abstracts away the default classof logic.

Differential Revision: https://reviews.llvm.org/D140197
2023-01-18 19:16:30 -08:00
River Riddle
3e731af912 [mlir] Limit Interface generation to the top-level input file
There are very few instances in which we use multiple files
for interface definitions (none upstream), and this allows
for including interfaces that shouldn't be generated (for
interface inheritance, dependencies, etc.)

Differential Revision: https://reviews.llvm.org/D140196
2023-01-18 19:16:30 -08:00
Martin Storsjö
d3da9067d1 [CMake] Allow setting the location of host tools with LLVM_NATIVE_TOOL_DIR
This avoids having to specify the location of all individual tools.
In current builds, one may want to specify LLVM_TABLEGEN, CLANG_TABLEGEN,
LLDB_TABLEGEN, LLVM_CONFIG_PATH, CLANG_PSEUDO_GEN and
CLANG_TIDY_CONFUSABLE_CHARS_GEN; specifying just the base directory
containing all of them is much more convenient.

Factorize the code for setting up use of a tool that is used during
the build (which either is newly built in the same build, or
built in a separate nested cmake build - when cross compiling or
when e.g. optimized tablegen is requested - or used from an existing
prebuilt binary).

Differential Revision: https://reviews.llvm.org/D131052
2023-01-18 23:56:15 +02:00
Mehdi Amini
0441272c45 Revert "Revert "Refactor OperationName to use virtual tables for dispatch (NFC)""
This streamlines the implementation and makes it so that the virtual
tables are in the binary instead of dynamically assembled during initialization.
The dynamic allocation size of op registration is also smaller with this
change.

This reverts commit 7bf1e441da6b59a25495fde8e34939f93548cc6d
and re-introduce e055aad5ffb348472c65dfcbede85f39efe8f906
after fixing the windows crash by making ParseAssemblyFn a
unique_function again

Differential Revision: https://reviews.llvm.org/D141492
2023-01-16 23:58:48 +00:00
Mehdi Amini
7bf1e441da Revert "Refactor OperationName to use virtual tables for dispatch (NFC)"
This reverts commit e055aad5ffb348472c65dfcbede85f39efe8f906.

This crashes on Windows at the moment for some reasons.
2023-01-16 23:11:38 +00:00
Joe Loser
a288d7f937 [llvm][ADT] Replace uses of makeMutableArrayRef with deduction guides
Similar to how `makeArrayRef` is deprecated in favor of deduction guides, do the
same for `makeMutableArrayRef`.

Once all of the places in-tree are using the deduction guides for
`MutableArrayRef`, we can mark `makeMutableArrayRef` as deprecated.

Differential Revision: https://reviews.llvm.org/D141814
2023-01-16 14:49:37 -07:00
Kazu Hirata
2140260d91 [mlir] Remove remaining uses of llvm::Optional (NFC)
This patch removes one "using" declaration and #include
"llvm/ADT/Optional.h".  It keeps several "using" declarations in
headers for downstream users.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14 01:34:49 -08:00
Kazu Hirata
0a81ace004 [mlir] Use std::optional instead of llvm::Optional (NFC)
This patch replaces (llvm::|)Optional< with std::optional<.  I'll post
a separate patch to remove #include "llvm/ADT/Optional.h".

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14 01:25:58 -08:00
Kazu Hirata
a1fe1f5f77 [mlir] Add #include <optional> (NFC)
This patch adds #include <optional> to those files containing
llvm::Optional<...> or Optional<...>.

I'll post a separate patch to actually replace llvm::Optional with
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-13 21:05:06 -08:00
Mehdi Amini
e055aad5ff Refactor OperationName to use virtual tables for dispatch (NFC)
This streamlines the implementation and makes it so that the virtual tables are in the binary instead of dynamically assembled during initialization.
The dynamic allocation size of op registration is also smaller with this
change.

Differential Revision: https://reviews.llvm.org/D141492
2023-01-14 01:27:38 +00:00
Jeff Niu
1b60f0d73c [mlir][ods] Generate inferReturnTypes for ops with TypesMatchWith
Ops that use TypesMatchWith to constrain result types for verification
and to infer result types during parser generation should also be able
to have the `inferReturnTypes` method auto generated. This patch
upgrades the logic for generating `inferReturnTypes` to handle the
TypesMatchWith trait by building a type inference graph where each edge
corresponds to "type of A can be inferred from type of B", supporting
transformers other than `"$_self"`.

Reviewed By: lattner, rriddle

Differential Revision: https://reviews.llvm.org/D141231
2023-01-12 13:26:12 -08:00
Markus Böck
7df761217c [mlir][NFC] Migrate rest of the dialects to the new fold API 2023-01-11 21:47:25 +01:00
Markus Böck
bbfa7ef16d [mlir] Add a new fold API using Generic Adaptors
This is part of the RFC for a better fold API: https://discourse.llvm.org/t/rfc-a-better-fold-api-using-more-generic-adaptors/67374

This patch implements the required foldHook changes and the TableGen machinery for generating `fold` method signatures using `FoldAdaptor` for ops, based on the value of `useFoldAPI` of the dialect. It may be one of 2 values, with convenient named constants to create a quasi enum. The new `fold` method will then be generated if `kEmitFoldAdaptorFolder` is used.

Since the new `FoldAdaptor` approach is strictly better than the old signature, part of this patch updates the documentation and all example to encourage use of the new `fold` signature.
Included are also tests exercising the new API, ensuring proper construction of the `FoldAdaptor` and proper generation by TableGen.

Differential Revision: https://reviews.llvm.org/D140886
2023-01-11 14:32:21 +01:00
Markus Böck
cf6f217516 [mlir][tblgen] Generate generic adaptors for Ops
This is part of the RFC for a better fold API: https://discourse.llvm.org/t/rfc-a-better-fold-api-using-more-generic-adaptors/67374

This patch implements the generation of generic adaptors through TableGen. These are essentially a generalization of Adaptors, as implemented previously, but instead of indexing into a `mlir::ValueRange`, they may index into any container, regardless of the element type. This allows the use of the convenient getter methods of Adaptors to be reused on ranges that are the result of some kind of mapping functions of an ops operands.
In the case of the fold API in the RFC, this would be `ArrayRef<Attribute>`, which is a mapping of the operands to their possibly-constant values.

Implementation wise, some special care was taken to not cause a compile time regression, nor to break any kind of source compatibility.
For that purpose, the current adaptor class was split into three:
* A generic adaptor base class, within the detail namespace as it is an implementation detail, which implements all APIs independent of the range type used for the operands. This is all the attribute and region related code. Since it is not templated, its implementation does not have to be inline and can be put into the cpp source file
* The actual generic adaptor, which has a template parameter for the range that should be indexed into for retrieving operands. It implements all the getters for operands, as they are dependent on the range type. It publicly inherits from the generic adaptor base class
* A class named as adaptors have been named so far, inheriting from the generic adaptor class with `mlir::ValueRange` as range to index into. It implements the rest of the API, specific to `mlir::ValueRange` adaptors, which have previously been part of the adaptor. This boils down to a constructor from the Op type as well as the verify function.

The last class having the exact same API surface and name as Adaptors did previously leads to full source compatibility.

Differential Revision: https://reviews.llvm.org/D140660
2023-01-11 14:32:21 +01:00
serge-sans-paille
984b800a03
Move from llvm::makeArrayRef to ArrayRef deduction guides - last part
This is a follow-up to https://reviews.llvm.org/D140896, split into
several parts as it touches a lot of files.

Differential Revision: https://reviews.llvm.org/D141298
2023-01-10 11:47:43 +01:00
Jakub Kuderski
a35a8f4b19 [mlir][spirv] Clean up transform pass definitions. NFC.
- Make naming more consistent.
- Drop unnecessary custom constructors definitions.
- Move pass documentation to pass descriptions.

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D141159
2023-01-09 10:51:08 -05:00
Jakub Kuderski
47232bea9e [mlir][spirv] Fix extended umul expansion for WebGPU
Fix an off-by-one error in extended umul extension for WebGPU.
Revert to the long multiplication algorithm originally added to wide
integer emulation, which was deleted in D139776. It is much easier
to see why it is correct.

Add runtime tests based on the mlir-vulkan-runner. These run both with
and without umul extension.

Issue: https://github.com/llvm/llvm-project/issues/59563

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D141085
2023-01-05 18:41:26 -05:00
Tobias Gysi
0cf066392f [mlir][llvm] Make the import of LLVM IR metadata extensible.
This revision extends the LLVMImportDialectInterface to make the import
of LLVM IR instruction-level metadata extensible. It extends the
signature of the existing dialect interface to provide a method to
import specific metadata kinds and attach them to the imported
operation. The conversion function can rely on the ModuleImport class
to perform support tasks.

The revision implements the second part of the
"extensible llvm ir import" rfc:
https://discourse.llvm.org/t/rfc-extensible-llvm-ir-import/67256/6

The interface method names changed a bit compared to the suggested
design. The hook to set the instruction level metadata is now called
setMetadataAttrs and takes the metadata kind as an additional parameter.
We do not hand in the original LLVM IR instruction since it is not used
at this point. Importing named module-level meta data can be added in a
later stage after gaining some experience with this extension mechanism.

Depends on D140374

Reviewed By: ftynse, Dinistro

Differential Revision: https://reviews.llvm.org/D140556
2023-01-03 14:47:25 +01:00
Tobias Gysi
cf487cce6f [mlir][llvm] Make the import of LLVM IR intrinsics extensible.
The revision introduces the LLVMImportDialectInterface to make the
import of LLVM IR intrinsics extensible. It uses a dialect interface
that enables external projects to provide their own conversion functions
for custom intrinsics. These conversion functions can rely on the
ModuleImport class to perform support tasks such as mapping LLVM
values to MLIR values or for converting types between the two worlds.

The implementation largely mirrors the export implementation. One major
difference is the dispatch to the appropriate dialect interface, since
LLVM IR intrinsics have no direct association to an MLIR dialect. The
dialect interfaces thus have to publish the supported intrinsics to
ensure incoming conversion calls are dispatched to the right dialect
interface.

The revision implements the extensible intrinsic import discussed as
part of the "extensible llvm ir import" rfc:
https://discourse.llvm.org/t/rfc-extensible-llvm-ir-import/67256/6

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D140374
2023-01-02 11:35:44 +01:00
Eugene Zhulenev
b074b356c9 [mlir] Add a test for default valued dictionary attributes
+ Replace special placeholders ($_builder, etc...) in default attribute string

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D140765
2022-12-30 12:15:54 -08:00
Schuyler Eldridge
794056e86a
[mlir] Fix missing OpInterface docs newline
Fix incorrect markdown generated by mlir-tblgen for an InterfaceMethod
that includes a body.  Previously, this would cause the next method to
show up on the same line and produce incorrect markdown.  Newlines would
only be added if the method did _not_ provide a body.  E.g., previously
this was generating markdown like:

    some function comment#### `next method`

This change makes this generate as:

    some function comment

    #### `next method`

Signed-off-by: Schuyler Eldridge <schuyler.eldridge@sifive.com>

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D140590
2022-12-27 11:52:45 -05:00
Jacques Pienaar
b57acb9a40 Revert "Revert "[mlir][py] Enable building ops with raw inputs""
Fix Python 3.6.9 issue encountered due to type checking here. Will
add back in follow up.

This reverts commit 1f47fee2948ef48781084afe0426171d000d7997.
2022-12-21 16:22:39 -08:00
Jacques Pienaar
1f47fee294 Revert "[mlir][py] Enable building ops with raw inputs"
Reverting to fix build bot.

This reverts commit 3781b7905d8d808e5d4e97d597263f8ac48541b8.
2022-12-21 14:53:12 -08:00
Jacques Pienaar
3781b7905d [mlir][py] Enable building ops with raw inputs
For cases where we can automatically construct the Attribute allow for more
user-friendly input. This is consistent with C++ builder generation as well
choice of which single builder to generate here (most
specialized/user-friendly).

Registration of attribute builders from more pythonic input is all Python side.
The downside is that
  * extra checking to see if user provided a custom builder in op builders,
  * the ODS attribute name is load bearing
upside is that
  * easily change these/register dialect specific ones in downstream projects,
  * adding support/changing to different convenience builders are all along with
    the rest of the convenience functions in Python (and no additional changes
    to tablegen file or recompilation needed);

Allow for both building with Attributes as well as raw inputs. This change
should therefore be backwards compatible as well as allow for avoiding
recreating Attribute where already available.

Differential Revision: https://reviews.llvm.org/D139568
2022-12-21 10:10:31 -08:00
Benjamin Kramer
30199d11d2 [mlir] Drop Optional::value() references from tblgen files
std::optional::value() has undesired exception checking semantics and is
unavailable in older Xcode (see _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS).
The call sites block std::optional migration.
2022-12-19 22:50:19 +01:00
Fangrui Song
cbb0981388 [mlir] llvm::Optional::value => operator*/operator->
std::optional::value() has undesired exception checking semantics and is
unavailable in older Xcode (see _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS). The
call sites block std::optional migration.
2022-12-17 19:07:38 +00:00
Ramkumar Ramachandra
22426110c5 mlir/tblgen: use std::optional in generation
This is part of an effort to migrate from llvm::Optional to
std::optional. This patch changes the way mlir-tblgen generates .inc
files, and modifies tests and documentation appropriately. It is a "no
compromises" patch, and doesn't leave the user with an unpleasant mix of
llvm::Optional and std::optional.

A non-trivial change has been made to ControlFlowInterfaces to split one
constructor into two, relating to a build failure on Windows.

See also: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716

Signed-off-by: Ramkumar Ramachandra <r@artagnon.com>

Differential Revision: https://reviews.llvm.org/D138934
2022-12-17 11:13:26 +01:00
Fangrui Song
4913e5da3c [mlir] std::optional::value => operator*/operator->
value() has undesired exception checking semantics and calls
__throw_bad_optional_access in libc++. Moreover, the API is unavailable without
_LIBCPP_NO_EXCEPTIONS on older Mach-O platforms (see
_LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS).
2022-12-17 04:38:27 +00:00
Fangrui Song
bef481df8b [mlir] Drop uses of operator<<(raw_ostream &OS, const Optional<T> &O) 2022-12-16 20:24:35 +00:00
Matthias Springer
325b58d59f [mlir][cf] Print message in cf.assert to LLVM lowering
The assert message was previously ignored. The lowered IR now calls `puts` it in case of a failed assertion.

Differential Revision: https://reviews.llvm.org/D138647
2022-12-15 17:45:34 +01:00
Kazu Hirata
6eb0b0a045 Don't include Optional.h
These files no longer use llvm::Optional.
2022-12-14 21:16:22 -08:00
Ramkumar Ramachandra
bcd6528554 mlir/ods-gen: use bash from env in shell script
On macOS, hardcoding the path of bash leads to a failure in executing
update_core_linalg_named_ops.sh, due to an old version of bash being
present in that location.

Signed-off-by: Ramkumar Ramachandra <r@artagnon.com>

Differential Revision: https://reviews.llvm.org/D139942
2022-12-14 12:28:07 +01:00
Matthias Kramm
4e98d611ef [mlir] Implement backward dataflow.
This enables interprocedural lifeness analysis, very busy expression
analysis, etc.

Reviewed By: Mogball

Differential Revision: https://reviews.llvm.org/D138935
2022-12-13 18:35:27 +01:00
Tom Eccles
5d91f79fce [mlir] Fix -Wstrict-prototypes warning
These warnings prevent compilation using clang and
-DLLVM_ENABLE_WERROR=On.

Differential revision: https://reviews.llvm.org/D139322
2022-12-12 12:04:58 +00:00
Kazu Hirata
4f81805a3f [mlir] Use std::optional instead of None in comments (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-10 17:11:23 -08:00
Jakub Kuderski
b4bdcea214 [mlir][arith] Define mului_extended op
Add conversion to the SPIR-V and LLVM dialects.

This was originally proposed in:
https://discourse.llvm.org/t/rfc-arith-add-extended-multiplication-ops/66869.

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D139688
2022-12-09 17:37:06 -05:00
Tobias Gysi
38e87e8af4 [mlir][llvm] Improve LLVM IR import error handling.
Instead of exiting in the middle of the import handle errors more
gracefully by printing an error message and returning failure. The
revision handles and tests the import of unsupported instructions,
values, constants, and intrinsics.

Depends on D139404

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D139405
2022-12-09 14:42:09 +02:00
Jeff Niu
9f1f44bb94 [mlir][ods] BitEnumAttr: Actually use the 'str' for the all unset case
Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D139657
2022-12-08 12:30:30 -08:00
Jeff Niu
15511c2e6c [mlir][ods] Generate remover methods with camelcase
The remove*Attr methods were not being generated with the correct
camelcase method.

Depends on D139470

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D139471
2022-12-08 11:32:35 -08:00
Markus Böck
836852878c [mlir][tblgen][docs] Use correct introductionary prefix for the Syntax description of attributes and types
The doc generator currently has the use of `!` as prefix hardcoded, despite being incorrect for Attributes. These start with `#`.

This patch fixes that little issue by using `#` for AttrDefs and `!` for TypeDefs in the `Syntax` field of the generated Markdown file.

Differential Revision: https://reviews.llvm.org/D139524
2022-12-07 12:25:47 +01:00
Jakub Kuderski
03e6bf5f56 [mlir][spirv] Define spirv.*Dot integer dot product ops
This covers `SDot`, `SUDot`, and `UDot`. The `*AccSat` version will be
added in a follow-up revision.

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D139242
2022-12-06 20:17:41 -05:00
Hanhan Wang
0f297cad4d [mlir][tensor][linalg] Introduce DataLayoutPropagation pass.
It introduces a pattern that swaps `linalg.generic + tensor.pack` to
`tensor.pack + linalg.generic`. It requires all the iteration types
being parallel; the indexing map of output operand is identiy. They can
all be relaxed in the future.

The user can decide whether the propagation should be applied or not by
passing a control function.

Reviewed By: mravishankar

Differential Revision: https://reviews.llvm.org/D138882
2022-12-06 15:00:07 -08:00
Krzysztof Parzyszek
c589730ad5 [YAML] Convert Optional to std::optional 2022-12-06 12:49:32 -08:00
Kevin Gleason
279d294d26 Use consistent spacing before custom directives for op and attr/type assemblyFormat.
Currently, assemblyFormat `custom<A>($a) custom<B>($b)` has different spacing
if used for Ops vs Attrs/Types. Ops insert a space if needed before the custom directive,
while attributes and types do not.

This leads to the following two patterns in attributes / types:

```
# 1. Whitespace literal
let assemblyFormat = "... ` ` custom<A>($a)"

# 2. Custom printer code includes spacing
void printB(...) {
  printer << ' ' << b;
}
```

Moving this spacing into the generated code allows for some cleanup in mlir and
improves the consistency of custom directives.

Reviewed By: mehdi_amini

Differential Revision: https://reviews.llvm.org/D138235
2022-12-06 12:13:02 -08:00
Fangrui Song
3cfe412e4c [TableGen] llvm::Optional => std::optional 2022-12-06 07:21:02 +00:00
Kazu Hirata
192d9dd731 [mlir] Use std::nullopt instead of None in comments (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-04 19:58:32 -08:00
Kazu Hirata
70c73d1b72 [mlir] Use std::nullopt instead of None in comments (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-04 17:23:50 -08:00