Skip to content

Commit d3bbe26

Browse files
AmrDeveloperdvbuka
authored andcommitted
[CIR] Implement global initializer for ComplexType (llvm#164610)
Implement a global initializer for ComplexType Issue llvm#141365
1 parent 56de1cf commit d3bbe26

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static void emitDeclInit(CIRGenFunction &cgf, const VarDecl *varDecl,
5353
cgf.emitScalarInit(init, cgf.getLoc(varDecl->getLocation()), lv, false);
5454
break;
5555
case cir::TEK_Complex:
56-
cgf.cgm.errorNYI(varDecl->getSourceRange(), "complex global initializer");
56+
cgf.emitComplexExprIntoLValue(init, lv, /*isInit=*/true);
5757
break;
5858
case cir::TEK_Aggregate:
5959
assert(!cir::MissingFeatures::aggValueSlotGC());

clang/test/CIR/CodeGen/global-init.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,60 @@ NeedsCtorDtor needsCtorDtor;
103103
// OGCG: call void @_ZN13NeedsCtorDtorC1Ev(ptr noundef nonnull align 1 dereferenceable(1) @needsCtorDtor)
104104
// OGCG: %{{.*}} = call i32 @__cxa_atexit(ptr @_ZN13NeedsCtorDtorD1Ev, ptr @needsCtorDtor, ptr @__dso_handle)
105105

106+
float num;
107+
float _Complex a = {num, num};
108+
109+
// CIR-BEFORE-LPP: cir.global external @num = #cir.fp<0.000000e+00> : !cir.float
110+
// CIR-BEFORE-LPP: cir.global external @a = ctor : !cir.complex<!cir.float> {
111+
// CIR-BEFORE-LPP: %[[THIS:.*]] = cir.get_global @a : !cir.ptr<!cir.complex<!cir.float>>
112+
// CIR-BEFORE-LPP: %[[NUM:.*]] = cir.get_global @num : !cir.ptr<!cir.float>
113+
// CIR-BEFORE-LPP: %[[REAL:.*]] = cir.load{{.*}} %[[NUM]] : !cir.ptr<!cir.float>, !cir.float
114+
// CIR-BEFORE-LPP: %[[NUM:.*]] = cir.get_global @num : !cir.ptr<!cir.float>
115+
// CIR-BEFORE-LPP: %[[IMAG:.*]] = cir.load{{.*}} %[[NUM]] : !cir.ptr<!cir.float>, !cir.float
116+
// CIR-BEFORE-LPP: %[[COMPLEX_VAL:.*]] = cir.complex.create %[[REAL]], %[[IMAG]] : !cir.float -> !cir.complex<!cir.float>
117+
// CIR-BEFORE-LPP: cir.store{{.*}} %[[COMPLEX_VAL:.*]], %[[THIS]] : !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>
118+
// CIR-BEFORE-LPP: }
119+
120+
// CIR: cir.global external @num = #cir.fp<0.000000e+00> : !cir.float
121+
// CIR: cir.global external @a = #cir.zero : !cir.complex<!cir.float>
122+
// CIR: cir.func internal private @__cxx_global_var_init.3()
123+
// CIR: %[[A_ADDR:.*]] = cir.get_global @a : !cir.ptr<!cir.complex<!cir.float>>
124+
// CIR: %[[NUM:.*]] = cir.get_global @num : !cir.ptr<!cir.float>
125+
// CIR: %[[REAL:.*]] = cir.load{{.*}} %[[NUM]] : !cir.ptr<!cir.float>, !cir.float
126+
// CIR: %[[NUM:.*]] = cir.get_global @num : !cir.ptr<!cir.float>
127+
// CIR: %[[IMAG:.*]] = cir.load{{.*}} %[[NUM]] : !cir.ptr<!cir.float>, !cir.float
128+
// CIR: %[[COMPLEX_VAL:.*]] = cir.complex.create %[[REAL]], %[[IMAG]] : !cir.float -> !cir.complex<!cir.float>
129+
// CIR: cir.store{{.*}} %[[COMPLEX_VAL]], %[[A_ADDR]] : !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>
130+
131+
// LLVM: define internal void @__cxx_global_var_init.3()
132+
// LLVM: %[[REAL:.*]] = load float, ptr @num, align 4
133+
// LLVM: %[[IMAG:.*]] = load float, ptr @num, align 4
134+
// LLVM: %[[TMP_COMPLEX_VAL:.*]] = insertvalue { float, float } {{.*}}, float %[[REAL]], 0
135+
// LLVM: %[[COMPLEX_VAL:.*]] = insertvalue { float, float } %[[TMP_COMPLEX_VAL]], float %[[IMAG]], 1
136+
// LLVM: store { float, float } %[[COMPLEX_VAL]], ptr @a, align 4
137+
138+
// OGCG: define internal void @__cxx_global_var_init.3() {{.*}} section ".text.startup"
139+
// OGCG: %[[REAL:.*]] = load float, ptr @num, align 4
140+
// OGCG: %[[IMAG:.*]] = load float, ptr @num, align 4
141+
// OGCG: store float %[[REAL]], ptr @a, align 4
142+
// OGCG: store float %[[IMAG]], ptr getelementptr inbounds nuw ({ float, float }, ptr @a, i32 0, i32 1), align 4
143+
106144
// Common init function for all globals with default priority
107145

108146
// CIR: cir.func private @_GLOBAL__sub_I_[[FILENAME:.*]]() {
109147
// CIR: cir.call @__cxx_global_var_init() : () -> ()
110148
// CIR: cir.call @__cxx_global_var_init.1() : () -> ()
111149
// CIR: cir.call @__cxx_global_var_init.2() : () -> ()
150+
// CIR: cir.call @__cxx_global_var_init.3() : () -> ()
112151

113152
// LLVM: define void @_GLOBAL__sub_I_[[FILENAME]]()
114153
// LLVM: call void @__cxx_global_var_init()
115154
// LLVM: call void @__cxx_global_var_init.1()
116155
// LLVM: call void @__cxx_global_var_init.2()
156+
// LLVM: call void @__cxx_global_var_init.3()
117157

118158
// OGCG: define internal void @_GLOBAL__sub_I_[[FILENAME]]() {{.*}} section ".text.startup" {
119159
// OGCG: call void @__cxx_global_var_init()
120160
// OGCG: call void @__cxx_global_var_init.1()
121161
// OGCG: call void @__cxx_global_var_init.2()
162+
// OGCG: call void @__cxx_global_var_init.3()

0 commit comments

Comments
 (0)