Alan Zhao c5b3fe2094
[clang] Automatically add the returns_twice attribute to certain functions even if -fno-builtin is set (#133511)
Certain functions require the `returns_twice` attribute in order to
produce correct codegen. However, `-fno-builtin` removes all knowledge
of functions that require this attribute, so this PR modifies Clang to
add the `returns_twice` attribute even if `-fno-builtin` is set. This
behavior is also consistent with what GCC does.

It's not (easily) possible to get the builtin information from
`Builtins.td` because `-fno-builtin` causes Clang to never initialize
any builtins, so functions never get tokenized as functions/builtins
that require `returns_twice`. Therefore, the most straightforward
solution is to explicitly hard code the function names that require
`returns_twice`.

Fixes #122840
2025-03-31 09:42:34 -07:00

60 lines
1.6 KiB
C

// RUN: %clang_cc1 -x c %s -triple x86_64-linux-gnu -emit-llvm -o - | FileCheck %s
// RUN: %clang_cc1 -x c %s -triple x86_64-linux-gnu -emit-llvm -fno-builtin -o - | FileCheck %s
// RUN: %clang_cc1 -x c++ %s -triple x86_64-linux-gnu -emit-llvm -o - | FileCheck %s
#ifdef __cplusplus
extern "C" {
#endif
struct __jmp_buf_tag { int n; };
struct __ucontext_t_tag { int n; };
int setjmp(struct __jmp_buf_tag*);
int sigsetjmp(struct __jmp_buf_tag*, int);
int _setjmp(struct __jmp_buf_tag*);
int __sigsetjmp(struct __jmp_buf_tag*, int);
int _setjmpex(struct __jmp_buf_tag* env);
int getcontext(struct __ucontext_t_tag*);
typedef struct __jmp_buf_tag jmp_buf[1];
typedef struct __jmp_buf_tag sigjmp_buf[1];
typedef struct __ucontext_t_tag ucontext_t[1];
#ifdef __cplusplus
}
#endif
void f(void) {
jmp_buf jb;
ucontext_t ut;
// CHECK: call {{.*}}@setjmp(
setjmp(jb);
// CHECK: call {{.*}}@sigsetjmp(
sigsetjmp(jb, 0);
// CHECK: call {{.*}}@_setjmp(
_setjmp(jb);
// CHECK: call {{.*}}@__sigsetjmp(
__sigsetjmp(jb, 0);
// CHECK: call {{.*}}@_setjmpex(
_setjmpex(jb);
// CHECK: call {{.*}}@getcontext(
getcontext(ut);
}
// CHECK: ; Function Attrs: returns_twice
// CHECK-NEXT: declare {{.*}} @setjmp(
// CHECK: ; Function Attrs: returns_twice
// CHECK-NEXT: declare {{.*}} @sigsetjmp(
// CHECK: ; Function Attrs: returns_twice
// CHECK-NEXT: declare {{.*}} @_setjmp(
// CHECK: ; Function Attrs: returns_twice
// CHECK-NEXT: declare {{.*}} @__sigsetjmp(
// CHECK: ; Function Attrs: returns_twice
// CHECK-NEXT: declare {{.*}} @_setjmpex(
// CHECK: ; Function Attrs: returns_twice
// CHECK-NEXT: declare {{.*}} @getcontext(