Skip to content

[TLI] Add support for inferring attr cold/noreturn on std::terminate and __cxa_throw #101622

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions llvm/include/llvm/Analysis/TargetLibraryInfo.def
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,16 @@ TLI_DEFINE_ENUM_INTERNAL(Exit)
TLI_DEFINE_STRING_INTERNAL("_Exit")
TLI_DEFINE_SIG_INTERNAL(Void, Int)

/// void std::terminate();
TLI_DEFINE_ENUM_INTERNAL(terminate)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't be here ZSt9terminatev similarly to other cpp functions? E.g.

TLI_DEFINE_ENUM_INTERNAL(ZdaPv)
TLI_DEFINE_STRING_INTERNAL("_ZdaPv")

TLI_DEFINE_STRING_INTERNAL("_ZSt9terminatev")
TLI_DEFINE_SIG_INTERNAL(Void)

/// void __cxa_throw(void *, void *, void (*)(void *));
TLI_DEFINE_ENUM_INTERNAL(cxa_throw)
TLI_DEFINE_STRING_INTERNAL("__cxa_throw")
TLI_DEFINE_SIG_INTERNAL(Void, Ptr, Ptr, Ptr)

/// void __cxa_guard_abort(guard_t *guard);
/// guard_t is int64_t in Itanium ABI or int32_t on ARM eabi.
TLI_DEFINE_ENUM_INTERNAL(cxa_guard_abort)
Expand Down
18 changes: 18 additions & 0 deletions llvm/lib/Transforms/Utils/BuildLibCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ STATISTIC(NumNoUndef, "Number of function returns inferred as noundef returns");
STATISTIC(NumReturnedArg, "Number of arguments inferred as returned");
STATISTIC(NumWillReturn, "Number of functions inferred as willreturn");
STATISTIC(NumCold, "Number of functions inferred as cold");
STATISTIC(NumNoReturn, "Number of functions inferred as no return");

static bool setDoesNotAccessMemory(Function &F) {
if (F.doesNotAccessMemory())
Expand All @@ -66,6 +67,14 @@ static bool setIsCold(Function &F) {
return true;
}

static bool setNoReturn(Function &F) {
if (F.hasFnAttribute(Attribute::NoReturn))
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it better to use F.doesNotReturn() and setDoesNotReturn as it is in setDoesNotAccessMemory instead of checking attributes explicitly? And rename to setDoesNotReturn probably...

return false;
F.addFnAttr(Attribute::NoReturn);
++NumNoReturn;
return true;
}

static bool setOnlyAccessesInaccessibleMemory(Function &F) {
if (F.onlyAccessesInaccessibleMemory())
return false;
Expand Down Expand Up @@ -1102,6 +1111,15 @@ bool llvm::inferNonMandatoryLibFuncAttrs(Function &F,
case LibFunc_abort:
Changed |= setIsCold(F);
break;
case LibFunc_terminate:
Changed |= setIsCold(F);
Changed |= setNoReturn(F);
break;
case LibFunc_cxa_throw:
Changed |= setIsCold(F);
Changed |= setNoReturn(F);
// Don't add `nofree` on `__cxa_throw`
return Changed;
// int __nvvm_reflect(const char *)
case LibFunc_nvvm_reflect:
Changed |= setRetAndArgsNoUndef(F);
Expand Down
8 changes: 8 additions & 0 deletions llvm/test/Transforms/InferFunctionAttrs/annotate.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,12 @@ declare i64 @write(i32, ptr, i64)
; CHECK: declare void @abort() [[NOFREE_COLD:#[0-9]+]]
declare void @abort()

; CHECK: declare void @__cxa_throw(ptr, ptr, ptr) [[COLD_NORETURN:#[0-9]+]]
declare void @__cxa_throw(ptr, ptr, ptr)

; CHECK: declare void @_ZSt9terminatev() [[NOFREE_COLD_NORETURN:#[0-9]+]]
declare void @_ZSt9terminatev()

; memset_pattern{4,8,16} aren't available everywhere.
; CHECK-DARWIN: declare void @memset_pattern4(ptr nocapture writeonly, ptr nocapture readonly, i64) [[ARGMEMONLY_NOFREE_NOUNWIND_WILLRETURN]]
declare void @memset_pattern4(ptr, ptr, i64)
Expand All @@ -1126,6 +1132,8 @@ declare void @memset_pattern16(ptr, ptr, i64)
; CHECK-DAG: attributes [[INACCESSIBLEMEMORARGMEMONLY_NOUNWIND_WILLRETURN_ALLOCKIND_REALLOC_ALLOCSIZE1_FAMILY_MALLOC]] = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" }
; CHECK-DAG: attributes [[INACCESSIBLEMEMORARGONLY_NOFREE_NOUNWIND_WILLRETURN_FAMILY_MALLOC]] = { mustprogress nofree nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" }
; CHECK-DAG: attributes [[NOFREE_COLD]] = { cold nofree }
; CHECK-DAG: attributes [[NOFREE_COLD_NORETURN]] = { cold nofree noreturn }
; CHECK-DAG: attributes [[COLD_NORETURN]] = { cold noreturn }

; CHECK-NVPTX-DAG: attributes [[NOFREE_NOUNWIND_READNONE]] = { nofree nosync nounwind memory(none) }

Expand Down
16 changes: 12 additions & 4 deletions llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@
#
# CHECK: << Total TLI yes SDK no: 12
# CHECK: >> Total TLI no SDK yes: 0
# CHECK: == Total TLI yes SDK yes: 248
# CHECK: == Total TLI yes SDK yes: 250
#
# WRONG_DETAIL: << TLI yes SDK no : '_ZdaPv' aka operator delete[](void*)
# WRONG_DETAIL: >> TLI no SDK yes: '_ZdaPvj' aka operator delete[](void*, unsigned int)
# WRONG_DETAIL-COUNT-8: << TLI yes SDK no : '_Zn{{.*}}__hot_cold_t
# WRONG_DETAIL-COUNT-4: << TLI yes SDK no : '__size_returning_new{{.*}}
# WRONG_SUMMARY: << Total TLI yes SDK no: 13{{$}}
# WRONG_SUMMARY: >> Total TLI no SDK yes: 1{{$}}
# WRONG_SUMMARY: == Total TLI yes SDK yes: 247
# WRONG_SUMMARY: == Total TLI yes SDK yes: 249
#
## The -COUNT suffix doesn't care if there are too many matches, so check
## the exact count first; the two directives should add up to that.
## Yes, this means additions to TLI will fail this test, but the argument
## to -COUNT can't be an expression.
# AVAIL: TLI knows 493 symbols, 260 available
# AVAIL-COUNT-260: {{^}} available
# AVAIL: TLI knows 495 symbols, 262 available
# AVAIL-COUNT-262: {{^}} available
# AVAIL-NOT: {{^}} available
# UNAVAIL-COUNT-233: not available
# UNAVAIL-NOT: not available
Expand Down Expand Up @@ -168,6 +168,10 @@ DynamicSymbols:
Type: STT_FUNC
Section: .text
Binding: STB_GLOBAL
- Name: __cxa_throw
Type: STT_FUNC
Section: .text
Binding: STB_GLOBAL
- Name: __cxa_guard_release
Type: STT_FUNC
Section: .text
Expand Down Expand Up @@ -280,6 +284,10 @@ DynamicSymbols:
Type: STT_FUNC
Section: .text
Binding: STB_GLOBAL
- Name: _ZSt9terminatev
Type: STT_FUNC
Section: .text
Binding: STB_GLOBAL
- Name: atof
Type: STT_FUNC
Section: .text
Expand Down
2 changes: 2 additions & 0 deletions llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,14 @@ TEST_F(TargetLibraryInfoTest, ValidProto) {
"declare void @__cxa_guard_abort(%struct*)\n"
"declare i32 @__cxa_guard_acquire(%struct*)\n"
"declare void @__cxa_guard_release(%struct*)\n"
"declare void @__cxa_throw(ptr, ptr, ptr)\n"

"declare i32 @atexit(void ()*)\n"

"declare void @abort()\n"
"declare void @exit(i32)\n"
"declare void @_Exit(i32)\n"
"declare void @_ZSt9terminatev()\n"

"declare i32 @__nvvm_reflect(i8*)\n"

Expand Down
Loading