LLVM  12.0.0git
InjectTLIMappings.cpp
Go to the documentation of this file.
1 //===- InjectTLIMAppings.cpp - TLI to VFABI attribute injection ----------===//
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 // Populates the VFABI attribute with the scalar-to-vector mappings
10 // from the TargetLibraryInfo.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/Statistic.h"
20 #include "llvm/IR/InstIterator.h"
21 #include "llvm/IR/IntrinsicInst.h"
22 #include "llvm/Transforms/Utils.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "inject-tli-mappings"
28 
29 STATISTIC(NumCallInjected,
30  "Number of calls in which the mappings have been injected.");
31 
32 STATISTIC(NumVFDeclAdded,
33  "Number of function declarations that have been added.");
34 STATISTIC(NumCompUsedAdded,
35  "Number of `@llvm.compiler.used` operands that have been added.");
36 
37 /// A helper function that adds the vector function declaration that
38 /// vectorizes the CallInst CI with a vectorization factor of VF
39 /// lanes. The TLI assumes that all parameters and the return type of
40 /// CI (other than void) need to be widened to a VectorType of VF
41 /// lanes.
42 static void addVariantDeclaration(CallInst &CI, const unsigned VF,
43  const StringRef VFName) {
44  Module *M = CI.getModule();
45 
46  // Add function declaration.
47  Type *RetTy = ToVectorTy(CI.getType(), VF);
49  for (Value *ArgOperand : CI.arg_operands())
50  Tys.push_back(ToVectorTy(ArgOperand->getType(), VF));
51  assert(!CI.getFunctionType()->isVarArg() &&
52  "VarArg functions are not supported.");
53  FunctionType *FTy = FunctionType::get(RetTy, Tys, /*isVarArg=*/false);
54  Function *VectorF =
57  ++NumVFDeclAdded;
58  LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Added to the module: `" << VFName
59  << "` of type " << *(VectorF->getType()) << "\n");
60 
61  // Make function declaration (without a body) "sticky" in the IR by
62  // listing it in the @llvm.compiler.used intrinsic.
63  assert(!VectorF->size() && "VFABI attribute requires `@llvm.compiler.used` "
64  "only on declarations.");
65  appendToCompilerUsed(*M, {VectorF});
66  LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Adding `" << VFName
67  << "` to `@llvm.compiler.used`.\n");
68  ++NumCompUsedAdded;
69 }
70 
71 static void addMappingsFromTLI(const TargetLibraryInfo &TLI, CallInst &CI) {
72  // This is needed to make sure we don't query the TLI for calls to
73  // bitcast of function pointers, like `%call = call i32 (i32*, ...)
74  // bitcast (i32 (...)* @goo to i32 (i32*, ...)*)(i32* nonnull %i)`,
75  // as such calls make the `isFunctionVectorizable` raise an
76  // exception.
77  if (CI.isNoBuiltin() || !CI.getCalledFunction())
78  return;
79 
80  const std::string ScalarName = std::string(CI.getCalledFunction()->getName());
81  // Nothing to be done if the TLI thinks the function is not
82  // vectorizable.
83  if (!TLI.isFunctionVectorizable(ScalarName))
84  return;
86  VFABI::getVectorVariantNames(CI, Mappings);
87  Module *M = CI.getModule();
88  const SetVector<StringRef> OriginalSetOfMappings(Mappings.begin(),
89  Mappings.end());
90  // All VFs in the TLI are powers of 2.
91  for (unsigned VF = 2, WidestVF = TLI.getWidestVF(ScalarName); VF <= WidestVF;
92  VF *= 2) {
93  const std::string TLIName =
94  std::string(TLI.getVectorizedFunction(ScalarName, VF));
95  if (!TLIName.empty()) {
96  std::string MangledName = VFABI::mangleTLIVectorName(
97  TLIName, ScalarName, CI.getNumArgOperands(), VF);
98  if (!OriginalSetOfMappings.count(MangledName)) {
99  Mappings.push_back(MangledName);
100  ++NumCallInjected;
101  }
102  Function *VariantF = M->getFunction(TLIName);
103  if (!VariantF)
104  addVariantDeclaration(CI, VF, TLIName);
105  }
106  }
107 
108  VFABI::setVectorVariantNames(&CI, Mappings);
109 }
110 
111 static bool runImpl(const TargetLibraryInfo &TLI, Function &F) {
112  for (auto &I : instructions(F))
113  if (auto CI = dyn_cast<CallInst>(&I))
114  addMappingsFromTLI(TLI, *CI);
115  // Even if the pass adds IR attributes, the analyses are preserved.
116  return false;
117 }
118 
119 ////////////////////////////////////////////////////////////////////////////////
120 // New pass manager implementation.
121 ////////////////////////////////////////////////////////////////////////////////
125  runImpl(TLI, F);
126  // Even if the pass adds IR attributes, the analyses are preserved.
127  return PreservedAnalyses::all();
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 // Legacy PM Implementation.
132 ////////////////////////////////////////////////////////////////////////////////
134  const TargetLibraryInfo &TLI =
135  getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
136  return runImpl(TLI, F);
137 }
138 
140  AU.setPreservesCFG();
149 }
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 // Legacy Pass manager initialization
153 ////////////////////////////////////////////////////////////////////////////////
155 
157  "Inject TLI Mappings", false, false)
160  false, false)
161 
163  return new InjectTLIMappingsLegacy();
164 }
Legacy wrapper pass to provide the GlobalsAAResult object.
size_t size() const
Definition: Function.h:710
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue *> Values)
Adds global values to the llvm.compiler.used list.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void getVectorVariantNames(const CallInst &CI, SmallVectorImpl< std::string > &VariantMappings)
Populates a set of strings representing the Vector Function ABI variants associated to the CallInst C...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:769
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This is the interface for a simple mod/ref and alias analysis over globals.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:67
void push_back(const T &Elt)
Definition: SmallVector.h:246
This class represents a function call, abstracting a target machine&#39;s calling convention.
Inject TLI Mappings
Externally visible function.
Definition: GlobalValue.h:48
STATISTIC(NumFunctions, "Total number of functions")
F(f)
AnalysisUsage & addRequired()
#define DEBUG_TYPE
Class to represent function types.
Definition: DerivedTypes.h:108
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
Type * ToVectorTy(Type *Scalar, unsigned VF, bool isScalable=false)
A helper function for converting Scalar types to vector types.
Definition: VectorUtils.h:305
bool isVarArg() const
Definition: DerivedTypes.h:128
iterator_range< User::op_iterator > arg_operands()
Definition: InstrTypes.h:1251
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:137
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
Definition: InstrTypes.h:1658
This analysis provides dependence information for the memory accesses of a loop.
static void addMappingsFromTLI(const TargetLibraryInfo &TLI, CallInst &CI)
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
Definition: Function.cpp:556
FunctionType * getFunctionType() const
Definition: InstrTypes.h:1162
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:311
FunctionPass * createInjectTLIMappingsLegacyPass()
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
bool isFunctionVectorizable(StringRef F, unsigned VF) const
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:883
Provides information about what library functions are available for the current target.
INITIALIZE_PASS_BEGIN(InjectTLIMappingsLegacy, DEBUG_TYPE, "Inject TLI Mappings", false, false) INITIALIZE_PASS_END(InjectTLIMappingsLegacy
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:253
StringRef getVectorizedFunction(StringRef F, unsigned VF) const
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:174
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:68
unsigned getNumArgOperands() const
Definition: InstrTypes.h:1257
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:270
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1314
#define I(x, y, z)
Definition: MD5.cpp:59
static void addVariantDeclaration(CallInst &CI, const unsigned VF, const StringRef VFName)
A helper function that adds the vector function declaration that vectorizes the CallInst CI with a ve...
Analysis pass providing the TargetLibraryInfo.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
A vector that has set insertion semantics.
Definition: SetVector.h:40
std::string mangleTLIVectorName(StringRef VectorName, StringRef ScalarName, unsigned numArgs, unsigned VF)
This routine mangles the given VectorName according to the LangRef specification for vector-function-...
OptimizationRemarkEmitter legacy analysis pass.
static bool runImpl(const TargetLibraryInfo &TLI, Function &F)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
inst_range instructions(Function *F)
Definition: InstIterator.h:133
A container for analyses that lazily runs them and caches their results.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
#define LLVM_DEBUG(X)
Definition: Debug.h:122
unsigned getWidestVF(StringRef ScalarF) const
Returns the largest vectorization factor used in the list of vector functions.
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:271
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
void setVectorVariantNames(CallInst *CI, const SmallVector< std::string, 8 > &VariantMappings)
Overwrite the Vector Function ABI variants attribute with the names provide in VariantMappings.