xref: /llvm-project/llvm/lib/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.cpp (revision 39f2d2f156ba8af31a15f6df223d3cb57c7a4906)
1 //===- SandboxVectorizer.cpp - Vectorizer based on Sandbox IR -------------===//
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/SandboxVectorizer.h"
10 #include "llvm/Analysis/TargetTransformInfo.h"
11 #include "llvm/SandboxIR/PassManager.h"
12 #include "llvm/SandboxIR/SandboxIR.h"
13 #include "llvm/Support/CommandLine.h"
14 #include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h"
15 
16 using namespace llvm;
17 
18 #define SV_NAME "sandbox-vectorizer"
19 #define DEBUG_TYPE SV_NAME
20 
21 cl::opt<bool>
22     PrintPassPipeline("sbvec-print-pass-pipeline", cl::init(false), cl::Hidden,
23                       cl::desc("Prints the pass pipeline and returns."));
24 
25 PreservedAnalyses SandboxVectorizerPass::run(Function &F,
26                                              FunctionAnalysisManager &AM) {
27   TTI = &AM.getResult<TargetIRAnalysis>(F);
28 
29   bool Changed = runImpl(F);
30   if (!Changed)
31     return PreservedAnalyses::all();
32 
33   PreservedAnalyses PA;
34   PA.preserveSet<CFGAnalyses>();
35   return PA;
36 }
37 
38 bool SandboxVectorizerPass::runImpl(Function &LLVMF) {
39   // If the target claims to have no vector registers early return.
40   if (!TTI->getNumberOfRegisters(TTI->getRegisterClassForType(true))) {
41     LLVM_DEBUG(dbgs() << "SBVec: Target has no vector registers, return.\n");
42     return false;
43   }
44   LLVM_DEBUG(dbgs() << "SBVec: Analyzing " << LLVMF.getName() << ".\n");
45   // Early return if the attribute NoImplicitFloat is used.
46   if (LLVMF.hasFnAttribute(Attribute::NoImplicitFloat)) {
47     LLVM_DEBUG(dbgs() << "SBVec: NoImplicitFloat attribute, return.\n");
48     return false;
49   }
50 
51   sandboxir::Context Ctx(LLVMF.getContext());
52   // Create SandboxIR for `LLVMF`.
53   sandboxir::Function &F = *Ctx.createFunction(&LLVMF);
54   // Create the passes and register them with the PassRegistry.
55   sandboxir::PassRegistry PR;
56   auto &PM = static_cast<sandboxir::FunctionPassManager &>(
57       PR.registerPass(std::make_unique<sandboxir::FunctionPassManager>("pm")));
58   auto &BottomUpVecPass = static_cast<sandboxir::FunctionPass &>(
59       PR.registerPass(std::make_unique<sandboxir::BottomUpVec>()));
60 
61   // Create the default pass pipeline.
62   PM.addPass(&BottomUpVecPass);
63 
64   if (PrintPassPipeline) {
65     PM.printPipeline(outs());
66     return false;
67   }
68 
69   // Run the pass pipeline.
70   bool Change = PM.runOnFunction(F);
71   return Change;
72 }
73