166 Commits

Author SHA1 Message Date
Austin
a550fef906
[llvm] Use llvm::fill instead of std::fill(NFC) (#146911)
Use llvm::fill instead of std::fill
2025-07-04 14:10:28 +08:00
David Green
77941eba7f
[CostModel] Add a DstTy to getShuffleCost (#141634)
A shuffle will take two input vectors and a mask, to produce a new
vector of size <MaskElts x SrcEltTy>. Historically it has been assumed
that the SrcTy and the DstTy are the same for getShuffleCost, with that
being relaxed in recent years. If the Tp passed to getShuffleCost is the
SrcTy, then the DstTy can be calculated from the Mask elts and the src
elt size, but the Mask is not always provided and the Tp is not reliably
always the SrcTy. This has led to situations notably in the SLP
vectorizer but also in the generic cost routines where assumption about
how vectors will be legalized are built into the generic cost routines -
for example whether they will widen or promote, with the cost modelling
assuming they will widen but the default lowering to promote for integer
vectors.

This patch attempts to start improving that - it originally tried to
alter more of the cost model but that too quickly became too many
changes at once, so this patch just plumbs in a DstTy to getShuffleCost
so that DstTy and SrcTy can be reliably distinguished. The callers of
getShuffleCost have been updated to try and include a DstTy that is more
accurate. Otherwise it tries to be fairly non-functional, keeping the
SrcTy used as the primary type used in shuffle cost routines, only using
DstTy where it was in the past (for InsertSubVector for example).

Some asserts have been added that help to check for consistent values
when a Mask and a DstTy are provided to getShuffleCost. Some of them
took a while to get right, and some non-mask calls might still be
incorrect. Hopefully this will provide a useful base to build more
shuffles that alter size.
2025-06-21 12:29:29 +01:00
Florian Hahn
0816bb32ac
[Matrix] Fix heap-use-after-free after 0fa373c77ded203eddb.
We need to skip instructions in FusedInsts, as they may have been
deleted. Fixes a heap-use-after-free after #141681.
2025-06-19 17:52:31 +01:00
Jon Roelofs
0fa373c77d
[Matrix] Propagate shape information through PHI insts (#141681)
... and split them as we lower them, avoiding several shuffles in the
process.
2025-06-18 09:00:48 -07:00
Jon Roelofs
1f10c6a277
[Matrix] Hoist more IRBuilder<>'s. NFC 2025-06-17 11:30:43 -07:00
Jon Roelofs
8ed43c47de
[Matrix] Hoist IRBuilder<> out of Visit* functions. NFC (#144369) 2025-06-16 11:38:27 -07:00
Jon Roelofs
56548e1d9b
[Matrix] Fix a crash in VisitSelectInst due to iteration length mismatch 2025-06-12 09:27:06 -07:00
Jon Roelofs
2a905dd1eb
[Matrix] Use range-for in Visit* Result construction. NFC 2025-06-12 08:50:50 -07:00
Jon Roelofs
ca5b71a455
[Matrix] Propagate shape information through Select insts (#141876) 2025-06-12 07:52:25 -07:00
Kazu Hirata
be3c6a06d3 [Scalar] Fix a warning
This patch fixes:

  llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp:628:22: error:
  variable 'Inst' set but not used [-Werror,-Wunused-but-set-variable]
2025-06-10 12:49:39 -07:00
Jon Roelofs
8345d62478
[Matrix] Hoist finalizeLowering into caller. NFC (#143038) 2025-06-10 12:36:37 -07:00
Jon Roelofs
77da1257b6
[Matrix] Collect split/reshape stats, even without -debug-only= 2025-06-10 12:31:35 -07:00
Jon Roelofs
44b928e0d5
[Matrix] Propagate shape information through cast insts (#141869) 2025-06-10 12:15:41 -07:00
Jorge Gorbe Moya
bdcfcf6105 Fix unused variable warning after 68bb005ae021cbfaef8fe9fa8b3efcdf0e13e2b3
Two of the newly-added STATISTIC variables are only used within
LLVM_DEBUG blocks.
2025-06-10 11:13:53 -07:00
Jon Roelofs
68bb005ae0
[Matrix] Add -debug-only prints when matrices get flattened (#142078)
This is a potential source of overhead, which we might be able to alleviate in some cases. For example, static element extracts, or shuffles that pluck out a specific row. Since these diagnostics are highly specific to the pass itself and not immediately actionable for compiler users, these prints don't make a whole lot of sense as Remarks.
2025-06-10 09:44:53 -07:00
Jon Roelofs
274f5a817b
[Matrix] Propagate shape information through (f)abs insts (#141704) 2025-06-09 12:52:43 -07:00
Jon Roelofs
7b2ac8ff54
[Matrix] Pass ShapeInfo to Visit* methods (NFC). (#142487)
They all require it now.
2025-06-05 11:22:17 -07:00
Florian Hahn
eb83c43fe9
[Matrix] Don't update Changed based on Visit* return value (NFC). (#142417)
Visit* are always modifying the IR, remove the boolean result.

Depends on https://github.com/llvm/llvm-project/pull/142416.

PR: https://github.com/llvm/llvm-project/pull/142417
2025-06-05 17:54:06 +01:00
Florian Hahn
370d01765c
[Matrix] Use shape info for StoreInst directly. (#142664)
ShapeInfo for the store operand may be dropped, e.g. because the operand
got folded by transpose optimizations to another instruction w/o shape
info. This was exposed by the assertion added in
https://github.com/llvm/llvm-project/pull/142416.

This updates VisitStore to use the shape-info directly from the
instruction, which is in line with the other Visit* functions and
ensures that we won't lose shape info.

PR: https://github.com/llvm/llvm-project/pull/142664
2025-06-04 09:15:57 +01:00
Florian Hahn
adba40e188
[Matrix] Assert that there's shapeinfo in Visit* (NFC). (#142416)
We should only call Visit* for instructions with shape info. Turn early
exit into assert.

PR: https://github.com/llvm/llvm-project/pull/142416
2025-06-02 18:18:42 +01:00
Jon Roelofs
5c28af4099
[Matrix] Use FixedVectorType everywhere in LowerMatrixIntrinsics. NFC (#142316)
These matrix ops do not support scalable vectors, so we should be really
explicit about that and avoid casting mistakes.
2025-06-02 07:45:44 -07:00
Jon Roelofs
1f1c725b68
[Matrix] Propagate shape information through all binops (#141705)
They all have vector variants, so the obvious "find and replace" does
the trick.
2025-05-28 11:20:06 -07:00
Kazu Hirata
0153b49f32
[Scalar] Avoid creating temporary instances of std::string (NFC) (#140327)
ExprLinearizer::write takes StringRef and immediately sends the
content to the stream without hanging onto the pointer, so we do not
need to create temporary instances of std::string.
2025-05-16 23:52:03 -07:00
Kazu Hirata
2d287f51ef
[llvm] Use *(Set|Map)::contains (NFC) (#138431) 2025-05-03 21:55:36 -07:00
Matt Arsenault
bdc523f31f
LowerMatrixIntrinsics: Use use_empty instead of hasNUses(0) (#137334) 2025-04-25 17:21:40 +02:00
Kazu Hirata
e555ccaa4d
[llvm] Call *Map::erase directly (NFC) (#135545) 2025-04-13 12:04:40 -07:00
Jon Roelofs
e838b8b7e7
[Matrix] Fix a miscompile due to an incorrect double-transpose fold (#135397)
Transposes are only inverses of each other when they have matching
shapes.

rdar://145592582
2025-04-12 07:31:13 -07:00
Florian Hahn
48441cb8a2
[Matrix] Properly set Changed status when optimizing transposes.
Currently Changed is not updated properly when transposes are optimized,
causing missing analysis invalidation. Update optimizeTransposes to
indicate if changes have been made.
2025-04-06 17:36:56 +01:00
Kazu Hirata
fae34938f6
[llvm] Use *Set::insert_range (NFC) (#132591)
DenseSet, SmallPtrSet, SmallSet, SetVector, and StringSet recently
gained C++23-style insert_range.  This patch uses insert_range with
iterator ranges.  For each case, I've verified that foos is defined as
make_range(foo_begin(), foo_end()) or in a similar manner.
2025-03-22 22:14:45 -07:00
Jeremy Morse
8e70273509
[NFC][DebugInfo] Use iterator moveBefore at many call-sites (#123583)
As part of the "RemoveDIs" project, BasicBlock::iterator now carries a
debug-info bit that's needed when getFirstNonPHI and similar feed into
instruction insertion positions. Call-sites where that's necessary were
updated a year ago; but to ensure some type safety however, we'd like to
have all calls to moveBefore use iterators.

This patch adds a (guaranteed dereferenceable) iterator-taking
moveBefore, and changes a bunch of call-sites where it's obviously safe
to change to use it by just calling getIterator() on an instruction
pointer. A follow-up patch will contain less-obviously-safe changes.

We'll eventually deprecate and remove the instruction-pointer
insertBefore, but not before adding concise documentation of what
considerations are needed (very few).
2025-01-24 10:53:11 +00:00
Florian Hahn
ffb1c21bd4
[Matrix] Fix crash in liftTranspose when instructions are folded.
Builder.Create(F)Add may constant fold the inputs, return a constant
instead of an instruction. Account for that instead of crashing.
2024-12-05 12:57:54 +00:00
Florian Hahn
7b6e0d9fc3
[Matrix] Use DenseMap for ShapeMap instead of ValueMap. (#118282)
ValueMap automatically updates entries with the new value if they have
been RAUW. This can lead to instructions that are expected to not have
shape info to be added to the map (e.g. shufflevector as in the added
test case).

This leads to incorrect results. Originally it was used for transpose
optimizations, but they now all use updateShapeAndReplaceAllUsesWith,
which takes care of updating the shape info as needed.

This fixes a crash in the newly added test cases.

PR: https://github.com/llvm/llvm-project/pull/118282
2024-12-04 14:51:31 +00:00
Florian Hahn
f8ce2e4bb3
[Matrix] Only retrieve analyses if there are any matrix intrinsics (NFC)
Only request analyses if there are any matrix intrinics to avoid
computing them if there are no matrix intrinsics.
2024-12-02 11:22:24 +00:00
Florian Hahn
12cefcc7ec
[Matrix] Skip already fused instructions before trying to fuse multiply.
lowerDotProduct called above may already lower a matrix multiply and
mark it as procssed by adding it to FusedInsts. Don't try to process it
again in LowerMatrixMultiplyFused by checking if FusedInsts.

Without this change, we trigger an assertion when trying to erase the
same original matrix multiply twice.
2024-11-28 16:11:40 +00:00
Kazu Hirata
eaa7b38536
[Transforms] Avoid repeated hash lookups (NFC) (#113120) 2024-10-21 10:36:19 -07:00
Jay Foad
85c17e4092
[LLVM] Make more use of IRBuilder::CreateIntrinsic. NFC. (#112706)
Convert many instances of:
  Fn = Intrinsic::getOrInsertDeclaration(...);
  CreateCall(Fn, ...)
to the equivalent CreateIntrinsic call.
2024-10-17 16:20:43 +01:00
Kazu Hirata
d1620c1d8c
[Scalar] Simplify code with *Map::operator[] (NFC) (#112120) 2024-10-13 07:34:47 -07:00
Rahul Joshi
fa789dffb1
[NFC] Rename Intrinsic::getDeclaration to getOrInsertDeclaration (#111752)
Rename the function to reflect its correct behavior and to be consistent
with `Module::getOrInsertFunction`. This is also in preparation of
adding a new `Intrinsic::getDeclaration` that will have behavior similar
to `Module::getFunction` (i.e, just lookup, no creation).
2024-10-11 05:26:03 -07:00
Youngsuk Kim
d31e314131 [llvm] Don't call raw_string_ostream::flush() (NFC)
Don't call raw_string_ostream::flush(), which is essentially a no-op.
As specified in the docs, raw_string_ostream is always unbuffered.
( 65b13610a5226b84889b923bae884ba395ad084d for further reference )
2024-09-20 12:19:59 -05:00
Jay Foad
e03f427196
[LLVM] Use {} instead of std::nullopt to initialize empty ArrayRef (#109133)
It is almost always simpler to use {} instead of std::nullopt to
initialize an empty ArrayRef. This patch changes all occurrences I could
find in LLVM itself. In future the ArrayRef(std::nullopt_t) constructor
could be deprecated or removed.
2024-09-19 16:16:38 +01:00
Kazu Hirata
b7146aed5b
[Transforms] Construct SmallVector with ArrayRef (NFC) (#101851) 2024-08-03 15:33:08 -07:00
Nikita Popov
9df71d7673
[IR] Add getDataLayout() helpers to Function and GlobalValue (#96919)
Similar to https://github.com/llvm/llvm-project/pull/96902, this adds
`getDataLayout()` helpers to Function and GlobalValue, replacing the
current `getParent()->getDataLayout()` pattern.
2024-06-28 08:36:49 +02:00
Nikita Popov
2d209d964a
[IR] Add getDataLayout() helpers to BasicBlock and Instruction (#96902)
This is a helper to avoid writing `getModule()->getDataLayout()`. I
regularly try to use this method only to remember it doesn't exist...

`getModule()->getDataLayout()` is also a common (the most common?)
reason why code has to include the Module.h header.
2024-06-27 16:38:15 +02:00
Stephen Tozer
d75f9dd1d2 Revert "[IR][NFC] Update IRBuilder to use InsertPosition (#96497)"
Reverts the above commit, as it updates a common header function and
did not update all callsites:

  https://lab.llvm.org/buildbot/#/builders/29/builds/382

This reverts commit 6481dc57612671ebe77fe9c34214fba94e1b3b27.
2024-06-24 18:00:22 +01:00
Stephen Tozer
6481dc5761
[IR][NFC] Update IRBuilder to use InsertPosition (#96497)
Uses the new InsertPosition class (added in #94226) to simplify some of
the IRBuilder interface, and removes the need to pass a BasicBlock
alongside a BasicBlock::iterator, using the fact that we can now get the
parent basic block from the iterator even if it points to the sentinel.
This patch removes the BasicBlock argument from each constructor or call
to setInsertPoint.

This has no functional effect, but later on as we look to remove the
`Instruction *InsertBefore` argument from instruction-creation
(discussed
[here](https://discourse.llvm.org/t/psa-instruction-constructors-changing-to-iterator-only-insertion/77845)),
this will simplify the process by allowing us to deprecate the
InsertPosition constructor directly and catch all the cases where we use
instructions rather than iterators.
2024-06-24 17:27:43 +01:00
Florian Hahn
e77378cc14
[Matrix] Adjust lifetime.ends during multiply fusion. (#84914)
At the moment, loads introduced by multiply fusion may be placed after
an objects lifetime has been terminated by lifetime.end. This introduces
reads to dead objects.

To avoid this, first collect all lifetime.end calls in the function.
During fusion, we deal with any lifetime.end calls that may alias any of
the loads.

Such lifetime.end calls are either moved when possible (both the
lifetime.end and the store are in the same block) or deleted.

PR: https://github.com/llvm/llvm-project/pull/84914
2024-03-16 20:41:36 +01:00
Florian Hahn
dbe4143f23
[Matrix] Fix dimensions when hoisting transpose across add. (#81507)
Row and column arguments for matrix_transpose indicate the shape of the
operand. When hoisting the transpose to the result of the add, the add
operates on the original operand's shape, and so does the hoisted
transpose.

This patch also adds an assert that the shape for the original add and
the transpose match, as well as the shape of the new add matches the
cached shape for it.

The assert could potentially be moved to
updateShapeAndReplaceAllUsesWith.
2024-02-12 18:45:13 +00:00
Florian Hahn
70caa316e9
[Matrix] Refactor shape info computation (NFCI).
Factor our forward shape computation for a given instruction. This
allows re-use in a follow-up fix.
2024-02-12 16:02:59 +00:00
Florian Hahn
f89fe08d77
[Matrix] Convert column-vector ops feeding dot product to row-vectors. (#72647)
Generalize the logic used to convert column-vector ops to row-vectors to
support converting chains of operations.

A potential next step is to further generalize this to convert
column-vector ops to row-vector ops in general, not just for operands of
dot products. Dot-product handling would then be driven by the general
conversion, rather than the other way around.

PR: https://github.com/llvm/llvm-project/pull/72647
2024-02-06 13:47:31 +00:00
Kazu Hirata
22b0f7ba6e [Transforms] Include llvm/ADT/SmallSet.h (NFC)
This patch adds #include "llvm/ADT/SmallSet.h" to a couple of files
that are relying on transitive includes of SmallSet.h.  It in turn
unblocks the removal of unnecessary includes of llvm/ADT/SmallSet.h in
several other files.
2023-11-11 12:25:39 -08:00