#include "llvm/Transforms/Utils/InjectTLIMappings.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/DemandedBits.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/VectorUtils.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/Transforms/Utils.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
using namespace llvm;
#define DEBUG_TYPE "inject-tli-mappings"
STATISTIC(NumCallInjected,
"Number of calls in which the mappings have been injected.");
STATISTIC(NumVFDeclAdded,
"Number of function declarations that have been added.");
STATISTIC(NumCompUsedAdded,
"Number of `@llvm.compiler.used` operands that have been added.");
static void addVariantDeclaration(CallInst &CI, const ElementCount &VF,
const StringRef VFName) {
Module *M = CI.getModule();
Type *RetTy = ToVectorTy(CI.getType(), VF);
SmallVector<Type *, 4> Tys;
for (Value *ArgOperand : CI.args())
Tys.push_back(ToVectorTy(ArgOperand->getType(), VF));
assert(!CI.getFunctionType()->isVarArg() &&
"VarArg functions are not supported.");
FunctionType *FTy = FunctionType::get(RetTy, Tys, false);
Function *VectorF =
Function::Create(FTy, Function::ExternalLinkage, VFName, M);
VectorF->copyAttributesFrom(CI.getCalledFunction());
++NumVFDeclAdded;
LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Added to the module: `" << VFName
<< "` of type " << *(VectorF->getType()) << "\n");
assert(!VectorF->size() && "VFABI attribute requires `@llvm.compiler.used` "
"only on declarations.");
appendToCompilerUsed(*M, {VectorF});
LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Adding `" << VFName
<< "` to `@llvm.compiler.used`.\n");
++NumCompUsedAdded;
}
static void addMappingsFromTLI(const TargetLibraryInfo &TLI, CallInst &CI) {
if (CI.isNoBuiltin() || !CI.getCalledFunction())
return;
StringRef ScalarName = CI.getCalledFunction()->getName();
if (!TLI.isFunctionVectorizable(ScalarName))
return;
SmallVector<std::string, 8> Mappings;
VFABI::getVectorVariantNames(CI, Mappings);
Module *M = CI.getModule();
const SetVector<StringRef> OriginalSetOfMappings(Mappings.begin(),
Mappings.end());
auto AddVariantDecl = [&](const ElementCount &VF) {
const std::string TLIName =
std::string(TLI.getVectorizedFunction(ScalarName, VF));
if (!TLIName.empty()) {
std::string MangledName =
VFABI::mangleTLIVectorName(TLIName, ScalarName, CI.arg_size(), VF);
if (!OriginalSetOfMappings.count(MangledName)) {
Mappings.push_back(MangledName);
++NumCallInjected;
}
Function *VariantF = M->getFunction(TLIName);
if (!VariantF)
addVariantDeclaration(CI, VF, TLIName);
}
};
ElementCount WidestFixedVF, WidestScalableVF;
TLI.getWidestVF(ScalarName, WidestFixedVF, WidestScalableVF);
for (ElementCount VF = ElementCount::getFixed(2);
ElementCount::isKnownLE(VF, WidestFixedVF); VF *= 2)
AddVariantDecl(VF);
assert(WidestScalableVF.isZero() &&
"Scalable vector mappings not yet supported");
VFABI::setVectorVariantNames(&CI, Mappings);
}
static bool runImpl(const TargetLibraryInfo &TLI, Function &F) {
for (auto &I : instructions(F))
if (auto CI = dyn_cast<CallInst>(&I))
addMappingsFromTLI(TLI, *CI);
return false;
}
PreservedAnalyses InjectTLIMappings::run(Function &F,
FunctionAnalysisManager &AM) {
const TargetLibraryInfo &TLI = AM.getResult<TargetLibraryAnalysis>(F);
runImpl(TLI, F);
return PreservedAnalyses::all();
}
bool InjectTLIMappingsLegacy::runOnFunction(Function &F) {
const TargetLibraryInfo &TLI =
getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
return runImpl(TLI, F);
}
void InjectTLIMappingsLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesCFG();
AU.addRequired<TargetLibraryInfoWrapperPass>();
AU.addPreserved<TargetLibraryInfoWrapperPass>();
AU.addPreserved<ScalarEvolutionWrapperPass>();
AU.addPreserved<AAResultsWrapperPass>();
AU.addPreserved<LoopAccessLegacyAnalysis>();
AU.addPreserved<DemandedBitsWrapperPass>();
AU.addPreserved<OptimizationRemarkEmitterWrapperPass>();
AU.addPreserved<GlobalsAAWrapperPass>();
}
char InjectTLIMappingsLegacy::ID = 0;
INITIALIZE_PASS_BEGIN(InjectTLIMappingsLegacy, DEBUG_TYPE,
"Inject TLI Mappings", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
INITIALIZE_PASS_END(InjectTLIMappingsLegacy, DEBUG_TYPE, "Inject TLI Mappings",
false, false)
FunctionPass *llvm::createInjectTLIMappingsLegacyPass() {
return new InjectTLIMappingsLegacy();
}