LLVM 17.0.0git
Go to the documentation of this file.
1//===- InjectTLIMAppings.cpp - TLI to VFABI attribute injection ----------===//
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
9// Populates the VFABI attribute with the scalar-to-vector mappings
10// from the TargetLibraryInfo.
15#include "llvm/ADT/Statistic.h"
25using namespace llvm;
27#define DEBUG_TYPE "inject-tli-mappings"
30 "Number of calls in which the mappings have been injected.");
33 "Number of function declarations that have been added.");
35 "Number of `@llvm.compiler.used` operands that have been added.");
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.
42static void addVariantDeclaration(CallInst &CI, const ElementCount &VF,
43 const StringRef VFName) {
44 Module *M = CI.getModule();
46 // Add function declaration.
47 Type *RetTy = ToVectorTy(CI.getType(), VF);
49 for (Value *ArgOperand : CI.args())
50 Tys.push_back(ToVectorTy(ArgOperand->getType(), VF));
52 "VarArg functions are not supported.");
53 FunctionType *FTy = FunctionType::get(RetTy, Tys, /*isVarArg=*/false);
54 Function *VectorF =
55 Function::Create(FTy, Function::ExternalLinkage, VFName, M);
57 ++NumVFDeclAdded;
58 LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Added to the module: `" << VFName
59 << "` of type " << *(VectorF->getType()) << "\n");
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;
71static 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;
80 StringRef ScalarName = CI.getCalledFunction()->getName();
82 // Nothing to be done if the TLI thinks the function is not
83 // vectorizable.
84 if (!TLI.isFunctionVectorizable(ScalarName))
85 return;
88 Module *M = CI.getModule();
89 const SetVector<StringRef> OriginalSetOfMappings(Mappings.begin(),
90 Mappings.end());
92 auto AddVariantDecl = [&](const ElementCount &VF) {
93 const std::string TLIName =
94 std::string(TLI.getVectorizedFunction(ScalarName, VF));
95 if (!TLIName.empty()) {
96 std::string MangledName =
97 VFABI::mangleTLIVectorName(TLIName, ScalarName, CI.arg_size(), 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 };
108 // All VFs in the TLI are powers of 2.
109 ElementCount WidestFixedVF, WidestScalableVF;
110 TLI.getWidestVF(ScalarName, WidestFixedVF, WidestScalableVF);
113 ElementCount::isKnownLE(VF, WidestFixedVF); VF *= 2)
114 AddVariantDecl(VF);
116 // TODO: Add scalable variants once we're able to test them.
117 assert(WidestScalableVF.isZero() &&
118 "Scalable vector mappings not yet supported");
123static bool runImpl(const TargetLibraryInfo &TLI, Function &F) {
124 for (auto &I : instructions(F))
125 if (auto CI = dyn_cast<CallInst>(&I))
126 addMappingsFromTLI(TLI, *CI);
127 // Even if the pass adds IR attributes, the analyses are preserved.
128 return false;
132// New pass manager implementation.
137 runImpl(TLI, F);
138 // Even if the pass adds IR attributes, the analyses are preserved.
139 return PreservedAnalyses::all();
143// Legacy PM Implementation.
146 const TargetLibraryInfo &TLI =
147 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
148 return runImpl(TLI, F);
152 AU.setPreservesCFG();
164// Legacy Pass manager initialization
169 "Inject TLI Mappings", false, false)
175 return new InjectTLIMappingsLegacy();
return RetTy
#define LLVM_DEBUG(X)
Definition: Debug.h:101
static bool runImpl(Function &F, const TargetLowering &TLI)
#define DEBUG_TYPE
This is the interface for a simple mod/ref and alias analysis over globals.
Inject TLI Mappings
static void addMappingsFromTLI(const TargetLibraryInfo &TLI, CallInst &CI)
static void addVariantDeclaration(CallInst &CI, const ElementCount &VF, const StringRef VFName)
A helper function that adds the vector function declaration that vectorizes the CallInst CI with a ve...
static bool runImpl(const TargetLibraryInfo &TLI, Function &F)
#define DEBUG_TYPE
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
print must be executed print the must be executed context for all instructions
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
Definition: Statistic.h:167
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:774
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:265
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
Definition: InstrTypes.h:1858
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1406
FunctionType * getFunctionType() const
Definition: InstrTypes.h:1264
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
Definition: InstrTypes.h:1342
unsigned arg_size() const
Definition: InstrTypes.h:1349
This class represents a function call, abstracting a target machine's calling convention.
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition: TypeSize.h:291
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
bool isVarArg() const
Definition: DerivedTypes.h:123
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:136
size_t size() const
Definition: Function.h:756
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
Definition: Function.cpp:739
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:290
Legacy wrapper pass to provide the GlobalsAAResult object.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:70
This analysis provides dependence information for the memory accesses of a loop.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
OptimizationRemarkEmitter legacy analysis pass.
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
A vector that has set insertion semantics.
Definition: SetVector.h:40
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition: SetVector.h:208
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
StringRef getVectorizedFunction(StringRef F, const ElementCount &VF) const
void getWidestVF(StringRef ScalarF, ElementCount &FixedVF, ElementCount &ScalableVF) const
Returns the largest vectorization factor used in the list of vector functions.
bool isFunctionVectorizable(StringRef F, const ElementCount &VF) const
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:308
constexpr bool isZero() const
Definition: TypeSize.h:151
std::string mangleTLIVectorName(StringRef VectorName, StringRef ScalarName, unsigned numArgs, ElementCount VF)
This routine mangles the given VectorName according to the LangRef specification for vector-function-...
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...
void setVectorVariantNames(CallInst *CI, ArrayRef< std::string > VariantMappings)
Overwrite the Vector Function ABI variants attribute with the names provide in VariantMappings.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FunctionPass * createInjectTLIMappingsLegacyPass()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Type * ToVectorTy(Type *Scalar, ElementCount EC)
A helper function for converting Scalar types to vector types.
Definition: VectorUtils.h:300
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.