Skip to content

Commit

Permalink
[InstCombine][TLI] Fix function prototype of labs (#69077)
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
dtcxzyw committed Oct 15, 2023
1 parent e1bb059 commit 9451004
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion llvm/include/llvm/Analysis/TargetLibraryInfo.def
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions llvm/test/Transforms/InstCombine/pr69059.ll
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9451004

Please sign in to comment.