1//===- SeedCollection.cpp - Seed collection pass --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/SeedCollection.h"
10#include "llvm/Analysis/TargetTransformInfo.h"
11#include "llvm/SandboxIR/Module.h"
12#include "llvm/Transforms/Vectorize/SandboxVectorizer/RegionWithScore.h"
13#include "llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizerPassBuilder.h"
14#include "llvm/Transforms/Vectorize/SandboxVectorizer/SeedCollector.h"
15#include "llvm/Transforms/Vectorize/SandboxVectorizer/VecUtils.h"
16
17namespace llvm {
18
19static cl::opt<unsigned>
20 OverrideVecRegBits("sbvec-vec-reg-bits", cl::init(Val: 0), cl::Hidden,
21 cl::desc("Override the vector register size in bits, "
22 "which is otherwise found by querying TTI."));
23static cl::opt<bool>
24 AllowNonPow2("sbvec-allow-non-pow2", cl::init(Val: false), cl::Hidden,
25 cl::desc("Allow non-power-of-2 vectorization."));
26
27#define LoadSeedsDef "loads"
28#define StoreSeedsDef "stores"
29static cl::opt<std::string> CollectSeeds(
30 "sbvec-collect-seeds", cl::init(StoreSeedsDef), cl::Hidden,
31 cl::desc("Collect these seeds. Use empty for none or a comma-separated "
32 "list of '" StoreSeedsDef "' and '" LoadSeedsDef "'."));
33
34namespace sandboxir {
35
36SeedCollection::SeedCollection(StringRef Pipeline, StringRef AuxArg)
37 : FunctionPass("seed-collection"),
38 RPM("rpm", Pipeline, SandboxVectorizerPassBuilder::createRegionPass) {
39 ArgsRegistry.parse(ArgsStr: AuxArg);
40 // If the user has not specified seed types default to stores.
41 if (!CollectStores && !CollectLoads) {
42 CollectStores = true;
43 }
44}
45
46bool SeedCollection::runOnFunction(Function &F, const Analyses &A) {
47 bool Change = false;
48 const auto &DL = F.getParent()->getDataLayout();
49
50 // TODO: Start from innermost BBs first
51 for (auto &BB : F) {
52 SeedCollector SC(&BB, A.getScalarEvolution(), CollectStores, CollectLoads,
53 AllowDiffTypes);
54 for (auto &SeedRange : {SC.getStoreSeeds(), SC.getLoadSeeds()}) {
55 for (SeedBundle &Seeds : SeedRange) {
56 if (Seeds.allUsed())
57 continue;
58 unsigned FirstUnusedIdx = Seeds.getFirstUnusedElementIdx();
59 unsigned ElmBits =
60 Utils::getNumBits(Ty: VecUtils::getElementType(Ty: Utils::getExpectedType(
61 V: Seeds[FirstUnusedIdx])),
62 DL);
63 unsigned AS = getLoadStoreAddressSpace(I: Seeds[FirstUnusedIdx]);
64 unsigned VecRegBits = OverrideVecRegBits != 0
65 ? OverrideVecRegBits
66 : A.getTTI().getLoadStoreVecRegBitWidth(AddrSpace: AS);
67
68 auto DivideBy2 = [](unsigned Num) {
69 auto Floor = VecUtils::getFloorPowerOf2(Num);
70 if (Floor == Num)
71 return Floor / 2;
72 return Floor;
73 };
74 // Try to create the largest vector supported by the target. If it fails
75 // reduce the vector size by half.
76 for (unsigned SliceElms = std::min(a: VecRegBits / ElmBits,
77 b: Seeds.getNumUnusedBits() / ElmBits);
78 SliceElms >= 2u; SliceElms = DivideBy2(SliceElms)) {
79 if (Seeds.allUsed())
80 break;
81 // Keep trying offsets after FirstUnusedElementIdx, until we vectorize
82 // the slice. This could be quite expensive, so we enforce a limit.
83 for (unsigned Offset = Seeds.getFirstUnusedElementIdx(),
84 OE = Seeds.size();
85 Offset + 1 < OE; Offset += 1) {
86 // Seeds are getting used as we vectorize, so skip them.
87 if (Seeds.isUsed(Element: Offset))
88 continue;
89 if (Seeds.allUsed())
90 break;
91
92 auto SeedSlice =
93 Seeds.getSlice(StartIdx: Offset, MaxVecRegBits: SliceElms * ElmBits, ForcePowOf2: !AllowNonPow2);
94 if (SeedSlice.empty())
95 continue;
96
97 assert(SeedSlice.size() >= 2 && "Should have been rejected!");
98
99 // Create a region containing the seed slice.
100 auto &Ctx = F.getContext();
101 RegionWithScore Rgn(Ctx, A.getTTI());
102 Rgn.setAux(SeedSlice);
103 // Run the region pass pipeline.
104 Change |= RPM.runOnRegion(R&: Rgn, A);
105 Rgn.clearAux();
106 }
107 }
108 }
109 }
110 }
111 return Change;
112}
113} // namespace sandboxir
114} // namespace llvm
115