Commit Graph

92 Commits

Author SHA1 Message Date
Jakub Kuderski
d61ec513c4 [mlir][spirv] Add IsInf/IsNan expansion for WebGPU (#86903)
These non-finite math ops are supported by SPIR-V but not by WGSL.
Assume finite floating point values and expand these ops into `false`.

Previously, this worked by adding fast math flags during conversion from
arith to spirv, but this got removed in
https://github.com/llvm/llvm-project/pull/86578.

Also do some misc cleanups in the surrounding code.
2024-03-28 14:13:04 -04:00
Finn Plummer
cc3b6f9495 [mlir][spirv] Add folding for [S|U|GreaterThan[Equal] (#85434)
Add missing constant propogation folder for [S|U]GreaterThan[Equal].

Implement additional folding when the operands are equal for all ops.

Allows for constant folding in the IndexToSPIRV pass.

Part of work #70704
2024-03-25 19:49:12 -07:00
Finn Plummer
230b1895c4 [mlir][spirv] Add folding for [S|U|LessThan[Equal] (#85435)
Add missing constant propogation folder for [S|U]LessThan[Equal].

Implement additional folding when the operands are equal for all ops.

Allows for constant folding in the IndexToSPIRV pass.

Part of work #70704
2024-03-24 18:56:47 -07:00
Finn Plummer
8cbb8ac02c [mlir][spirv] Add folding for SelectOp (#85430)
Add missing constant propogation folder for spirv.Select

Implement additional folding when both selections are equivalent or the
condition is a constant Scalar/SplatVector.

Allows for constant folding in the IndexToSPIRV pass.

Part of work #70704
2024-03-19 13:27:35 -07:00
Artem Tyurin
9a5fb74fd1 [mlir][spirv] Handle a missing case when inlining spirv.ReturnValue (#80733)
Fixes https://github.com/llvm/llvm-project/issues/73285.
2024-02-05 19:10:39 -08:00
Alex Beloi
c63febb102 [mlir][spirv] Use assemblyFormat to define atomic op assembly (#76323)
see #73359

Declarative assemblyFormat ODS is more concise and requires less
boilerplate than filling out CPP interfaces.

Changes:
* updates the Ops defined in `SPIRVAtomicOps.td` to use assemblyFormat.
* Removes print/parse from`AtomcOps.cpp` which is now generated by
assemblyFormat
* Adds `Trait` to verify that a pointer operand `foo`'s pointee type
matches operand `bar`'s type
* * Updates error message expected in tests from new Trait
* Updates tests to updated format (largely using <operand> in place of
"operand")
2024-01-06 19:55:55 -08:00
Kohei Yamaguchi
747d8fb01c [mlir][spirv] Support alias/restrict function argument decorations (#76353)
Closes #76106

---------

Co-authored-by: Lei Zhang <antiagainst@gmail.com>
2024-01-06 11:51:23 -08:00
Finn Plummer
88151dd428 [mlir][spirv] Add folding for SNegate, [Logical]Not (#74992)
Add missing constant propogation folder for SNegate, [Logical]Not.

Implement additional folding when !(!x) for all ops.

This helps for readability of lowered code into SPIR-V.

Part of work for #70704
2023-12-21 18:24:01 +01:00
Finn Plummer
4c83c27c91 [mlir][spirv] Add folding for [I|Logical][Not]Equal (#74194) 2023-12-20 11:00:28 +01:00
Finn Plummer
40e2bb5330 [mlir][spirv] Add folding for Bitwise[Or|And|Xor] (#74193)
Add missing constant propogation folder for Bitwise[Or|And|Xor].

Move previous Bitwise[Or|And] fold implementations to
SPIRVCanonicalization for consistency.

Implement additional folding when lhs == rhs and rhs = 0 for Xor. As
well as, update an Xor testcase to account for this introduced folding.

This helps for readability of lowered code into SPIR-V.

Part of work for #70704
2023-12-11 13:09:40 -05:00
Finn Plummer
2ba9720a76 [mlir][spirv] Add folding for SPIR-V Shifting ops (#74192)
Add missing constant propogation folder for LeftShiftLogical,
RightShift[Logical|Arithmetic].

Implement additional folding when Shift value is 0.

This helps for readability of lowered code into SPIR-V.

Part of work for #70704
2023-12-06 18:00:44 -05:00
Finn Plummer
14028ec0a6 [mlir][spirv] Add canon patterns for IAddCarry/[S|U]MulExtended (#73340)
Add missing constant propogation folder for IAddCarry and
[S|U]MulExtended. Due to currently missing constant value for
spirv.struct the folding is done using canonicalization patterns.

Implement additional folding when rhs is 0 for all ops and when rhs is 1
for UMulExt.

This helps for readability of lowered code into SPIR-V.

Part of work for #70704
2023-11-29 14:32:13 -05:00
Finn Plummer
5259e14e7d [mlir][spirv] Add folding for [S|U]Mod, [S|U]Div, SRem (#73341)
Add missing constant propogation folder for [S|U]Mod, [S|U]Div, SRem

Implement additional folding when rhs is 1 for all ops.

This helps for readability of lowered code into SPIR-V.

Part of work for #70704
2023-11-29 13:40:14 -05:00
Lei Zhang
6d9eb31c31 [mlir][spirv] Fix spirv.Select min version requirement (#72173)
Per the spec, "Before version 1.4, results are only computed per
component." So using scalar condition to select composite needs SPIR-V
v1.4 at least.
2023-11-13 15:53:53 -08:00
Finn Plummer
6ddc03d97c [mlir][spirv][webgpu] Add lowering of IAddCarry to IAdd (#68495)
WebGPU does not currently support extended arithmetic, this is an issue
when we want to lower from SPIR-V. This commit adds a pattern to
transform and emulate spirv.IAddCarry with spirv.IAdd operations

Fixes #65154
2023-10-19 15:59:04 -07:00
Ivan Butygin
5dce74817b [mlir][ub] Add poison support to CommonFolders.h
Return poison from foldBinary/unary if argument(s) is poison. Add ub dialect as dependency to affected dialects (arith, math, spirv, shape).
Add poison materialization to dialects. Add tests for some ops from each dialect.
Not all affected ops are covered as it will involve a huge copypaste.

Differential Revision: https://reviews.llvm.org/D159013
2023-09-07 12:30:29 +02:00
Jakub Kuderski
07f9a0ddd6 [mlir][spirv] Do not introduce vector<1xT> in UnifyAliasedResource
1-element vectors are not valid in SPIR-V and fail `Bitcast` op verification.

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D156207
2023-07-25 11:30:18 -04:00
Nishant Patel
0e3426f3fe [mlir][spirv] Add a canonicalization pattern for UModOp
Add a transformation for a pattern like

%6 = "spirv.Constant"() <{value = 32 : i32}> : () -> i32
%7 = "spirv.UMod"(%5, %6) : (i32, i32) -> i32
%8 = "spirv.Constant"() <{value = 4 : i32}> : () -> i32
%9 = "spirv.UMod"(%7, %8) : (i32, i32) -> i32

to transform to

%6 = "spirv.Constant"() <{value = 32 : i32}> : () -> i32
%7 = "spirv.UMod"(%5, %6) : (i32, i32) -> i32
%8 = "spirv.Constant"() <{value = 4 : i32}> : () -> i32
%9= "spirv.UMod"(%5, %8) : (i32, i32) -> i32

Patch By: nbpatel
Reviewed By: antiagainst, kuhar

Differential Revision: https://reviews.llvm.org/D152341
2023-06-08 10:32:01 -04:00
Nishant Patel
79f6f92e10 [mlir][spirv] Enhance folding capability of spirv::CompositeExtractOp::fold
This PR improves the `spirv::CompositeExtractOp::fold` function by adding a backtracking mechanism.
The updated function can now traverse a chain of `CompositeInsertOp`s to find a match.

Patch By: nbpatel
Reviewed By: kuhar

Differential Revision: https://reviews.llvm.org/D151536
2023-05-26 19:23:02 -04:00
Jakub Kuderski
797594a043 [mlir][spirv] Fix nullptr dereference in UnifyAliasedResource
Fixes: https://github.com/llvm/llvm-project/issues/62368

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D149376
2023-04-28 11:39:23 -04:00
Lei Zhang
141b7d49a3 [mlir][spirv] Fix UnifyAliasedResourcePass for 64-bit index
Reviewed By: kuhar

Differential Revision: https://reviews.llvm.org/D145079
2023-03-14 23:54:27 +00:00
Jakub Kuderski
31c35285d2 [mlir][spirv] Fix crash when decorating physical storage buffer pointers
Add a comment explaining `PhysicalStorageBufferAddresses` are not
supported yet.

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

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D144039
2023-02-14 16:14:39 -05:00
Lei Zhang
2c46051aa9 [mlir][spirv] Fix vector type mismatch in UnifyAliasedResourcePass
For the cases where we have aliases of `vector<4xf16>` and
`vector<4xf32>`, we need to do casting before composite
construction.

Reviewed By: kuhar

Differential Revision: https://reviews.llvm.org/D143042
2023-02-01 20:26:29 +00:00
Jakub Kuderski
466aa585c6 [mlir][spirv] Fix crash in spirv-lower-abi-attributes
... when the are no SPIR-V env attributes.

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

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D141695
2023-01-13 10:59:48 -05:00
Thomas Raoux
493459b6dd [mlir][spirv] Add folder for LogicalNotEqual
Add a folder for LogicalNotEqual when rhs is false. This pattern shows
up after lowering to SPIRV.

Differential Revision: https://reviews.llvm.org/D141163
2023-01-06 23:13:57 +00:00
Jakub Kuderski
1b82245370 [mlir][spirv] Add smul_extended expansion for WebGPU
We need this because WGSL does not support extended multiplication ops.

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

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D141096
2023-01-05 20:11:47 -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
Jakub Kuderski
624ed0ddaf [mlir][spirv] Relax instruction order checks in test
Fix a windows buildbot failure: https://lab.llvm.org/buildbot#builders/13/builds/30439.
2023-01-04 14:08:03 -05:00
Jakub Kuderski
c957fe0f60 [mlir][spirv] Add pattern to expand UMulExtended for WebGPU
This is needed because WGSL does not yet support extended multiplication
ops.

Set up pattern/pass stuff and handle the first op: `UMulExtended`.
`SMulExtended` handling will go to a separate patch.

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

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D140995
2023-01-04 13:29:47 -05:00
Matthias Springer
e7790fbed3 [mlir] Add test-convergence option to Canonicalizer tests
This new option is set to `false` by default. It should  be set only in Canonicalizer tests to detect faulty canonicalization patterns. I.e., patterns that prevent the canonicalizer from converging. The canonicalizer should always convergence on such small unit tests that we have in `canonicalize.mlir`.

Two faulty canonicalization patterns were detected and fixed with this change.

Differential Revision: https://reviews.llvm.org/D140873
2023-01-04 12:02:21 +01:00
Lei Zhang
52ca149931 [mlir][spirv] Allow controlling subgroup size
This commit extends the `ResourceLimitsAttr` to support specifying
a minimal and maximal subgroup size, and extends `EntryPointABIAttr`
to support specifying the requested subgroup size. This is possible
now in Vulkan with the VK_EXT_subgroup_size_control extension.
For OpenCL it's possible to use the `SubgroupSize` execution mode
directly.

Reviewed By: ThomasRaoux

Differential Revision: https://reviews.llvm.org/D138962
2022-11-30 12:34:09 -05:00
Jakub Kuderski
ce90957461 [mlir][spirv] Fold noop BitcastsOps
This allows for bitcast conversion to roundtrip.

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

Reviewed By: antiagainst, Hardcode84, mravishankar

Differential Revision: https://reviews.llvm.org/D137459
2022-11-04 17:37:30 -04:00
rkayaith
13bd410962 [mlir][Pass] Include anchor op in -pass-pipeline
In D134622 the printed form of a pass manager is changed to include the
name of the op that the pass manager is anchored on. This updates the
`-pass-pipeline` argument format to include the anchor op as well, so
that the printed form of a pipeline can be directly passed to
`-pass-pipeline`. In most cases this requires updating
`-pass-pipeline='pipeline'` to
`-pass-pipeline='builtin.module(pipeline)'`.

This also fixes an outdated assert that prevented running a
`PassManager` anchored on `'any'`.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D134900
2022-11-03 11:36:12 -04:00
Jakub Kuderski
ce82530cd0 Revert "[mlir][spirv] Change td def/class prefix from SPV to SPIRV"
This reverts commit a2052b8794.

This commit renamed some Vulkan identifiers that shouldn't have been
renamed, e.g., `SPV_KHR_storage_buffer_storage_class`.
2022-09-26 12:40:35 -04:00
Jakub Kuderski
a2052b8794 [mlir][spirv] Change td def/class prefix from SPV to SPIRV
Tested with `check-mlir` and `check-mlir-integration`.

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

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D134649
2022-09-26 11:42:49 -04:00
Jakub Kuderski
5ab6ef758f [mlir][spirv] Change dialect name from 'spv' to 'spirv'
Tested with `check-mlir` and `check-mlir-integration`.

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

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D134620
2022-09-26 10:58:30 -04:00
Jakub Kuderski
817de304d5 [mlir][spirv] Change vendor op mnemonics to spv.VENDOR.name
Make vendor ops more consistent with the naming scheme within the SPIR-V
dialect.

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

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D133247
2022-09-06 13:35:08 -04:00
Lei Zhang
217b5c50b9 [mlir][spirv] Add some folders for spv.CompositeExtract
Reviewed By: kuhar

Differential Revision: https://reviews.llvm.org/D133167
2022-09-02 17:20:58 -04:00
Lei Zhang
a29fffc475 [mlir][spirv] Migrate to use specalized enum attributes
Previously we are using IntegerAttr to back all SPIR-V enum
attributes. Therefore we all such attributes are showed like
IntegerAttr in IRs, which is barely readable and breaks
roundtripability of the IR. This commit changes to use
`EnumAttr` as the base directly so that we can have separate
attribute definitions and better IR printing.

Reviewed By: kuhar

Differential Revision: https://reviews.llvm.org/D131311
2022-08-09 14:14:54 -04:00
Lei Zhang
d9728a9baa [mlir][spirv] Unify mixed scalar/vector primitive type resources
This further relaxes the requirement to allow aliased resources
to have different primitive types and some are scalars while the
other are vectors.

Reviewed By: ThomasRaoux

Differential Revision: https://reviews.llvm.org/D131207
2022-08-08 14:30:14 -04:00
Lei Zhang
067daa56a9 [mlir][spirv] Unify resources of different vector sizes
This commit extends UnifyAliasedResourcePass to handle the case
where aliased resources have different vector sizes. (It still
requires all scalar types to be of the same bitwidth.) This is
effectively reusing the code for handling different-bitwidth
scalar types.

Reviewed By: ThomasRaoux

Differential Revision: https://reviews.llvm.org/D130671
2022-07-27 19:22:50 -04:00
Lei Zhang
7668e58210 [mlir][spirv] Fix spv.CompositeConstruct assembly and validation
This commit fixes spv.CompositeConstruct to assembly to list
operand types to enable vector construction out of smaller vectors.
Validation is also fixed to properly check the cases for vector
construction.

Reviewed By: ThomasRaoux

Differential Revision: https://reviews.llvm.org/D130669
2022-07-27 19:17:23 -04:00
Jakub Kuderski
52b630da02 [mlir][spirv] Rename spv.GLSL ops to spv.GL. NFC.
This is to improve consistency within the SPIR-V dialect and make these ops a bit shorter.

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D130280
2022-07-21 13:03:30 -04:00
Lei Zhang
b2671d8898 [mlir][spirv] Fix bitcast input order for UnifyAliasedResourcePass
spv.bitcast from a vector to a scalar expects the lower-numbered
components of the the vector to map to the lower-ordered bits of
the scalar. That actually already matches how little endian stores
data in the memory. So we just need to read and push to the back
of the vector sequentially.

Reviewed By: hanchung

Differential Revision: https://reviews.llvm.org/D128473
2022-06-23 22:19:08 -04:00
Lei Zhang
11cf2d5f62 [mlir][spirv] Unify aliases of different bitwidth scalar types
This commit extends the UnifyAliasedResourcePass to handle scalar
types of different bitwidths. It requires to get the smaller bitwidth
resource as the canonical resource so that we can avoid subcomponent
load/store. Instead we load/store multiple smaller bitwidth ones.

Reviewed By: hanchung

Differential Revision: https://reviews.llvm.org/D127266
2022-06-10 18:01:31 -04:00
Mogball
a31ff0af9b [mlir][spirv] Replace StructAttrs with AttrDefs
Depends on D127370

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D127373
2022-06-09 23:16:44 +00:00
Alexander Batashev
8324561e33 [mlir][spirv] Correctly deduce PhysicalStorageBuffer64 addressing model
According to the SPIR-V specification[1], PhysicalStorageBuffer storage
class can only be used iff addressing model is PhysicalStorageBuffer64.

[1]: https://www.khronos.org/registry/SPIR-V/specs/unified1/SPIRV.html#_addressing_model

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D127067
2022-06-07 12:14:38 +03:00
Lei Zhang
e0ea1fc6f8 [mlir][spirv] Fix capability check for 64-bit element types
Using 64-bit integer/float type in interface storage classes would
require Int64/Float64 capability, per the Vulkan spec:

```
shaderInt64 specifies whether 64-bit integers (signed and unsigned) are
supported in shader code. If this feature is not enabled, 64-bit integer
types must not be used in shader code. This also specifies whether
shader modules can declare the Int64 capability. Declaring and using
64-bit integers is enabled for all storage classes that SPIR-V allows
with the Int64 capability.
```

This is different from, say, 16-bit element types, where:

```
shaderInt16 specifies whether 16-bit integers (signed and unsigned) are
supported in shader code. If this feature is not enabled, 16-bit integer
types must not be used in shader code. This also specifies whether
shader modules can declare the Int16 capability. However, this only
enables a subset of the storage classes that SPIR-V allows for the Int16
SPIR-V capability: Declaring and using 16-bit integers in the Private,
Workgroup (for non-Block variables), and Function storage classes is
enabled, while declaring them in the interface storage classes (e.g.,
UniformConstant, Uniform, StorageBuffer, Input, Output, and
PushConstant) is not enabled.
```

Reviewed By: hanchung

Differential Revision: https://reviews.llvm.org/D126256
2022-05-25 10:57:31 -04:00
River Riddle
2c7836ef15 [mlir][NFC] Update textual references of func to func.func in SPIRV tests
The special case parsing of `func` operations is being removed.
2022-04-20 22:17:29 -07:00
River Riddle
3655069234 [mlir] Move the Builtin FuncOp to the Func dialect
This commit moves FuncOp out of the builtin dialect, and into the Func
dialect. This move has been planned in some capacity from the moment
we made FuncOp an operation (years ago). This commit handles the
functional aspects of the move, but various aspects are left untouched
to ease migration: func::FuncOp is re-exported into mlir to reduce
the actual API churn, the assembly format still accepts the unqualified
`func`. These temporary measures will remain for a little while to
simplify migration before being removed.

Differential Revision: https://reviews.llvm.org/D121266
2022-03-16 17:07:03 -07:00