Skip to content

Commit cde7206

Browse files
committed
Update all tests to not use LLVM opts, and generally fix them up; allow fake use emission with disable-O0-optnone
1 parent fdfa695 commit cde7206

16 files changed

+213
-186
lines changed

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,11 @@ void CodeGenFunction::EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr) {
13531353
}
13541354

13551355
void CodeGenFunction::EmitFakeUse(Address Addr) {
1356+
// We do not emit a fake use if we want to apply optnone to this function,
1357+
// even if we might not apply it anyway due to minsize or similar attributes.
1358+
if (!CGM.getCodeGenOpts().DisableO0ImplyOptNone &&
1359+
CGM.getCodeGenOpts().OptimizationLevel == 0)
1360+
return;
13561361
auto NL = ApplyDebugLocation::CreateEmpty(*this);
13571362
llvm::Value *V = Builder.CreateLoad(Addr, "fake.use");
13581363
llvm::CallInst *C = Builder.CreateCall(CGM.getLLVMFakeUseFn(), {V});
@@ -1446,8 +1451,8 @@ static bool extendLifetime(const ASTContext &Context, const Decl *FuncDecl,
14461451
// Don't extend variables that exceed a certain size.
14471452
if (Context.getTypeSize(Ty) > maxFakeUseAggregateSize(Context))
14481453
return false;
1449-
// Do not extend variables in nodebug functions.
1450-
if (FuncDecl->hasAttr<NoDebugAttr>())
1454+
// Do not extend variables in nodebug or optnone functions.
1455+
if (FuncDecl->hasAttr<NoDebugAttr>() || FuncDecl->hasAttr<OptimizeNoneAttr>())
14511456
return false;
14521457
return true;
14531458
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %clang_cc1 %s -O0 -disable-O0-optnone -emit-llvm -fextend-lifetimes -fcxx-exceptions -fexceptions -o - | FileCheck %s
2+
// This test checks that the fake uses can be generated in exception handling
3+
// blocks and that we can emit fake uses for the __int128 data type.
4+
5+
extern int bar();
6+
7+
/// Try block: fake use ends at try-block scope.
8+
// [[BAR_VAL::%[a-zA-Z0-9\.]+]] = invoke{{.*}} i32 @_Z3barv()
9+
// store i32 %[[BAR_VAL]], ptr [[K_ALLOC_VAL:%[a-zA-Z0-9\.]+]], align 4
10+
// [[K_FAKE_USE:%[a-zA-Z0-9\.]+]] = load i32, ptr [[K_ALLOC_VAL]], align 4
11+
// call void (...) @llvm.fake.use(i32 [[K_FAKE_USE]]) #2
12+
// br label
13+
14+
/// Catch block: fetching the caught value...
15+
// CHECK: [[CATCH_PTR:%[a-zA-Z0-9\.]+]] = call ptr @__cxa_begin_catch(
16+
// CHECK: [[L_VAL:%[a-zA-Z0-9\.]+]] = load i32, ptr [[CATCH_PTR]], align 4
17+
18+
/// Storing to allocas...
19+
// CHECK-DAG: store i32 8, ptr [[M_ALLOC_VAL:%[a-zA-Z0-9\.]+]]
20+
// CHECK-DAG: store i32 [[L_VAL]], ptr [[L_ALLOC_VAL:%[a-zA-Z0-9\.]+]], align 4
21+
22+
/// Load into fake uses - expect M to precede L.
23+
// CHECK: [[M_FAKE_VAL:%[a-zA-Z0-9\.]+]] = load i32, ptr [[M_ALLOC_VAL]]
24+
// CHECK: call void (...) @llvm.fake.use(i32 [[M_FAKE_VAL]])
25+
// CHECK: [[L_FAKE_VAL:%[a-zA-Z0-9\.]+]] = load i32, ptr [[L_ALLOC_VAL]]
26+
// CHECK: call void (...) @llvm.fake.use(i32 [[L_FAKE_VAL]])
27+
void foo() {
28+
try {
29+
int k = bar();
30+
} catch (int l) {
31+
/// The catch block contains a fake use for the local within its scope.
32+
int m = 8;
33+
}
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 %s -disable-O0-optnone -emit-llvm -fextend-lifetimes -triple x86_64-unknown-linux -o - | FileCheck %s
2+
// REQUIRES: x86-registered-target
3+
// This test checks that the fake uses can be generated in exception handling
4+
// blocks and that we can emit fake uses for the __int128 data type.
5+
6+
void bar();
7+
8+
// CHECK: call void (...) @llvm.fake.use(i128 %
9+
void foo(__int128 wide_int) {
10+
bar();
11+
}

clang/test/CodeGen/extend-liveness1.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
// RUN: %clang_cc1 %s -O2 -emit-llvm -fextend-lifetimes -o - | FileCheck %s
1+
// RUN: %clang_cc1 %s -O0 -disable-O0-optnone -emit-llvm -fextend-lifetimes -o - | FileCheck %s --implicit-check-not=llvm.fake.use
22
// Check that fake use calls are emitted at the correct locations, i.e.
33
// at the end of lexical blocks and at the end of the function.
44

5-
extern int use(int);
6-
int glob1;
7-
int glob2;
8-
float globf;
5+
int glob_i;
6+
char glob_c;
7+
float glob_f;
98

109
int foo(int i) {
11-
// CHECK: define{{.*}}foo
10+
// CHECK-LABEL: define{{.*}}foo
1211
if (i < 4) {
13-
int j = i * 3;
14-
if (glob1 > 3) {
15-
float f = globf;
16-
// CHECK: [[SSAVAL:%[a-z0-9]*]] = load float{{.*}}globf
12+
char j = i * 3;
13+
if (glob_i > 3) {
14+
float f = glob_f;
1715
j = f;
18-
glob2 = j;
19-
// CHECK: store{{.*}}glob2
20-
// CHECK-NEXT: call void (...) @llvm.fake.use(float [[SSAVAL]])
16+
glob_c = j;
17+
// CHECK: call void (...) @llvm.fake.use(float %
18+
// CHECK-NEXT: br label %
2119
}
22-
glob1 = j;
23-
// CHECK: store{{.*}}glob1
24-
// CHECK-NEXT: call void (...) @llvm.fake.use(i32 %j.
20+
glob_i = j;
21+
// CHECK: call void (...) @llvm.fake.use(i8 %
22+
// CHECK-NEXT: br label %
2523
}
26-
// CHECK: call void (...) @llvm.fake.use(i32 %i)
24+
// CHECK: call void (...) @llvm.fake.use(i32 %
2725
// CHECK-NEXT: ret
2826
return 4;
2927
}
28+
29+
// CHECK: declare void @llvm.fake.use(...)

clang/test/CodeGen/extend-liveness2.cpp

Lines changed: 0 additions & 34 deletions
This file was deleted.

clang/test/CodeGen/fake-use-determinism.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
// RUN: %clang -S -O2 -emit-llvm -fextend-lifetimes %s -o - | FileCheck %s
2-
// REQUIRES: asserts
1+
// RUN: %clang_cc1 -O0 -disable-O0-optnone -emit-llvm -fextend-lifetimes %s -o - | FileCheck %s
32
//
43
// We are checking that the fake.use calls for i, j and k appear
54
// in a particular order. It is not the order itself that is important
65
// but that it remains the same between different test runs.
76

8-
// CHECK: call {{.*}}void (...) @llvm.fake.use(i32 %k)
9-
// CHECK-NEXT: call {{.*}}void (...) @llvm.fake.use(i32 %j)
10-
// CHECK-NEXT: call {{.*}}void (...) @llvm.fake.use(i32 %i)
7+
// CHECK: [[K_FAKE_USE:%[a-zA-Z0-9\.]+]] = load i32, ptr %k.addr
8+
// CHECK-NEXT: call void (...) @llvm.fake.use(i32 [[K_FAKE_USE]]) #2
9+
// CHECK-NEXT: [[J_FAKE_USE:%[a-zA-Z0-9\.]+]] = load i32, ptr %j.addr
10+
// CHECK-NEXT: call void (...) @llvm.fake.use(i32 [[J_FAKE_USE]]) #2
11+
// CHECK-NEXT: [[I_FAKE_USE:%[a-zA-Z0-9\.]+]] = load i32, ptr %i.addr
12+
// CHECK-NEXT: call void (...) @llvm.fake.use(i32 [[I_FAKE_USE]]) #2
1113

12-
extern void bar();
14+
void bar();
1315
void foo(int i, int j, int k)
1416
{
1517
for (int l = 0; l < i; l++) {

clang/test/CodeGen/fake-use-lambda.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ int func(int param) {
2727
// The mangled function name contains a $_0, followed by 'cl'.
2828
// This lambda is an orphaned lambda, i.e. one without lexical parent.
2929
//
30-
// CHECK: define internal {{.+\"_Z.+\$_0.*cl.*\"}}
31-
// CHECK-NOT: ret
32-
// CHECK: fake.use(i32
33-
// CHECK-NOT: ret
34-
// CHECK: fake.use(ptr
30+
// CHECK-LABEL: define internal {{.+\"_Z.+\$_0.*cl.*\"}}
31+
// CHECK-NOT: ret
32+
// CHECK: fake.use(i32
33+
// CHECK-NOT: ret
34+
// CHECK: fake.use(ptr
3535

3636
// The second lambda. We are looking for 3 fake uses.
37-
// CHECK: define internal {{.+\"_Z.+\$_0.*cl.*\"}}
38-
// CHECK-NOT: ret
39-
// CHECK: fake.use(i32
40-
// CHECK-NOT: ret
41-
// CHECK: fake.use(i32
42-
// CHECK-NOT: ret
43-
// CHECK: fake.use(ptr
37+
// CHECK-LABEL: define internal {{.+\"_Z.+\$_0.*cl.*\"}}
38+
// CHECK-NOT: ret
39+
// CHECK: fake.use(i32
40+
// CHECK-NOT: ret
41+
// CHECK: fake.use(i32
42+
// CHECK-NOT: ret
43+
// CHECK: fake.use(ptr

clang/test/CodeGen/fake-use-landingpad.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 %s -O3 -emit-llvm -fextend-lifetimes -fexceptions -o - | FileCheck %s
1+
// RUN: %clang_cc1 %s -O0 -disable-O0-optnone -emit-llvm -fextend-lifetimes -fexceptions -o - | FileCheck %s --implicit-check-not=landingpad
22

33
// Check that fake uses do not mistakenly cause a landing pad to be generated when
44
// exceptions are enabled.
@@ -11,6 +11,4 @@ void foo(int p) {
1111

1212
// CHECK: define {{.*}} @foo
1313
// CHECK-NOT: personality
14-
// CHECK: entry:
15-
// CHECK: llvm.fake.use
16-
// CHECK-NOT: landingpad
14+
// CHECK: call void (...) @llvm.fake.use

clang/test/CodeGen/fake-use-noreturn.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
// RUN: %clang %s -S -emit-llvm -fextend-lifetimes -O2 -o - -fno-discard-value-names | FileCheck %s
1+
// RUN: %clang_cc1 %s -emit-llvm -fextend-lifetimes -O0 -disable-O0-optnone -o - | FileCheck %s
22
//
33
// Check we can correctly produce fake uses for function-level variables even
44
// when we have a return in a nested conditional and there is no code at the end
55
// of the function.
66

77
// CHECK-LABEL: define{{.*}}@_Z3fooi
8-
// CHECK: call{{.*}}llvm.fake.use(i32 %i)
8+
// CHECK: [[I_FAKE_USE:%[a-zA-Z0-9\.]+]] = load i32, ptr %i.addr
9+
// CHECK: call void (...) @llvm.fake.use(i32 [[I_FAKE_USE]])
910
// CHECK-LABEL: define{{.*}}@_ZN1C3barEi
10-
// CHECK-DAG: call{{.*}}llvm.fake.use(i32 %i)
11-
// CHECK-DAG: call{{.*}}llvm.fake.use({{.*}}%this)
11+
// CHECK: [[J_FAKE_USE:%[a-zA-Z0-9\.]+]] = load i32, ptr %j.addr
12+
// CHECK: call void (...) @llvm.fake.use(i32 [[J_FAKE_USE]])
1213

1314
void foo(int i) {
1415
while (0)
@@ -17,10 +18,10 @@ void foo(int i) {
1718
}
1819

1920
class C {
20-
void bar(int i);
21+
void bar(int j);
2122
};
2223

23-
void C::bar(int i) {
24+
void C::bar(int j) {
2425
while (0)
2526
if (1)
2627
return;

clang/test/CodeGen/fake-use-return-line.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// RUN: %clang_cc1 -emit-llvm -O2 -debug-info-kind=limited -fextend-lifetimes -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -emit-llvm -O0 -disable-O0-optnone -debug-info-kind=limited -fextend-lifetimes -o - %s | FileCheck %s
22

33
// Clang adjusts the line numbers of returns based on the line numbers of
44
// dominating stores to %retval; we test that fake use intrinsics do not affect
55
// this, and the return is given the correct line.
66

77
// CHECK: define{{.*}}@main
8-
// CHECK: ret i32{{.*}}!dbg ![[MDINDEX:[0-9]*]]
8+
// CHECK: call void (...) @llvm.fake.use(i32
9+
// CHECK-NEXT: ret i32{{.*}}!dbg ![[MDINDEX:[0-9]*]]
910
// CHECK: ![[MDINDEX]] = !DILocation(line: [[# @LINE + 5]]
1011
int main()
1112
{
Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,61 @@
1-
// RUN: %clang_cc1 %s -O1 -emit-llvm -fextend-lifetimes -fsanitize=null -fsanitize-trap=null -o - | FileCheck --check-prefix TRAP %s
2-
// RUN: %clang_cc1 %s -O1 -emit-llvm -fextend-lifetimes -o - | FileCheck --check-prefix FAKEUSE %s
1+
// RUN: %clang_cc1 %s -O0 -disable-O0-optnone -emit-llvm -fextend-lifetimes -fsanitize=null -fsanitize-trap=null -o - | FileCheck --check-prefixes=CHECK,NULL --implicit-check-not=ubsantrap %s
2+
// RUN: %clang_cc1 %s -O0 -disable-O0-optnone -emit-llvm -fextend-lifetimes -o - | FileCheck %s
33

4-
// With -fextend-lifetimes the compiler generated a fake.use of a
5-
// reference variable at the end of the function, in the cleanup block. This prompted the
6-
// address sanitizer to verify that the variable has been allocated properly, even when
7-
// the function returns early.
8-
// We check that sanitizers are disabled for code generated for the benefit of fake.use
9-
// intrinsics, as well as that the fake.use is no longer generated in the cleanup block.
10-
// It should be generated in the block preceding the cleanup block instead.
4+
// With -fextend-lifetimes, the compiler previously generated a fake.use of any
5+
// reference variable at the end of the scope in which its alloca exists. This
6+
// caused two issues, where we would get fake uses for uninitialized variables
7+
// if that variable was declared after an early-return, and UBSan's null checks
8+
// would complain about this.
9+
// This test verifies that UBSan does not produce null-checks for arguments to
10+
// llvm.fake.use, and that fake uses are not emitted for a variable on paths
11+
// it has not been declared.
1112

1213
struct A { short s1, s2; };
13-
extern A& getA();
14+
extern long& getA();
1415

1516
void foo()
1617
{
1718
auto& va = getA();
18-
short s = va.s1 & ~va.s2;
19-
if (s == 0)
19+
if (va < 5)
2020
return;
2121

2222
auto& vb = getA();
2323
}
2424

25-
// TRAP: define{{.*}}foo
26-
// TRAP: [[COMPARE:%[^\s]*]] = icmp eq
27-
// TRAP-NOT: br i1 [[COMPARE]]{{.*}}trap
28-
// TRAP: br i1 [[COMPARE]]{{.*}}%if.end
29-
// TRAP-NOT: trap:
30-
// TRAP: if.end:
31-
// TRAP-NOT: call{{.*}}llvm.trap
32-
33-
// FAKEUSE: if.end:
34-
// FAKEUSE-NEXT: [[CALL:%[^\s]*]] = {{.*}}call{{.*}}getA
35-
// FAKEUSE-NOT: br{{.*}}cleanup
36-
// FAKEUSE: call{{.*}}fake.use({{.*}}[[CALL]])
37-
// FAKEUSE: cleanup:
25+
// CHECK-LABEL: define{{.*}}foo
26+
// CHECK: [[VA_CALL:%.+]] = call{{.*}} ptr @_Z4getAv()
27+
28+
/// We check here for the first UBSan check for "va".
29+
// NULL: [[VA_ISNULL:%.+]] = icmp ne ptr [[VA_CALL]], null
30+
// NULL: br i1 [[VA_ISNULL]], label %{{[^,]+}}, label %[[VA_TRAP:[^,]+]]
31+
// NULL: [[VA_TRAP]]:
32+
// NULL: call void @llvm.ubsantrap(
33+
34+
// CHECK: [[VA_PTR:%.+]] = load ptr, ptr %va
35+
// CHECK-NEXT: [[VA_CMP:%.+]] = load i64, ptr [[VA_PTR]]
36+
// CHECK-NEXT: [[VA_CMP_RES:%.+]] = icmp slt i64 [[VA_CMP]], 5
37+
// CHECK-NEXT: br i1 [[VA_CMP_RES]], label %[[EARLY_EXIT:[^,]+]], label %[[NOT_EARLY_EXIT:[^,]+]]
38+
39+
// CHECK: [[EARLY_EXIT]]:
40+
// CHECK: br label %cleanup
41+
42+
/// The fake use for "vb" only appears on the path where its declaration is
43+
/// reached.
44+
// CHECK: [[NOT_EARLY_EXIT]]:
45+
// CHECK: [[VB_CALL:%.+]] = call{{.*}} ptr @_Z4getAv()
46+
47+
/// We check here for the second UBSan check for "vb".
48+
// NULL: [[VB_ISNULL:%.+]] = icmp ne ptr [[VB_CALL]], null
49+
// NULL: br i1 [[VB_ISNULL]], label %{{[^,]+}}, label %[[VB_TRAP:[^,]+]]
50+
// NULL: [[VB_TRAP]]:
51+
// NULL: call void @llvm.ubsantrap(
52+
53+
// CHECK: [[VB_FAKE_USE:%.+]] = load ptr, ptr %vb
54+
// CHECK-NEXT: call void (...) @llvm.fake.use(ptr [[VB_FAKE_USE]])
55+
// CHECK: br label %cleanup
56+
57+
// CHECK: cleanup:
58+
// CHECK: [[VA_FAKE_USE:%.+]] = load ptr, ptr %va
59+
// CHECK-NEXT: call void (...) @llvm.fake.use(ptr [[VA_FAKE_USE]])
60+
61+
// NULL: declare void @llvm.ubsantrap

0 commit comments

Comments
 (0)