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

[InstCombine][TLI] Fix function prototype of labs #69077

Merged
merged 1 commit into from
Oct 15, 2023

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Oct 14, 2023

i64 @labs(i32) is incorrectly recognized as LibFunc_labs because type ID Long matches both i32 and i64. This PR requires the type of argument to match the return value.

Fixes #69059.

@llvmbot
Copy link
Member

llvmbot commented Oct 14, 2023

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-analysis

Author: Yingwei Zheng (dtcxzyw)

Changes

i64 @<!-- -->labs(i32) is incorrectly recognized as LibFunc_labs because type ID Long matches both i32 and i64. This PR requires the type of argument to match the return value.

Fixes #69059.


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

3 Files Affected:

  • (modified) llvm/include/llvm/Analysis/TargetLibraryInfo.def (+1-1)
  • (added) llvm/test/Transforms/InstCombine/pr69059.ll (+16)
  • (modified) llvm/unittests/Analysis/TargetLibraryInfoTest.cpp (+10)
diff --git a/llvm/include/llvm/Analysis/TargetLibraryInfo.def b/llvm/include/llvm/Analysis/TargetLibraryInfo.def
index 03ac422d3e6b786..6bd922eed89e15e 100644
--- a/llvm/include/llvm/Analysis/TargetLibraryInfo.def
+++ b/llvm/include/llvm/Analysis/TargetLibraryInfo.def
@@ -1570,7 +1570,7 @@ TLI_DEFINE_SIG_INTERNAL(Int, Int)
 /// long int labs(long int j);
 TLI_DEFINE_ENUM_INTERNAL(labs)
 TLI_DEFINE_STRING_INTERNAL("labs")
-TLI_DEFINE_SIG_INTERNAL(Long, Long)
+TLI_DEFINE_SIG_INTERNAL(Long, Same)
 
 /// int lchown(const char *path, uid_t owner, gid_t group);
 TLI_DEFINE_ENUM_INTERNAL(lchown)
diff --git a/llvm/test/Transforms/InstCombine/pr69059.ll b/llvm/test/Transforms/InstCombine/pr69059.ll
new file mode 100644
index 000000000000000..75690b8396520ad
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/pr69059.ll
@@ -0,0 +1,16 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i64 @pr69059() {
+; CHECK-LABEL: define i64 @pr69059() {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CALL:%.*]] = call i64 @labs(i32 0)
+; CHECK-NEXT:    ret i64 [[CALL]]
+;
+entry:
+  %call = call i64 @labs(i32 0)
+  ret i64 %call
+}
+
+; negative test: not a valid libfunc proto
+declare i64 @labs(i32)
diff --git a/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp b/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
index 8c2328ee1c9be38..292b5cade9509b9 100644
--- a/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
+++ b/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
@@ -69,6 +69,16 @@ TEST_F(TargetLibraryInfoTest, InvalidProto) {
         M->getOrInsertFunction(TLI.getName(LF), InvalidFTy).getCallee());
     EXPECT_FALSE(isLibFunc(F, LF));
   }
+
+  // i64 @labs(i32)
+  {
+    auto *InvalidLabsFTy = FunctionType::get(Type::getInt64Ty(Context),
+                                             {Type::getInt32Ty(Context)},
+                                             /*isVarArg=*/false);
+    auto *F = cast<Function>(
+        M->getOrInsertFunction("labs", InvalidLabsFTy).getCallee());
+    EXPECT_FALSE(isLibFunc(F, LibFunc_labs));
+  }
 }
 
 // Check that we do accept know-correct prototypes.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants