Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Utils/InjectTLIMappings.cpp
35271 views
//===- InjectTLIMAppings.cpp - TLI to VFABI attribute injection ----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Populates the VFABI attribute with the scalar-to-vector mappings9// from the TargetLibraryInfo.10//11//===----------------------------------------------------------------------===//1213#include "llvm/Transforms/Utils/InjectTLIMappings.h"14#include "llvm/ADT/Statistic.h"15#include "llvm/Analysis/DemandedBits.h"16#include "llvm/Analysis/GlobalsModRef.h"17#include "llvm/Analysis/OptimizationRemarkEmitter.h"18#include "llvm/Analysis/TargetLibraryInfo.h"19#include "llvm/Analysis/VectorUtils.h"20#include "llvm/IR/InstIterator.h"21#include "llvm/IR/VFABIDemangler.h"22#include "llvm/Transforms/Utils/ModuleUtils.h"2324using namespace llvm;2526#define DEBUG_TYPE "inject-tli-mappings"2728STATISTIC(NumCallInjected,29"Number of calls in which the mappings have been injected.");3031STATISTIC(NumVFDeclAdded,32"Number of function declarations that have been added.");33STATISTIC(NumCompUsedAdded,34"Number of `@llvm.compiler.used` operands that have been added.");3536/// A helper function that adds the vector variant declaration for vectorizing37/// the CallInst \p CI with a vectorization factor of \p VF lanes. For each38/// mapping, TLI provides a VABI prefix, which contains all information required39/// to create vector function declaration.40static void addVariantDeclaration(CallInst &CI, const ElementCount &VF,41const VecDesc *VD) {42Module *M = CI.getModule();43FunctionType *ScalarFTy = CI.getFunctionType();4445assert(!ScalarFTy->isVarArg() && "VarArg functions are not supported.");4647const std::optional<VFInfo> Info = VFABI::tryDemangleForVFABI(48VD->getVectorFunctionABIVariantString(), ScalarFTy);4950assert(Info && "Failed to demangle vector variant");51assert(Info->Shape.VF == VF && "Mangled name does not match VF");5253const StringRef VFName = VD->getVectorFnName();54FunctionType *VectorFTy = VFABI::createFunctionType(*Info, ScalarFTy);55Function *VecFunc =56Function::Create(VectorFTy, Function::ExternalLinkage, VFName, M);57VecFunc->copyAttributesFrom(CI.getCalledFunction());58++NumVFDeclAdded;59LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Added to the module: `" << VFName60<< "` of type " << *VectorFTy << "\n");6162// Make function declaration (without a body) "sticky" in the IR by63// listing it in the @llvm.compiler.used intrinsic.64assert(!VecFunc->size() && "VFABI attribute requires `@llvm.compiler.used` "65"only on declarations.");66appendToCompilerUsed(*M, {VecFunc});67LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Adding `" << VFName68<< "` to `@llvm.compiler.used`.\n");69++NumCompUsedAdded;70}7172static void addMappingsFromTLI(const TargetLibraryInfo &TLI, CallInst &CI) {73// This is needed to make sure we don't query the TLI for calls to74// bitcast of function pointers, like `%call = call i32 (i32*, ...)75// bitcast (i32 (...)* @goo to i32 (i32*, ...)*)(i32* nonnull %i)`,76// as such calls make the `isFunctionVectorizable` raise an77// exception.78if (CI.isNoBuiltin() || !CI.getCalledFunction())79return;8081StringRef ScalarName = CI.getCalledFunction()->getName();8283// Nothing to be done if the TLI thinks the function is not84// vectorizable.85if (!TLI.isFunctionVectorizable(ScalarName))86return;87SmallVector<std::string, 8> Mappings;88VFABI::getVectorVariantNames(CI, Mappings);89Module *M = CI.getModule();90const SetVector<StringRef> OriginalSetOfMappings(Mappings.begin(),91Mappings.end());9293auto AddVariantDecl = [&](const ElementCount &VF, bool Predicate) {94const VecDesc *VD = TLI.getVectorMappingInfo(ScalarName, VF, Predicate);95if (VD && !VD->getVectorFnName().empty()) {96std::string MangledName = VD->getVectorFunctionABIVariantString();97if (!OriginalSetOfMappings.count(MangledName)) {98Mappings.push_back(MangledName);99++NumCallInjected;100}101Function *VariantF = M->getFunction(VD->getVectorFnName());102if (!VariantF)103addVariantDeclaration(CI, VF, VD);104}105};106107// All VFs in the TLI are powers of 2.108ElementCount WidestFixedVF, WidestScalableVF;109TLI.getWidestVF(ScalarName, WidestFixedVF, WidestScalableVF);110111for (bool Predicated : {false, true}) {112for (ElementCount VF = ElementCount::getFixed(2);113ElementCount::isKnownLE(VF, WidestFixedVF); VF *= 2)114AddVariantDecl(VF, Predicated);115116for (ElementCount VF = ElementCount::getScalable(2);117ElementCount::isKnownLE(VF, WidestScalableVF); VF *= 2)118AddVariantDecl(VF, Predicated);119}120121VFABI::setVectorVariantNames(&CI, Mappings);122}123124static bool runImpl(const TargetLibraryInfo &TLI, Function &F) {125for (auto &I : instructions(F))126if (auto CI = dyn_cast<CallInst>(&I))127addMappingsFromTLI(TLI, *CI);128// Even if the pass adds IR attributes, the analyses are preserved.129return false;130}131132////////////////////////////////////////////////////////////////////////////////133// New pass manager implementation.134////////////////////////////////////////////////////////////////////////////////135PreservedAnalyses InjectTLIMappings::run(Function &F,136FunctionAnalysisManager &AM) {137const TargetLibraryInfo &TLI = AM.getResult<TargetLibraryAnalysis>(F);138runImpl(TLI, F);139// Even if the pass adds IR attributes, the analyses are preserved.140return PreservedAnalyses::all();141}142143144