aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CIR/CodeGen/CIRGenModule.cpp
diff options
context:
space:
mode:
authorAndy Kaylor <akaylor@nvidia.com>2025-08-12 10:00:38 -0700
committerGitHub <noreply@github.com>2025-08-12 10:00:38 -0700
commit7f195b36eefcaa7423cd0c60c003460bf571e9e5 (patch)
tree4dd0ec4dc77699a6f8f50386cf6abb6180177b91 /clang/lib/CIR/CodeGen/CIRGenModule.cpp
parent7f22f5bac19bac6ed67d7ce2160528a33c68edf2 (diff)
downloadllvm-7f195b36eefcaa7423cd0c60c003460bf571e9e5.zip
llvm-7f195b36eefcaa7423cd0c60c003460bf571e9e5.tar.gz
llvm-7f195b36eefcaa7423cd0c60c003460bf571e9e5.tar.bz2
[CIR] Initialize vptr in dynamic classes (#152574)
This adds support for initializing the vptr member of a dynamic class in the constructor of that class. This does not include support for lowering the `cir.vtable.address_point` operation to the LLVM dialect. That handling will be added in a follow-up patch.
Diffstat (limited to 'clang/lib/CIR/CodeGen/CIRGenModule.cpp')
-rw-r--r--clang/lib/CIR/CodeGen/CIRGenModule.cpp45
1 files changed, 44 insertions, 1 deletions
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 06b030f..895a07e 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -64,7 +64,7 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
langOpts(astContext.getLangOpts()), codeGenOpts(cgo),
theModule{mlir::ModuleOp::create(mlir::UnknownLoc::get(&mlirContext))},
diags(diags), target(astContext.getTargetInfo()),
- abi(createCXXABI(*this)), genTypes(*this) {
+ abi(createCXXABI(*this)), genTypes(*this), vtables(*this) {
// Initialize cached types
VoidTy = cir::VoidType::get(&getMLIRContext());
@@ -75,6 +75,7 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
SInt64Ty = cir::IntType::get(&getMLIRContext(), 64, /*isSigned=*/true);
SInt128Ty = cir::IntType::get(&getMLIRContext(), 128, /*isSigned=*/true);
UInt8Ty = cir::IntType::get(&getMLIRContext(), 8, /*isSigned=*/false);
+ UInt8PtrTy = cir::PointerType::get(UInt8Ty);
UInt16Ty = cir::IntType::get(&getMLIRContext(), 16, /*isSigned=*/false);
UInt32Ty = cir::IntType::get(&getMLIRContext(), 32, /*isSigned=*/false);
UInt64Ty = cir::IntType::get(&getMLIRContext(), 64, /*isSigned=*/false);
@@ -946,6 +947,39 @@ void CIRGenModule::applyReplacements() {
}
}
+cir::GlobalOp CIRGenModule::createOrReplaceCXXRuntimeVariable(
+ mlir::Location loc, StringRef name, mlir::Type ty,
+ cir::GlobalLinkageKind linkage, clang::CharUnits alignment) {
+ auto gv = mlir::dyn_cast_or_null<cir::GlobalOp>(
+ mlir::SymbolTable::lookupSymbolIn(theModule, name));
+
+ if (gv) {
+ // There should be handling added here to check the type as assert that
+ // gv was a declaration if the type doesn't match and handling below
+ // to replace the variable if it was a declaration.
+ errorNYI(loc, "createOrReplaceCXXRuntimeVariable: already exists");
+ return gv;
+ }
+
+ // Create a new variable.
+ gv = createGlobalOp(*this, loc, name, ty);
+
+ // Set up extra information and add to the module
+ gv.setLinkageAttr(
+ cir::GlobalLinkageKindAttr::get(&getMLIRContext(), linkage));
+ mlir::SymbolTable::setSymbolVisibility(gv,
+ CIRGenModule::getMLIRVisibility(gv));
+
+ if (supportsCOMDAT() && cir::isWeakForLinker(linkage) &&
+ !gv.hasAvailableExternallyLinkage()) {
+ gv.setComdat(true);
+ }
+
+ gv.setAlignmentAttr(getSize(alignment));
+ setDSOLocal(static_cast<mlir::Operation *>(gv));
+ return gv;
+}
+
// TODO(CIR): this could be a common method between LLVM codegen.
static bool isVarDeclStrongDefinition(const ASTContext &astContext,
CIRGenModule &cgm, const VarDecl *vd,
@@ -1941,6 +1975,15 @@ CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
}
mlir::SymbolTable::Visibility
+CIRGenModule::getMLIRVisibility(cir::GlobalOp op) {
+ // MLIR doesn't accept public symbols declarations (only
+ // definitions).
+ if (op.isDeclaration())
+ return mlir::SymbolTable::Visibility::Private;
+ return getMLIRVisibilityFromCIRLinkage(op.getLinkage());
+}
+
+mlir::SymbolTable::Visibility
CIRGenModule::getMLIRVisibilityFromCIRLinkage(cir::GlobalLinkageKind glk) {
switch (glk) {
case cir::GlobalLinkageKind::InternalLinkage: