-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[CIR] Add support for constructor aliases #145792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,17 +79,102 @@ void CIRGenItaniumCXXABI::emitInstanceFunctionProlog(SourceLocation loc, | |
| } | ||
| } | ||
|
|
||
| // Find out how to cirgen the complete destructor and constructor | ||
| namespace { | ||
| enum class StructorCIRGen { Emit, RAUW, Alias, COMDAT }; | ||
| } | ||
|
|
||
| static StructorCIRGen getCIRGenToUse(CIRGenModule &cgm, | ||
| const CXXMethodDecl *md) { | ||
| if (!cgm.getCodeGenOpts().CXXCtorDtorAliases) | ||
| return StructorCIRGen::Emit; | ||
|
|
||
| // The complete and base structors are not equivalent if there are any virtual | ||
| // bases, so emit separate functions. | ||
| if (md->getParent()->getNumVBases()) { | ||
| // The return value is correct here, but other support for this is NYI. | ||
| cgm.errorNYI(md->getSourceRange(), "getCIRGenToUse: virtual bases"); | ||
| return StructorCIRGen::Emit; | ||
| } | ||
|
|
||
| GlobalDecl aliasDecl; | ||
| if (const auto *dd = dyn_cast<CXXDestructorDecl>(md)) { | ||
| // The assignment is correct here, but other support for this is NYI. | ||
| cgm.errorNYI(md->getSourceRange(), "getCIRGenToUse: dtor"); | ||
| aliasDecl = GlobalDecl(dd, Dtor_Complete); | ||
| } else { | ||
| const auto *cd = cast<CXXConstructorDecl>(md); | ||
| aliasDecl = GlobalDecl(cd, Ctor_Complete); | ||
| } | ||
|
|
||
| cir::GlobalLinkageKind linkage = cgm.getFunctionLinkage(aliasDecl); | ||
|
|
||
| if (cir::isDiscardableIfUnused(linkage)) | ||
| return StructorCIRGen::RAUW; | ||
|
|
||
| // FIXME: Should we allow available_externally aliases? | ||
| if (!cir::isValidLinkage(linkage)) | ||
| return StructorCIRGen::RAUW; | ||
|
|
||
| if (cir::isWeakForLinker(linkage)) { | ||
| // Only ELF and wasm support COMDATs with arbitrary names (C5/D5). | ||
| if (cgm.getTarget().getTriple().isOSBinFormatELF() || | ||
| cgm.getTarget().getTriple().isOSBinFormatWasm()) | ||
| return StructorCIRGen::COMDAT; | ||
| return StructorCIRGen::Emit; | ||
| } | ||
|
|
||
| return StructorCIRGen::Alias; | ||
| } | ||
|
|
||
| static void emitConstructorDestructorAlias(CIRGenModule &cgm, | ||
| GlobalDecl aliasDecl, | ||
| GlobalDecl targetDecl) { | ||
| cir::GlobalLinkageKind linkage = cgm.getFunctionLinkage(aliasDecl); | ||
|
|
||
| // Does this function alias already exists? | ||
| StringRef mangledName = cgm.getMangledName(aliasDecl); | ||
| auto globalValue = dyn_cast_or_null<cir::CIRGlobalValueInterface>( | ||
| cgm.getGlobalValue(mangledName)); | ||
| if (globalValue && !globalValue.isDeclaration()) | ||
| return; | ||
|
|
||
| auto entry = cast_or_null<cir::FuncOp>(cgm.getGlobalValue(mangledName)); | ||
|
|
||
| // Retrieve aliasee info. | ||
| auto aliasee = cast<cir::FuncOp>(cgm.getAddrOfGlobal(targetDecl)); | ||
|
|
||
| // Populate actual alias. | ||
| cgm.emitAliasForGlobal(mangledName, entry, aliasDecl, aliasee, linkage); | ||
| } | ||
|
|
||
| void CIRGenItaniumCXXABI::emitCXXStructor(GlobalDecl gd) { | ||
| auto *md = cast<CXXMethodDecl>(gd.getDecl()); | ||
| auto *cd = dyn_cast<CXXConstructorDecl>(md); | ||
|
|
||
| StructorCIRGen cirGenType = getCIRGenToUse(cgm, md); | ||
|
|
||
| if (!cd) { | ||
| cgm.errorNYI(md->getSourceRange(), "CXCABI emit destructor"); | ||
| return; | ||
| } | ||
|
|
||
| if (cgm.getCodeGenOpts().CXXCtorDtorAliases) | ||
| cgm.errorNYI(md->getSourceRange(), "Ctor/Dtor aliases"); | ||
| if (gd.getCtorType() == Ctor_Complete) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Destructor doesn't require special handling here? Seems a little odd to me...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's an artifact of destructors not being implemented yet. See the errorNYI above. |
||
| GlobalDecl baseDecl = gd.getWithCtorType(Ctor_Base); | ||
|
|
||
| if (cirGenType == StructorCIRGen::Alias || | ||
| cirGenType == StructorCIRGen::COMDAT) { | ||
| emitConstructorDestructorAlias(cgm, gd, baseDecl); | ||
| return; | ||
| } | ||
|
|
||
| if (cirGenType == StructorCIRGen::RAUW) { | ||
| StringRef mangledName = cgm.getMangledName(gd); | ||
| mlir::Operation *aliasee = cgm.getAddrOfGlobal(baseDecl); | ||
| cgm.addReplacement(mangledName, aliasee); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| auto fn = cgm.codegenCXXStructor(gd); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -888,6 +888,69 @@ void CIRGenModule::updateCompletedType(const TagDecl *td) { | |
| genTypes.updateCompletedType(td); | ||
| } | ||
|
|
||
| void CIRGenModule::addReplacement(StringRef name, mlir::Operation *op) { | ||
| replacements[name] = op; | ||
| } | ||
|
|
||
| void CIRGenModule::replacePointerTypeArgs(cir::FuncOp oldF, cir::FuncOp newF) { | ||
| std::optional<mlir::SymbolTable::UseRange> optionalUseRange = | ||
| oldF.getSymbolUses(theModule); | ||
| if (!optionalUseRange) | ||
| return; | ||
|
|
||
| for (const mlir::SymbolTable::SymbolUse &u : *optionalUseRange) { | ||
| // CallTryOp only shows up after FlattenCFG. | ||
| auto call = mlir::dyn_cast<cir::CallOp>(u.getUser()); | ||
| if (!call) | ||
| continue; | ||
|
|
||
| mlir::OperandRange argOps = call.getArgs(); | ||
| mlir::ArrayRef<mlir::Type> funcArgTypes = | ||
| newF.getFunctionType().getInputs(); | ||
| // In the case of variadic functions, the call may have more arguments that | ||
| // the function type, so we can't use llvm::enumerate here. | ||
|
||
| for (unsigned i = 0; i < funcArgTypes.size(); i++) { | ||
| if (argOps[i].getType() == funcArgTypes[i]) | ||
| continue; | ||
|
|
||
| // The purpose of this entire function is to insert bitcasts in the case | ||
| // where these types don't match, but I haven't seen a case where that | ||
| // happens. | ||
| errorNYI(call.getLoc(), "replace call with mismatched types"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void CIRGenModule::applyReplacements() { | ||
| for (auto &i : replacements) { | ||
| StringRef mangledName = i.first(); | ||
| mlir::Operation *replacement = i.second; | ||
| mlir::Operation *entry = getGlobalValue(mangledName); | ||
| if (!entry) | ||
| continue; | ||
| assert(isa<cir::FuncOp>(entry) && "expected function"); | ||
| auto oldF = cast<cir::FuncOp>(entry); | ||
| auto newF = dyn_cast<cir::FuncOp>(replacement); | ||
| if (!newF) { | ||
| // In classic codegen, this can be a global alias, a bitcast, or a GEP. | ||
| errorNYI(replacement->getLoc(), "replacement is not a function"); | ||
| continue; | ||
| } | ||
|
|
||
| // LLVM has opaque pointer but CIR not. So we may have to handle these | ||
| // different pointer types when performing replacement. | ||
| replacePointerTypeArgs(oldF, newF); | ||
|
|
||
| // Replace old with new, but keep the old order. | ||
| if (oldF.replaceAllSymbolUses(newF.getSymNameAttr(), theModule).failed()) | ||
| llvm_unreachable("internal error, cannot RAUW symbol"); | ||
| if (newF) { | ||
| newF->moveBefore(oldF); | ||
| oldF->erase(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO(CIR): this could be a common method between LLVM codegen. | ||
| static bool isVarDeclStrongDefinition(const ASTContext &astContext, | ||
| CIRGenModule &cgm, const VarDecl *vd, | ||
|
|
@@ -1797,11 +1860,52 @@ CIRGenModule::getGlobalVisibilityAttrFromDecl(const Decl *decl) { | |
|
|
||
| void CIRGenModule::release() { | ||
| emitDeferred(); | ||
| applyReplacements(); | ||
|
|
||
| // There's a lot of code that is not implemented yet. | ||
| assert(!cir::MissingFeatures::cgmRelease()); | ||
| } | ||
|
|
||
| void CIRGenModule::emitAliasForGlobal(StringRef mangledName, | ||
| mlir::Operation *op, GlobalDecl aliasGD, | ||
| cir::FuncOp aliasee, | ||
| cir::GlobalLinkageKind linkage) { | ||
|
|
||
| auto *aliasFD = dyn_cast<FunctionDecl>(aliasGD.getDecl()); | ||
| assert(aliasFD && "expected FunctionDecl"); | ||
|
|
||
| // The aliasee function type is different from the alias one, this difference | ||
| // is specific to CIR because in LLVM the ptr types are already erased at this | ||
| // point. | ||
| const CIRGenFunctionInfo &fnInfo = | ||
| getTypes().arrangeCXXStructorDeclaration(aliasGD); | ||
| cir::FuncType fnType = getTypes().getFunctionType(fnInfo); | ||
|
|
||
| cir::FuncOp alias = | ||
| createCIRFunction(getLoc(aliasGD.getDecl()->getSourceRange()), | ||
| mangledName, fnType, aliasFD); | ||
| alias.setAliasee(aliasee.getName()); | ||
| alias.setLinkage(linkage); | ||
| // Declarations cannot have public MLIR visibility, just mark them private | ||
| // but this really should have no meaning since CIR should not be using | ||
| // this information to derive linkage information. | ||
| mlir::SymbolTable::setSymbolVisibility( | ||
| alias, mlir::SymbolTable::Visibility::Private); | ||
|
|
||
| // Alias constructors and destructors are always unnamed_addr. | ||
| assert(!cir::MissingFeatures::opGlobalUnnamedAddr()); | ||
|
|
||
| // Switch any previous uses to the alias. | ||
| if (op) { | ||
| errorNYI(aliasFD->getSourceRange(), "emitAliasForGlobal: previous uses"); | ||
| } else { | ||
| // Name already set by createCIRFunction | ||
| } | ||
|
|
||
| // Finally, set up the alias with its proper name and attributes. | ||
| setCommonAttributes(aliasGD, alias); | ||
| } | ||
|
|
||
| mlir::Type CIRGenModule::convertType(QualType type) { | ||
| return genTypes.convertType(type); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -mconstructor-aliases -fclangir -emit-cir %s -o %t.cir | ||
| // RUN: FileCheck --input-file=%t.cir %s | ||
| // RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -mconstructor-aliases -fclangir -emit-llvm %s -o %t-cir.ll | ||
| // RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s | ||
| // RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -mconstructor-aliases -emit-llvm %s -o %t.ll | ||
| // RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s | ||
|
|
||
| struct B { | ||
| B(); | ||
| }; | ||
| B::B() { | ||
| } | ||
|
|
||
| // OGCG: @_ZN1BC1Ev = unnamed_addr alias void (ptr), ptr @_ZN1BC2Ev | ||
|
|
||
| // CHECK: cir.func{{.*}} @_ZN1BC2Ev(%arg0: !cir.ptr<!rec_B> | ||
| // CHECK: %[[THIS_ADDR:.*]] = cir.alloca !cir.ptr<!rec_B>, !cir.ptr<!cir.ptr<!rec_B>>, ["this", init] | ||
| // CHECK: cir.store %arg0, %[[THIS_ADDR]] | ||
| // CHECK: %[[THIS:.*]] = cir.load %[[THIS_ADDR]] : !cir.ptr<!cir.ptr<!rec_B>>, !cir.ptr<!rec_B> | ||
|
|
||
| // CHECK: cir.func{{.*}} private dso_local @_ZN1BC1Ev(!cir.ptr<!rec_B>) alias(@_ZN1BC2Ev) | ||
|
|
||
| // LLVM: define{{.*}} @_ZN1BC2Ev(ptr %[[THIS_ARG:.*]]) | ||
| // LLVM: %[[THIS_ADDR:.*]] = alloca ptr | ||
| // LLVM: store ptr %[[THIS_ARG]], ptr %[[THIS_ADDR]] | ||
| // LLVM: %[[THIS:.*]] = load ptr, ptr %[[THIS_ADDR]] | ||
|
|
||
| // This should be an alias, like the similar OGCG alias above, but that's not | ||
| // implemented yet. | ||
| // LLVM: declare dso_local void @_ZN1BC1Ev(ptr) | ||
|
|
||
| // OGCG: define{{.*}} @_ZN1BC2Ev(ptr{{.*}} %[[THIS_ARG:.*]]) | ||
| // OGCG: %[[THIS_ADDR:.*]] = alloca ptr | ||
| // OGCG: store ptr %[[THIS_ARG]], ptr %[[THIS_ADDR]] | ||
| // OGCG: %[[THIS:.*]] = load ptr, ptr %[[THIS_ADDR]] | ||
|
|
||
| // The constructor in this cases is handled by RAUW rather than aliasing. | ||
| struct Struk { | ||
| Struk() {} | ||
| }; | ||
|
|
||
| void baz() { | ||
| Struk s; | ||
| } | ||
|
|
||
| // CHECK: cir.func{{.*}} @_ZN5StrukC2Ev(%arg0: !cir.ptr<!rec_Struk> | ||
| // CHECK: %[[THIS_ADDR:.*]] = cir.alloca !cir.ptr<!rec_Struk>, !cir.ptr<!cir.ptr<!rec_Struk>>, ["this", init] | ||
| // CHECK: cir.store %arg0, %[[THIS_ADDR]] : !cir.ptr<!rec_Struk>, !cir.ptr<!cir.ptr<!rec_Struk>> | ||
| // CHECK: %[[THIS:.*]] = cir.load %[[THIS_ADDR]] : !cir.ptr<!cir.ptr<!rec_Struk>>, !cir.ptr<!rec_Struk> | ||
| // CHECK: cir.return | ||
|
|
||
| // CHECK-NOT: cir.func{{.*}} @_ZN5StrukC1Ev | ||
|
|
||
| // CHECK: cir.func{{.*}} @_Z3bazv() | ||
| // CHECK: %[[S_ADDR:.*]] = cir.alloca !rec_Struk, !cir.ptr<!rec_Struk>, ["s", init] | ||
| // CHECK: cir.call @_ZN5StrukC2Ev(%[[S_ADDR]]) : (!cir.ptr<!rec_Struk>) -> () | ||
|
|
||
| // LLVM: define linkonce_odr void @_ZN5StrukC2Ev(ptr %[[THIS_ARG]]) | ||
| // LLVM: %[[THIS_ADDR:.*]] = alloca ptr | ||
| // LLVM: store ptr %[[THIS_ARG]], ptr %[[THIS_ADDR]] | ||
| // LLVM: %[[THIS:.*]] = load ptr, ptr %[[THIS_ADDR]] | ||
|
|
||
| // LLVM: define{{.*}} void @_Z3bazv() | ||
| // LLVM: %[[S_ADDR:.*]] = alloca %struct.Struk | ||
| // LLVM: call void @_ZN5StrukC2Ev(ptr{{.*}} %[[S_ADDR]]) | ||
|
|
||
| // This function gets emitted before the constructor in OGCG. | ||
| // OGCG: define{{.*}} void @_Z3bazv() | ||
| // OGCG: %[[S_ADDR:.*]] = alloca %struct.Struk | ||
| // OGCG: call void @_ZN5StrukC2Ev(ptr{{.*}} %[[S_ADDR]]) | ||
|
|
||
| // OGCG: define linkonce_odr void @_ZN5StrukC2Ev(ptr{{.*}} %[[THIS_ARG]]) | ||
| // OGCG: %[[THIS_ADDR:.*]] = alloca ptr | ||
| // OGCG: store ptr %[[THIS_ARG]], ptr %[[THIS_ADDR]] | ||
| // OGCG: %[[THIS:.*]] = load ptr, ptr %[[THIS_ADDR]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a cir to cir test to exercise the parser?