4177 Commits

Author SHA1 Message Date
Craig Topper
644899addd [RISCV][GISel] Port portions of float-intrinsics.ll and double-intrinsics.ll. NFC
Remove the legalizer test for the same intrinsics as it is no longer
interesting with end to end tests.
2024-09-18 13:13:34 -07:00
Craig Topper
d5d1417659
[RISCV][GISel] Use libcalls for rint, nearbyint, trunc, round, and roundeven intrinsics. (#108779) 2024-09-18 12:07:44 -07:00
Craig Topper
8e4909aa19
[RISCV] Remove unnecessary vand.vi from vXi1 and nvXvi1 VECTOR_REVERSE codegen. (#109071)
Use a setne with 0 instead of a trunc. We know we zero extended the node
so we can get by with a non-zero check only. The truncate lowering
doesn't know that we zero extended so has to mask the lsb.

I don't think DAG combine sees the trunc before we lower it to RISCVISD
nodes so we don't get a chance to use computeKnownBits to remove the
AND.
2024-09-18 09:43:48 -07:00
Luke Lau
edac1b2d63
[RISCV] Promote bf16 ops to f32 with zvfbfmin (#108937)
For f16 with zvfhmin, we promote most ops and VP ops to f32. This does
the same for bf16 with zvfbfmin, so the two fp types should now be in
sync.

There are a few places in the custom lowering where we need to check for
a LMUL 8 f16/bf16 vector that can't be promoted and must be split, this
extracts that out into isPromotedOpNeedingSplit.

In a follow up NFC we can deduplicate the code that sets up the
promotions.
2024-09-18 17:39:40 +08:00
Luke Lau
8d7d4c25cb
[RISCV] Split fp rounding ops with zvfhmin nxv32f16 (#108765)
This adds zvfhmin test coverage for fceil, ffloor, fnearbyint, frint,
fround and froundeven and splits them at nxv32f16 to avoid crashing,
similarly to what we do for other nodes that we promote.

This also sets ftrunc to promote which was previously missing. We
already promote the VP version of it, vp_froundtozero.
Marking it as promoted affects some of the cost model tests since
they're no longer expanded.
2024-09-18 16:36:13 +08:00
Michael Maitland
1ebe16bf43 [RISCV] Add VL optimization related tests
These tests are good candidate for VL optimization. This is a pre-commit for
PR #108640, but can could probably also be improved by the peephole VL
optimizations.
2024-09-17 11:14:20 -07:00
Michael Maitland
64972834c1
[RISCV][GISEL] Introduce the RISCVPostLegalizerLowering pass (#108991)
This is mostly a copy of the AArch64PostLegalizerLoweringPass, except it
removes all of the AArch64 combines.

This pass allows us to lower instructions after the generic
post-legalization combiner has had a chance to run.

We will be adding combines to this pass in future patches.
2024-09-17 13:18:35 -04:00
Craig Topper
da46244e49 Revert "[LegalizeVectorOps] Make the AArch64 hack in ExpandFNEG more specific."
This reverts commit 884ff9e3f9741ac282b6cf8087b8d3f62b8e138a.

Regression was reported in Halide for arm32.
2024-09-17 09:04:43 -07:00
Philip Reames
594579b7af [RISCV] Autogenerate compress-opt-select.ll
I realized after spending way too much time looking at this, that
we can avoid objdump entirely here by having the assembly simply
not print the aliases.  Once we do that, we can simply autogen
this test, and updates become trivial and understandable.
2024-09-17 08:47:20 -07:00
Philip Reames
c532e6db27 [RISCV] Restructure compress-opt-select.ll
Two major changes:
- Remove use of sed preprocessing - this was being used to create two
  versions of each test, and the result is much more readable if we
  just duplicate the tests.
- Use a regex for matching the condition.  An upcoming change causes
  us to reverse the branch direction (which doesn't matter to the
  purpose of these tests at all), so using the regex makes the test
  more stable.
2024-09-17 08:29:03 -07:00
Philip Reames
b153cc5c2b [RISCV] Fix boundary error in compress-opt-select.ll
Per the comment, this test is intending to test the first constant which
can't be encoded via a c.addi.  However, -32 *can* be encoded as in a
c.addi, and all that's preventing it from doing so is the register
allocators choice to use a difference destination register on the
add than it's source.  (Which compressed doesn't support.)

The current LLC codegen for this test looks like:

	addi	a1, a0, -32
	li	a0, -99
	bnez	a1, .LBB0_2
	li	a0, 42
.LBB0_2:
	ret

After https://github.com/llvm/llvm-project/pull/108889, we sink the LI, and
the register allocator picks the same source and dest register for the addi
resulting in the c.addi form being emitted.  So, to avoid a confusing diff
let's fix the test to check what was originally intended.
2024-09-17 07:43:28 -07:00
Mikhail R. Gadelha
d2125e1db6
[RISCV] Support STRICT_UINT_TO_FP and STRICT_SINT_TO_FP (#102503)
This patch adds support for the missing STRICT_UINT_TO_FP and
STRICT_SINT_TO_FP for riscv and adds a test case for rv32 which was
previously crashing.

The code is in line with how other strict_* nodes are handled
(e.g., getting op(1) instead of op(0) when it's a strict node, as op(0)
in a strict node is the entry token).
2024-09-17 11:21:52 -03:00
Luke Lau
6af2f225a0
[RISCV] Restrict combineOp_VLToVWOp_VL w/ bf16 to vfwmadd_vl with zvfbfwma (#108798)
We currently make sure to check that if folding an op to an f16 widening
op that we have zvfh. We need to do the same for bf16 vectors, but with
the further restriction that we can only combine vfmadd_vl to vfwmadd_vl
(to get vfwmaccbf16.v{v,f}).

The added test case currently crashes because we try to fold an add to a
bf16 widening add, which doesn't exist in zvfbfmin or zvfbfwma

This moves the checks into the extension support checks to keep it one
place.
2024-09-17 13:35:25 +08:00
Craig Topper
884ff9e3f9 [LegalizeVectorOps] Make the AArch64 hack in ExpandFNEG more specific.
Only scalarize single element vectors when vector FSUB is not
supported and scalar FNEG is supported.
2024-09-16 21:48:42 -07:00
Craig Topper
f022111e64 [RISCV][GISel] Restore s32 support on RV64 for DIV, and REM.
This reverts commit 2599d695128381e6932b43f0e95649c533308d6d.

I was was plannig to remove s32 as a legal type on RV64, but
I'm rethinking that.
2024-09-16 16:29:20 -07:00
Craig Topper
a366323c8d [RISCV][GISel] Restore s32 support for G_ABS on RV64.
This reverts commit 5e6a1987a5d4574d3c3811f878ddbbbf7c35fa01.

I was was plannig to remove s32 as a legal type on RV64, but
I'm rethinking that.
2024-09-16 16:29:20 -07:00
Philip Reames
7e56a09278 [RISCV] Add a testcase for an unprofitable machine-sink issue
This corresponds to an upcoming change which will fully explain
why this is a machine-sink issue.
2024-09-16 14:17:42 -07:00
Craig Topper
4eb9780261 [RISCV] Fix IR for store_large_offset_no_opt_i16 in make-compressible-zbc.mir. NFC
The IR used loads instead of stores.
2024-09-16 12:51:07 -07:00
Philip Reames
8f023ec81d [RISCV] Add coverage for select C, C1, C2 where (C1-C2)*[0,1] is cheap 2024-09-16 12:46:43 -07:00
Luke Lau
8d89f83db0 [RISCV] Add some missing VP zvfhmin test coverage. NFC
We're also missing coverage for the vp_reduce_* nodes, but they
currently crash with zvfhmin. It looks like they're getting expanded
instead of promoted.
2024-09-16 17:11:14 +08:00
Nikita Popov
dfa54298ff
[InitUndef] Enable the InitUndef pass on non-AMDGPU targets (#108353)
The InitUndef pass works around a register allocation issue, where undef
operands can be allocated to the same register as early-clobber result
operands. This may lead to ISA constraint violations, where certain
input and output registers are not allowed to overlap.

Originally this pass was implemented for RISCV, and then extended to ARM
in #77770. I've since removed the target-specific parts of the pass in
#106744 and #107885. This PR reduces the pass to use a single
requiresDisjointEarlyClobberAndUndef() target hook and enables it by
default. The hook is disabled for AMDGPU, because overlapping
early-clobber and undef operands are known to be safe for that target,
and we get significant codegen diffs otherwise.

The motivating case is the one in arm64-ldxr-stxr.ll, where we were
previously incorrectly allocating a stxp input and output to the same
register.
2024-09-16 09:48:25 +02:00
Robert Dazi
8837898b8d
[DAGCombine] Count leading ones: refine post DAG/Type Legalisation if promotion (#102877)
This PR is related to #99591. In this PR, instead of modifying how the
legalisation occurs depending on surrounding instructions, we refine
after legalisation.

This PR has two parts:

* `SDPatternMatch/MatchContext`: Modify a little bit the code to match
Operands (used by `m_Node(...)`) and Unary/Binary/Ternary Patterns to
make it compatible with `VPMatchContext`, instead of only `m_Opc`
supported. Some tests were added to ensure no regressions.
* `DAGCombiner`: Add a `foldSubCtlzNot` which detect and rewrite the
patterns using matching context.

Remaining Tasks:

- [ ] GlobalISel
- [ ] Currently the pattern matching will occur even before
legalisation. Should I restrict it to specific stages instead ?
- [ ] Style: Add a visitVP_SUB ?? Move `foldSubCtlzNot` in another
location for style consistency purpose ?

@topperc

---------

Co-authored-by: v01dxyz <v01dxyz@v01d.xyz>
2024-09-15 15:48:36 +04:00
Craig Topper
367c145e5f
[IRTranslator][RISCV] Support scalable vector zeroinitializer. (#108666) 2024-09-14 15:46:18 -07:00
Craig Topper
6e410bc9f5
[RISCV][GISel] Legalize G_STACKSAVE/RESTORE. (#108438)
Test was copied from AArch64 and X86.
2024-09-12 22:48:11 -07:00
Luke Lau
587d4cc797
[RISCV] Lower bf16 {S,U}INT_TO_FP, FP_TO_{S,U}INT and VP variants (#108338)
This handles int->fp/fp->int nodes for zvfbfmin, reusing the same parts
that f16 uses with zvfhmin.

There's quite a bit of replication here that can probably be cleaned up
at some point.
2024-09-13 13:04:56 +08:00
Craig Topper
9d9d2b4799
[RISCV] Rename XCValu cv.slet(u) to cv.sle(u). (#108481)
According to https://github.com/openhwgroup/cv32e40p/issues/833 this
instruction was renamed last year to remove the 't'.

I used MnemonicAlias to support the old name. Unfortunately, this gives
a generic error if XCValu is not enabled. Since its an old name I hope
this isn't too big of an issue.

CC: @jeremybennett
2024-09-12 21:39:01 -07:00
Craig Topper
7ba49685c0
[RISCV] Enable floating point CSR alias mnemonics for Zfinx. (#108464) 2024-09-12 18:40:52 -07:00
Luke Lau
c6ca13db41
[RISCV] Lower interleave + deinterleave for zvfhmin and zvfbfmin (#108404)
Fortunately f16 and bf16 are always < EEW, so we can always lower via
widening or narrowing. This means we don't need to add patterns for
vrgather_vv_vl just yet.
2024-09-13 00:09:17 +08:00
Craig Topper
8c05515032
[LegalizeIntegerTypes] Simplify ExpandIntRes_FP_TO_XINT when operand needs to be SoftPromoted. (#107634)
Create an FP_EXTEND instead of handling the soft promote directly. This
FP_EXTEND will be visited and soft promoted itself.

This removes a zero extend from the generated code when the f32 type is
itself softened. Previously we softened it as an fp16_to_fp which sees
the operand as an integer type so we extend it. When we soften the
result as an fp_extend we see the source as f16 and don't extend. It
only becomes an integer inside call lowering not by type legalization.

If this extend is really necessary, then we have an issue when an
f16->f32 fp_extend exists in the source and f32 needs to be softened.

This simplifies part of #102503.
2024-09-12 08:28:06 -07:00
Joe Faulls
bf8101e4fd
[CodeGen] Clear InitUndef pass new register cache between pass runs (#90967)
Multiple invocations of the pass could interfere with eachother,
preventing some undefs being initialised.

I found it very difficult to create a unit test for this due to it being
dependent on particular allocations of a previous function. However, the
bug can be observed here: https://godbolt.org/z/7xnMo41Gv with the
creation of the illegal instruction `vnsrl.wi v9, v8, 0`
2024-09-12 15:01:55 +02:00
Luke Lau
ecf260e1dd [RISCV] Merge bf16 tests into respective tests. NFC
I added these in #108245, but given the sheer number of tests that
will need to be added to cover bf16 promotion to f32 it seems better
to keep them in one place to avoid an explosion of files.
2024-09-12 18:41:46 +08:00
Craig Topper
de6d7a6c30
[RISCV] Expand Zfa fli+fneg cases during lowering instead of during isel. (#108316)
Most of the constants fli can generate are positive numbers. We can use
fli+fneg to generate their negative versions.

Previously, we considered such negative constants as "legal" and let
isel generate the fli+fneg. However, it is useful to expose the fneg to
DAG combines to fold with fadd to produce fsub or with fma to produce
fnmadd, fnmsub, or fmsub.

This patch moves the fneg creation to lowering so that the fneg will be
visible to the last DAG combine.

I might move the rest of Zfa handling from isel to lowering as a follow
up.

Fixes #107772.
2024-09-11 22:31:45 -07:00
Craig Topper
b2e8b8fac0
[RISCV] Lower f16/bf16 splat_vector by bitcasting to i16 instead of promoting to f32. (#108298)
If f16/bf16 scalar types are not legal we also need to custom legalize
to prevent a crash. We do similar lowering for build_vector.
2024-09-11 22:29:20 -07:00
Luke Lau
cbcf5313e8
[RISCV] Expand bf16 FNEG/FABS/FCOPYSIGN (#108245)
The motivation for this is to start promoting bf16 ops to f32 so that we
can mark bf16 as a supported type in
RISCVTTIImpl::isElementTypeLegalForScalableVector and scalably-vectorize
it.

This starts with expanding the nodes that can't be promoted to f32 due
to canonicalizing NaNs, similarly to f16 in #106652.
2024-09-12 13:19:52 +08:00
Craig Topper
8c17ed1512
[RISCV] Generalize RISCVDAGToDAGISel::selectFPImm to handle bitcasts from int to FP. (#108284)
selectFPImm previously handled cases where an FPImm could be
materialized in an integer register.

We can generalize this to cases where a value was in an integer register
and then copied to a scalar FP register to be used by a vector
instruction.

In the affected test, the call lowering code used up all of the FP
argument registers and started using GPRs. Now we use integer vector
instructions to consume those GPRs instead of moving them to scalar FP
first.
2024-09-11 21:13:26 -07:00
Luke Lau
44d122188e
[RISCV] Expand bf16 vector truncstores and extloads (#108235)
Previously they were legal by default, so the truncstore/extload test
cases would get combined and crash during selection.
These are set to expand for f16 so do the same for bf16.
2024-09-12 08:42:25 +08:00
Luke Lau
480f07ff6c
[RISCV] Add fixed length vector patterns for vfwmaccbf16.vv (#108204)
This adds VL patterns for vfwmaccbf16.vv so that we can handle fixed
length vectors.

It does this by teaching combineOp_VLToVWOp_VL to emit
RISCVISD::VFWMADD_VL for bf16. The change in getOrCreateExtendedOp is
needed because getNarrowType is based off of the bitwidth so returns
f16. We need to explicitly check for bf16.

Note that the .vf patterns don't work yet, since the build_vector splat
gets lowered to a (vmv_v_x_vl (fmv_x_anyexth x)) instead of a vfmv.v.f,
which SplatFP doesn't pick up, see #106637.
2024-09-12 08:41:50 +08:00
Jonathon Penix
866b93e6b3
[RISCV] Don't outline pcrel_lo when the function has a section prefix (#107943)
GNU ld will error when encountering a pcrel_lo whose corresponding
pcrel_hi is in a different section. [1] introduced a check to help
prevent this issue by preventing outlining in a few circumstances.
However, we can also hit this same issue when outlining from functions
with prefixes ("hot"/"unlikely"/"unknown" from profile information, for
example) as the outlined function might not have the same prefix,
possibly resulting in a "paired" pcrel_lo and pcrel_hi ending up in
different sections.

To prevent this issue, take a similar approach as [1] and additionally
prevent outlining when we see a pcrel_lo and the function has a prefix.

[1]
96c85f80f0

Fixes #107520
2024-09-11 09:53:11 -07:00
Philip Reames
65e0574076
[RISCV] Expand mul X, C where C=2^N*(3,5,9)*(3,5,9) (#108100)
This is a three deep expression which is deeper than we've otherwise
gone for multiple expansions, but I think it's reasonable to do so. This
covers mul by 50, 100, and 200 which are reasonably common naturally
arising numbers.
2024-09-11 07:33:06 -07:00
Simon Pilgrim
43da8a7a10 [DAG] Add test coverage for ABD "sub of selects" patterns based off #53045
Add tests for "sub(select(icmp(a,b),a,b),select(icmp(a,b),b,a)) -> abd(a,b)" patterns that still fail to match to abd nodes

This will hopefully be helped by #108218
2024-09-11 15:18:29 +01:00
Nikita Popov
1e3a24d2e4
[InitUndef] Don't use largest super class (#107885)
The InitUndef pass currently uses the getLargestSuperClass() hook (which
is only used by that pass) to chose the register to initialize. This was done
to reduce the number of undef init pseudos needed, e.g. so that the vrnov0
regclass would use the same pseudo as v0. After #106744 we use a single
generic pseudo, so this is no longer necessary.
2024-09-11 09:36:20 +02:00
Philip Reames
12530015a4
[RISCV] Add reductions to list of roots in tryToReduceVL (#107595)
This allows us to reduce VLs feeding reduction instructions. In
particular, this means that <3 x Ty> reduce(load) like sequences no
longer require a VL toggle.

This was waiting on 3d72957; now that the latent correctness issue is
fixed, we can expand this transform.
2024-09-10 18:47:20 -07:00
Luke Lau
933fc63a1d
[RISCV] Rematerialize vmv.s.x and vfmv.s.f (#108012)
Continuing with #107993 and #108007, this handles the last of the main
rematerializable vector instructions.

There's an extra spill in one of the test cases, but it's likely noise
from the spill weights and isn't an issue in practice.
2024-09-11 09:44:57 +08:00
Luke Lau
21a0176c58
[RISCV] Rematerialize vfmv.v.f (#108007)
This is the same principle as vmv.v.x in #107993, but for floats.
2024-09-11 09:38:29 +08:00
Luke Lau
77fc8dae22
[RISCV] Rematerialize vmv.v.x (#107993)
Even though vmv.v.x has a non constant scalar operand, we can still
rematerialize it because we have split register allocation between
vectors and scalars.

InlineSpiller will check to make sure that the scalar operand is live at
the point where the rematerialization occurs, so this won't extend any
scalar live ranges. However this also means we may not be able to
rematerialize in some cases, as shown in @vmv.v.x_needs_extended.

It might be worthwhile teaching InlineSpiller to extend scalar live
ranges in a future patch. I experimented with this locally and it
reduced spills on 531.deepsjeng_r by a further 3%.
2024-09-11 09:13:23 +08:00
Philip Reames
0fc4147c6d [RISCV] Add test coverage for mul X, C where C=2^N*(3,5,9)*(3,5,9) 2024-09-10 14:37:33 -07:00
Kyungwoo Lee
2cfdcfb490
Fix for llvm/test/CodeGen/RISCV/O3-pipeline.ll (#108050)
The previous `Fix for Attempt to fix [CGData][MachineOutliner] Global
Outlining (#90074) #108037 (#108047)` somehow dropped this file.
2024-09-10 09:25:00 -07:00
Kyungwoo Lee
0f52545289
[CGData][MachineOutliner] Global Outlining (#90074)
This commit introduces support for outlining functions across modules
using codegen data generated from previous codegen. The codegen data
currently manages the outlined hash tree, which records outlining
instances that occurred locally in the past.
    
The machine outliner now operates in one of three modes:

1. CGDataMode::None: This is the default outliner mode that uses the
suffix tree to identify (local) outlining candidates within a module.
This mode is also used by (full)LTO to maintain optimal behavior with
the combined module.
2. CGDataMode::Write (`-codegen-data-generate`): This mode is identical
to the default mode, but it also publishes the stable hash sequences of
instructions in the outlined functions into a local outlined hash tree.
It then encodes this into the `__llvm_outline` section, which will be
dead-stripped at link time.
3. CGDataMode::Read (`-codegen-data-use-path={.cgdata}`): This mode
reads a codegen data file (.cgdata) and initializes a global outlined
hash tree. This tree is used to generate global outlining candidates.
Note that the codegen data file has been post-processed with the raw
`__llvm_outline` sections from all native objects using the
`llvm-cgdata` tool (or a linker, `LLD`, or a new ThinLTO pipeline
later).

This depends on https://github.com/llvm/llvm-project/pull/105398. After
this PR, LLD (https://github.com/llvm/llvm-project/pull/90166) and Clang
(https://github.com/llvm/llvm-project/pull/90304) will follow for each
client side support.
This is a patch for
https://discourse.llvm.org/t/rfc-enhanced-machine-outliner-part-2-thinlto-nolto/78753.
2024-09-10 06:56:31 -07:00
Luke Lau
7ba6768df8 Revert "[RISCV] Update V0Defs after moving Src in peepholes (#107359)"
This fixes #107950 and adds a test case for it. The issue was due to
us incorrectly assuming that we stored a V0Defs entry for every single
instruction.

We actually only store them for instructions that use V0, so when we
updated the V0Def after moving we sometimes ended up copying nullptr
over from an instruction that doesn't use V0 and clearing the V0Def
entry inadvertently.

Because we don't have V0Defs on instructions that don't use V0, the
FIXME was never actually needed in the first place since the
bookkeeping wasn't out of sync to begin with.

That commit also mentioned that a future unmasked to masked pseudo
peephole might need unmasked pseudos to have V0Defs entries, but after
working on this locally it turns out we don't.

This reverts commit ce3648094d44e8c098396a353b215acecb363cda.
2024-09-10 13:26:07 +08:00
Luke Lau
b71d88ca5b [RISCV] Constrain passthru regclass in vmerge -> vmv peephole
In #107827 we now set true's passthru to the false operand if it was
undef. We need to remember to also constrain the regclass in case true
is a masked pseudo which needs its passthrus to be in VR[M*]NoV0
2024-09-10 13:26:07 +08:00