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

[InjectTLIMappings] Remove signext/zeroext attributes from vector functions. #80546

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

yetingk
Copy link
Contributor

@yetingk yetingk commented Feb 3, 2024

Previously, when declaring vector functions, its attributes are copied by its scalar function. It may be illegal, since signext/zeroext could not be used for vector types.

@yetingk yetingk changed the title [InjectTLIMappings] Remove SExt/ZExt attributes from vector functions. [InjectTLIMappings] Remove signext/zeroext attributes from vector functions. Feb 3, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 3, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Yeting Kuo (yetingk)

Changes

Previously, when declaring vector functions, its attributes are copied by its scalar function. It may be illegal, since signext/zeroext could not be used for vector types.


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/InjectTLIMappings.cpp (+15)
diff --git a/llvm/lib/Transforms/Utils/InjectTLIMappings.cpp b/llvm/lib/Transforms/Utils/InjectTLIMappings.cpp
index 9bfac2ac9167e..27bfee4dfc7a9 100644
--- a/llvm/lib/Transforms/Utils/InjectTLIMappings.cpp
+++ b/llvm/lib/Transforms/Utils/InjectTLIMappings.cpp
@@ -34,6 +34,19 @@ STATISTIC(NumVFDeclAdded,
 STATISTIC(NumCompUsedAdded,
           "Number of `@llvm.compiler.used` operands that have been added.");
 
+static void removeIllegalAttributes(Function *Func, const FunctionType *FTy) {
+  // Vector types could not have attributes signext/zeroext.
+  if (FTy->getReturnType()->isVectorTy()) {
+    Func->removeRetAttr(Attribute::SExt);
+    Func->removeRetAttr(Attribute::ZExt);
+  }
+  for (const auto I : enumerate(FTy->params()))
+    if (I.value()->isVectorTy()) {
+      Func->removeParamAttr(I.index(), Attribute::SExt);
+      Func->removeParamAttr(I.index(), Attribute::ZExt);
+    }
+}
+
 /// A helper function that adds the vector variant declaration for vectorizing
 /// the CallInst \p CI with a vectorization factor of \p VF lanes. For each
 /// mapping, TLI provides a VABI prefix, which contains all information required
@@ -56,6 +69,8 @@ static void addVariantDeclaration(CallInst &CI, const ElementCount &VF,
   Function *VecFunc =
       Function::Create(VectorFTy, Function::ExternalLinkage, VFName, M);
   VecFunc->copyAttributesFrom(CI.getCalledFunction());
+  removeIllegalAttributes(VecFunc, VectorFTy);
+
   ++NumVFDeclAdded;
   LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Added to the module: `" << VFName
                     << "` of type " << *VectorFTy << "\n");

@yetingk
Copy link
Contributor Author

yetingk commented Feb 15, 2024

Ping.

Previously, when declaring vector functions, its attributes are copied by its
scalar function. It may be illegal, since signext/zeroext could not be used for
vector types.
@@ -34,6 +34,19 @@ STATISTIC(NumVFDeclAdded,
STATISTIC(NumCompUsedAdded,
"Number of `@llvm.compiler.used` operands that have been added.");

static void removeIllegalAttributes(Function *Func, const FunctionType *FTy) {
// Vector types could not have attributes signext/zeroext.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could not -> cannot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@arcbbb
Copy link
Contributor

arcbbb commented Feb 22, 2024

Is there a test for this change?

@yetingk
Copy link
Contributor Author

yetingk commented Feb 22, 2024

Is there a test for this change?

Actually no, but there were bugs related to it in sifive internal.

@@ -56,6 +69,8 @@ static void addVariantDeclaration(CallInst &CI, const ElementCount &VF,
Function *VecFunc =
Function::Create(VectorFTy, Function::ExternalLinkage, VFName, M);
VecFunc->copyAttributesFrom(CI.getCalledFunction());
removeIllegalAttributes(VecFunc, VectorFTy);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe to just always remove the attributes if doing so would lead to incorrect results? Do the vector result and arguments now need explicit sign or zero-extending?

Copy link
Contributor Author

@yetingk yetingk Feb 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signext and zeroext attribute is illegal to vector attributes. It will cause ICE like Attribute 'signext' applied to incompatible type!.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I understand. But then is it safe to even use a vector mapping in that case? I'm just asking if perhaps this is the wrong fix, and really we shouldn't be vectorising at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think signext/zeroext is to specify the behavior of a data is narrower than its storage. For most vector/simd implement, their scalar datas are packed into its vector register, so we don't need signext/zeroext for them. And I think it's the reason that I don't see an issue to adding signext/zeroext attributes for vector types.

@yetingk
Copy link
Contributor Author

yetingk commented May 7, 2024

Ping.

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

5 participants