LLVM 24.0.0git
SandboxVectorizer.cpp
Go to the documentation of this file.
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
10#include "../VectorizeOptions.h"
12#include "llvm/IR/Module.h"
14#include "llvm/Support/Regex.h"
17
18using namespace llvm;
19
20/// A magic string for the default pass pipeline.
21static const char *DefaultPipelineMagicStr = "*";
22
23static constexpr char AllowFilesDelim = ',';
24
26
27void SandboxVectorizerPass::buildPipeline() {
28 if (Opts->sbvec_passes == DefaultPipelineMagicStr) {
29 // TODO: Add passes to the default pipeline. It currently contains:
30 // - Seed collection, which creates seed regions and runs the pipeline
31 // - Bundle Vectorizer pass that starts from a seed
32 // - Load-Store Vectorizer pass for load-store chains
33 // - Accept or revert IR state pass
35 "seed-collection<tr-save,bundle-vec(bottom-up),load-store-vec,"
36 "tr-accept-or-revert>",
38 } else {
39 // Create the user-defined pipeline.
41 Opts->sbvec_passes,
43 }
44}
45
47 default;
48
50
53 TTI = &AM.getResult<TargetIRAnalysis>(F);
54 AA = &AM.getResult<AAManager>(F);
56
57 bool Changed = runImpl(F);
58 if (!Changed)
60
63 return PA;
64}
65
66bool SandboxVectorizerPass::allowFile(const std::string &SrcFilePath) {
67 // Iterate over all files in AllowFiles separated by `AllowFilesDelim`.
68 size_t DelimPos = 0;
69 do {
70 size_t LastPos = DelimPos != 0 ? DelimPos + 1 : DelimPos;
71 DelimPos = Opts->sbvec_allow_files.find(AllowFilesDelim, LastPos);
72 auto FileNameToMatch =
73 Opts->sbvec_allow_files.substr(LastPos, DelimPos - LastPos);
74 if (FileNameToMatch.empty())
75 return false;
76 // Note: This only runs when debugging so its OK not to reuse the regex.
77 Regex FileNameRegex((".*" + FileNameToMatch + "$").str());
78 assert(FileNameRegex.isValid() && "Bad regex!");
79 if (FileNameRegex.match(SrcFilePath))
80 return true;
81 } while (DelimPos != std::string::npos);
82 return false;
83}
84
85bool SandboxVectorizerPass::runImpl(Function &LLVMF) {
86 Opts = &LLVMF.getContext().getLibraryOptions().get<VectorizeOptions>();
87 if (Ctx == nullptr) {
88 Ctx = std::make_unique<sandboxir::Context>(LLVMF.getContext());
89 buildPipeline();
90 }
91
92 if (Opts->sbvec_print_pass_pipeline) {
93 FPM.printPipeline(outs());
94 return false;
95 }
96
97 // This is used for debugging.
98 if (LLVM_UNLIKELY(Opts->sbvec_allow_files != ".*")) {
99 const auto &SrcFilePath = LLVMF.getParent()->getSourceFileName();
100 if (!allowFile(SrcFilePath))
101 return false;
102 }
103
104 // If the target claims to have no vector registers early return.
105 if (!TTI->getNumberOfRegisters(TTI->getRegisterClassForType(true))) {
107 << "Target has no vector registers, return.\n");
108 return false;
109 }
110 LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Analyzing " << LLVMF.getName()
111 << ".\n");
112 // Early return if the attribute NoImplicitFloat is used.
113 if (LLVMF.hasFnAttribute(Attribute::NoImplicitFloat)) {
115 << "NoImplicitFloat attribute, return.\n");
116 return false;
117 }
118
119 // Create SandboxIR for LLVMF and run BundleVec on it.
120 sandboxir::Function &F = *Ctx->createFunction(&LLVMF);
121 sandboxir::Analyses A(*AA, *SE, *TTI);
122 bool Change = FPM.runOnFunction(F, A);
123 // Given that sandboxir::Context `Ctx` is defined at a pass-level scope, the
124 // maps from LLVM IR to Sandbox IR may go stale as later passes remove LLVM IR
125 // objects. To avoid issues caused by this clear the context's state.
126 // NOTE: The alternative would be to define Ctx and FPM within runOnFunction()
127 Ctx->clear();
128 return Change;
129}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:352
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
static constexpr char AllowFilesDelim
static const char * DefaultPipelineMagicStr
A magic string for the default pass pipeline.
#define LLVM_DEBUG(...)
Definition Debug.h:119
This pass exposes codegen information to IR-level passes.
#define DEBUG_PREFIX
Definition Debug.h:19
A manager for alias analyses.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:356
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:734
Module * getParent()
Get the module that this global value is contained inside of...
LLVM_ABI cl::LibraryOptionsContext & getLibraryOptions() const
The library options this context reads in place of their process-wide values.
const std::string & getSourceFileName() const
Get the module's original source file name.
Definition Module.h:310
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Analysis pass that exposes the ScalarEvolution for a function.
Analysis pass providing the TargetTransformInfo.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
void setPassPipeline(StringRef Pipeline, CreatePassFunc CreatePass)
Parses Pipeline as a comma-separated sequence of pass names and sets the pass pipeline,...
Definition PassManager.h:98
static LLVM_ABI std::unique_ptr< FunctionPass > createFunctionPass(StringRef Name, StringRef Args, StringRef AuxArg)
Changed
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.