Skip to content

Commit 826abe4

Browse files
committed
[CIR][CIRGen] Add minimal support for building invariant globals
1 parent 29d3232 commit 826abe4

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ struct MissingFeatures {
162162
static bool armComputeVolatileBitfields() { return false; }
163163
static bool insertBuiltinUnpredictable() { return false; }
164164
static bool createInvariantGroup() { return false; }
165+
static bool createInvariantIntrinsic() { return false; }
165166
static bool addAutoInitAnnotation() { return false; }
166167
static bool addHeapAllocSiteMetadata() { return false; }
167168
static bool loopInfoStack() { return false; }

clang/lib/CIR/CodeGen/CIRGenCXX.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,21 @@ mlir::cir::FuncOp CIRGenModule::codegenCXXStructor(GlobalDecl GD) {
291291
return Fn;
292292
}
293293

294+
/// Emit code to cause the variable at the given address to be considered as
295+
/// constant from this point onwards.
296+
static void buildDeclInvariant(CIRGenFunction &CGF, const VarDecl *D) {
297+
return CGF.buildInvariantStart(
298+
CGF.getContext().getTypeSizeInChars(D->getType()));
299+
}
300+
301+
void CIRGenFunction::buildInvariantStart([[maybe_unused]] CharUnits Size) {
302+
// Do not emit the intrinsic if we're not optimizing.
303+
if (!CGM.getCodeGenOpts().OptimizationLevel)
304+
return;
305+
306+
assert(!MissingFeatures::createInvariantIntrinsic());
307+
}
308+
294309
void CIRGenModule::codegenGlobalInitCxxStructor(const VarDecl *D,
295310
mlir::cir::GlobalOp Addr,
296311
bool NeedsCtor, bool NeedsDtor,
@@ -312,8 +327,7 @@ void CIRGenModule::codegenGlobalInitCxxStructor(const VarDecl *D,
312327
}
313328

314329
if (isCstStorage) {
315-
// buildDeclInvariant(CGF, D, DeclPtr);
316-
llvm_unreachable("NYI");
330+
buildDeclInvariant(CGF, D);
317331
} else {
318332
// If not constant storage we'll emit this regardless of NeedsDtor value.
319333
mlir::OpBuilder::InsertionGuard guard(builder);

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,8 @@ class CIRGenFunction : public CIRGenTypeCache {
866866
mlir::Value buildRuntimeCall(mlir::Location loc, mlir::cir::FuncOp callee,
867867
ArrayRef<mlir::Value> args = {});
868868

869+
void buildInvariantStart(CharUnits Size);
870+
869871
/// Create a check for a function parameter that may potentially be
870872
/// declared as non-null.
871873
void buildNonNullArgCheck(RValue RV, QualType ArgType, SourceLocation ArgLoc,

clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,8 +2154,7 @@ void CIRGenItaniumCXXABI::registerGlobalDtor(CIRGenFunction &CGF,
21542154
llvm_unreachable("NYI");
21552155

21562156
// The default behavior is to use atexit. This is handled in lowering
2157-
// prepare. For now just emit the body for the dtor.
2158-
// ....
2157+
// prepare. Nothing to be done for CIR here.
21592158
}
21602159

21612160
mlir::Value CIRGenItaniumCXXABI::getCXXDestructorImplicitParam(

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@ e *g = new e(0);
2121
// CIR_AFTER: {{%.*}} = cir.const #cir.int<1> : !u64i
2222
// CIR_AFTER: {{%.*}} = cir.call @_Znwm(%1) : (!u64i) -> !cir.ptr<!void>
2323

24-
// LLVM-DAG: @llvm.global_ctors = appending constant [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65536, ptr @__cxx_global_var_init, ptr null }]
24+
// LLVM-DAG: @llvm.global_ctors = appending constant [2 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65536, ptr @__cxx_global_var_init, ptr null }, { i32, ptr, ptr } { i32 65536, ptr @__cxx_global_var_init.1, ptr null }]
2525
// LLVM: define internal void @__cxx_global_var_init()
2626
// LLVM: call ptr @_Znwm(i64 1)
2727

28+
// LLVM: define internal void @__cxx_global_var_init.1()
29+
// LLVM: call ptr @_Znwm(i64 1)
30+
2831
// LLVM: define void @_GLOBAL__sub_I_global_new.cpp()
2932
// LLVM: call void @__cxx_global_var_init()
33+
// LLVM: call void @__cxx_global_var_init.1()
34+
35+
struct PackedStruct {
36+
};
37+
PackedStruct*const packed_2 = new PackedStruct();

0 commit comments

Comments
 (0)