55755 Commits

Author SHA1 Message Date
Paul Walker
02dd6b1014
[LLVM][CodeGen] Add lowering for scalable vector bfloat operations. (#109803)
Specifically:
  fabs, fadd, fceil, fdiv, ffloor, fma, fmax, fmaxnm, fmin, fminnm,
  fmul, fnearbyint, fneg, frint, fround, froundeven, fsub, fsqrt &
  ftrunc
2024-10-07 13:01:59 +01:00
Simon Pilgrim
8b6e1dc924 [X86] getIntImmCostInst - reduce i64 imm costs of AND(X,CMASK) case that can fold to BEXT/BZHI
With BEXT/BZHI the i64 imm mask will be replaced with a i16/i8 control mask

Fixes #111323
2024-10-07 12:55:54 +01:00
Simon Pilgrim
9a222a1126 [X86] Add test coverage for #111323 2024-10-07 12:36:27 +01:00
Simon Pilgrim
db1340412e [x86] combineMul - handle 0/-1 KnownBits cases before MUL_IMM logic (REAPPLIED)
Followup to 3d862c78bbb5ecbdfe93996bdf2dcfc64325ae87 fix - always fold multiply to zero/negation
2024-10-07 11:25:42 +01:00
Simon Pilgrim
3d862c78bb [x86] combineMul - use computeKnownBits directly to find MUL_IMM constant splat. (REAPPLIED)
As we're after a constant splat value we can avoid all the complexities of trying to recreate the correct constant via getTargetConstantFromNode.
2024-10-07 11:01:22 +01:00
Hans Wennborg
74a98fdbc4 Revert "[x86] combineMul - use computeKnownBits directly to find MUL_IMM constant splat."
> As we're after a constant splat value we can avoid all the complexities of trying to recreate the correct constant via getTargetConstantFromNode.

This caused builds to fail with an assertion:
X86ISelLowering.cpp:48569
Assertion `C.getZExtValue() != 0 && C.getZExtValue() != maxUIntN(VT.getScalarSizeInBits())
&& "Both cases that could cause potential overflows should have " "already been handled."

See https://github.com/llvm/llvm-project/issues/111325

This reverts commit 1bc87c9f3cb20a51191f522bf4d69338ad6bb4e6.
2024-10-07 11:48:24 +02:00
Luke Lau
c98e41f858
[LegalizeVectorTypes] Always widen fabs (#111298)
fabs and fneg are similar nodes in that they can always be expanded to
integer ops, but currently they diverge when widened.

If the widened vector fabs is marked as expand (and the corresponding
scalar type is too), LegalizeVectorTypes thinks that it may be turned
into a libcall and so will unroll it to avoid the overhead on the undef
elements.

However unlike the other ops in that list like fsin, fround, flog etc.,
an fabs marked as expand will never be legalized into a libcall. Like
fneg, it can always be expanded into an integer op.

This moves it below unrollExpandedOp to bring it in line with fneg,
which fixes an issue on RISC-V with f16 fabs being unexpectedly
scalarized when there's no zfhmin.
2024-10-07 17:40:32 +08:00
Jonas Paulsson
76aa370f44
[SystemZ] Remove inlining threshold multiplier. (#106058)
Due to recently reported problems with having the inlining threshold multiplier
set fairly high (x3), this patch removes the multiplier while addressing
the regressions seen by doing so in adjustInliningThreshold().

The specific cases that benefit from inlining that were now found to be in need
of handling contain a considerable number of memory accesses to the same
memory in both caller and callee.
2024-10-07 10:59:45 +02:00
Luke Lau
18d3a5d558
[LegalizeVectorTypes] When widening don't check for libcalls if promoted (#111297)
When widening some FP ops, LegalizeVectorTypes will check to see if the
widened op may be scalarized and then turned into a bunch of libcalls,
and if so unroll early to avoid unnecessary libcalls of the padded undef
elements.

It checks if the widened op is legal or custom to see if it will be
scalarized, but promoted ops will also avoid scalarization.

This relaxes the check to account for this which fixes some illegal
vector types on RISC-V from being scalarized when they could be widened.
2024-10-07 16:42:36 +08:00
Pierre van Houtryve
924a64a348
[AMDGPU] Only emit SCOPE_SYS global_wb (#110636)
global_wb with scopes lower than SCOPE_SYS is unnecessary for
correctness.

I was initially optimistic they would be very cheap no-ops but they can
actually be quite expensive so let's avoid them.
2024-10-07 07:35:31 +02:00
Austin Kerbow
c4d89203f3
[AMDGPU] Support preloading hidden kernel arguments (#98861)
Adds hidden kernel arguments to the function signature and marks them
inreg if they should be preloaded into user SGPRs. The normal kernarg
preloading logic then takes over with some additional checks for the
correct implicitarg_ptr alignment.

Special care is needed so that metadata for the hidden arguments is not
added twice when generating the code object.
2024-10-06 17:44:33 -07:00
Simon Pilgrim
56757e52eb [X86] combineAndLoadToBZHI - don't do an return early return if we fail to match a load
Just continue so we can test the commutated pattern as well.
2024-10-06 17:29:33 +01:00
Simon Pilgrim
326a61506a [X86] replace-load-and-with-bzhi.ll - add commuted test cases to show failure to fold
Tests showing combineAndLoadToBZHI commutation folding is currently broken
2024-10-06 17:29:33 +01:00
Simon Pilgrim
75bcf57c16 [X86] replace-load-and-with-bzhi.ll - cleanup check-prefixes to use X86/X64 for 32/64-bit targets 2024-10-06 17:29:33 +01:00
Luke Lau
abdf4ca4f1 [RISCV] Simplify fixed-vector-fp.ll run lines. NFC
This removes the different scalar fp16 configurations i.e. zfh and
zfhmin, since all these ops should be able to be lowered without
scalarizing.

This ends up revealing a couple of cases where we end up scalarizing
unexpectedly, e.g. sqrt/fabs/round with v6f16 and zvfhmin.

It also removes the zvl256b configurations, since I couldn't find
anything that specifically needed to test this.
2024-10-07 00:20:13 +08:00
Simon Pilgrim
1bc87c9f3c [x86] combineMul - use computeKnownBits directly to find MUL_IMM constant splat.
As we're after a constant splat value we can avoid all the complexities of trying to recreate the correct constant via getTargetConstantFromNode.
2024-10-06 15:23:38 +01:00
Luke Lau
20864d2cf6
[ValueTypes][RISCV] Add v1bf16 type (#111112)
When trying to add RISC-V fadd reduction cost model tests for bf16, I
noticed a crash when the vector was of <1 x bfloat>.

It turns out that this was being scalarized because unlike f16/f32/f64,
there's no v1bf16 value type, and the existing cost model code assumed
that the legalized type would always be a vector.

This adds v1bf16 to bring bf16 in line with the other fp types.

It also adds some more RISC-V bf16 reduction tests which previously
crashed, including tests to ensure that SLP won't emit fadd/fmul
reductions for bf16 or f16 w/ zvfhmin after #111000.
2024-10-06 22:20:51 +08:00
Rose
6de5305b3d
[X86] For minsize memset/memcpy, use byte or double-word accesses (#87003)
repstosb and repstosd are the same size, but stosd is only done for 0
because the process of multiplying the constant so that it is copied
across the bytes of the 32-bit number adds extra instructions that cause
the size to increase. For 0, repstosb and repstosd are the same size,
but stosd is only done for 0 because the process of multiplying the
constant so that it is copied across the bytes of the 32-bit number adds
extra instructions that cause the size to increase. For 0, we do not
need to do that at all.

For memcpy, the same goes, and as a result the minsize check was moved
ahead because a jmp to memcpy encoded takes more bytes than repmovsb.
2024-10-06 12:30:18 +08:00
NAKAMURA Takumi
e075dcf7d2 Revert "Reapply "[AMDGPU][GlobalISel] Fix load/store of pointer vectors, buffer.*.pN (#110714)" (#111059)"
This reverts commit 98a15c7b0c6ec129d371f0c121dbe9396c4f5609.
(llvmorg-20-init-8051-g98a15c7b0c6e)
2024-10-06 10:50:51 +09:00
Craig Topper
20e37f03c6
[GISel] Don't preserve NSW flag when converting G_MUL of INT_MIN to G_SHL. (#111230)
mul and shl have different meanings for the nsw flag. We need to drop it
when converting a multiply by the minimum negative value.
2024-10-05 10:27:09 -07:00
David Green
1789534ac1
[SelectOpt] Don't convert constant selects to branches. (#110858)
Selects that choose between two constants will be less profitable to
turn into branches, especially if the constants can be folded somehow
into the surrounding instructions. They will also be cost modelled in a
way that can make them over-optimistically converted to branches, as
neither branch will have a latency depth but the constants still need to
be materialized.

This patch disabled selectopt for selects with two constant branches. It
is currently in the target independent part, as it sounds generic, but I
could move it into AArch64 if needed.
2024-10-05 16:17:22 +01:00
Yingwei Zheng
e6549b8036
[RISCV][ISel] Allow emitting addiw with u32simm12 rhs (#111116)
In InstCombine, we shrink the constant by setting unused bits to zero
(e.g. `((X + -2) & 4294967295) -> ((X + 4294967294) & 4294967295)`).
However, this canonicalization blocks emitting `addiw` and creates
redundant li for simm32 rhs:
```
; bin/llc -mtriple=riscv64 -mattr=+zba test.ll -o -
define i64 @add_u32simm32_zextw(i64 %x) nounwind {
entry:
  %add = add i64 %x, 4294967294
  %and = and i64 %add, 4294967295
  ret i64 %and
}
```
```
add_u32simm32_zextw:                    # @add_u32simm32_zextw
# %bb.0:                                # %entry
        li      a1, -2
        add     a0, a0, a1
        zext.w  a0, a0
        ret
```

This patch addresses the issue by matching u32simm12 rhs.
2024-10-05 09:23:29 +08:00
Manasij Mukherjee
fda2fea3d1
[NVPTX] Promote v2i8 to v2i16 (#111189)
Promote v2i8 to v2i16, fixes a crash. 
Re-enable a test in NVPTX/vector-returns.ll

https://github.com/llvm/llvm-project/issues/104864
2024-10-04 14:15:30 -07:00
Yaxun (Sam) Liu
3b88805ca2
[AMDGPU] Fix SDWA commuting (#106920)
SDWA insts miss reverse opcode, which causes them to be treated as
commutable with default reverse opcode i.e. their own opcode. As a
result, SWDA F16 sub A, B and Sub B, A are merged by machine CSE. The
correct behavior is to merged sub A, B and subrev B, A instead of sub B,
A. This issues caused failures in rocFFT tests.

Another issue is that src0_sel and src1_sel are not swapped when SDWA
insts are commuted.

Verified that this fixes rocFFT tests failure.
2024-10-04 15:53:40 -04:00
Adam Yang
1e75d08659
[DXIL] Add radians intrinsic (#110616)
makes progress on #99151

### Changes
- Added int_dx_radians intrinsic in IntrinsicsDirectX.td
- Added expansion for int_dx_radians in DXILIntrinsicExpansion.cpp`
- Added DXIL backend test case

### Related PRs
* [[clang][HLSL] Add radians intrinsic
#110802](https://github.com/llvm/llvm-project/pull/110802)
* [[SPIRV] Add radians intrinsic
#110800](https://github.com/llvm/llvm-project/pull/110800)
2024-10-04 14:54:05 -04:00
Ramkumar Ramachandra
45817aa726
LICM: hoist BO assoc for and, or, xor (#111146)
Trivially lift the Opcode limitation on hoistBOAssociation to also hoist
and, or, and xor.

Alive2 proofs: https://alive2.llvm.org/ce/z/rVNP2X
2024-10-04 19:13:51 +01:00
Adam Yang
c0f8889774
[SPIRV] Add radians intrinsic (#110800)
partially fixes #99151

### Changes
* Added int_spv_radians intrinsic in IntrinsicsSPIRV.td
* Added lowering for int_spv_radians in SPIRVInstructionSelector.cpp
* Added DXIL backend test case

### Related PRs
* [[clang][HLSL] Add radians intrinsic
#110802](https://github.com/llvm/llvm-project/pull/110802)
* [[DXIL] Add radians intrinsic
#110616](https://github.com/llvm/llvm-project/pull/110616)
2024-10-04 14:09:50 -04:00
Krzysztof Drewniak
98a15c7b0c
Reapply "[AMDGPU][GlobalISel] Fix load/store of pointer vectors, buffer.*.pN (#110714)" (#111059)
This reverts commit 650c41aad2eb43c634a05b2b5799a0c13a73b92f.

The test failures appear to be from conflicts with other PRs that landed around this time.
2024-10-04 12:33:26 -05:00
Simon Pilgrim
9459d729d2 [x86] combineMUL - when looking for a vector multiply by splat constant, ensure we're only accepting ConstantInt splat scalars.
Fixes #111170
2024-10-04 18:12:01 +01:00
Michael Maitland
f873fc3ae2
[RISCV][GISEL] instruction-select vmclr (#110782)
This is stacked on #110778. This PR adds and tests renderVLOp too, as
that is needed from vmclr.
2024-10-04 12:51:04 -04:00
Paul Walker
87c799af31 Regenerate output for llvm/test/CodeGen/AArch64/sve-intrinsics-while.ll 2024-10-04 16:06:38 +00:00
Matt Arsenault
e5a0c30e4a AMDGPU: Work around machine verifier failure with convergence tokens
Apparently any function with convergence tokens will fail the
machine verifier after register allocation. The existing codegen tests
for tokens use stop-before, and do not run to the end. Work around this
by splitting out tests with convergence tokens. Fixes EXPENSIVE_CHECKS
bot failures after c08d7b3de7409aecadd7f9edfe0f3a1ce28a6374 and
428ae0f12e29eff1ddcaf59bdcce904ec056963e
2024-10-04 16:59:23 +04:00
Stephen Tozer
d826b0c90f
[LLVM] Add HasFakeUses to MachineFunction (#110097)
Following the addition of the llvm.fake.use intrinsic and corresponding
MIR instruction, two further changes are planned: to add an
-fextend-lifetimes flag to Clang that emits these intrinsics, and to
have -Og enable this flag by default. Currently, some logic for handling
fake uses is gated by the optdebug attribute, which is intended to be
switched on by -fextend-lifetimes (and by extension -Og later on).
However, the decision was made that a general optdebug attribute should
be incompatible with other opt_ attributes (e.g. optsize, optnone),
since they all express different intents for how to optimize the
program. We would still like to allow -fextend-lifetimes with optsize
however (i.e. -Os -fextend-lifetimes should be legal), since it may be a
useful configuration and there is no technical reason to not allow it.

This patch resolves this by tracking MachineFunctions that have fake
uses, allowing us to run passes that interact with them and skip passes
that clash with them.
2024-10-04 13:13:30 +01:00
Martin Storsjö
b837c9e289 Revert "[llvm] Mark win x64 SEH pseudo instruction as meta instructions (#110889)"
This reverts commit db33d82026b8742b2719289547e3cb9910c8d7d1.

This commit caused errors when compiling for i686 with codeview
debug info enabled, see
https://github.com/llvm/llvm-project/pull/110889#issuecomment-2393405613
for details.
2024-10-04 13:48:42 +03:00
Simon Pilgrim
b5598c33f0 [X86] Regenerate test checks with vpternlog comments 2024-10-04 11:04:37 +01:00
dlav-sc
7be2ce7312
[RISCV] fix SP recovery in a function epilogue (#110809)
Currently, in the cases when fp register is presented and sp register is
adjusted at the second time, sp recovery in a function epilogue isn't
performed in the best way, for example:
```
lui a0, 2
sub sp, s0, a0
addi a0, a0, -2044
add sp, sp, a0
```

This patch improves sp register recovery in such cases and the code
snippet above becomes:
```
addi sp, s0, -2044
```
2024-10-04 12:22:11 +03:00
Yingwei Zheng
9cf8c094c7
[RISCV][DAGCombine] Combine sext_inreg (shl X, Y), i32 into sllw X, Y (#111101)
Alive2: https://alive2.llvm.org/ce/z/ncf36D
2024-10-04 16:03:09 +08:00
Matt Arsenault
428ae0f12e
AMDGPU: Do not tail call if an inreg argument requires waterfalling (#111002)
If we have a divergent value passed to an outgoing inreg argument,
the call needs to be executed in a waterfall loop and thus cannot
be tail called.

The waterfall handling of arbitrary calls is broken on the selectiondag
path, so some of these cases still hit an error later.

I also noticed the argument evaluation code in isEligibleForTailCallOptimization
is not correctly accounting for implicit argument assignments. It also seems
inreg codegen is generally broken; we are assigning arguments to the reserved
private resource descriptor.
2024-10-04 00:04:02 +04:00
Steven Perron
5114758b1c
[SPIRV] Make access qualifier optional for spirv.Image type (#110852)
The SPIRV backend has a special type named `spirv.Image`. This type is
meant to correspond to the OpTypeImage instruction in SPIR-V, but there
is one difference. The access qualifier operand in OpTypeImage is
optional. On top of that, the access qualifiers are only valid for
kernels, and not for shaders.

We want to reuse this type when generating shader from HLSL, but we
can't use the access qualifier. This commit make the access qualifer
optional in the target extension type.

The same is done for `spirv.SampledImage`.

Contributes to #81036
2024-10-03 14:11:06 -04:00
Matt Arsenault
c08d7b3de7
AMDGPU: Fix verifier error on tail call target in vgprs (#110984)
We allow tail calls of known uniform function pointers. This
would produce a verifier error if the uniform value is in VGPRs.
Insert readfirstlanes just in case this occurs, which will fold
out later if it is unnecessary.

GlobalISel should need a similar fix, but it currently does not
attempt tail calls of indirect calls.

Fixes #107447
Fixes subissue of #110930
2024-10-03 21:50:56 +04:00
Joshua Batista
c098435eaa
Add cross builtins and cross HLSL function to DirectX and SPIR-V backend (#109180)
This PR adds the step intrinsic and an HLSL function that uses it.
The SPIRV backend is also implemented.

Used https://github.com/llvm/llvm-project/pull/106471 as a reference.
Fixes https://github.com/llvm/llvm-project/issues/99095
2024-10-03 10:24:09 -07:00
Jorge Botto
a4516da49f
[AArch64] - Fold and and cmp into tst (#110347)
Fixes https://github.com/llvm/llvm-project/issues/102703.

https://godbolt.org/z/nfj8xsb1Y

The following pattern:

```
%2 = and i32 %0, 254
%3 = icmp eq i32 %2, 0
```
is optimised by instcombine into:

```%3 = icmp ult i32 %0, 2```

However, post instcombine leads to worse aarch64 than the unoptimised version.

Pre instcombine:
```
        tst     w0, #0xfe
        cset    w0, eq
        ret
```
Post instcombine:
```
        and     w8, w0, #0xff
        cmp     w8, #2
        cset    w0, lo
        ret
```


In the unoptimised version, SelectionDAG converts `SETCC (AND X 254) 0 EQ` into `CSEL 0 1 1 (ANDS X 254)`, which gets emitted as a `tst`.

In the optimised version, SelectionDAG converts `SETCC (AND X 255) 2 ULT` into `CSEL 0 1 2 (SUBS (AND X 255) 2)`, which gets emitted as an `and`/`cmp`.

This PR adds an optimisation to `AArch64ISelLowering`, converting `SETCC (AND X Y) Z ULT` into `SETCC (AND X (Y & ~(Z - 1))) 0 EQ` when `Z` is a power of two. This makes SelectionDAG/Codegen produce the same optimised code for both examples.
2024-10-03 17:56:01 +01:00
Luke Lau
487686b82e
[SDAG][RISCV] Don't promote VP_REDUCE_{FADD,FMUL} (#111000)
In https://reviews.llvm.org/D153848, promotion was added for a variety
of f16 ops with zvfhmin, including VP reductions.

However I don't believe it's correct to promote f16 fadd or fmul
reductions to f32 since we need to round the intermediate results.

Today if we lower @llvm.vp.reduce.fadd.nxv1f16 on RISC-V, we'll get two
different results depending on whether we compiled with +zvfh or
+zvfhmin, for example with a 3 element reduction:

	; v9 = [0.1563, 5.97e-8, 0.00006104]

	; zvfh
	vsetivli x0, 3, e16, m1, ta, ma
	vmv.v.i v8, 0
	vfredosum.vs v8, v9, v8
	vfmv.f.s fa0, v8
	; fa0 = 0.1563

	; zvfhmin
	vsetivli x0, 3, e16, m1, ta, ma
	vfwcvt.f.f.v v10, v9
	vsetivli x0, 3, e32, m1, ta, ma
	vmv.v.i v8, 0
	vfredosum.vs v8, v10, v8
	vfmv.f.s fa0, v8
	fcvt.h.s fa0, fa0
	; fa0 = 0.1564

This same thing happens with reassociative reductions e.g. vfredusum.vs,
and this also applies for bf16.

I couldn't find anything in the LangRef for reductions that suggest the
excess precision is allowed. There may be something we can do in Clang
with -fexcess-precision=fast, but I haven't looked into this yet.

I presume the same precision issue occurs with fmul, but not with
fmin/fmax/fminimum/fmaximum.

I can't think of another way of lowering these other than scalarizing,
and we can't scalarize scalable vectors, so this just removes the
promotion and adjusts the cost model to return an invalid cost. (It
looks like we also don't currently cost fmul reductions, so presumably
they also have an invalid cost?)

I think this should be enough to stop the loop vectorizer or SLP from
emitting these intrinsics.
2024-10-04 00:17:45 +08:00
Juan Manuel Martinez Caamaño
b9bb77f0c7
[AMDGPU] Update branch-condition-and.ll to auto-generated checks (#110860) 2024-10-03 17:12:31 +02:00
Paul Walker
9fd75233ca [LLVM] Port SVE tests in llvm/test/CodeGen/AArch64 to use splat()
This is preparation work towards making such splats use Constant{Int,FP}
by default for scalable vectors.
2024-10-03 14:35:32 +00:00
Paul Walker
d283705829
[AArch64][SVE] Fix definition of bfloat fcvt intrinsics. (#110281)
Affected intrinsics:
  llvm.aarch64.sve.fcvt.bf16f32
  llvm.aarch64.sve.fcvtnt.bf16f32
    
The named intrinsics took a predicate based on the smallest element type
when it should be based on the largest. The intrinsics have been replace
by v2 equivalents and affected code ported to use them.
    
Patch includes changes to getSVEPredicateBitCast() that ensure the
generated code for the auto-upgraded old intrinsics is unchanged.
2024-10-03 12:36:01 +01:00
vikashgu
870bdc6ea7 Reapply "[AMDGPU]Optimize SGPR spills (#93668)"
This reverts commit c2fc7f75f67039bb1ed577bc0edbd699a850cd9d. As the
dependent patch about split vgpr regalloc pipeline solved the issue(#96353).
2024-10-03 09:47:15 +00:00
David Green
eb4a91dcea [AArch64][GlobalISel] Add frem fp128 libcall legalization.
Like other operations, these need to be marked as libcall.
2024-10-03 08:48:26 +01:00
David Green
8ecbad7e41 [AArch64][GlobalISel] Add fpowi fp128 libcall legalization. 2024-10-03 08:48:26 +01:00
Yingwei Zheng
944f4adcd2
[RISCV][ISel] Select binvi for pattern icmp eq/ne X, pow2 (#110957)
This patch selects `binvi` for pattern `icmp eq/ne X, pow2` when zbs is
available.
2024-10-03 15:13:02 +08:00