Skip to content

Commit ed6b578

Browse files
committed
[CodeGen] Emit a call instruction instead of an invoke if the called
llvm function is marked nounwind This fixes cases where an invoke is emitted, despite the called llvm function being marked nounwind, because ConstructAttributeList failed to add the attribute to the attribute list. llvm optimization passes turn invokes into calls and optimize away the exception handling code, but it's better to avoid emitting the code in the front-end if the called function is known not to raise an exception. Differential Revision: https://reviews.llvm.org/D83906
1 parent 2815429 commit ed6b578

20 files changed

+31
-24
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4841,6 +4841,10 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
48414841
} else {
48424842
// Otherwise, nounwind call sites will never throw.
48434843
CannotThrow = Attrs.hasFnAttribute(llvm::Attribute::NoUnwind);
4844+
4845+
if (auto *FPtr = dyn_cast<llvm::Function>(CalleePtr))
4846+
if (FPtr->hasFnAttribute(llvm::Attribute::NoUnwind))
4847+
CannotThrow = true;
48444848
}
48454849

48464850
// If we made a temporary, be sure to clean up after ourselves. Note that we

clang/test/CodeGenCXX/debug-info-class.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class B {
1313
virtual ~B();
1414
};
1515

16-
B::~B() {
16+
B::~B() { extern void mayThrow(); mayThrow();
1717
}
1818

1919
struct C {

clang/test/CodeGenObjCXX/arc-list-init-destruct.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ @interface Class0;
1616
};
1717

1818
bool getBool() {
19+
extern void mayThrow();
20+
mayThrow();
1921
return false;
2022
}
2123

clang/test/CodeGenObjCXX/os_log.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
void release(int *lock);
77

88
// CHECK-LABEL: define {{.*}} @_ZN13no_eh_cleanup3logERiPcS1_(
9+
// CHECK: call void @__os_log_helper_1_2_2_4_0_8_34(
10+
911
void log(int &i, char *data, char *buf) {
1012
int lock __attribute__((cleanup(release)));
1113
__builtin_os_log_format(buf, "%d %{public}s", i, data);
1214
}
1315

14-
// An `invoke` of a `nounwind` callee is simplified to a direct
15-
// call by an optimization in llvm. Just check that we emit `nounwind`.
16+
// Check that the os_log_helper is marked `nounwind`.
1617
// CHECK: define {{.*}} @__os_log_helper_1_2_2_4_0_8_34({{.*}} [[NUW:#[0-9]+]]
1718
}
1819

clang/test/OpenMP/atomic_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void parallel_atomic_ewc() {
8282
}
8383
}
8484

85-
int &foo() { return a; }
85+
int &foo() { extern void mayThrow(); mayThrow(); return a; }
8686

8787
// TERM_DEBUG-LABEL: parallel_atomic
8888
void parallel_atomic() {

clang/test/OpenMP/critical_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
// ALL: define {{.*}}void [[FOO:@.+]]()
2424

25-
void foo() {}
25+
void foo() { extern void mayThrow(); mayThrow(); }
2626

2727
// ALL-LABEL: @main
2828
// TERM_DEBUG-LABEL: @main

clang/test/OpenMP/distribute_parallel_for_num_threads_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void foo();
2222
struct S {
2323
intptr_t a, b, c;
2424
S(intptr_t a) : a(a) {}
25-
operator char() { return a; }
25+
operator char() { extern void mayThrow(); mayThrow(); return a; }
2626
~S() {}
2727
};
2828

clang/test/OpenMP/distribute_parallel_for_simd_num_threads_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void foo();
2222
struct S {
2323
intptr_t a, b, c;
2424
S(intptr_t a) : a(a) {}
25-
operator char() { return a; }
25+
operator char() { extern void mayThrow(); mayThrow(); return a; }
2626
~S() {}
2727
};
2828

clang/test/OpenMP/for_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ void test_precond() {
536536
}
537537

538538
// TERM_DEBUG-LABEL: foo
539-
int foo() {return 0;};
539+
int foo() { extern void mayThrow(); mayThrow(); return 0;};
540540

541541
// TERM_DEBUG-LABEL: parallel_for
542542
void parallel_for(float *a) {

clang/test/OpenMP/for_simd_codegen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#ifndef HEADER
2121
#define HEADER
2222

23-
long long get_val() { return 0; }
23+
long long get_val() { extern void mayThrow(); mayThrow(); return 0; }
2424
double *g_ptr;
2525

2626
// CHECK-LABEL: define {{.*void}} @{{.*}}simple{{.*}}(float* {{.+}}, float* {{.+}}, float* {{.+}}, float* {{.+}})
@@ -785,7 +785,7 @@ void widened(float *a, float *b, float *c, float *d) {
785785
}
786786

787787
// TERM_DEBUG-LABEL: bar
788-
int bar() {return 0;};
788+
int bar() { extern void mayThrow(); mayThrow(); return 0; };
789789

790790
// TERM_DEBUG-LABEL: parallel_simd
791791
void parallel_simd(float *a) {

clang/test/OpenMP/master_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
// ALL: define {{.*}}void [[FOO:@.+]]()
2121

22-
void foo() {}
22+
void foo() { extern void mayThrow(); mayThrow(); }
2323

2424
// ALL-LABEL: @main
2525
// TERM_DEBUG-LABEL: @main

clang/test/OpenMP/parallel_for_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ void runtime(float *a, float *b, float *c, float *d) {
372372
}
373373

374374
// TERM_DEBUG-LABEL: foo
375-
int foo() {return 0;};
375+
int foo() { extern void mayThrow(); mayThrow(); return 0; };
376376

377377
// TERM_DEBUG-LABEL: parallel_for
378378
// CLEANUP: parallel_for

clang/test/OpenMP/parallel_for_simd_codegen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#ifndef HEADER
2323
#define HEADER
2424

25-
long long get_val() { return 0; }
25+
long long get_val() { extern void mayThrow(); mayThrow(); return 0; }
2626
double *g_ptr;
2727

2828
// CHECK-LABEL: define {{.*void}} @{{.*}}simple{{.*}}(float* {{.+}}, float* {{.+}}, float* {{.+}}, float* {{.+}})
@@ -801,7 +801,7 @@ for (int i = 0; i < 10; ++i);
801801
// OMP50-DAG: ![[NOVM]] = !{!"llvm.loop.vectorize.enable", i1 false}
802802

803803
// TERM_DEBUG-LABEL: bar
804-
int bar() {return 0;};
804+
int bar() { extern void mayThrow(); mayThrow(); return 0; };
805805

806806
// TERM_DEBUG-LABEL: parallel_simd
807807
void parallel_simd(float *a) {

clang/test/OpenMP/parallel_master_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// CK1-DAG: [[DEF_LOC:@.+]] = private unnamed_addr global %struct.ident_t { i32 0, i32 2, i32 0, i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* [[STR]], i32 0, i32 0) }
1919

2020
// CK1-LABEL: foo
21-
void foo() {}
21+
void foo() { extern void mayThrow(); mayThrow(); }
2222

2323
void parallel_master() {
2424
#pragma omp parallel master

clang/test/OpenMP/parallel_num_threads_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void foo();
2222
struct S {
2323
intptr_t a, b, c;
2424
S(intptr_t a) : a(a) {}
25-
operator char() { return a; }
25+
operator char() { extern void mayThrow(); mayThrow(); return a; }
2626
~S() {}
2727
};
2828

clang/test/OpenMP/parallel_sections_codegen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#ifndef HEADER
1111
#define HEADER
1212
// CHECK-LABEL: foo
13-
void foo() {};
13+
void foo() { extern void mayThrow(); mayThrow(); };
1414
// CHECK-LABEL: bar
15-
void bar() {};
15+
void bar() { extern void mayThrow(); mayThrow(); };
1616

1717
template <class T>
1818
T tmain() {

clang/test/OpenMP/sections_codegen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
// CHECK-DAG: [[IMPLICIT_BARRIER_SECTIONS_LOC:@.+]] = private unnamed_addr global %{{.+}} { i32 0, i32 194, i32 0, i32 0, i8*
1313
// CHECK-DAG: [[SECTIONS_LOC:@.+]] = private unnamed_addr global %{{.+}} { i32 0, i32 1026, i32 0, i32 0, i8*
1414
// CHECK-LABEL: foo
15-
void foo() {};
15+
void foo() { extern void mayThrow(); mayThrow(); };
1616
// CHECK-LABEL: bar
17-
void bar() {};
17+
void bar() { extern void mayThrow(); mayThrow(); };
1818

1919
template <class T>
2020
T tmain() {

clang/test/OpenMP/simd_codegen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
// OMP50-DAG: [[LAST_IV:@.+]] = {{.*}}common global i64 0
2727
// OMP50-DAG: [[LAST_A:@.+]] = {{.*}}common global i32 0
2828

29-
long long get_val() { return 0; }
29+
long long get_val() { extern void mayThrow(); mayThrow(); return 0; }
3030
double *g_ptr;
3131

3232
struct S {
@@ -798,7 +798,7 @@ void bartfoo() {
798798

799799
#endif // OMP5
800800
// TERM_DEBUG-LABEL: bar
801-
int bar() {return 0;};
801+
int bar() { extern void mayThrow(); mayThrow(); return 0; };
802802

803803
// TERM_DEBUG-LABEL: parallel_simd
804804
void parallel_simd(float *a) {

clang/test/OpenMP/single_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ TestClass tc;
4242
TestClass tc2[2];
4343
#pragma omp threadprivate(tc, tc2)
4444

45-
void foo() {}
45+
void foo() { extern void mayThrow(); mayThrow(); }
4646

4747
struct SS {
4848
int a;

clang/test/OpenMP/taskgroup_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
// CHECK: define {{.*}}void [[FOO:@.+]]()
1818

19-
void foo() {}
19+
void foo() { extern void mayThrow(); mayThrow(); }
2020

2121
// CHECK-LABEL: @main
2222
// TERM_DEBUG-LABEL: @main

0 commit comments

Comments
 (0)