xref: /llvm-project/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp (revision eb9f4756bc3daaa4b19f4f46521dc05180814de4)
1 //===- BottomUpVec.cpp - A bottom-up vectorizer 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/BottomUpVec.h"
10 
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/SandboxIR/Function.h"
13 #include "llvm/SandboxIR/Instruction.h"
14 #include "llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizerPassBuilder.h"
15 
16 namespace llvm::sandboxir {
17 
18 BottomUpVec::BottomUpVec(StringRef Pipeline)
19     : FunctionPass("bottom-up-vec"),
20       RPM("rpm", Pipeline, SandboxVectorizerPassBuilder::createRegionPass) {}
21 
22 // TODO: This is a temporary function that returns some seeds.
23 //       Replace this with SeedCollector's function when it lands.
24 static llvm::SmallVector<Value *, 4> collectSeeds(BasicBlock &BB) {
25   llvm::SmallVector<Value *, 4> Seeds;
26   for (auto &I : BB)
27     if (auto *SI = llvm::dyn_cast<StoreInst>(&I))
28       Seeds.push_back(SI);
29   return Seeds;
30 }
31 
32 static SmallVector<Value *, 4> getOperand(ArrayRef<Value *> Bndl,
33                                           unsigned OpIdx) {
34   SmallVector<Value *, 4> Operands;
35   for (Value *BndlV : Bndl) {
36     auto *BndlI = cast<Instruction>(BndlV);
37     Operands.push_back(BndlI->getOperand(OpIdx));
38   }
39   return Operands;
40 }
41 
42 void BottomUpVec::vectorizeRec(ArrayRef<Value *> Bndl) {
43   auto LegalityRes = Legality.canVectorize(Bndl);
44   switch (LegalityRes.getSubclassID()) {
45   case LegalityResultID::Widen: {
46     auto *I = cast<Instruction>(Bndl[0]);
47     for (auto OpIdx : seq<unsigned>(I->getNumOperands())) {
48       auto OperandBndl = getOperand(Bndl, OpIdx);
49       vectorizeRec(OperandBndl);
50     }
51     break;
52   }
53   case LegalityResultID::Pack: {
54     // TODO: Unimplemented
55     llvm_unreachable("Unimplemented");
56   }
57   }
58 }
59 
60 void BottomUpVec::tryVectorize(ArrayRef<Value *> Bndl) { vectorizeRec(Bndl); }
61 
62 bool BottomUpVec::runOnFunction(Function &F) {
63   Change = false;
64   // TODO: Start from innermost BBs first
65   for (auto &BB : F) {
66     // TODO: Replace with proper SeedCollector function.
67     auto Seeds = collectSeeds(BB);
68     // TODO: Slice Seeds into smaller chunks.
69     // TODO: If vectorization succeeds, run the RegionPassManager on the
70     // resulting region.
71     if (Seeds.size() >= 2)
72       tryVectorize(Seeds);
73   }
74   return Change;
75 }
76 
77 } // namespace llvm::sandboxir
78