[llvm] [InstCombine][TLI] Fix function prototype of `labs` (PR #69077)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 14 13:00:39 PDT 2023


https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/69077

`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.


>From bfed1a7aa1fb1383d8d84ee1f3a4af4ce8598cf9 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sun, 15 Oct 2023 03:43:29 +0800
Subject: [PATCH] [InstCombine][TLI] Fix function prototype of `labs`

---
 llvm/include/llvm/Analysis/TargetLibraryInfo.def |  2 +-
 llvm/test/Transforms/InstCombine/pr69059.ll      | 16 ++++++++++++++++
 .../unittests/Analysis/TargetLibraryInfoTest.cpp | 10 ++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Transforms/InstCombine/pr69059.ll

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.



More information about the llvm-commits mailing list