Skip to content

Commit 0bd11c7

Browse files
committed
[CIR] [CodeGen] Handle NYI in CIRGenModule::tryEmitBaseDestructorAsAlias
1 parent 888f00c commit 0bd11c7

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

clang/lib/CIR/CodeGen/CIRGenCXX.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ bool CIRGenModule::tryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) {
145145
// members with attribute "AlwaysInline" and expect no reference to
146146
// be generated. It is desirable to reenable this optimisation after
147147
// corresponding LLVM changes.
148-
llvm_unreachable("NYI");
148+
addReplacement(MangledName, Aliasee);
149+
return false;
149150
}
150151

151152
// If we have a weak, non-discardable alias (weak, weak_odr), like an
@@ -154,7 +155,8 @@ bool CIRGenModule::tryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) {
154155
// symbol reference from another TU. The other TU must also mark the
155156
// referenced symbol as weak, which we cannot rely on.
156157
if (cir::isWeakForLinker(Linkage) && getTriple().isOSBinFormatCOFF()) {
157-
llvm_unreachable("NYI");
158+
assert(false && "please sent a PR with a test and remove this.\n");
159+
return true;
158160
}
159161

160162
// If we don't have a definition for the destructor yet or the definition
@@ -168,8 +170,10 @@ bool CIRGenModule::tryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) {
168170
// different COMDATs in different TUs. Another option would be to
169171
// output the alias both for weak_odr and linkonce_odr, but that
170172
// requires explicit comdat support in the IL.
171-
if (cir::isWeakForLinker(TargetLinkage))
172-
llvm_unreachable("NYI");
173+
if (cir::isWeakForLinker(TargetLinkage)) {
174+
assert(false && "please sent a PR with a test and remove this.\n");
175+
return true;
176+
}
173177

174178
// Create the alias with no name.
175179
emitAliasForGlobal(MangledName, Entry, AliasDecl, Aliasee, Linkage);

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3205,6 +3205,40 @@ void CIRGenModule::applyReplacements() {
32053205
auto NewF = dyn_cast<cir::FuncOp>(Replacement);
32063206
assert(NewF && "not implemented");
32073207

3208+
// LLVM has opaque pointer but CIR not. So we may have to handle these
3209+
// different pointer types when performing replacement.
3210+
auto optionalUseRange = OldF.getSymbolUses(theModule);
3211+
if (optionalUseRange) {
3212+
for (auto U : *optionalUseRange) {
3213+
auto Call = mlir::dyn_cast<cir::CallOp>(U.getUser());
3214+
if (!Call)
3215+
continue;
3216+
3217+
auto ArgOps = Call.getArgOps();
3218+
auto FuncArgTypes = NewF.getFunctionType().getInputs();
3219+
for (unsigned I = 0; I < FuncArgTypes.size(); I++) {
3220+
if (ArgOps[I].getType() == FuncArgTypes[I])
3221+
continue;
3222+
3223+
auto argPointerTy =
3224+
mlir::dyn_cast<cir::PointerType>(ArgOps[I].getType());
3225+
auto funcArgPointerTy =
3226+
mlir::dyn_cast<cir::PointerType>(FuncArgTypes[I]);
3227+
3228+
// If we can't solve it, leave it for the verifier to bail out.
3229+
if (!argPointerTy || !funcArgPointerTy)
3230+
continue;
3231+
3232+
mlir::OpBuilder::InsertionGuard guard(builder);
3233+
builder.setInsertionPoint(Call);
3234+
auto castedArg =
3235+
builder.createBitcast(Call.getLoc(), ArgOps[I], funcArgPointerTy);
3236+
// FIXME: Raw API. Wrap this into cir::CallOp.
3237+
Call.setOperand(I, castedArg);
3238+
}
3239+
}
3240+
}
3241+
32083242
// Replace old with new, but keep the old order.
32093243
if (OldF.replaceAllSymbolUses(NewF.getSymNameAttr(), theModule).failed())
32103244
llvm_unreachable("internal error, cannot RAUW symbol");

clang/test/CIR/CodeGen/dtor-alias.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// FIXME: Remove of -clangir-disable-passes may trigger a memory safe bug in CIR internally during
2+
// lowering
3+
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -O1 \
4+
// RUN: -mconstructor-aliases -fclangir -emit-cir %s -o %t.cir \
5+
// RUN: -clangir-disable-passes -o %t.cir
6+
// RUN: FileCheck %s --input-file=%t.cir
7+
8+
namespace {
9+
struct A {
10+
~A() {}
11+
};
12+
13+
struct B : public A {};
14+
}
15+
16+
B x;
17+
18+
// CHECK: cir.call @_ZN12_GLOBAL__N_11AD2Ev({{.*}}) : (!cir.ptr<!ty_28anonymous_namespace293A3AA>) -> ()

0 commit comments

Comments
 (0)