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/IR/Module.h"
12#include "llvm/SandboxIR/Constant.h"
13#include "llvm/Support/CommandLine.h"
14#include "llvm/Support/Regex.h"
15#include "llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h"
16#include "llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizerPassBuilder.h"
17
18using namespace llvm;
19
20static cl::opt<bool>
21 PrintPassPipeline("sbvec-print-pass-pipeline", cl::init(Val: false), cl::Hidden,
22 cl::desc("Prints the pass pipeline and returns."));
23
24/// A magic string for the default pass pipeline.
25static const char *DefaultPipelineMagicStr = "*";
26
27static cl::opt<std::string> UserDefinedPassPipeline(
28 "sbvec-passes", cl::init(Val: DefaultPipelineMagicStr), cl::Hidden,
29 cl::desc("Comma-separated list of vectorizer passes. If not set "
30 "we run the predefined pipeline."));
31
32// This option is useful for bisection debugging.
33// For example you may use it to figure out which filename is the one causing a
34// miscompile. You can specify a regex for the filename like: "/[a-m][^/]*"
35// which will enable any file name starting with 'a' to 'm' and disable the
36// rest. If the miscompile goes away, then we try "/[n-z][^/]*" for the other
37// half of the range, from 'n' to 'z'. If we can reproduce the miscompile then
38// we can keep looking in [n-r] and [s-z] and so on, in a binary-search fashion.
39//
40// Please note that we are using [^/]* and not .* to make sure that we are
41// matching the actual filename and not some other directory in the path.
42cl::opt<std::string> AllowFiles(
43 "sbvec-allow-files", cl::init(Val: ".*"), cl::Hidden,
44 cl::desc("Run the vectorizer only on file paths that match any in the "
45 "list of comma-separated regex's."));
46static constexpr char AllowFilesDelim = ',';
47
48SandboxVectorizerPass::SandboxVectorizerPass() : FPM("fpm") {
49 if (UserDefinedPassPipeline == DefaultPipelineMagicStr) {
50 // TODO: Add passes to the default pipeline. It currently contains:
51 // - Seed collection, which creates seed regions and runs the pipeline
52 // - Bottom-up Vectorizer pass that starts from a seed
53 // - Load-Store Vectorizer pass for load-store chains
54 // - Accept or revert IR state pass
55 FPM.setPassPipeline(
56 Pipeline: "seed-collection<tr-save,bottom-up-vec,load-store-vec,tr-accept-or-"
57 "revert>",
58 CreatePass: sandboxir::SandboxVectorizerPassBuilder::createFunctionPass);
59 } else {
60 // Create the user-defined pipeline.
61 FPM.setPassPipeline(
62 Pipeline: UserDefinedPassPipeline,
63 CreatePass: sandboxir::SandboxVectorizerPassBuilder::createFunctionPass);
64 }
65}
66
67SandboxVectorizerPass::SandboxVectorizerPass(SandboxVectorizerPass &&) =
68 default;
69
70SandboxVectorizerPass::~SandboxVectorizerPass() = default;
71
72PreservedAnalyses SandboxVectorizerPass::run(Function &F,
73 FunctionAnalysisManager &AM) {
74 TTI = &AM.getResult<TargetIRAnalysis>(IR&: F);
75 AA = &AM.getResult<AAManager>(IR&: F);
76 SE = &AM.getResult<ScalarEvolutionAnalysis>(IR&: F);
77
78 bool Changed = runImpl(F);
79 if (!Changed)
80 return PreservedAnalyses::all();
81
82 PreservedAnalyses PA;
83 PA.preserveSet<CFGAnalyses>();
84 return PA;
85}
86
87bool SandboxVectorizerPass::allowFile(const std::string &SrcFilePath) {
88 // Iterate over all files in AllowFiles separated by `AllowFilesDelim`.
89 size_t DelimPos = 0;
90 do {
91 size_t LastPos = DelimPos != 0 ? DelimPos + 1 : DelimPos;
92 DelimPos = AllowFiles.find(c: AllowFilesDelim, pos: LastPos);
93 auto FileNameToMatch = AllowFiles.substr(pos: LastPos, n: DelimPos - LastPos);
94 if (FileNameToMatch.empty())
95 return false;
96 // Note: This only runs when debugging so its OK not to reuse the regex.
97 Regex FileNameRegex(".*" + FileNameToMatch + "$");
98 assert(FileNameRegex.isValid() && "Bad regex!");
99 if (FileNameRegex.match(String: SrcFilePath))
100 return true;
101 } while (DelimPos != std::string::npos);
102 return false;
103}
104
105bool SandboxVectorizerPass::runImpl(Function &LLVMF) {
106 if (Ctx == nullptr)
107 Ctx = std::make_unique<sandboxir::Context>(args&: LLVMF.getContext());
108
109 if (PrintPassPipeline) {
110 FPM.printPipeline(OS&: outs());
111 return false;
112 }
113
114 // This is used for debugging.
115 if (LLVM_UNLIKELY(AllowFiles != ".*")) {
116 const auto &SrcFilePath = LLVMF.getParent()->getSourceFileName();
117 if (!allowFile(SrcFilePath))
118 return false;
119 }
120
121 // If the target claims to have no vector registers early return.
122 if (!TTI->getNumberOfRegisters(ClassID: TTI->getRegisterClassForType(Vector: true))) {
123 LLVM_DEBUG(dbgs() << DEBUG_PREFIX
124 << "Target has no vector registers, return.\n");
125 return false;
126 }
127 LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Analyzing " << LLVMF.getName()
128 << ".\n");
129 // Early return if the attribute NoImplicitFloat is used.
130 if (LLVMF.hasFnAttribute(Kind: Attribute::NoImplicitFloat)) {
131 LLVM_DEBUG(dbgs() << DEBUG_PREFIX
132 << "NoImplicitFloat attribute, return.\n");
133 return false;
134 }
135
136 // Create SandboxIR for LLVMF and run BottomUpVec on it.
137 sandboxir::Function &F = *Ctx->createFunction(F: &LLVMF);
138 sandboxir::Analyses A(*AA, *SE, *TTI);
139 bool Change = FPM.runOnFunction(F, A);
140 // Given that sandboxir::Context `Ctx` is defined at a pass-level scope, the
141 // maps from LLVM IR to Sandbox IR may go stale as later passes remove LLVM IR
142 // objects. To avoid issues caused by this clear the context's state.
143 // NOTE: The alternative would be to define Ctx and FPM within runOnFunction()
144 Ctx->clear();
145 return Change;
146}
147