xref: /llvm-project/clang/test/CodeGenCXX/static-init-wasm.cpp (revision 1b9a6e58a8b831193c9e5e733f881aabe0d2d06b)
1 // RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -target-feature +atomics -target-feature +bulk-memory -o - %s \
2 // RUN:   | FileCheck %s -check-prefix=WEBASSEMBLY32
3 // RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -target-feature +atomics -target-feature +bulk-memory -o - %s \
4 // RUN:   | FileCheck %s -check-prefix=WEBASSEMBLY64
5 
6 // Test that we don't create common blocks.
7 int tentative;
8 // WEBASSEMBLY32: @tentative = global i32 0, align 4
9 // WEBASSEMBLY64: @tentative = global i32 0, align 4
10 
11 // Test that WebAssembly uses the ARM-style ABI in which the static
12 // variable's guard variable is tested via "load i8 and test the
13 // bottom bit" rather than the Itanium/x86 ABI which uses "load i8
14 // and compare with zero".
15 int f();
g()16 void g() {
17   static int a = f();
18 }
19 // WEBASSEMBLY32-LABEL: @_Z1gv()
20 // WEBASSEMBLY32:       %[[R0:.+]] = load atomic i8, ptr @_ZGVZ1gvE1a acquire, align 4
21 // WEBASSEMBLY32-NEXT:  %[[R1:.+]] = and i8 %[[R0]], 1
22 // WEBASSEMBLY32-NEXT:  %[[R2:.+]] = icmp eq i8 %[[R1]], 0
23 // WEBASSEMBLY32-NEXT:  br i1 %[[R2]], label %[[CHECK:.+]], label %[[END:.+]],
24 // WEBASSEMBLY32:       [[CHECK]]
25 // WEBASSEMBLY32:       call i32 @__cxa_guard_acquire
26 // WEBASSEMBLY32:       [[END]]
27 // WEBASSEMBLY32:       call void @__cxa_guard_release
28 //
29 // WEBASSEMBLY64-LABEL: @_Z1gv()
30 // WEBASSEMBLY64:       %[[R0:.+]] = load atomic i8, ptr @_ZGVZ1gvE1a acquire, align 8
31 // WEBASSEMBLY64-NEXT:  %[[R1:.+]] = and i8 %[[R0]], 1
32 // WEBASSEMBLY64-NEXT:  %[[R2:.+]] = icmp eq i8 %[[R1]], 0
33 // WEBASSEMBLY64-NEXT:  br i1 %[[R2]], label %[[CHECK:.+]], label %[[END:.+]],
34 // WEBASSEMBLY64:       [[CHECK]]
35 // WEBASSEMBLY64:       call i32 @__cxa_guard_acquire
36 // WEBASSEMBLY64:       [[END]]
37 // WEBASSEMBLY64:       call void @__cxa_guard_release
38 
39 // Test various aspects of static constructor calls.
40 struct A {
41   A();
42 };
43 
44 A theA;
45 
46 // WEBASSEMBLY32: define internal void @__cxx_global_var_init() #3 {
47 // WEBASSEMBLY32: call noundef ptr @_ZN1AC1Ev(ptr {{[^,]*}} @theA)
48 // WEBASSEMBLY32: define internal void @_GLOBAL__sub_I_static_init_wasm.cpp() #3 {
49 // WEBASSEMBLY32: call void @__cxx_global_var_init()
50 //
51 // WEBASSEMBLY64: define internal void @__cxx_global_var_init() #3 {
52 // WEBASSEMBLY64: call noundef ptr @_ZN1AC1Ev(ptr {{[^,]*}} @theA)
53 // WEBASSEMBLY64: define internal void @_GLOBAL__sub_I_static_init_wasm.cpp() #3 {
54 // WEBASSEMBLY64: call void @__cxx_global_var_init()
55 
56 // RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -target-feature +bulk-memory -o - %s \
57 // RUN:   | FileCheck %s -check-prefix=NOATOMICS
58 // RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -target-feature +bulk-memory -o - %s \
59 // RUN:   | FileCheck %s -check-prefix=NOATOMICS
60 
61 // NOATOMICS-LABEL: @_Z1gv()
62 // NOATOMICS:       %[[R0:.+]] = load i8, ptr @_ZGVZ1gvE1a, align 1
63 // NOATOMICS-NEXT:  %guard.uninitialized = icmp eq i8 %[[R0]], 0
64 // NOATOMICS-NEXT:  br i1 %guard.uninitialized, label %[[CHECK:.+]], label %[[END:.+]],
65 // NOATOMICS:       [[CHECK]]:
66 // NOATOMICS-NOT:   __cxa_guard_acquire
67 // NOATOMICS:       [[END]]:
68 // NOATOMICS-NEXT:  ret void
69 
70 // RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -target-feature +atomics -o - %s \
71 // RUN:   | FileCheck %s -check-prefix=NOBULKMEM
72 // RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -target-feature +atomics -o - %s \
73 // RUN:   | FileCheck %s -check-prefix=NOBULKMEM
74 
75 // NOBULKMEM-LABEL: @_Z1gv()
76 // NOBULKMEM:       %[[R0:.+]] = load i8, ptr @_ZGVZ1gvE1a, align 1
77 // NOBULKMEM-NEXT:  %guard.uninitialized = icmp eq i8 %[[R0]], 0
78 // NOBULKMEM-NEXT:  br i1 %guard.uninitialized, label %[[CHECK:.+]], label %[[END:.+]],
79 // NOBULKMEM:       [[CHECK]]:
80 // NOBULKMEM-NOT:   __cxa_guard_acquire
81 // NOBULKMEM:       [[END]]:
82 // NOBULKMEM-NEXT:  ret void
83