xref: /freebsd-src/contrib/llvm-project/llvm/lib/Transforms/Utils/InjectTLIMappings.cpp (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
1*480093f4SDimitry Andric //===- InjectTLIMAppings.cpp - TLI to VFABI attribute injection  ----------===//
2*480093f4SDimitry Andric //
3*480093f4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*480093f4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*480093f4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*480093f4SDimitry Andric //
7*480093f4SDimitry Andric //===----------------------------------------------------------------------===//
8*480093f4SDimitry Andric //
9*480093f4SDimitry Andric // Populates the VFABI attribute with the scalar-to-vector mappings
10*480093f4SDimitry Andric // from the TargetLibraryInfo.
11*480093f4SDimitry Andric //
12*480093f4SDimitry Andric //===----------------------------------------------------------------------===//
13*480093f4SDimitry Andric 
14*480093f4SDimitry Andric #include "llvm/Transforms/Utils/InjectTLIMappings.h"
15*480093f4SDimitry Andric #include "llvm/ADT/Statistic.h"
16*480093f4SDimitry Andric #include "llvm/Analysis/VectorUtils.h"
17*480093f4SDimitry Andric #include "llvm/IR/InstIterator.h"
18*480093f4SDimitry Andric #include "llvm/Transforms/Utils.h"
19*480093f4SDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
20*480093f4SDimitry Andric 
21*480093f4SDimitry Andric using namespace llvm;
22*480093f4SDimitry Andric 
23*480093f4SDimitry Andric #define DEBUG_TYPE "inject-tli-mappings"
24*480093f4SDimitry Andric 
25*480093f4SDimitry Andric STATISTIC(NumCallInjected,
26*480093f4SDimitry Andric           "Number of calls in which the mappings have been injected.");
27*480093f4SDimitry Andric 
28*480093f4SDimitry Andric STATISTIC(NumVFDeclAdded,
29*480093f4SDimitry Andric           "Number of function declarations that have been added.");
30*480093f4SDimitry Andric STATISTIC(NumCompUsedAdded,
31*480093f4SDimitry Andric           "Number of `@llvm.compiler.used` operands that have been added.");
32*480093f4SDimitry Andric 
33*480093f4SDimitry Andric /// Helper function to map the TLI name to a strings that holds
34*480093f4SDimitry Andric /// scalar-to-vector mapping.
35*480093f4SDimitry Andric ///
36*480093f4SDimitry Andric ///    _ZGV<isa><mask><vlen><vparams>_<scalarname>(<vectorname>)
37*480093f4SDimitry Andric ///
38*480093f4SDimitry Andric /// where:
39*480093f4SDimitry Andric ///
40*480093f4SDimitry Andric /// <isa> = "_LLVM_"
41*480093f4SDimitry Andric /// <mask> = "N". Note: TLI does not support masked interfaces.
42*480093f4SDimitry Andric /// <vlen> = Number of concurrent lanes, stored in the `VectorizationFactor`
43*480093f4SDimitry Andric ///          field of the `VecDesc` struct.
44*480093f4SDimitry Andric /// <vparams> = "v", as many as are the number of parameters of CI.
45*480093f4SDimitry Andric /// <scalarname> = the name of the scalar function called by CI.
46*480093f4SDimitry Andric /// <vectorname> = the name of the vector function mapped by the TLI.
47*480093f4SDimitry Andric static std::string mangleTLIName(StringRef VectorName, const CallInst &CI,
48*480093f4SDimitry Andric                                  unsigned VF) {
49*480093f4SDimitry Andric   SmallString<256> Buffer;
50*480093f4SDimitry Andric   llvm::raw_svector_ostream Out(Buffer);
51*480093f4SDimitry Andric   Out << "_ZGV" << VFABI::_LLVM_ << "N" << VF;
52*480093f4SDimitry Andric   for (unsigned I = 0; I < CI.getNumArgOperands(); ++I)
53*480093f4SDimitry Andric     Out << "v";
54*480093f4SDimitry Andric   Out << "_" << CI.getCalledFunction()->getName() << "(" << VectorName << ")";
55*480093f4SDimitry Andric   return Out.str();
56*480093f4SDimitry Andric }
57*480093f4SDimitry Andric 
58*480093f4SDimitry Andric /// A helper function for converting Scalar types to vector types.
59*480093f4SDimitry Andric /// If the incoming type is void, we return void. If the VF is 1, we return
60*480093f4SDimitry Andric /// the scalar type.
61*480093f4SDimitry Andric static Type *ToVectorTy(Type *Scalar, unsigned VF, bool isScalable = false) {
62*480093f4SDimitry Andric   if (Scalar->isVoidTy() || VF == 1)
63*480093f4SDimitry Andric     return Scalar;
64*480093f4SDimitry Andric   return VectorType::get(Scalar, {VF, isScalable});
65*480093f4SDimitry Andric }
66*480093f4SDimitry Andric 
67*480093f4SDimitry Andric /// A helper function that adds the vector function declaration that
68*480093f4SDimitry Andric /// vectorizes the CallInst CI with a vectorization factor of VF
69*480093f4SDimitry Andric /// lanes. The TLI assumes that all parameters and the return type of
70*480093f4SDimitry Andric /// CI (other than void) need to be widened to a VectorType of VF
71*480093f4SDimitry Andric /// lanes.
72*480093f4SDimitry Andric static void addVariantDeclaration(CallInst &CI, const unsigned VF,
73*480093f4SDimitry Andric                                   const StringRef VFName) {
74*480093f4SDimitry Andric   Module *M = CI.getModule();
75*480093f4SDimitry Andric 
76*480093f4SDimitry Andric   // Add function declaration.
77*480093f4SDimitry Andric   Type *RetTy = ToVectorTy(CI.getType(), VF);
78*480093f4SDimitry Andric   SmallVector<Type *, 4> Tys;
79*480093f4SDimitry Andric   for (Value *ArgOperand : CI.arg_operands())
80*480093f4SDimitry Andric     Tys.push_back(ToVectorTy(ArgOperand->getType(), VF));
81*480093f4SDimitry Andric   assert(!CI.getFunctionType()->isVarArg() &&
82*480093f4SDimitry Andric          "VarArg functions are not supported.");
83*480093f4SDimitry Andric   FunctionType *FTy = FunctionType::get(RetTy, Tys, /*isVarArg=*/false);
84*480093f4SDimitry Andric   Function *VectorF =
85*480093f4SDimitry Andric       Function::Create(FTy, Function::ExternalLinkage, VFName, M);
86*480093f4SDimitry Andric   VectorF->copyAttributesFrom(CI.getCalledFunction());
87*480093f4SDimitry Andric   ++NumVFDeclAdded;
88*480093f4SDimitry Andric   LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Added to the module: `" << VFName
89*480093f4SDimitry Andric                     << "` of type " << *(VectorF->getType()) << "\n");
90*480093f4SDimitry Andric 
91*480093f4SDimitry Andric   // Make function declaration (without a body) "sticky" in the IR by
92*480093f4SDimitry Andric   // listing it in the @llvm.compiler.used intrinsic.
93*480093f4SDimitry Andric   assert(!VectorF->size() && "VFABI attribute requires `@llvm.compiler.used` "
94*480093f4SDimitry Andric                              "only on declarations.");
95*480093f4SDimitry Andric   appendToCompilerUsed(*M, {VectorF});
96*480093f4SDimitry Andric   LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Adding `" << VFName
97*480093f4SDimitry Andric                     << "` to `@llvm.compiler.used`.\n");
98*480093f4SDimitry Andric   ++NumCompUsedAdded;
99*480093f4SDimitry Andric }
100*480093f4SDimitry Andric 
101*480093f4SDimitry Andric static void addMappingsFromTLI(const TargetLibraryInfo &TLI, CallInst &CI) {
102*480093f4SDimitry Andric   // This is needed to make sure we don't query the TLI for calls to
103*480093f4SDimitry Andric   // bitcast of function pointers, like `%call = call i32 (i32*, ...)
104*480093f4SDimitry Andric   // bitcast (i32 (...)* @goo to i32 (i32*, ...)*)(i32* nonnull %i)`,
105*480093f4SDimitry Andric   // as such calls make the `isFunctionVectorizable` raise an
106*480093f4SDimitry Andric   // exception.
107*480093f4SDimitry Andric   if (CI.isNoBuiltin() || !CI.getCalledFunction())
108*480093f4SDimitry Andric     return;
109*480093f4SDimitry Andric 
110*480093f4SDimitry Andric   const std::string ScalarName = CI.getCalledFunction()->getName();
111*480093f4SDimitry Andric   // Nothing to be done if the TLI thinks the function is not
112*480093f4SDimitry Andric   // vectorizable.
113*480093f4SDimitry Andric   if (!TLI.isFunctionVectorizable(ScalarName))
114*480093f4SDimitry Andric     return;
115*480093f4SDimitry Andric   SmallVector<std::string, 8> Mappings;
116*480093f4SDimitry Andric   VFABI::getVectorVariantNames(CI, Mappings);
117*480093f4SDimitry Andric   Module *M = CI.getModule();
118*480093f4SDimitry Andric   const SetVector<StringRef> OriginalSetOfMappings(Mappings.begin(),
119*480093f4SDimitry Andric                                                    Mappings.end());
120*480093f4SDimitry Andric   //  All VFs in the TLI are powers of 2.
121*480093f4SDimitry Andric   for (unsigned VF = 2, WidestVF = TLI.getWidestVF(ScalarName); VF <= WidestVF;
122*480093f4SDimitry Andric        VF *= 2) {
123*480093f4SDimitry Andric     const std::string TLIName = TLI.getVectorizedFunction(ScalarName, VF);
124*480093f4SDimitry Andric     if (!TLIName.empty()) {
125*480093f4SDimitry Andric       std::string MangledName = mangleTLIName(TLIName, CI, VF);
126*480093f4SDimitry Andric       if (!OriginalSetOfMappings.count(MangledName)) {
127*480093f4SDimitry Andric         Mappings.push_back(MangledName);
128*480093f4SDimitry Andric         ++NumCallInjected;
129*480093f4SDimitry Andric       }
130*480093f4SDimitry Andric       Function *VariantF = M->getFunction(TLIName);
131*480093f4SDimitry Andric       if (!VariantF)
132*480093f4SDimitry Andric         addVariantDeclaration(CI, VF, TLIName);
133*480093f4SDimitry Andric     }
134*480093f4SDimitry Andric   }
135*480093f4SDimitry Andric 
136*480093f4SDimitry Andric   VFABI::setVectorVariantNames(&CI, Mappings);
137*480093f4SDimitry Andric }
138*480093f4SDimitry Andric 
139*480093f4SDimitry Andric static bool runImpl(const TargetLibraryInfo &TLI, Function &F) {
140*480093f4SDimitry Andric   for (auto &I : instructions(F))
141*480093f4SDimitry Andric     if (auto CI = dyn_cast<CallInst>(&I))
142*480093f4SDimitry Andric       addMappingsFromTLI(TLI, *CI);
143*480093f4SDimitry Andric   // Even if the pass adds IR attributes, the analyses are preserved.
144*480093f4SDimitry Andric   return false;
145*480093f4SDimitry Andric }
146*480093f4SDimitry Andric 
147*480093f4SDimitry Andric ////////////////////////////////////////////////////////////////////////////////
148*480093f4SDimitry Andric // New pass manager implementation.
149*480093f4SDimitry Andric ////////////////////////////////////////////////////////////////////////////////
150*480093f4SDimitry Andric PreservedAnalyses InjectTLIMappings::run(Function &F,
151*480093f4SDimitry Andric                                          FunctionAnalysisManager &AM) {
152*480093f4SDimitry Andric   const TargetLibraryInfo &TLI = AM.getResult<TargetLibraryAnalysis>(F);
153*480093f4SDimitry Andric   runImpl(TLI, F);
154*480093f4SDimitry Andric   // Even if the pass adds IR attributes, the analyses are preserved.
155*480093f4SDimitry Andric   return PreservedAnalyses::all();
156*480093f4SDimitry Andric }
157*480093f4SDimitry Andric 
158*480093f4SDimitry Andric ////////////////////////////////////////////////////////////////////////////////
159*480093f4SDimitry Andric // Legacy PM Implementation.
160*480093f4SDimitry Andric ////////////////////////////////////////////////////////////////////////////////
161*480093f4SDimitry Andric bool InjectTLIMappingsLegacy::runOnFunction(Function &F) {
162*480093f4SDimitry Andric   const TargetLibraryInfo &TLI =
163*480093f4SDimitry Andric       getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
164*480093f4SDimitry Andric   return runImpl(TLI, F);
165*480093f4SDimitry Andric }
166*480093f4SDimitry Andric 
167*480093f4SDimitry Andric void InjectTLIMappingsLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
168*480093f4SDimitry Andric   AU.setPreservesCFG();
169*480093f4SDimitry Andric   AU.addRequired<TargetLibraryInfoWrapperPass>();
170*480093f4SDimitry Andric   AU.addPreserved<TargetLibraryInfoWrapperPass>();
171*480093f4SDimitry Andric }
172*480093f4SDimitry Andric 
173*480093f4SDimitry Andric ////////////////////////////////////////////////////////////////////////////////
174*480093f4SDimitry Andric // Legacy Pass manager initialization
175*480093f4SDimitry Andric ////////////////////////////////////////////////////////////////////////////////
176*480093f4SDimitry Andric char InjectTLIMappingsLegacy::ID = 0;
177*480093f4SDimitry Andric 
178*480093f4SDimitry Andric INITIALIZE_PASS_BEGIN(InjectTLIMappingsLegacy, DEBUG_TYPE,
179*480093f4SDimitry Andric                       "Inject TLI Mappings", false, false)
180*480093f4SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
181*480093f4SDimitry Andric INITIALIZE_PASS_END(InjectTLIMappingsLegacy, DEBUG_TYPE, "Inject TLI Mappings",
182*480093f4SDimitry Andric                     false, false)
183*480093f4SDimitry Andric 
184*480093f4SDimitry Andric FunctionPass *llvm::createInjectTLIMappingsLegacyPass() {
185*480093f4SDimitry Andric   return new InjectTLIMappingsLegacy();
186*480093f4SDimitry Andric }
187