Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TLI] Use the VFABI demangling when declaring vector variants. #76753

Merged
merged 1 commit into from
Jan 3, 2024

Conversation

labrinea
Copy link
Collaborator

@labrinea labrinea commented Jan 2, 2024

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 #76060).

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 2, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Alexandros Lamprineas (labrinea)

Changes

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 #76060).


Full diff: https://github.com/llvm/llvm-project/pull/76753.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/InjectTLIMappings.cpp (+26-7)
  • (modified) llvm/test/Transforms/Util/add-TLI-mappings.ll (+26)
diff --git a/llvm/lib/Transforms/Utils/InjectTLIMappings.cpp b/llvm/lib/Transforms/Utils/InjectTLIMappings.cpp
index 0990c750af55f1..1b0963cc2a9cd3 100644
--- a/llvm/lib/Transforms/Utils/InjectTLIMappings.cpp
+++ b/llvm/lib/Transforms/Utils/InjectTLIMappings.cpp
@@ -37,20 +37,39 @@ STATISTIC(NumCompUsedAdded,
 /// vectorizes the CallInst CI with a vectorization factor of VF
 /// lanes. The TLI assumes that all parameters and the return type of
 /// CI (other than void) need to be widened to a VectorType of VF
-/// lanes.
+/// lanes unless the VFABI says otherwise.
 static void addVariantDeclaration(CallInst &CI, const ElementCount &VF,
-                                  bool Predicate, const StringRef VFName) {
+                                  const VecDesc *VD) {
   Module *M = CI.getModule();
 
   // Add function declaration.
   Type *RetTy = ToVectorTy(CI.getType(), VF);
   SmallVector<Type *, 4> Tys;
-  for (Value *ArgOperand : CI.args())
-    Tys.push_back(ToVectorTy(ArgOperand->getType(), VF));
+
+  const std::optional<VFInfo> Info = VFABI::tryDemangleForVFABI(
+      VD->getVectorFunctionABIVariantString(), CI.getFunctionType());
+
+  assert(Info && Info->Shape.VF == VF && "Mangled name does not match VF");
+
+  for (VFParameter Param : Info->Shape.Parameters) {
+    Type *ParamType = nullptr;
+    switch (Param.ParamKind){
+    case VFParamKind::Vector:
+      ParamType = ToVectorTy(CI.getArgOperand(Param.ParamPos)->getType(), VF);
+      break;
+    case VFParamKind::GlobalPredicate:
+      ParamType = ToVectorTy(Type::getInt1Ty(RetTy->getContext()), VF);
+      break;
+    default:
+      ParamType = CI.getArgOperand(Param.ParamPos)->getType();
+    }
+    Tys.push_back(ParamType);
+  }
+
   assert(!CI.getFunctionType()->isVarArg() &&
          "VarArg functions are not supported.");
-  if (Predicate)
-    Tys.push_back(ToVectorTy(Type::getInt1Ty(RetTy->getContext()), VF));
+
+  const StringRef VFName = VD->getVectorFnName();
   FunctionType *FTy = FunctionType::get(RetTy, Tys, /*isVarArg=*/false);
   Function *VectorF =
       Function::Create(FTy, Function::ExternalLinkage, VFName, M);
@@ -100,7 +119,7 @@ static void addMappingsFromTLI(const TargetLibraryInfo &TLI, CallInst &CI) {
       }
       Function *VariantF = M->getFunction(VD->getVectorFnName());
       if (!VariantF)
-        addVariantDeclaration(CI, VF, Predicate, VD->getVectorFnName());
+        addVariantDeclaration(CI, VF, VD);
     }
   };
 
diff --git a/llvm/test/Transforms/Util/add-TLI-mappings.ll b/llvm/test/Transforms/Util/add-TLI-mappings.ll
index a40798665a50d2..67ca00b8e2cf40 100644
--- a/llvm/test/Transforms/Util/add-TLI-mappings.ll
+++ b/llvm/test/Transforms/Util/add-TLI-mappings.ll
@@ -65,6 +65,32 @@ define float @call_llvm.log10.f32(float %in) {
 }
 
 declare float @llvm.log10.f32(float) #0
+
+; SVML: declare <2 x double> @__svml_sin2(<2 x double>)
+; SVML: declare <4 x double> @__svml_sin4(<4 x double>)
+; SVML: declare <8 x double> @__svml_sin8(<8 x double>)
+; SVML: declare <4 x float> @__svml_log10f4(<4 x float>)
+; SVML: declare <8 x float> @__svml_log10f8(<8 x float>)
+; SVML: declare <16 x float> @__svml_log10f16(<16 x float>)
+
+; MASSV: declare <2 x double> @__sind2(<2 x double>)
+; MASSV: declare <4 x float> @__log10f4(<4 x float>)
+
+; LIBMVEC-X86: declare <2 x double> @_ZGVbN2v_sin(<2 x double>)
+; LIBMVEC-X86: declare <4 x double> @_ZGVdN4v_sin(<4 x double>)
+
+; ACCELERATE: declare <4 x float> @vlog10f(<4 x float>)
+
+; SLEEFGNUABI: declare <2 x double> @_ZGVnN2v_sin(<2 x double>)
+; SLEEFGNUABI: declare <vscale x 2 x double> @_ZGVsMxv_sin(<vscale x 2 x double>, <vscale x 2 x i1>)
+; SLEEFGNUABI: declare <4 x float> @_ZGVnN4v_log10f(<4 x float>)
+; SLEEFGNUABI: declare <vscale x 4 x float> @_ZGVsMxv_log10f(<vscale x 4 x float>, <vscale x 4 x i1>)
+
+; ARMPL: declare <2 x double> @armpl_vsinq_f64(<2 x double>)
+; ARMPL: declare <vscale x 2 x double> @armpl_svsin_f64_x(<vscale x 2 x double>, <vscale x 2 x i1>)
+; ARMPL: declare <4 x float> @armpl_vlog10q_f32(<4 x float>)
+; ARMPL: declare <vscale x 4 x float> @armpl_svlog10_f32_x(<vscale x 4 x float>, <vscale x 4 x i1>)
+
 attributes #0 = { nounwind readnone }
 
 ; SVML:      attributes #[[SIN]] = { "vector-function-abi-variant"=

Copy link

github-actions bot commented Jan 2, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@labrinea labrinea force-pushed the tli-mappings-variant-declaration branch from 4ed6312 to 78fa7f8 Compare January 2, 2024 21:07
@labrinea
Copy link
Collaborator Author

labrinea commented Jan 2, 2024

Unfortunately the test cannot be autogenerated since the script can create check lines for globals and attributes but not for declarations.

llvm/lib/Transforms/Utils/InjectTLIMappings.cpp Outdated Show resolved Hide resolved
llvm/lib/Transforms/Utils/InjectTLIMappings.cpp Outdated Show resolved Hide resolved
llvm/lib/Transforms/Utils/InjectTLIMappings.cpp Outdated Show resolved Hide resolved
llvm/lib/Transforms/Utils/InjectTLIMappings.cpp Outdated Show resolved Hide resolved
@labrinea labrinea force-pushed the tli-mappings-variant-declaration branch from 78fa7f8 to 4010d48 Compare January 3, 2024 11:20
@labrinea labrinea changed the title [TLI] Use tryDemangleForVFABI when declaring vector variants. [TLI] Use the VFABI demangling when declaring vector variants. Jan 3, 2024
@labrinea labrinea force-pushed the tli-mappings-variant-declaration branch from 4010d48 to 2dbb4d6 Compare January 3, 2024 12:09
llvm/lib/Transforms/Utils/InjectTLIMappings.cpp Outdated Show resolved Hide resolved
llvm/lib/Transforms/Utils/InjectTLIMappings.cpp Outdated Show resolved Hide resolved
llvm/lib/Transforms/Utils/InjectTLIMappings.cpp Outdated Show resolved Hide resolved
llvm/lib/Transforms/Utils/InjectTLIMappings.cpp Outdated Show resolved Hide resolved
llvm/lib/Transforms/Utils/InjectTLIMappings.cpp Outdated Show resolved Hide resolved
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).
@labrinea labrinea force-pushed the tli-mappings-variant-declaration branch from 2dbb4d6 to b540855 Compare January 3, 2024 13:01
@labrinea labrinea merged commit ec7a231 into llvm:main Jan 3, 2024
4 checks passed
@labrinea labrinea deleted the tli-mappings-variant-declaration branch January 3, 2024 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants