| 1 |
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %s -o %t.cir 2> %t-before.cir |
| 2 |
// RUN: FileCheck --check-prefix=CIR-BEFORE-LPP --input-file=%t-before.cir %s |
| 3 |
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s |
| 4 |
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -O1 %s -o %t-o1.cir |
| 5 |
// RUN: FileCheck --check-prefix=CIR-O1 --input-file=%t-o1.cir %s |
| 6 |
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll |
| 7 |
// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s |
| 8 |
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll |
| 9 |
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s |
| 10 |
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm -O1 -disable-llvm-passes %s -o %t-opt.ll |
| 11 |
// RUN: FileCheck --check-prefix=LLVM-OPT --input-file=%t-opt.ll %s |
| 12 |
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm -O1 -disable-llvm-passes %s -o %t-opt-ogcg.ll |
| 13 |
// RUN: FileCheck --check-prefix=OGCG-OPT --input-file=%t-opt-ogcg.ll %s |
| 14 |
|
| 15 |
// Test for global with constant storage - const object with constructor but no destructor |
| 16 |
// Check that we add an llvm.invariant.start to mark when a global becomes read-only. |
| 17 |
|
| 18 |
struct A { |
| 19 |
A(); |
| 20 |
int n; |
| 21 |
}; |
| 22 |
|
| 23 |
// Should emit invariant.start - has constructor, no destructor, no mutable |
| 24 |
extern const A a = A(); |
| 25 |
|
| 26 |
struct A2 { |
| 27 |
A2(); |
| 28 |
constexpr ~A2() {} |
| 29 |
int n; |
| 30 |
}; |
| 31 |
|
| 32 |
// Should emit invariant.start - constexpr destructor doesn't prevent constant storage |
| 33 |
extern const A2 a2 = A2(); |
| 34 |
|
| 35 |
struct B { |
| 36 |
B(); |
| 37 |
mutable int n; |
| 38 |
}; |
| 39 |
|
| 40 |
// Should NOT emit invariant.start - has mutable member |
| 41 |
extern const B b = B(); |
| 42 |
|
| 43 |
// Simple case - just const C c; (no initializer) - Andy's suggestion |
| 44 |
class C { |
| 45 |
public: |
| 46 |
C(); |
| 47 |
int a; |
| 48 |
int b; |
| 49 |
}; |
| 50 |
|
| 51 |
const C c; |
| 52 |
|
| 53 |
// CIR checks before LoweringPrepare transformation - globals have ctor regions |
| 54 |
// Test case 'a' - before LoweringPrepare |
| 55 |
// CIR-BEFORE-LPP: cir.global external @a = ctor : !rec_A { |
| 56 |
// CIR-BEFORE-LPP: %[[OBJ:.*]] = cir.get_global @a : !cir.ptr<!rec_A> |
| 57 |
// CIR-BEFORE-LPP: cir.call @_ZN1AC1Ev(%[[OBJ]]) : (!cir.ptr<!rec_A>) -> () |
| 58 |
// CIR-BEFORE-LPP: %[[OBJ2:.*]] = cir.get_global @a : !cir.ptr<!rec_A> |
| 59 |
// CIR-BEFORE-LPP: } |
| 60 |
|
| 61 |
// Test case 'a2' - before LoweringPrepare |
| 62 |
// CIR-BEFORE-LPP: cir.global external @a2 = ctor : !rec_A2 { |
| 63 |
// CIR-BEFORE-LPP: %[[OBJ:.*]] = cir.get_global @a2 : !cir.ptr<!rec_A2> |
| 64 |
// CIR-BEFORE-LPP: cir.call @_ZN2A2C1Ev(%[[OBJ]]) : (!cir.ptr<!rec_A2>) -> () |
| 65 |
// CIR-BEFORE-LPP: %[[OBJ2:.*]] = cir.get_global @a2 : !cir.ptr<!rec_A2> |
| 66 |
// CIR-BEFORE-LPP: } |
| 67 |
|
| 68 |
// Test case 'b' - before LoweringPrepare |
| 69 |
// CIR-BEFORE-LPP: cir.global external @b = ctor : !rec_B { |
| 70 |
// CIR-BEFORE-LPP: %[[OBJ:.*]] = cir.get_global @b : !cir.ptr<!rec_B> |
| 71 |
// CIR-BEFORE-LPP: cir.call @_ZN1BC1Ev(%[[OBJ]]) : (!cir.ptr<!rec_B>) -> () |
| 72 |
// CIR-BEFORE-LPP: } |
| 73 |
|
| 74 |
// Test case 'c' - before LoweringPrepare (internal linkage) |
| 75 |
// CIR-BEFORE-LPP: cir.global {{.*}} internal {{.*}} @_ZL1c = ctor : !rec_C { |
| 76 |
// CIR-BEFORE-LPP: %[[OBJ:.*]] = cir.get_global @_ZL1c : !cir.ptr<!rec_C> |
| 77 |
// CIR-BEFORE-LPP: cir.call @_ZN1CC1Ev(%[[OBJ]]) : (!cir.ptr<!rec_C>) -> () |
| 78 |
// CIR-BEFORE-LPP: %[[OBJ2:.*]] = cir.get_global @_ZL1c : !cir.ptr<!rec_C> |
| 79 |
// CIR-BEFORE-LPP: } |
| 80 |
|
| 81 |
// Check all globals first (they appear at the top of LLVM/OGCG output) |
| 82 |
// LLVM: @a ={{.*}} global {{.*}} zeroinitializer |
| 83 |
// LLVM: @a2 ={{.*}} global {{.*}} zeroinitializer |
| 84 |
// LLVM: @b ={{.*}} global {{.*}} zeroinitializer |
| 85 |
// LLVM: @_ZL1c ={{.*}} global {{.*}} zeroinitializer |
| 86 |
|
| 87 |
// OGCG: @a ={{.*}} global {{.*}} zeroinitializer |
| 88 |
// OGCG: @a2 ={{.*}} global {{.*}} zeroinitializer |
| 89 |
// OGCG: @b ={{.*}} global {{.*}} zeroinitializer |
| 90 |
// OGCG: @_ZL1c ={{.*}} global {{.*}} zeroinitializer |
| 91 |
|
| 92 |
// Test case 'a' - should have constant storage |
| 93 |
// CIR checks for 'a' |
| 94 |
// CIR: cir.global external @a = #cir.zero : !rec_A |
| 95 |
// CIR: cir.func internal private @__cxx_global_var_init() { |
| 96 |
// CIR: %[[OBJ:.*]] = cir.get_global @a : !cir.ptr<!rec_A> |
| 97 |
// CIR: cir.call @_ZN1AC1Ev(%[[OBJ]]) : (!cir.ptr<!rec_A>) -> () |
| 98 |
// CIR: cir.return |
| 99 |
// CIR: } |
| 100 |
|
| 101 |
// LLVM checks for 'a' (no optimization) |
| 102 |
// LLVM: define internal void @__cxx_global_var_init() { |
| 103 |
// LLVM: call void @_ZN1AC1Ev(ptr @a) |
| 104 |
// LLVM: ret void |
| 105 |
// LLVM: } |
| 106 |
|
| 107 |
// OGCG checks for 'a' (no optimization) |
| 108 |
// OGCG: define internal void @__cxx_global_var_init() {{.*}} section ".text.startup" { |
| 109 |
// OGCG: call void @_ZN1AC1Ev(ptr noundef nonnull align 4 dereferenceable(4) @a) |
| 110 |
// OGCG: ret void |
| 111 |
// OGCG: } |
| 112 |
|
| 113 |
// Test case 'a2' - should have constant storage (constexpr dtor) |
| 114 |
// CIR checks for 'a2' |
| 115 |
// CIR: cir.global external @a2 = #cir.zero : !rec_A2 |
| 116 |
// CIR: cir.func internal private @__cxx_global_var_init.1() { |
| 117 |
// CIR: %[[OBJ:.*]] = cir.get_global @a2 : !cir.ptr<!rec_A2> |
| 118 |
// CIR: cir.call @_ZN2A2C1Ev(%[[OBJ]]) : (!cir.ptr<!rec_A2>) -> () |
| 119 |
// CIR: cir.return |
| 120 |
// CIR: } |
| 121 |
|
| 122 |
// LLVM checks for 'a2' (no optimization) |
| 123 |
// LLVM: define internal void @__cxx_global_var_init.1() { |
| 124 |
// LLVM: call void @_ZN2A2C1Ev(ptr @a2) |
| 125 |
// LLVM: ret void |
| 126 |
// LLVM: } |
| 127 |
|
| 128 |
// OGCG checks for 'a2' (no optimization) |
| 129 |
// OGCG: define internal void @__cxx_global_var_init.1() {{.*}} section ".text.startup" { |
| 130 |
// OGCG: call void @_ZN2A2C1Ev(ptr noundef nonnull align 4 dereferenceable(4) @a2) |
| 131 |
// OGCG: ret void |
| 132 |
// OGCG: } |
| 133 |
|
| 134 |
// Test case 'b' - should NOT have constant storage (mutable member) |
| 135 |
// CIR checks for 'b' |
| 136 |
// CIR: cir.global external @b = #cir.zero : !rec_B |
| 137 |
// CIR: cir.func internal private @__cxx_global_var_init.2() { |
| 138 |
// CIR: %[[OBJ:.*]] = cir.get_global @b : !cir.ptr<!rec_B> |
| 139 |
// CIR: cir.call @_ZN1BC1Ev(%[[OBJ]]) : (!cir.ptr<!rec_B>) -> () |
| 140 |
// CIR: cir.return |
| 141 |
// CIR: } |
| 142 |
|
| 143 |
// LLVM checks for 'b' (no optimization) |
| 144 |
// LLVM: define internal void @__cxx_global_var_init.2() { |
| 145 |
// LLVM: call void @_ZN1BC1Ev(ptr @b) |
| 146 |
// LLVM: ret void |
| 147 |
// LLVM: } |
| 148 |
|
| 149 |
// OGCG checks for 'b' (no optimization) |
| 150 |
// OGCG: define internal void @__cxx_global_var_init.2() {{.*}} section ".text.startup" { |
| 151 |
// OGCG: call void @_ZN1BC1Ev(ptr noundef nonnull align 4 dereferenceable(4) @b) |
| 152 |
// OGCG: ret void |
| 153 |
// OGCG: } |
| 154 |
|
| 155 |
// Test case 'c' - Andy's simple case, should have constant storage (internal linkage) |
| 156 |
// CIR checks for 'c' |
| 157 |
// CIR: cir.global {{.*}} internal {{.*}} @_ZL1c = #cir.zero : !rec_C |
| 158 |
// CIR: cir.func internal private @__cxx_global_var_init.3() { |
| 159 |
// CIR: %[[OBJ:.*]] = cir.get_global @_ZL1c : !cir.ptr<!rec_C> |
| 160 |
// CIR: cir.call @_ZN1CC1Ev(%[[OBJ]]) : (!cir.ptr<!rec_C>) -> () |
| 161 |
// CIR: cir.return |
| 162 |
// CIR: } |
| 163 |
|
| 164 |
// LLVM checks for 'c' (no optimization) |
| 165 |
// LLVM: define internal void @__cxx_global_var_init.3() { |
| 166 |
// LLVM: call void @_ZN1CC1Ev(ptr @_ZL1c) |
| 167 |
// LLVM: ret void |
| 168 |
// LLVM: } |
| 169 |
|
| 170 |
// OGCG checks for 'c' (no optimization) |
| 171 |
// OGCG: define internal void @__cxx_global_var_init.3() {{.*}} section ".text.startup" { |
| 172 |
// OGCG: call void @_ZN1CC1Ev(ptr noundef nonnull align 4 dereferenceable(8) @_ZL1c) |
| 173 |
// OGCG: ret void |
| 174 |
// OGCG: } |
| 175 |
|
| 176 |
// With optimization enabled, should emit invariant.start intrinsic for constant storage cases |
| 177 |
|
| 178 |
// Check all globals first (they appear at the top of optimized LLVM/OGCG output) |
| 179 |
// LLVM-OPT: @a ={{.*}} global {{.*}} zeroinitializer |
| 180 |
// LLVM-OPT: @a2 ={{.*}} global {{.*}} zeroinitializer |
| 181 |
// LLVM-OPT: @b ={{.*}} global {{.*}} zeroinitializer |
| 182 |
// LLVM-OPT: @_ZL1c ={{.*}} global {{.*}} zeroinitializer |
| 183 |
|
| 184 |
// OGCG-OPT: @a ={{.*}} global {{.*}} zeroinitializer |
| 185 |
// OGCG-OPT: @a2 ={{.*}} global {{.*}} zeroinitializer |
| 186 |
// OGCG-OPT: @b ={{.*}} global {{.*}} zeroinitializer |
| 187 |
// OGCG-OPT: @_ZL1c ={{.*}} global {{.*}} zeroinitializer |
| 188 |
|
| 189 |
// Test case 'a' - optimized checks |
| 190 |
// LLVM-OPT: define internal void @__cxx_global_var_init() { |
| 191 |
// LLVM-OPT: call void @_ZN1AC1Ev(ptr @a) |
| 192 |
// LLVM-OPT: call {{.*}}@llvm.invariant.start.p0(i64 4, ptr @a) |
| 193 |
// LLVM-OPT: ret void |
| 194 |
// LLVM-OPT: } |
| 195 |
|
| 196 |
// OGCG-OPT: define internal void @__cxx_global_var_init() {{.*}} section ".text.startup" { |
| 197 |
// OGCG-OPT: call void @_ZN1AC1Ev(ptr noundef nonnull align 4 dereferenceable(4) @a) |
| 198 |
// OGCG-OPT: call {{.*}}@llvm.invariant.start.p0(i64 4, ptr @a) |
| 199 |
// OGCG-OPT: ret void |
| 200 |
// OGCG-OPT: } |
| 201 |
|
| 202 |
// Test case 'a2' - optimized checks |
| 203 |
// LLVM-OPT: define internal void @__cxx_global_var_init.1() { |
| 204 |
// LLVM-OPT: call void @_ZN2A2C1Ev(ptr @a2) |
| 205 |
// LLVM-OPT: call {{.*}}@llvm.invariant.start.p0(i64 4, ptr @a2) |
| 206 |
// LLVM-OPT: ret void |
| 207 |
// LLVM-OPT: } |
| 208 |
|
| 209 |
// OGCG-OPT: define internal void @__cxx_global_var_init.1() {{.*}} section ".text.startup" { |
| 210 |
// OGCG-OPT: call void @_ZN2A2C1Ev(ptr noundef nonnull align 4 dereferenceable(4) @a2) |
| 211 |
// OGCG-OPT: call {{.*}}@llvm.invariant.start.p0(i64 4, ptr @a2) |
| 212 |
// OGCG-OPT: ret void |
| 213 |
// OGCG-OPT: } |
| 214 |
|
| 215 |
// Test case 'b' - optimized checks (should NOT emit invariant.start) |
| 216 |
// LLVM-OPT: define internal void @__cxx_global_var_init.2() { |
| 217 |
// LLVM-OPT: call void @_ZN1BC1Ev(ptr @b) |
| 218 |
// LLVM-OPT-NOT: call {{.*}}@llvm.invariant.start.p0(i64 {{.*}}, ptr @b) |
| 219 |
// LLVM-OPT: ret void |
| 220 |
// LLVM-OPT: } |
| 221 |
|
| 222 |
// OGCG-OPT: define internal void @__cxx_global_var_init.2() {{.*}} section ".text.startup" { |
| 223 |
// OGCG-OPT: call void @_ZN1BC1Ev(ptr noundef nonnull align 4 dereferenceable(4) @b) |
| 224 |
// OGCG-OPT-NOT: call {{.*}}@llvm.invariant.start.p0(i64 {{.*}}, ptr @b) |
| 225 |
// OGCG-OPT: ret void |
| 226 |
// OGCG-OPT: } |
| 227 |
|
| 228 |
// Test case 'c' - optimized checks |
| 229 |
// LLVM-OPT: define internal void @__cxx_global_var_init.3() { |
| 230 |
// LLVM-OPT: call void @_ZN1CC1Ev(ptr @_ZL1c) |
| 231 |
// LLVM-OPT: call {{.*}}@llvm.invariant.start.p0(i64 8, ptr @_ZL1c) |
| 232 |
// LLVM-OPT: ret void |
| 233 |
// LLVM-OPT: } |
| 234 |
|
| 235 |
// OGCG-OPT: define internal void @__cxx_global_var_init.3() {{.*}} section ".text.startup" { |
| 236 |
// OGCG-OPT: call void @_ZN1CC1Ev(ptr noundef nonnull align 4 dereferenceable(8) @_ZL1c) |
| 237 |
// OGCG-OPT: call {{.*}}@llvm.invariant.start.p0(i64 8, ptr @_ZL1c) |
| 238 |
// OGCG-OPT: ret void |
| 239 |
// OGCG-OPT: } |
| 240 |
|
| 241 |
// CIR checks at -O1 - should include invariant.start intrinsic calls for constant storage cases |
| 242 |
// CIR-O1: module {{.*}} attributes {{.*}} cir.opt_info = #cir.opt_info<level = 1 |
| 243 |
|
| 244 |
// Test case 'a' - CIR at -O1 |
| 245 |
// CIR-O1: cir.func internal private @__cxx_global_var_init() { |
| 246 |
// CIR-O1: cir.call @_ZN1AC1Ev(%{{.*}}) : (!cir.ptr<!rec_A>) -> () |
| 247 |
// CIR-O1: cir.call_llvm_intrinsic "invariant.start" {{.*}} : (!s64i, !cir.ptr<!rec_A>) -> !cir.ptr<!rec_A> |
| 248 |
// CIR-O1: } |
| 249 |
|
| 250 |
// Test case 'a2' - CIR at -O1 |
| 251 |
// CIR-O1: cir.func internal private @__cxx_global_var_init.1() { |
| 252 |
// CIR-O1: cir.call @_ZN2A2C1Ev(%{{.*}}) : (!cir.ptr<!rec_A2>) -> () |
| 253 |
// CIR-O1: cir.call_llvm_intrinsic "invariant.start" {{.*}} : (!s64i, !cir.ptr<!rec_A2>) -> !cir.ptr<!rec_A2> |
| 254 |
// CIR-O1: } |
| 255 |
|
| 256 |
// Test case 'b' - CIR at -O1 (should NOT emit invariant.start) |
| 257 |
// CIR-O1: cir.func internal private @__cxx_global_var_init.2() { |
| 258 |
// CIR-O1: cir.call @_ZN1BC1Ev(%{{.*}}) : (!cir.ptr<!rec_B>) -> () |
| 259 |
// CIR-O1-NOT: cir.call_llvm_intrinsic "invariant.start" |
| 260 |
// CIR-O1: } |
| 261 |
|
| 262 |
// Test case 'c' - CIR at -O1 |
| 263 |
// CIR-O1: cir.func internal private @__cxx_global_var_init.3() { |
| 264 |
// CIR-O1: cir.call @_ZN1CC1Ev(%{{.*}}) : (!cir.ptr<!rec_C>) -> () |
| 265 |
// CIR-O1: cir.call_llvm_intrinsic "invariant.start" {{.*}} : (!s64i, !cir.ptr<!rec_C>) -> !cir.ptr<!rec_C> |
| 266 |
// CIR-O1: } |