-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Enhance TLI detection of __size_returning_new lib funcs. #102391
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
Conversation
@llvm/pr-subscribers-llvm-analysis Author: Snehasish Kumar (snehasish) ChangesThis patch adds a unittest to check that the LibFunc variants have the expected return type of {void*, size_t}. We only check with the 64 bit variant. As requested in followup comment on #101564. Full diff: https://github.com/llvm/llvm-project/pull/102391.diff 1 Files Affected:
diff --git a/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp b/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
index d200956f74102..690b4122b2968 100644
--- a/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
+++ b/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
@@ -8,6 +8,7 @@
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/SourceMgr.h"
@@ -81,6 +82,30 @@ TEST_F(TargetLibraryInfoTest, InvalidProto) {
}
}
+TEST_F(TargetLibraryInfoTest, SizeReturningNewProto) {
+ parseAssembly("target datalayout = \"p:64:64:64\"\n"
+ "declare {i8*, i64} @__size_returning_new(i64)\n"
+ "declare {i8*, i64} @__size_returning_new_hot_cold(i64, i8)\n"
+ "declare {i8*, i64} @__size_returning_new_aligned(i64, i64)\n"
+ "declare {i8*, i64} "
+ "@__size_returning_new_aligned_hot_cold(i64, i64, i8)\n");
+
+ llvm::Type *I8Ty = Type::getInt8Ty(Context);
+ llvm::PointerType *I8PtrTy = PointerType::get(I8Ty, 0);
+ llvm::StructType *SizedPtrT =
+ llvm::StructType::get(Context, {I8PtrTy, Type::getInt64Ty(Context)});
+
+ for (const LibFunc LF :
+ {LibFunc_size_returning_new, LibFunc_size_returning_new_aligned,
+ LibFunc_size_returning_new_hot_cold,
+ LibFunc_size_returning_new_aligned_hot_cold}) {
+ TLII.setAvailable(LF);
+ Function *F = M->getFunction(TLI.getName(LF));
+ ASSERT_NE(F, nullptr);
+ EXPECT_EQ(F->getReturnType(), SizedPtrT);
+ }
+}
+
// Check that we do accept know-correct prototypes.
TEST_F(TargetLibraryInfoTest, ValidProto) {
parseAssembly(
@@ -472,10 +497,11 @@ TEST_F(TargetLibraryInfoTest, ValidProto) {
"declare i8* @_ZnwmSt11align_val_tRKSt9nothrow_t(i64, i64, %struct*)\n"
"declare i8* @_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t(i64, i64, "
"%struct*, i8)\n"
- "declare %struct @__size_returning_new(i64)\n"
- "declare %struct @__size_returning_new_hot_cold(i64, i8)\n"
- "declare %struct @__size_returning_new_aligned(i64, i64)\n"
- "declare %struct @__size_returning_new_aligned_hot_cold(i64, i64, i8)\n"
+ "declare {i8*, i64} @__size_returning_new(i64)\n"
+ "declare {i8*, i64} @__size_returning_new_hot_cold(i64, i8)\n"
+ "declare {i8*, i64} @__size_returning_new_aligned(i64, i64)\n"
+ "declare {i8*, i64} @__size_returning_new_aligned_hot_cold(i64, i64, "
+ "i8)\n"
"declare void @\"??3@YAXPEAX@Z\"(i8*)\n"
"declare void @\"??3@YAXPEAXAEBUnothrow_t@std@@@Z\"(i8*, %struct*)\n"
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't what I meant. You need to add custom checking code to make sure that only declare {ptr, i64} @__size_returning_new(i64)
gets recognized as the TLI function, but not for example declare {i32, float} @__size_returning_new(i64)
. Unless I missed something, you will currently accept both.
Previously the return types of __size_returning_new variants were not validated based on their members. This patch checks the members manually, also generalizes the size_t checks to be based on the module instead of being hardcoded.
Thanks for the clarification, I've updated the pull request to add the custom logic that checks the return type. Please take a look, thanks. |
✅ With the latest revision this PR passed the C/C++ code formatter. |
Function *F = M->getFunction(TLI.getName(LF)); | ||
ASSERT_NE(F, nullptr); | ||
EXPECT_EQ(F->getReturnType(), SizedPtrT); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please also add some negative tests with incorrect signature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rewrote this test to only check invalid signatures since the test below should cover the correct match for the struct return type.
@nikic ptal, thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/123/builds/3604 Here is the relevant piece of the build log for the reference:
|
Previously the return types of __size_returning_new variants were not
validated based on their members. This patch checks the members
manually, also generalizes the size_t checks to be based on the module
instead of being hardcoded.
As requested in followup comment on #101564.