xref: /llvm-project/llvm/lib/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.cpp (revision 2e8ad49e7cffb22a169c22d02607708c71a80c65)
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/Constant.h"
12 #include "llvm/Support/CommandLine.h"
13 #include "llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizerPassBuilder.h"
14 
15 using namespace llvm;
16 
17 #define SV_NAME "sandbox-vectorizer"
18 #define DEBUG_TYPE SV_NAME
19 
20 static cl::opt<bool>
21     PrintPassPipeline("sbvec-print-pass-pipeline", cl::init(false), cl::Hidden,
22                       cl::desc("Prints the pass pipeline and returns."));
23 
24 /// A magic string for the default pass pipeline.
25 static const char *DefaultPipelineMagicStr = "*";
26 
27 static cl::opt<std::string> UserDefinedPassPipeline(
28     "sbvec-passes", cl::init(DefaultPipelineMagicStr), cl::Hidden,
29     cl::desc("Comma-separated list of vectorizer passes. If not set "
30              "we run the predefined pipeline."));
31 
32 SandboxVectorizerPass::SandboxVectorizerPass() : FPM("fpm") {
33   if (UserDefinedPassPipeline == DefaultPipelineMagicStr) {
34     // TODO: Add region passes to the default pipeline.
35     FPM.setPassPipeline(
36         "bottom-up-vec<>",
37         sandboxir::SandboxVectorizerPassBuilder::createFunctionPass);
38   } else {
39     // Create the user-defined pipeline.
40     FPM.setPassPipeline(
41         UserDefinedPassPipeline,
42         sandboxir::SandboxVectorizerPassBuilder::createFunctionPass);
43   }
44 }
45 
46 SandboxVectorizerPass::SandboxVectorizerPass(SandboxVectorizerPass &&) =
47     default;
48 
49 SandboxVectorizerPass::~SandboxVectorizerPass() = default;
50 
51 PreservedAnalyses SandboxVectorizerPass::run(Function &F,
52                                              FunctionAnalysisManager &AM) {
53   TTI = &AM.getResult<TargetIRAnalysis>(F);
54 
55   bool Changed = runImpl(F);
56   if (!Changed)
57     return PreservedAnalyses::all();
58 
59   PreservedAnalyses PA;
60   PA.preserveSet<CFGAnalyses>();
61   return PA;
62 }
63 
64 bool SandboxVectorizerPass::runImpl(Function &LLVMF) {
65   if (PrintPassPipeline) {
66     FPM.printPipeline(outs());
67     return false;
68   }
69 
70   // If the target claims to have no vector registers early return.
71   if (!TTI->getNumberOfRegisters(TTI->getRegisterClassForType(true))) {
72     LLVM_DEBUG(dbgs() << "SBVec: Target has no vector registers, return.\n");
73     return false;
74   }
75   LLVM_DEBUG(dbgs() << "SBVec: Analyzing " << LLVMF.getName() << ".\n");
76   // Early return if the attribute NoImplicitFloat is used.
77   if (LLVMF.hasFnAttribute(Attribute::NoImplicitFloat)) {
78     LLVM_DEBUG(dbgs() << "SBVec: NoImplicitFloat attribute, return.\n");
79     return false;
80   }
81 
82   // Create SandboxIR for LLVMF and run BottomUpVec on it.
83   sandboxir::Context Ctx(LLVMF.getContext());
84   sandboxir::Function &F = *Ctx.createFunction(&LLVMF);
85   return FPM.runOnFunction(F);
86 }
87