Skip to content

Commit

Permalink
[CIR][CIRGen] Add minimal support for building invariant globals
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardosolopes authored and smeenai committed Oct 9, 2024
1 parent 1d3b7d6 commit 54a854b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ struct MissingFeatures {
static bool armComputeVolatileBitfields() { return false; }
static bool insertBuiltinUnpredictable() { return false; }
static bool createInvariantGroup() { return false; }
static bool createInvariantIntrinsic() { return false; }
static bool addAutoInitAnnotation() { return false; }
static bool addHeapAllocSiteMetadata() { return false; }
static bool loopInfoStack() { return false; }
Expand Down
18 changes: 16 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,21 @@ mlir::cir::FuncOp CIRGenModule::codegenCXXStructor(GlobalDecl GD) {
return Fn;
}

/// Emit code to cause the variable at the given address to be considered as
/// constant from this point onwards.
static void buildDeclInvariant(CIRGenFunction &CGF, const VarDecl *D) {
return CGF.buildInvariantStart(
CGF.getContext().getTypeSizeInChars(D->getType()));
}

void CIRGenFunction::buildInvariantStart([[maybe_unused]] CharUnits Size) {
// Do not emit the intrinsic if we're not optimizing.
if (!CGM.getCodeGenOpts().OptimizationLevel)
return;

assert(!MissingFeatures::createInvariantIntrinsic());
}

void CIRGenModule::codegenGlobalInitCxxStructor(const VarDecl *D,
mlir::cir::GlobalOp Addr,
bool NeedsCtor, bool NeedsDtor,
Expand All @@ -312,8 +327,7 @@ void CIRGenModule::codegenGlobalInitCxxStructor(const VarDecl *D,
}

if (isCstStorage) {
// buildDeclInvariant(CGF, D, DeclPtr);
llvm_unreachable("NYI");
buildDeclInvariant(CGF, D);
} else {
// If not constant storage we'll emit this regardless of NeedsDtor value.
mlir::OpBuilder::InsertionGuard guard(builder);
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,8 @@ class CIRGenFunction : public CIRGenTypeCache {
mlir::Value buildRuntimeCall(mlir::Location loc, mlir::cir::FuncOp callee,
ArrayRef<mlir::Value> args = {});

void buildInvariantStart(CharUnits Size);

/// Create a check for a function parameter that may potentially be
/// declared as non-null.
void buildNonNullArgCheck(RValue RV, QualType ArgType, SourceLocation ArgLoc,
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2150,8 +2150,7 @@ void CIRGenItaniumCXXABI::registerGlobalDtor(CIRGenFunction &CGF,
llvm_unreachable("NYI");

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

mlir::Value CIRGenItaniumCXXABI::getCXXDestructorImplicitParam(
Expand Down
10 changes: 9 additions & 1 deletion clang/test/CIR/CodeGen/global-new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ e *g = new e(0);
// CIR_AFTER: {{%.*}} = cir.const #cir.int<1> : !u64i
// CIR_AFTER: {{%.*}} = cir.call @_Znwm(%1) : (!u64i) -> !cir.ptr<!void>

// LLVM-DAG: @llvm.global_ctors = appending constant [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65536, ptr @__cxx_global_var_init, ptr null }]
// 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 }]
// LLVM: define internal void @__cxx_global_var_init()
// LLVM: call ptr @_Znwm(i64 1)

// LLVM: define internal void @__cxx_global_var_init.1()
// LLVM: call ptr @_Znwm(i64 1)

// LLVM: define void @_GLOBAL__sub_I_global_new.cpp()
// LLVM: call void @__cxx_global_var_init()
// LLVM: call void @__cxx_global_var_init.1()

struct PackedStruct {
};
PackedStruct*const packed_2 = new PackedStruct();

0 comments on commit 54a854b

Please sign in to comment.