Skip to content

Commit b540855

Browse files
committed
[TLI] Use the VFABI demangling when declaring vector variants.
When creating a declaration for a vector variant, in order to determine the argument types we need to consult the VFABI demangler. This will allow us to add TLI mappings with linear arguments (see llvm#76060).
1 parent 7dc0ba9 commit b540855

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

llvm/lib/Transforms/Utils/InjectTLIMappings.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,37 +33,37 @@ STATISTIC(NumVFDeclAdded,
3333
STATISTIC(NumCompUsedAdded,
3434
"Number of `@llvm.compiler.used` operands that have been added.");
3535

36-
/// A helper function that adds the vector function declaration that
37-
/// vectorizes the CallInst CI with a vectorization factor of VF
38-
/// lanes. The TLI assumes that all parameters and the return type of
39-
/// CI (other than void) need to be widened to a VectorType of VF
40-
/// lanes.
36+
/// A helper function that adds the vector variant declaration for vectorizing
37+
/// the CallInst \p CI with a vectorization factor of \p VF lanes. For each
38+
/// mapping, TLI provides a VABI prefix, which contains all information required
39+
/// to create vector function declaration.
4140
static void addVariantDeclaration(CallInst &CI, const ElementCount &VF,
42-
bool Predicate, const StringRef VFName) {
41+
const VecDesc *VD) {
4342
Module *M = CI.getModule();
43+
FunctionType *ScalarFTy = CI.getFunctionType();
4444

45-
// Add function declaration.
46-
Type *RetTy = ToVectorTy(CI.getType(), VF);
47-
SmallVector<Type *, 4> Tys;
48-
for (Value *ArgOperand : CI.args())
49-
Tys.push_back(ToVectorTy(ArgOperand->getType(), VF));
50-
assert(!CI.getFunctionType()->isVarArg() &&
51-
"VarArg functions are not supported.");
52-
if (Predicate)
53-
Tys.push_back(ToVectorTy(Type::getInt1Ty(RetTy->getContext()), VF));
54-
FunctionType *FTy = FunctionType::get(RetTy, Tys, /*isVarArg=*/false);
55-
Function *VectorF =
56-
Function::Create(FTy, Function::ExternalLinkage, VFName, M);
57-
VectorF->copyAttributesFrom(CI.getCalledFunction());
45+
assert(!ScalarFTy->isVarArg() && "VarArg functions are not supported.");
46+
47+
const std::optional<VFInfo> Info = VFABI::tryDemangleForVFABI(
48+
VD->getVectorFunctionABIVariantString(), ScalarFTy);
49+
50+
assert(Info && "Failed to demangle vector variant");
51+
assert(Info->Shape.VF == VF && "Mangled name does not match VF");
52+
53+
const StringRef VFName = VD->getVectorFnName();
54+
FunctionType *VectorFTy = VFABI::createFunctionType(*Info, ScalarFTy);
55+
Function *VecFunc =
56+
Function::Create(VectorFTy, Function::ExternalLinkage, VFName, M);
57+
VecFunc->copyAttributesFrom(CI.getCalledFunction());
5858
++NumVFDeclAdded;
5959
LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Added to the module: `" << VFName
60-
<< "` of type " << *(VectorF->getType()) << "\n");
60+
<< "` of type " << *VectorFTy << "\n");
6161

6262
// Make function declaration (without a body) "sticky" in the IR by
6363
// listing it in the @llvm.compiler.used intrinsic.
64-
assert(!VectorF->size() && "VFABI attribute requires `@llvm.compiler.used` "
64+
assert(!VecFunc->size() && "VFABI attribute requires `@llvm.compiler.used` "
6565
"only on declarations.");
66-
appendToCompilerUsed(*M, {VectorF});
66+
appendToCompilerUsed(*M, {VecFunc});
6767
LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Adding `" << VFName
6868
<< "` to `@llvm.compiler.used`.\n");
6969
++NumCompUsedAdded;
@@ -100,7 +100,7 @@ static void addMappingsFromTLI(const TargetLibraryInfo &TLI, CallInst &CI) {
100100
}
101101
Function *VariantF = M->getFunction(VD->getVectorFnName());
102102
if (!VariantF)
103-
addVariantDeclaration(CI, VF, Predicate, VD->getVectorFnName());
103+
addVariantDeclaration(CI, VF, VD);
104104
}
105105
};
106106

llvm/test/Transforms/Util/add-TLI-mappings.ll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,32 @@ define float @call_llvm.log10.f32(float %in) {
6565
}
6666

6767
declare float @llvm.log10.f32(float) #0
68+
69+
; SVML: declare <2 x double> @__svml_sin2(<2 x double>)
70+
; SVML: declare <4 x double> @__svml_sin4(<4 x double>)
71+
; SVML: declare <8 x double> @__svml_sin8(<8 x double>)
72+
; SVML: declare <4 x float> @__svml_log10f4(<4 x float>)
73+
; SVML: declare <8 x float> @__svml_log10f8(<8 x float>)
74+
; SVML: declare <16 x float> @__svml_log10f16(<16 x float>)
75+
76+
; MASSV: declare <2 x double> @__sind2(<2 x double>)
77+
; MASSV: declare <4 x float> @__log10f4(<4 x float>)
78+
79+
; LIBMVEC-X86: declare <2 x double> @_ZGVbN2v_sin(<2 x double>)
80+
; LIBMVEC-X86: declare <4 x double> @_ZGVdN4v_sin(<4 x double>)
81+
82+
; ACCELERATE: declare <4 x float> @vlog10f(<4 x float>)
83+
84+
; SLEEFGNUABI: declare <2 x double> @_ZGVnN2v_sin(<2 x double>)
85+
; SLEEFGNUABI: declare <vscale x 2 x double> @_ZGVsMxv_sin(<vscale x 2 x double>, <vscale x 2 x i1>)
86+
; SLEEFGNUABI: declare <4 x float> @_ZGVnN4v_log10f(<4 x float>)
87+
; SLEEFGNUABI: declare <vscale x 4 x float> @_ZGVsMxv_log10f(<vscale x 4 x float>, <vscale x 4 x i1>)
88+
89+
; ARMPL: declare <2 x double> @armpl_vsinq_f64(<2 x double>)
90+
; ARMPL: declare <vscale x 2 x double> @armpl_svsin_f64_x(<vscale x 2 x double>, <vscale x 2 x i1>)
91+
; ARMPL: declare <4 x float> @armpl_vlog10q_f32(<4 x float>)
92+
; ARMPL: declare <vscale x 4 x float> @armpl_svlog10_f32_x(<vscale x 4 x float>, <vscale x 4 x i1>)
93+
6894
attributes #0 = { nounwind readnone }
6995

7096
; SVML: attributes #[[SIN]] = { "vector-function-abi-variant"=

0 commit comments

Comments
 (0)