1710 Commits

Author SHA1 Message Date
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
737f56fdf7 [RISCV] Deduplicate zvfhmin and zvfbfmin operation actions. NFC
After #108937 fp16 w/o zvfh and bf16 are now in sync and should have
the same lowering.
2024-09-18 18:07:11 +08: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
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
Kazu Hirata
1e4e1ceeeb
[Target] Avoid repeated hash lookups (NFC) (#108677) 2024-09-14 07:39:09 -07:00
Craig Topper
ee4582f9c8
[RISCV] Use CCValAssign::getCustomReg for fixed vector arguments/returns with RVV. (#108470)
We need to insert a insert_subvector or extract_subvector which feels
pretty custom.

This should make it easier to support fixed vector arguments for GISel.
2024-09-13 07:23:44 -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
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
9c56a61105 [RISCV] Combine two hasStdExtZfhminOrZhinxmin() blocks in RISCVTargetLowering constructor. NFC 2024-09-11 22:47:56 -07:00
Craig Topper
c7cf2cc59e [RISCV] Remove unneeded customization of bf16 bitcast. NFC
We custom legalize the bitcast using the i16 type not the bf16 type.
2024-09-11 22:42:27 -07: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
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
Luke Lau
30fbfe577e [RISCV] Reorder zvfbfmin operation actions to match zvfhmin. NFC
This makes it slightly easier to see what's different between the two.
2024-09-12 00:13:40 +08: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
Craig Topper
c30c0659d6 [RISCV] Fix crashes with Zfhmin+Zfa.
We were incorrectly making ISD::FMAXIMUM, ISD::FMINIMUM, and
ISD::FNEARBYINT legal with Zfhmin+Zfa when we really need Zfh+Zfa.
2024-09-08 21:19:29 -07:00
Jim Lin
fef84c56dc
[RISCV] Support the large code model. (#70308)
Implement large code model for GlobalAddressSDNode and ExternalSymbolSDNode.

See discussion on
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/388.

---------

Co-authored-by: Kuan-Lin Chen <rufus@andestech.com>
2024-09-09 09:15:07 +08:00
Craig Topper
090b07f2e1 [RISCV] Simplify some code in unpackFromMemLoc. NFC
Use VA.getLocInfo() == CCValAssign::Indirect instead of checking
for scalable vector VT.
2024-09-06 20:14:35 -07:00
Craig Topper
62180dfd8d
[RISCV] Reduce the interface to RISCVCCAssignFn. NFC (#107503)
DataLayout, ABI, and TargetLowering can all be obtained via the
MachineFunction reference in the State object. This is how the targets
that use TableGen for CC handlers get these objects.

This might be a little slower, but it simplies all the callers in
SelectionDAG and GlobalISel.
2024-09-06 09:28:33 -07:00
anjenner
4af249fe6e
Add usub_cond and usub_sat operations to atomicrmw (#105568)
These both perform conditional subtraction, returning the minuend and
zero respectively, if the difference is negative.
2024-09-06 16:19:20 +01:00
Craig Topper
093b8bfe6b
[RISCV] Separate the calling convention handlers into their own file. NFC (#107484)
These are used by both SelectionDAG and GlobalISel and are separate from
RISCVTargetLowering.

Having a separate file is how other targets are structured. Though other
targets generate most of their calling convention code through tablegen.

I moved the `CC_RISV` functions from the `llvm::RISCV` namespace to
`llvm::`. That's what the tablegen code on other targets does and the
functions already have RISCV in their name. `RISCVCCAssignFn` is moved
from `RISCVTargetLowering` to the `llvm` namespace.
2024-09-05 22:29:23 -07:00
Craig Topper
13013bdc6a
[RISCV] Don't cost Fmv for Zfinx in isFPImmLegal. (#107361)
There is no Fmv with Zfinx.
2024-09-05 08:42:13 -07:00
Brandon Wu
1465e23985
[RISCV][llvm] Handle ptr element type in lowerDeinterleaveIntrinsicToLoad and lowerInterleaveIntrinsicToStore (#107079)
Resolve https://github.com/llvm/llvm-project/issues/106970

currently it returns 0 fixed size for `ptr` element type. The `ptr`
element size should depend on `XLen` which is 64 in riscv64 and 32 in
riscv32 respectively.
2024-09-05 12:46:20 +08:00
Craig Topper
c82a5496c8
[RISCV] Support fixed vector VP_LOAD/STORE for bf16 and f16 without Zvfh. (#107297)
This allows odd sized vector load/store to be legalized to a
VP_LOAD/STORE using EVL.

I changed the bf16 tests in fixed-vectors-load.ll and
fixed-vectors-store.ll to use an illegal type to be consistent with the
intent of these files. A legal type is already tested in
fixed-vectors-load-store.ll
2024-09-04 17:49:50 -07:00
Craig Topper
d21e731c42 [RISCV] Fix typos in comment. NFC 2024-09-04 13:02:33 -07:00
Craig Topper
36c210bb34
[RISCV] Remove pre-assignment of mask vectors during call lowering. NFC (#107192)
The first mask vector operand is supposed to be assigned to V0. No other
vector types will be assigned to V0. We don't need to pre-assign, we can
just try V0 first for any mask vectors in the normal processing.
2024-09-04 11:14:31 -07:00
Craig Topper
7deda4ed0c [RISCV] Use MCRegister for variables returned from AllocateReg. NFC
Avoids a cast from Register to MCRegister for the CCValAssign
functions.
2024-09-03 23:14:49 -07:00
Craig Topper
41402c6a8a
[RISCV][GISel] Use CCValAssign::getCustomReg for converting f16/f32<->GPR. (#105700)
This gives us much better control of the generated code for GISel. I've
tried to closely match the current gisel code, but it looks like we had
2 layers of G_ANYEXT in some cases before.

SelectionDAG now checks needsCustom() instead of detecting the special
cases in the Bitcast handler.

Unfortunately, IRTranslator for bitcast still generates copies between
register classes of different sizes. Because of this we can't handle
i16<->f16 bitcasts without crashing. Not sure if I should teach
RISCVInstrInfo::copyPhysReg to allow copies between FPR16 and GPR or if
I should convert the copies to instructions in GISel.
2024-09-03 22:49:02 -07:00
Craig Topper
3e798476de
[LegalizeDAG][RISCV] Don't promote f16 vector ISD::FNEG/FABS/FCOPYSIGN to f32 when we don't have Zvfh. (#106652)
The fp_extend will canonicalize NaNs which is not the semantics of
FNEG/FABS/FCOPYSIGN.

For fixed vectors I'm scalarizing due to test changes on other targets
where the scalarization is expected. I will try to address in a follow
up.

For scalable vectors, we bitcast to integer and use integer logic ops.
2024-09-03 22:44:49 -07:00
Craig Topper
a5ce66423b [RISCV] Remove RISCVISD::FP_ROUND_BF16.
Use isel patterns on regular FP_ROUND. For double->bf16 we need
to emit two instructions. Note the double->bf16 conversion does
double rounding, but I don't know a good way to fix that.
2024-09-03 20:18:01 -07:00
Craig Topper
ff0f2011e4
[RISCV] Bitcast fixed length bf16/f16 build_vector to i16 with Zvfbfmin/Zvfhmin+Zfbfmin/Zfhmin. (#106637)
Previously, if Zfbfmin/Zfhmin were enabled, we only handled
build_vectors that could be turned into splat_vectors. We promoted them
to f32 splats by extending in the scalar domain and narrowing in the
vector domain.

This patch fixes a crash where we failed to account for whether the f32
vector type fit in LMUL<=8.

Because the new lowering occurs after type legalization, we have to be
careful to use XLenVT for the scalar integer type and use custom cast
nodes.
2024-09-03 17:50:04 -07:00
Craig Topper
1c874bbbd6
[RISCV] Don't promote f16/bf16 SELECT with Zfhmin/Zfbfmin. (#107138)
Select only needs branches and moves so we don't need to promote it.
Promoting would canonicalize NaNs which select shouldn't do.
2024-09-03 16:04:51 -07:00
Craig Topper
db3792b87a
[RISCV] Custom promote f16/bf16 fp_to_(s/u)int to reduce isel patterns that emit two instructions. (#107011)
All of the test changes are because integer type legalization prefers to promote
fp_to_uint to fp_to_sint if neither is "Legal".
2024-09-03 15:34:25 -07:00
Craig Topper
ec8e1c623a
[RISCV] Custom promote f16/bf16 (s/u)int_to_fp. (#107026)
This avoids having isel patterns that emit two instrutions. It also
allows us to remove sext.w and slli+srli pairs by using fcvt.s.w(u) on
RV64.
2024-09-03 13:33:14 -07:00
Craig Topper
9a1eded9b9
[RISCV] Custom legalize f16/bf16 FCOPYSIGN with Zfhmin/Zbfmin. (#107039)
The LegalizeDAG expansion will go through memory since i16 isn't a legal
type. Avoid this by using FMV nodes.

Similar to what we did for #106886 for FNEG and FABS. Special care is
needed to handle the Sign operand being a different type.
2024-09-02 22:04:09 -07:00
Craig Topper
55eb93b268
[RISCV] Remove RISCVISD::FP_EXTEND_BF16. (#106939)
I don't think we need this node. We can isel fp_extend directly.
fp_extend to f64 requires two instructions, but we can emit them with an
isel pattern.

I have not removed RISCVISD::FP_ROUND_BF16 because f64->bf16 needs more
work to fix the double rounding.
2024-09-02 10:14:04 -07:00
Craig Topper
357bd61744 [RISCV] Custom promote f16 (l)lround/(l)lrint with Zfhmin/Zhinxmin instead of using isel patterns. 2024-09-01 13:44:52 -07:00
Craig Topper
3bdec31316
[RISCV] Custom legalize f16/bf16 FNEG/FABS with Zfhmin/Zbfmin. (#106886)
The LegalizeDAG expansion will go through memory since i16 isn't a legal
type. Avoid this by using FMV nodes.
2024-08-31 23:57:40 -07:00
Craig Topper
6f682c26b0 [RISCV] Merge similar code for legalizing i16<->f16 and i<->bf16 bitcasts. NFC 2024-08-31 20:01:06 -07:00
Craig Topper
2afa975928 [RISCV] Use MCRegister for vectors in CC_RISCV_FastCC. NFC 2024-08-31 10:51:39 -07:00
Craig Topper
6d9c6f0ca5 [RISCV] Use MCRegister for return value from allocateRVVReg. NFC 2024-08-31 10:30:44 -07:00
Jie Fu
1061c6da53 [RISCV] Fix -Wunused-variable in RISCVISelLowering.cpp (NFC)
/llvm-project/llvm/lib/Target/RISCV/RISCVISelLowering.cpp:21558:14: error: unused variable 'ValLMUL' [-Werror,-Wunused-variable]
    unsigned ValLMUL =
             ^
/llvm-project/llvm/lib/Target/RISCV/RISCVISelLowering.cpp:21561:14: error: unused variable 'PartLMUL' [-Werror,-Wunused-variable]
    unsigned PartLMUL =
             ^
2 errors generated.
2024-08-31 22:18:59 +08:00
Brandon Wu
22f98740b6
[llvm][RISCV] Support RISCV vector tuple CodeGen and Calling Convention (#97995)
This patch handles target lowering and calling convention.

For target lowering, the vector tuple type represented as multiple
scalable vectors is now changed to a single `MVT`, each `MVT` has a
corresponding register class.

The load/store of vector tuples are handled as the same way but need
another vector insert/extract instructions to get sub-register group.

Inline assembly constraint for vector tuple type can directly be modeled
as "vr" which is identical to normal vector registers.

For calling convention, it no longer needs an alternative algorithm to
handle register allocation, this makes the code easier to maintain and
read.

Stacked on https://github.com/llvm/llvm-project/pull/97994
2024-08-31 19:28:36 +08:00
Brandon Wu
db67a66e8e
Revert "[RISCV] RISCV vector calling convention (2/2)" (#97994)
This reverts commit 91dd844aa499d69c7ff75bf3156e2e3593a88057.

Stacked on https://github.com/llvm/llvm-project/pull/97993
2024-08-31 19:02:35 +08:00
Craig Topper
c25293c6dd
[LegalizeVectorOps][RISCV] Don't promote VP_FABS/FNEG/FCOPYSIGN. (#106659)
Promoting canonicalizes NaNs which changes the semantics. Bitcast to
integer and use logic ops instead.
2024-08-30 09:44:51 -07:00
Craig Topper
688843bda8
[RISCV] Add constant folding combine for FMV_X_ANYEXTW/H. (#106653) 2024-08-30 09:43:42 -07:00