1706b4fc4SDimitry Andric //===- InjectTLIMAppings.cpp - TLI to VFABI attribute injection ----------===//
2706b4fc4SDimitry Andric //
3706b4fc4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4706b4fc4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5706b4fc4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6706b4fc4SDimitry Andric //
7706b4fc4SDimitry Andric //===----------------------------------------------------------------------===//
8706b4fc4SDimitry Andric //
9706b4fc4SDimitry Andric // Populates the VFABI attribute with the scalar-to-vector mappings
10706b4fc4SDimitry Andric // from the TargetLibraryInfo.
11706b4fc4SDimitry Andric //
12706b4fc4SDimitry Andric //===----------------------------------------------------------------------===//
13706b4fc4SDimitry Andric
14706b4fc4SDimitry Andric #include "llvm/Transforms/Utils/InjectTLIMappings.h"
15706b4fc4SDimitry Andric #include "llvm/ADT/Statistic.h"
16cfca06d7SDimitry Andric #include "llvm/Analysis/DemandedBits.h"
17cfca06d7SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
18cfca06d7SDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h"
19b60736ecSDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
20706b4fc4SDimitry Andric #include "llvm/Analysis/VectorUtils.h"
21706b4fc4SDimitry Andric #include "llvm/IR/InstIterator.h"
224df029ccSDimitry Andric #include "llvm/IR/VFABIDemangler.h"
23706b4fc4SDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
24706b4fc4SDimitry Andric
25706b4fc4SDimitry Andric using namespace llvm;
26706b4fc4SDimitry Andric
27706b4fc4SDimitry Andric #define DEBUG_TYPE "inject-tli-mappings"
28706b4fc4SDimitry Andric
29706b4fc4SDimitry Andric STATISTIC(NumCallInjected,
30706b4fc4SDimitry Andric "Number of calls in which the mappings have been injected.");
31706b4fc4SDimitry Andric
32706b4fc4SDimitry Andric STATISTIC(NumVFDeclAdded,
33706b4fc4SDimitry Andric "Number of function declarations that have been added.");
34706b4fc4SDimitry Andric STATISTIC(NumCompUsedAdded,
35706b4fc4SDimitry Andric "Number of `@llvm.compiler.used` operands that have been added.");
36706b4fc4SDimitry Andric
37aca2e42cSDimitry Andric /// A helper function that adds the vector variant declaration for vectorizing
38aca2e42cSDimitry Andric /// the CallInst \p CI with a vectorization factor of \p VF lanes. For each
39aca2e42cSDimitry Andric /// mapping, TLI provides a VABI prefix, which contains all information required
40aca2e42cSDimitry Andric /// to create vector function declaration.
addVariantDeclaration(CallInst & CI,const ElementCount & VF,const VecDesc * VD)41344a3780SDimitry Andric static void addVariantDeclaration(CallInst &CI, const ElementCount &VF,
42aca2e42cSDimitry Andric const VecDesc *VD) {
43706b4fc4SDimitry Andric Module *M = CI.getModule();
44aca2e42cSDimitry Andric FunctionType *ScalarFTy = CI.getFunctionType();
45706b4fc4SDimitry Andric
46aca2e42cSDimitry Andric assert(!ScalarFTy->isVarArg() && "VarArg functions are not supported.");
47aca2e42cSDimitry Andric
48aca2e42cSDimitry Andric const std::optional<VFInfo> Info = VFABI::tryDemangleForVFABI(
49aca2e42cSDimitry Andric VD->getVectorFunctionABIVariantString(), ScalarFTy);
50aca2e42cSDimitry Andric
51aca2e42cSDimitry Andric assert(Info && "Failed to demangle vector variant");
52aca2e42cSDimitry Andric assert(Info->Shape.VF == VF && "Mangled name does not match VF");
53aca2e42cSDimitry Andric
54aca2e42cSDimitry Andric const StringRef VFName = VD->getVectorFnName();
55aca2e42cSDimitry Andric FunctionType *VectorFTy = VFABI::createFunctionType(*Info, ScalarFTy);
56aca2e42cSDimitry Andric Function *VecFunc =
57aca2e42cSDimitry Andric Function::Create(VectorFTy, Function::ExternalLinkage, VFName, M);
58aca2e42cSDimitry Andric VecFunc->copyAttributesFrom(CI.getCalledFunction());
59706b4fc4SDimitry Andric ++NumVFDeclAdded;
60706b4fc4SDimitry Andric LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Added to the module: `" << VFName
61aca2e42cSDimitry Andric << "` of type " << *VectorFTy << "\n");
62706b4fc4SDimitry Andric
63706b4fc4SDimitry Andric // Make function declaration (without a body) "sticky" in the IR by
64706b4fc4SDimitry Andric // listing it in the @llvm.compiler.used intrinsic.
65aca2e42cSDimitry Andric assert(!VecFunc->size() && "VFABI attribute requires `@llvm.compiler.used` "
66706b4fc4SDimitry Andric "only on declarations.");
67aca2e42cSDimitry Andric appendToCompilerUsed(*M, {VecFunc});
68706b4fc4SDimitry Andric LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Adding `" << VFName
69706b4fc4SDimitry Andric << "` to `@llvm.compiler.used`.\n");
70706b4fc4SDimitry Andric ++NumCompUsedAdded;
71706b4fc4SDimitry Andric }
72706b4fc4SDimitry Andric
addMappingsFromTLI(const TargetLibraryInfo & TLI,CallInst & CI)73706b4fc4SDimitry Andric static void addMappingsFromTLI(const TargetLibraryInfo &TLI, CallInst &CI) {
74706b4fc4SDimitry Andric // This is needed to make sure we don't query the TLI for calls to
75706b4fc4SDimitry Andric // bitcast of function pointers, like `%call = call i32 (i32*, ...)
76706b4fc4SDimitry Andric // bitcast (i32 (...)* @goo to i32 (i32*, ...)*)(i32* nonnull %i)`,
77706b4fc4SDimitry Andric // as such calls make the `isFunctionVectorizable` raise an
78706b4fc4SDimitry Andric // exception.
79706b4fc4SDimitry Andric if (CI.isNoBuiltin() || !CI.getCalledFunction())
80706b4fc4SDimitry Andric return;
81706b4fc4SDimitry Andric
82b60736ecSDimitry Andric StringRef ScalarName = CI.getCalledFunction()->getName();
83b60736ecSDimitry Andric
84706b4fc4SDimitry Andric // Nothing to be done if the TLI thinks the function is not
85706b4fc4SDimitry Andric // vectorizable.
86706b4fc4SDimitry Andric if (!TLI.isFunctionVectorizable(ScalarName))
87706b4fc4SDimitry Andric return;
88706b4fc4SDimitry Andric SmallVector<std::string, 8> Mappings;
89706b4fc4SDimitry Andric VFABI::getVectorVariantNames(CI, Mappings);
90706b4fc4SDimitry Andric Module *M = CI.getModule();
91706b4fc4SDimitry Andric const SetVector<StringRef> OriginalSetOfMappings(Mappings.begin(),
92706b4fc4SDimitry Andric Mappings.end());
93344a3780SDimitry Andric
947fa27ce4SDimitry Andric auto AddVariantDecl = [&](const ElementCount &VF, bool Predicate) {
95b1c73532SDimitry Andric const VecDesc *VD = TLI.getVectorMappingInfo(ScalarName, VF, Predicate);
96b1c73532SDimitry Andric if (VD && !VD->getVectorFnName().empty()) {
97b1c73532SDimitry Andric std::string MangledName = VD->getVectorFunctionABIVariantString();
98706b4fc4SDimitry Andric if (!OriginalSetOfMappings.count(MangledName)) {
99706b4fc4SDimitry Andric Mappings.push_back(MangledName);
100706b4fc4SDimitry Andric ++NumCallInjected;
101706b4fc4SDimitry Andric }
102b1c73532SDimitry Andric Function *VariantF = M->getFunction(VD->getVectorFnName());
103706b4fc4SDimitry Andric if (!VariantF)
104aca2e42cSDimitry Andric addVariantDeclaration(CI, VF, VD);
105706b4fc4SDimitry Andric }
106344a3780SDimitry Andric };
107344a3780SDimitry Andric
108344a3780SDimitry Andric // All VFs in the TLI are powers of 2.
109344a3780SDimitry Andric ElementCount WidestFixedVF, WidestScalableVF;
110344a3780SDimitry Andric TLI.getWidestVF(ScalarName, WidestFixedVF, WidestScalableVF);
111344a3780SDimitry Andric
1127fa27ce4SDimitry Andric for (bool Predicated : {false, true}) {
113344a3780SDimitry Andric for (ElementCount VF = ElementCount::getFixed(2);
114344a3780SDimitry Andric ElementCount::isKnownLE(VF, WidestFixedVF); VF *= 2)
1157fa27ce4SDimitry Andric AddVariantDecl(VF, Predicated);
116344a3780SDimitry Andric
1177fa27ce4SDimitry Andric for (ElementCount VF = ElementCount::getScalable(2);
1187fa27ce4SDimitry Andric ElementCount::isKnownLE(VF, WidestScalableVF); VF *= 2)
1197fa27ce4SDimitry Andric AddVariantDecl(VF, Predicated);
1207fa27ce4SDimitry Andric }
121706b4fc4SDimitry Andric
122706b4fc4SDimitry Andric VFABI::setVectorVariantNames(&CI, Mappings);
123706b4fc4SDimitry Andric }
124706b4fc4SDimitry Andric
runImpl(const TargetLibraryInfo & TLI,Function & F)125706b4fc4SDimitry Andric static bool runImpl(const TargetLibraryInfo &TLI, Function &F) {
126706b4fc4SDimitry Andric for (auto &I : instructions(F))
127706b4fc4SDimitry Andric if (auto CI = dyn_cast<CallInst>(&I))
128706b4fc4SDimitry Andric addMappingsFromTLI(TLI, *CI);
129706b4fc4SDimitry Andric // Even if the pass adds IR attributes, the analyses are preserved.
130706b4fc4SDimitry Andric return false;
131706b4fc4SDimitry Andric }
132706b4fc4SDimitry Andric
133706b4fc4SDimitry Andric ////////////////////////////////////////////////////////////////////////////////
134706b4fc4SDimitry Andric // New pass manager implementation.
135706b4fc4SDimitry Andric ////////////////////////////////////////////////////////////////////////////////
run(Function & F,FunctionAnalysisManager & AM)136706b4fc4SDimitry Andric PreservedAnalyses InjectTLIMappings::run(Function &F,
137706b4fc4SDimitry Andric FunctionAnalysisManager &AM) {
138706b4fc4SDimitry Andric const TargetLibraryInfo &TLI = AM.getResult<TargetLibraryAnalysis>(F);
139706b4fc4SDimitry Andric runImpl(TLI, F);
140706b4fc4SDimitry Andric // Even if the pass adds IR attributes, the analyses are preserved.
141706b4fc4SDimitry Andric return PreservedAnalyses::all();
142706b4fc4SDimitry Andric }
143