132 Commits

Author SHA1 Message Date
vporpo
a1b0b4997e
[SandboxVec][NFC] Replace std::regex with llvm::Regex (#134110) 2025-04-02 13:46:56 -07:00
vporpo
3db5be79d2
[SandboxVec] Add print-region pass (#131019)
This patch implements a simple printing pass for regions. This is meant
to be used in tests and for debugging.
2025-03-29 10:42:51 -07:00
Kazu Hirata
cde58bfc16
[Transforms] Use range constructors of *Set (NFC) (#133203) 2025-03-27 07:51:58 -07:00
Vasileios Porpodas
96be34e595 [SandboxVec][SeedCollector][NFC] Fix typo in banner 2025-03-12 10:53:09 -07:00
vporpo
2fb1f03224
[SandboxVec] Add region-from-bbs helper pass (#130153)
RegionFromBBs is a helper Sandbox IR function pass that builds a region
for each BB. This helps test region passes in isolation without relying
on other parts of the vectorizer, which is especially useful for
stress-testing.
2025-03-08 10:47:19 -08:00
chrisPyr
71f4c7dabe
[NFC]Make file-local cl::opt global variables static (#126486)
#125983
2025-03-03 13:46:33 +07:00
Jie Fu
7cf2f602df [Vectorize] Fix unused variable warnings (NFC)
/llvm-project/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.cpp:24:8: error: unused variable 'CostBefore' [-Werror,-Wunused-variable]
  auto CostBefore = SB.getBeforeCost();
       ^
/llvm-project/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.cpp:25:8: error: unused variable 'CostAfter' [-Werror,-Wunused-variable]
  auto CostAfter = SB.getAfterCost();
       ^
2 errors generated.
2025-03-01 09:39:58 +08:00
vporpo
45d018097c
[SandboxVec][NFC] Add LLVM_DEBUG dumps (#129335)
This patch updates/adds LLVM_DEBUG dumps.
It moves the DEBUG_TYPE into SandboxVectorizer/Debug.h such that it can
be shared across all components of the vectorizer.
2025-02-28 16:10:34 -08:00
vporpo
6ff0f69fec
[SandboxVec][BottomUpVec] Fix vectorization of vector constants (#129290)
This patch fixes the value we generate when we vectorize constants.
2025-02-28 14:37:48 -08:00
vporpo
c7529248cd
[SandboxVec][BottomUpVec] Add -sbvec-stop-bndl flag for debugging (#129132)
This patch adds a helper flag for bisection debugging. This flag
force-stops vectorization after this many bundles have been considered
for vectorization.
Using -sbvec-stop-bndl=0 will not vectorize the code at all.
2025-02-28 11:19:41 -08:00
vporpo
32bcc9f0d3
[SandboxVec] Add option -sbvec-allow-file for bisection debugging (#129127)
This new option lets you specify an allow-list of source files and
disables vectorization if the IR is not in the list. This can be used
for debugging miscompiles.
2025-02-27 14:15:04 -08:00
vporpo
adf0abf354
[SandboxVec][BottomUpVec] Add -sbvec-stop-at flag for debugging (#129097)
When debugging miscompiles we need a way to force-stop the vectorizer
early. This helps figure out which invocation is generating incorrect
code.
2025-02-27 13:33:54 -08:00
vporpo
e2b0d5df84
[SandboxVec][Scheduler] Enforce scheduling SchedBundle instrs back-to-back (#128092)
This patch fixes the behavior of the scheduler by making sure the instrs
that are part of a SchedBundle are scheduled back-to-back.
2025-02-27 10:23:50 -08:00
vporpo
4d92975b5c
[SandboxVec][Scheduler] Don't allow rescheduling of already scheduled (#128050)
This patch implements the check for not allowing re-scheduling of
instructions that have already been scheduled in a scheduling bundle.
Rescheduling should only happen if the instructions were temporarily
scheduled in singleton bundles during a previous call to
`trySchedule()`.
2025-02-20 16:16:34 -08:00
Vasileios Porpodas
2ff80d2448 [SandboxVec][Scheduler] Fix reassignment of SchedBundle to DGNode
When assigning a bundle to a DAG Node that is already assigned to a
SchedBundle we need to remove the node from the old bundle.
2025-02-20 15:28:16 -08:00
vporpo
10b99e97ff
[SandboxVec][BottomUpVec] Separate vectorization decisions from code generation (#127727)
Up until now the generation of vector instructions was taking place
during the top-down post-order traversal of vectorizeRec(). The issue
with this approach is that the vector instructions emitted during the
traversal can be reordered by the scheduler, making it challenging to
place them without breaking the def-before-uses rule.

With this patch we separate the vectorization decisions (done in
`vectorizeRec()`) from the code generation phase (`emitVectors()`). The
vectorization decisions are stored in the `Actions` vector and are used
by `emitVectors()` to drive code generation.
2025-02-20 10:21:25 -08:00
vporpo
0cc7381543
[SandboxVec][Scheduler] Don't insert scheduled instrs into the ready list (#127688)
In a particular scenario (see test) we used to insert scheduled
instructions into the ready list. This patch fixes this by fixing the
trimSchedule() function.
2025-02-18 16:17:46 -08:00
vporpo
0f6c18e8c6
[SandboxVec] Replace hard-coded context save() with transaction-save pass (#127690)
This patch implements a small region pass that saves the context's
state. The patch is now used in the default pipeline to save the context
state instead of the hard-coded call to Context::save().

The concept behind this is that the passes themselves should not have to
do the actual saving/restoring of the IR state, because that would make
it challenging to reorder them in the pipeline. Having separate
save/restore passes makes the transformation passes more composable as
parts of arbitrary pipelines.
2025-02-18 13:34:51 -08:00
vporpo
5ecce45ea2
[SandboxVec] Move seed collection into its own separate pass (#127132)
This patch moves the seed collection logic from the BottomUpVec pass
into a new Sandbox IR Function pass. The new "seed-collection" pass
collects the seeds, builds a region and runs the region pass pipeline.
2025-02-18 11:11:07 -08:00
vporpo
426148b269
[SandboxVec][DAG] Implement DAG maintainance on Instruction removal (#127361)
This patch implements dependency maintenance upon receiveing the
notification that an instruction gets deleted.
2025-02-18 10:59:31 -08:00
vporpo
48c92dda00
[SandboxVec][DAG] Update DAG whenever a Use is set (#127247)
This patch implements automatic DAG updating whenever a Use is set. This
maintains the UnscheduledSuccs counter that the scheduler relies on.
2025-02-14 11:58:31 -08:00
vporpo
1c207f1b6e
[SandboxVec][DAG] Fix DAG when old interval is mem free (#126983)
This patch fixes a bug in `DependencyGraph::extend()` when the old
interval contains no memory instructions. When this is the case we
should do a full dependency scan of the new interval.
2025-02-12 15:06:30 -08:00
vporpo
31cb807537
[SanbdoxVec][BottomUpVec] Fix diamond shuffle with multiple vector inputs (#126965)
When the operand comes from multiple inputs then we need additional
packing code. When the operands are scalar then we can use a single
InsertElementInst. But when the operands are vectors then we need a
chain of ExtractElementInst and InsertElementInst instructions to insert
the vector value into the destination vector. This is what this patch
implements.
2025-02-12 14:33:05 -08:00
Vasileios Porpodas
e75e61728e [SandboxVec] Fix warnings introduced by 7a7f9190d03e 2025-02-12 12:43:24 -08:00
vporpo
7a7f9190d0
[SandboxVec][Legality] Fix mask on diamond reuse with shuffle (#126963)
This patch fixes a bug in the creation of shuffle masks when vectorizing
vectors in case of a diamond reuse with shuffle. The mask needs to
enumerate all elements of a vector, not treat the original vector value
as a single element. That is: if vectorizing two <2 x float> vectors
into a <4 x float> the mask needs to have 4 indices, not just 2.
2025-02-12 12:29:09 -08:00
vporpo
6d7a84d72b
[SandboxVec][Scheduler] Fix top of schedule (#126820)
This patch fixes the way the top-of-schedule variable gets set and
updated. Before this patch it used to get updated whenever we scheduled
a bundle, which is wrong, as the top-of-schedule needs to be maintained
across scheduling attempts.

It should get reset only when we clear the schedule or when we destroy
the current schedule and re-schedule.
2025-02-12 11:52:01 -08:00
vporpo
69b8cf4f06
[SandboxVec][BottomUpVec] Add cost estimation and tr-accept-or-revert pass (#126325)
The TransactionAcceptOrRevert pass is the final pass in the Sandbox
Vectorizer's default pass pipeline. It's job is to check the cost
before/after vectorization and accept or revert the IR to its original
state.

Since we are now starting the transaction in BottomUpVec, tests that run
a custom pipeline need to accept the transaction. This is done with the
help of the TransactionAlwaysAccept pass (tr-accept).
2025-02-08 08:34:18 -08:00
vporpo
a0d86b23c0
[SandboxVec][Scheduler] Notify scheduler about instruction creation (#126141)
This patch implements the vectorizer's callback for getting notified
about new instructions being created. This updates the scheduler state,
which may involve removing dependent instructions from the ready list
and update the "scheduled" flag.
Since we need to remove elements from the ready list, this patch also
implements the `remove()` operation.
2025-02-06 15:45:44 -08:00
vporpo
166b2e8837
[SandboxVec][DAG] Update DAG when a new instruction is created (#126124)
The DAG will now receive a callback whenever a new instruction is
created and will update itself accordingly.
2025-02-06 14:12:03 -08:00
vporpo
788c88e2f6
[SandboxVec][DependencyGraph] Fix dependency node iterators (#125616)
This patch fixes a bug in the dependency node iterators that would
incorrectly not skip nodes that are not in the current DAG. This
resulted in iterators returning nullptr when dereferenced.

The fix is to update the existing "skip" function to not only skip
non-instruction values but also to skip instructions not in the DAG.
2025-02-06 12:30:49 -08:00
vporpo
c5f99e1bd4
[SandboxVec][Legality] Fix legality of SelectInst (#125005)
SelectInsts need special treatment because they are not always
straightforward to vectorize. This patch disables vectorization unless
they are trivially vectorizable.
2025-02-03 17:33:09 -08:00
vporpo
e094c0fa67
[SandboxVec][Legality] Don't vectorize when instructions repeat (#124479)
This patch adds a legality check that checks for repeated instrs in a
bundle and won't vectorize if such pattern is found.
2025-01-29 15:54:15 -08:00
vporpo
79cbad188a
[SandboxVec] Clear Context's state within runOnFunction() (#124842)
`sandboxir::Context` is defined at a pass-level scope with the
`SandboxVectorizerPass` class because the function pass manager `FPM`
object depends on it, and that is in pass-level scope to avoid
recreating the pass pipeline every single time `runOnFunction()` is
called.

This means that the Context's state lives on across function passes. The
problem is twofold:
(i) the LLVM IR to Sandbox IR map can grow very large including objects
from different functions, which is of no use to the vectorizer, as it's
a function-level pass.
(ii) this can result in stale data in the LLVM IR to Sandbox IR object
map, as other passes may delete LLVM IR objects.

To fix both issues this patch introduces a `Context::clear()` function
that clears the `LLVMValueToValueMap`.
2025-01-28 18:28:08 -08:00
vporpo
334a1cdbfa
[SandboxIR] createFunction() should always create a function (#124665)
This patch removes the assertion that checks for an existing function.
If one exists it will remove it and create a new one. This helps remove
a crash when a function declaration object already exists and we are
about to create a SandboxIR object for the definition.
2025-01-27 20:16:30 -08:00
Vasileios Porpodas
1c4341d176 [SandboxVec][DAG] Fix interval check without Node
This patch moves the check of whether a node exists before the check of
whether it is contained in the interval.
2025-01-26 11:54:09 -08:00
Vasileios Porpodas
b178c2d63e [SandboxVec][DAG] Fix trim schedule
Fix trimSchedule by skipping instructions without a DAG Node.
2025-01-25 09:42:14 -08:00
vporpo
5cb2db3b51
[SandboxVec][Scheduler] Forbid crossing BBs (#124369)
This patch updates the scheduler to forbid scheduling across BBs. It
should eventually be able to handle this, but we disable it for now.
2025-01-25 08:19:27 -08:00
vporpo
6409799bdc
[SandboxVec][Legality] Pack from different BBs (#124363)
When the inputs of the pack come from different BBs we need to make sure
we emit the pack instructions at the correct place.
2025-01-24 15:39:37 -08:00
vporpo
ac75d32280
[SandboxVec][VecUtils] Filter out instructions not in BB in VecUtils:getLowest() (#124360)
This patch changes the functionality of `VecUtils::getLowest(Vals, BB)`
such that it filters out any instructions in `Vals` that are not in BB.
This is useful when Vals contains instructions from different BBs,
because in that case we are only interested in one BB.
2025-01-24 14:52:57 -08:00
vporpo
cff7ad56ba
[SandboxVec][Utils] Implement Utils::verifyFunction() (#124356)
This patch implements a wrapper function for the LLVM IR verifier for
functions, and calls it (flag-guarded) within the bottom-up-vectorizer
for finding IR bugs as soon as they happen.
2025-01-24 14:35:20 -08:00
vporpo
4b209c5d87
[SandboxIR][Region] Add cost modeling to the region (#124354)
This patch implements cost modeling for Region. All instructions that
are added or removed get their cost counted in the Scoreboard. This is
used for checking if the region before or after a transformation is more
profitable.
2025-01-24 14:28:55 -08:00
vporpo
b41987beae
[SandboxVec][DAG] Fix MemDGNode chain maintenance when move destination is non-mem (#124227)
This patch fixes a bug in the maintenance of the MemDGNode chain of the
DAG. Whenever we move a memory instruction, the DAG gets notified about
the move and maintains the chain of memory nodes. The bug was that if
the destination of the move was not a memory instruction, then the
memory node's next node would end up pointing to itself.
2025-01-24 13:59:32 -08:00
vporpo
d2234ca163
[SandboxVec][BottomUpVec] Fix packing when PHIs are present (#124206)
Before this patch we might have emitted pack instructions in between PHI
nodes. This patch fixes it by fixing the insert point of the new packs.
2025-01-23 16:29:01 -08:00
vporpo
c7053ac202
[SandboxVec][BottomUpVec] Disable crossing BBs (#124039)
Crossing BBs is not currently supported by the structures of the
vectorizer. This patch fixes instances where this was happening,
including:
- a walk of use-def operands that updates the UnscheduledSuccs counter,
- the dead instruction removal is now done per BB,
- the scheduler, which will reject bundles that cross BBs.
2025-01-23 15:08:13 -08:00
vporpo
8110af75b1
[SandboxVec][BottomUpVec] Fix codegen when packing constants. (#124033)
Before this patch packing a bundle of constants would crash because
`getInsertPointAfterInstrs()` expected instructions. This patch fixes
this.
2025-01-22 16:38:10 -08:00
vporpo
2dc1c95595
[SandboxVec][VecUtils] Implement VecUtils::getLowest() (#124024)
VecUtils::getLowest(Valse) returns the lowest instruction in the BB among Vals.
If the instructions are not in the same BB, or if none of them is an
instruction it returns nullptr.
2025-01-22 16:08:15 -08:00
vporpo
fd087135ef
[SandboxVec][Legality] Diamond reuse multi input (#123426)
This patch implements the diamond pattern where we are vectorizing
toward the top of the diamond from both edges, but the second edge may
use elements from a different vector or just scalar values. This
requires some additional packing code (see lit test).
2025-01-22 15:23:47 -08:00
Vasileios Porpodas
4089314907 [SandboxVec][DAG][NFC] Remove early return in notifyMoveInstr()
It used to early return when destination is same as origin. But it's redundant
because in that case the callback won't get called in the first place.
2025-01-21 18:38:39 -08:00
vporpo
87e4b68195
[SandboxVec][Legality] Implement ShuffleMask (#123404)
This patch implements a helper ShuffleMask data structure that helps
describe shuffles of elements across lanes.
2025-01-17 15:48:24 -08:00
vporpo
d6315afff0
[SandboxVec][InstrMaps] EraseInstr callback (#123256)
This patch hooks up InstrMaps to the Sandbox IR callbacks such that it
gets updated when instructions get erased.
2025-01-17 13:36:42 -08:00