LLVM 19.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 variant declaration for vectorizing
38/// the CallInst \p CI with a vectorization factor of \p VF lanes. For each
39/// mapping, TLI provides a VABI prefix, which contains all information required
40/// to create vector function declaration.
41static void addVariantDeclaration(CallInst &CI, const ElementCount &VF,
42 const VecDesc *VD) {
43 Module *M = CI.getModule();
44 FunctionType *ScalarFTy = CI.getFunctionType();
45
46 assert(!ScalarFTy->isVarArg() && "VarArg functions are not supported.");
47
48 const std::optional<VFInfo> Info = VFABI::tryDemangleForVFABI(
49 VD->getVectorFunctionABIVariantString(), ScalarFTy);
50
51 assert(Info && "Failed to demangle vector variant");
52 assert(Info->Shape.VF == VF && "Mangled name does not match VF");
53
54 const StringRef VFName = VD->getVectorFnName();
55 FunctionType *VectorFTy = VFABI::createFunctionType(*Info, ScalarFTy);
56 Function *VecFunc =
57 Function::Create(VectorFTy, Function::ExternalLinkage, VFName, M);
59 ++NumVFDeclAdded;
60 LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Added to the module: `" << VFName
61 << "` of type " << *VectorFTy << "\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(!VecFunc->size() && "VFABI attribute requires `@llvm.compiler.used` "
66 "only on declarations.");
67 appendToCompilerUsed(*M, {VecFunc});
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;
89 VFABI::getVectorVariantNames(CI, Mappings);
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 VecDesc *VD = TLI.getVectorMappingInfo(ScalarName, VF, Predicate);
96 if (VD && !VD->getVectorFnName().empty()) {
97 std::string MangledName = VD->getVectorFunctionABIVariantString();
98 if (!OriginalSetOfMappings.count(MangledName)) {
99 Mappings.push_back(MangledName);
100 ++NumCallInjected;
101 }
102 Function *VariantF = M->getFunction(VD->getVectorFnName());
103 if (!VariantF)
104 addVariantDeclaration(CI, VF, VD);
105 }
106 };
107
108 // All VFs in the TLI are powers of 2.
109 ElementCount WidestFixedVF, WidestScalableVF;
110 TLI.getWidestVF(ScalarName, WidestFixedVF, WidestScalableVF);
111
112 for (bool Predicated : {false, true}) {
114 ElementCount::isKnownLE(VF, WidestFixedVF); VF *= 2)
115 AddVariantDecl(VF, Predicated);
116
118 ElementCount::isKnownLE(VF, WidestScalableVF); VF *= 2)
119 AddVariantDecl(VF, Predicated);
120 }
121
122 VFABI::setVectorVariantNames(&CI, Mappings);
123}
124
125static bool runImpl(const TargetLibraryInfo &TLI, Function &F) {
126 for (auto &I : instructions(F))
127 if (auto CI = dyn_cast<CallInst>(&I))
128 addMappingsFromTLI(TLI, *CI);
129 // Even if the pass adds IR attributes, the analyses are preserved.
130 return false;
131}
132
133////////////////////////////////////////////////////////////////////////////////
134// New pass manager implementation.
135////////////////////////////////////////////////////////////////////////////////
139 runImpl(TLI, F);
140 // Even if the pass adds IR attributes, the analyses are preserved.
141 return PreservedAnalyses::all();
142}
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_DEBUG(X)
Definition: Debug.h:101
static bool runImpl(Function &F, const TargetLowering &TLI)
This is the interface for a simple mod/ref and alias analysis over globals.
static void addMappingsFromTLI(const TargetLibraryInfo &TLI, CallInst &CI)
static void addVariantDeclaration(CallInst &CI, const ElementCount &VF, const VecDesc *VD)
A helper function that adds the vector variant declaration for vectorizing the CallInst CI with a vec...
static bool runImpl(const TargetLibraryInfo &TLI, Function &F)
#define DEBUG_TYPE
Select target instructions out of generic instructions
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
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 container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:348
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:500
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
Definition: InstrTypes.h:1945
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1481
FunctionType * getFunctionType() const
Definition: InstrTypes.h:1339
This class represents a function call, abstracting a target machine's calling convention.
static constexpr ElementCount getScalable(ScalarTy MinVal)
Definition: TypeSize.h:299
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition: TypeSize.h:296
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:162
size_t size() const
Definition: Function.h:800
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
Definition: Function.cpp:781
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:71
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
A vector that has set insertion semantics.
Definition: SetVector.h:57
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition: SetVector.h:264
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
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.
const VecDesc * getVectorMappingInfo(StringRef F, const ElementCount &VF, bool Masked) const
bool isFunctionVectorizable(StringRef F, const ElementCount &VF) const
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
Provides info so a possible vectorization of a function can be computed.
std::string getVectorFunctionABIVariantString() const
Returns a vector function ABI variant string on the form: ZGV<isa><mask><vlen><vparams><scalarname>(<...
StringRef getVectorFnName() const
std::optional< VFInfo > tryDemangleForVFABI(StringRef MangledName, const FunctionType *FTy)
Function to construct a VFInfo out of a mangled names in the following format:
FunctionType * createFunctionType(const VFInfo &Info, const FunctionType *ScalarFTy)
Constructs a FunctionType by applying vector function information to the type of a matching scalar fu...
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
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.