LLVM 17.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"
24
25using namespace llvm;
26
27#define DEBUG_TYPE "inject-tli-mappings"
28
29STATISTIC(NumCallInjected,
30 "Number of calls in which the mappings have been injected.");
31
32STATISTIC(NumVFDeclAdded,
33 "Number of function declarations that have been added.");
34STATISTIC(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.
42static void addVariantDeclaration(CallInst &CI, const ElementCount &VF,
43 bool Predicate, 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.args())
50 Tys.push_back(ToVectorTy(ArgOperand->getType(), VF));
52 "VarArg functions are not supported.");
53 if (Predicate)
54 Tys.push_back(ToVectorTy(Type::getInt1Ty(RetTy->getContext()), VF));
55 FunctionType *FTy = FunctionType::get(RetTy, Tys, /*isVarArg=*/false);
56 Function *VectorF =
57 Function::Create(FTy, Function::ExternalLinkage, VFName, M);
59 ++NumVFDeclAdded;
60 LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Added to the module: `" << VFName
61 << "` of type " << *(VectorF->getType()) << "\n");
62
63 // Make function declaration (without a body) "sticky" in the IR by
64 // listing it in the @llvm.compiler.used intrinsic.
65 assert(!VectorF->size() && "VFABI attribute requires `@llvm.compiler.used` "
66 "only on declarations.");
67 appendToCompilerUsed(*M, {VectorF});
68 LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Adding `" << VFName
69 << "` to `@llvm.compiler.used`.\n");
70 ++NumCompUsedAdded;
71}
72
73static void addMappingsFromTLI(const TargetLibraryInfo &TLI, CallInst &CI) {
74 // This is needed to make sure we don't query the TLI for calls to
75 // bitcast of function pointers, like `%call = call i32 (i32*, ...)
76 // bitcast (i32 (...)* @goo to i32 (i32*, ...)*)(i32* nonnull %i)`,
77 // as such calls make the `isFunctionVectorizable` raise an
78 // exception.
79 if (CI.isNoBuiltin() || !CI.getCalledFunction())
80 return;
81
82 StringRef ScalarName = CI.getCalledFunction()->getName();
83
84 // Nothing to be done if the TLI thinks the function is not
85 // vectorizable.
86 if (!TLI.isFunctionVectorizable(ScalarName))
87 return;
90 Module *M = CI.getModule();
91 const SetVector<StringRef> OriginalSetOfMappings(Mappings.begin(),
92 Mappings.end());
93
94 auto AddVariantDecl = [&](const ElementCount &VF, bool Predicate) {
95 const std::string TLIName =
96 std::string(TLI.getVectorizedFunction(ScalarName, VF, Predicate));
97 if (!TLIName.empty()) {
98 std::string MangledName = VFABI::mangleTLIVectorName(
99 TLIName, ScalarName, CI.arg_size(), VF, Predicate);
100 if (!OriginalSetOfMappings.count(MangledName)) {
101 Mappings.push_back(MangledName);
102 ++NumCallInjected;
103 }
104 Function *VariantF = M->getFunction(TLIName);
105 if (!VariantF)
106 addVariantDeclaration(CI, VF, Predicate, TLIName);
107 }
108 };
109
110 // All VFs in the TLI are powers of 2.
111 ElementCount WidestFixedVF, WidestScalableVF;
112 TLI.getWidestVF(ScalarName, WidestFixedVF, WidestScalableVF);
113
114 for (bool Predicated : {false, true}) {
116 ElementCount::isKnownLE(VF, WidestFixedVF); VF *= 2)
117 AddVariantDecl(VF, Predicated);
118
120 ElementCount::isKnownLE(VF, WidestScalableVF); VF *= 2)
121 AddVariantDecl(VF, Predicated);
122 }
123
125}
126
127static bool runImpl(const TargetLibraryInfo &TLI, Function &F) {
128 for (auto &I : instructions(F))
129 if (auto CI = dyn_cast<CallInst>(&I))
130 addMappingsFromTLI(TLI, *CI);
131 // Even if the pass adds IR attributes, the analyses are preserved.
132 return false;
133}
134
135////////////////////////////////////////////////////////////////////////////////
136// New pass manager implementation.
137////////////////////////////////////////////////////////////////////////////////
141 runImpl(TLI, F);
142 // Even if the pass adds IR attributes, the analyses are preserved.
143 return PreservedAnalyses::all();
144}
145
146////////////////////////////////////////////////////////////////////////////////
147// Legacy PM Implementation.
148////////////////////////////////////////////////////////////////////////////////
150 const TargetLibraryInfo &TLI =
151 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
152 return runImpl(TLI, F);
153}
154
156 AU.setPreservesCFG();
165}
166
167////////////////////////////////////////////////////////////////////////////////
168// Legacy Pass manager initialization
169////////////////////////////////////////////////////////////////////////////////
171
173 "Inject TLI Mappings", false, false)
177
179 return new InjectTLIMappingsLegacy();
180}
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, bool Predicate, 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
#define INITIALIZE_PASS_DEPENDENCY(depName)
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...
#define STATISTIC(VARNAME, DESC)
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:1868
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1408
FunctionType * getFunctionType() const
Definition: InstrTypes.h:1266
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
Definition: InstrTypes.h:1344
unsigned arg_size() const
Definition: InstrTypes.h:1351
This class represents a function call, abstracting a target machine's calling convention.
static constexpr ElementCount getScalable(ScalarTy MinVal)
Definition: TypeSize.h:294
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:761
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
Definition: Function.cpp:743
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:51
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition: SetVector.h:219
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.
void getWidestVF(StringRef ScalarF, ElementCount &FixedVF, ElementCount &ScalableVF) const
Returns the largest vectorization factor used in the list of vector functions.
StringRef getVectorizedFunction(StringRef F, const ElementCount &VF, bool Masked=false) const
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
static IntegerType * getInt1Ty(LLVMContext &C)
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
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.
std::string mangleTLIVectorName(StringRef VectorName, StringRef ScalarName, unsigned numArgs, ElementCount VF, bool Masked=false)
This routine mangles the given VectorName according to the LangRef specification for vector-function-...
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:316
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.