This repository was archived by the owner on Feb 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathCIRGenVTables.cpp
More file actions
920 lines (767 loc) 路 35 KB
/
Copy pathCIRGenVTables.cpp
File metadata and controls
920 lines (767 loc) 路 35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
//===--- CIRGenVTables.cpp - Emit CIR Code for C++ vtables ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This contains code dealing with C++ code generation of virtual tables.
//
//===----------------------------------------------------------------------===//
#include "CIRGenCXXABI.h"
#include "CIRGenFunction.h"
#include "CIRGenModule.h"
#include "mlir/IR/Attributes.h"
#include "clang/AST/Attr.h"
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/RecordLayout.h"
#include "clang/AST/VTTBuilder.h"
#include "clang/Basic/CodeGenOptions.h"
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CodeGen/CGFunctionInfo.h"
#include "clang/CodeGen/ConstantInitBuilder.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include <algorithm>
#include <cstdio>
using namespace clang;
using namespace clang::CIRGen;
CIRGenVTables::CIRGenVTables(CIRGenModule &CGM)
: CGM(CGM), VTContext(CGM.getASTContext().getVTableContext()) {}
cir::FuncOp CIRGenModule::getAddrOfThunk(StringRef name, mlir::Type fnTy,
GlobalDecl gd) {
return GetOrCreateCIRFunction(name, fnTy, gd, /*ForVTable=*/true,
/*DontDefer=*/true, /*IsThunk=*/true);
}
static void setThunkProperties(CIRGenModule &cgm, const ThunkInfo &thunk,
cir::FuncOp thunkFn, bool forVTable,
GlobalDecl gd) {
llvm_unreachable("NYI");
}
void CIRGenFunction::startThunk(cir::FuncOp Fn, GlobalDecl GD,
const CIRGenFunctionInfo &FnInfo,
bool IsUnprototyped) {
assert(!CurGD.getDecl() && "CurGD was already set!");
CurGD = GD;
CurFuncIsThunk = true;
// Build FunctionArgs.
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
QualType ThisType = MD->getThisType();
QualType ResultType;
if (IsUnprototyped)
ResultType = CGM.getASTContext().VoidTy;
else if (CGM.getCXXABI().HasThisReturn(GD))
ResultType = ThisType;
else if (CGM.getCXXABI().hasMostDerivedReturn(GD))
ResultType = CGM.getASTContext().VoidPtrTy;
else
ResultType = MD->getType()->castAs<FunctionProtoType>()->getReturnType();
FunctionArgList FunctionArgs;
// Create the implicit 'this' parameter declaration.
CGM.getCXXABI().buildThisParam(*this, FunctionArgs);
// Add the rest of the parameters, if we have a prototype to work with.
if (!IsUnprototyped) {
FunctionArgs.append(MD->param_begin(), MD->param_end());
if (isa<CXXDestructorDecl>(MD))
CGM.getCXXABI().addImplicitStructorParams(*this, ResultType,
FunctionArgs);
}
// Start defining the function.
// NOTE(cir): No ApplyDebugLocation in CIR
StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
MD->getLocation(), MD->getLocation());
// NOTE(cir): No ApplyDebugLocation in CIR
// Since we didn't pass a GlobalDecl to StartFunction, do this ourselves.
CGM.getCXXABI().emitInstanceFunctionProlog(MD->getLocation(), *this);
CXXThisValue = CXXABIThisValue;
CurCodeDecl = MD;
CurFuncDecl = MD;
}
void CIRGenFunction::emitCallAndReturnForThunk(cir::FuncOp Callee,
const ThunkInfo *Thunk,
bool IsUnprototyped) {
assert(isa<CXXMethodDecl>(CurGD.getDecl()) &&
"Please use a new CGF for this thunk");
llvm_unreachable("NYI: emitCallAndReturnForThunk");
}
void CIRGenFunction::emitMustTailThunk(GlobalDecl GD,
mlir::Value AdjustedThisPtr,
cir::FuncOp Callee) {
llvm_unreachable("NYI");
}
void CIRGenFunction::generateThunk(cir::FuncOp Fn,
const CIRGenFunctionInfo &FnInfo,
GlobalDecl GD, const ThunkInfo &Thunk,
bool IsUnprototyped) {
startThunk(Fn, GD, FnInfo, IsUnprototyped);
// NOTE(cir): No ApplyDebugLocation in CIR
// Get our callee. Use a placeholder type if this method is unprototyped so
// that CIRGenModule doesn't try to set attributes.
mlir::Type Ty;
if (IsUnprototyped)
llvm_unreachable("NYI: unprototyped thunk placeholder type");
else
Ty = CGM.getTypes().GetFunctionType(FnInfo);
cir::FuncOp Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
// Make the call and return the result.
emitCallAndReturnForThunk(Callee, &Thunk, IsUnprototyped);
}
static bool UseRelativeLayout(const CIRGenModule &CGM) {
return CGM.getTarget().getCXXABI().isItaniumFamily() &&
CGM.getItaniumVTableContext().isRelativeLayout();
}
bool CIRGenVTables::useRelativeLayout() const { return UseRelativeLayout(CGM); }
mlir::Type CIRGenModule::getVTableComponentType() {
mlir::Type ptrTy = builder.getUInt8PtrTy();
if (UseRelativeLayout(*this))
ptrTy = builder.getUInt32PtrTy();
return ptrTy;
}
mlir::Type CIRGenVTables::getVTableComponentType() {
return CGM.getVTableComponentType();
}
mlir::Type CIRGenVTables::getVTableType(const VTableLayout &layout) {
SmallVector<mlir::Type, 4> tys;
auto componentType = getVTableComponentType();
for (unsigned i = 0, e = layout.getNumVTables(); i != e; ++i)
tys.push_back(cir::ArrayType::get(componentType, layout.getVTableSize(i)));
// FIXME(cir): should VTableLayout be encoded like we do for some
// AST nodes?
return CGM.getBuilder().getAnonRecordTy(tys, /*incomplete=*/false);
}
/// At this point in the translation unit, does it appear that can we
/// rely on the vtable being defined elsewhere in the program?
///
/// The response is really only definitive when called at the end of
/// the translation unit.
///
/// The only semantic restriction here is that the object file should
/// not contain a vtable definition when that vtable is defined
/// strongly elsewhere. Otherwise, we'd just like to avoid emitting
/// vtables when unnecessary.
/// TODO(cir): this should be merged into common AST helper for codegen.
bool CIRGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable.");
// We always synthesize vtables if they are needed in the MS ABI. MSVC doesn't
// emit them even if there is an explicit template instantiation.
if (CGM.getTarget().getCXXABI().isMicrosoft())
return false;
// If we have an explicit instantiation declaration (and not a
// definition), the vtable is defined elsewhere.
TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
if (TSK == TSK_ExplicitInstantiationDeclaration)
return true;
// Otherwise, if the class is an instantiated template, the
// vtable must be defined here.
if (TSK == TSK_ImplicitInstantiation ||
TSK == TSK_ExplicitInstantiationDefinition)
return false;
// Otherwise, if the class doesn't have a key function (possibly
// anymore), the vtable must be defined here.
const CXXMethodDecl *keyFunction =
CGM.getASTContext().getCurrentKeyFunction(RD);
if (!keyFunction)
return false;
// Otherwise, if we don't have a definition of the key function, the
// vtable must be defined somewhere else.
return !keyFunction->hasBody();
}
static bool shouldEmitAvailableExternallyVTable(const CIRGenModule &CGM,
const CXXRecordDecl *RD) {
return CGM.getCodeGenOpts().OptimizationLevel > 0 &&
CGM.getCXXABI().canSpeculativelyEmitVTable(RD);
}
/// Given that we're currently at the end of the translation unit, and
/// we've emitted a reference to the vtable for this class, should
/// we define that vtable?
static bool shouldEmitVTableAtEndOfTranslationUnit(CIRGenModule &CGM,
const CXXRecordDecl *RD) {
// If vtable is internal then it has to be done.
if (!CGM.getVTables().isVTableExternal(RD))
return true;
// If it's external then maybe we will need it as available_externally.
return shouldEmitAvailableExternallyVTable(CGM, RD);
}
/// Given that at some point we emitted a reference to one or more
/// vtables, and that we are now at the end of the translation unit,
/// decide whether we should emit them.
void CIRGenModule::emitDeferredVTables() {
#ifndef NDEBUG
// Remember the size of DeferredVTables, because we're going to assume
// that this entire operation doesn't modify it.
size_t savedSize = DeferredVTables.size();
#endif
for (const CXXRecordDecl *RD : DeferredVTables)
if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD))
VTables.GenerateClassData(RD);
else if (shouldOpportunisticallyEmitVTables())
opportunisticVTables.push_back(RD);
assert(savedSize == DeferredVTables.size() &&
"deferred extra vtables during vtable emission?");
DeferredVTables.clear();
}
/// This is a callback from Sema to tell us that a particular vtable is
/// required to be emitted in this translation unit.
///
/// This is only called for vtables that _must_ be emitted (mainly due to key
/// functions). For weak vtables, CodeGen tracks when they are needed and
/// emits them as-needed.
void CIRGenModule::emitVTable(CXXRecordDecl *rd) {
VTables.GenerateClassData(rd);
}
void CIRGenVTables::GenerateClassData(const CXXRecordDecl *RD) {
assert(!cir::MissingFeatures::generateDebugInfo());
if (RD->getNumVBases())
CGM.getCXXABI().emitVirtualInheritanceTables(RD);
CGM.getCXXABI().emitVTableDefinitions(*this, RD);
}
static void AddPointerLayoutOffset(CIRGenModule &CGM,
ConstantArrayBuilder &builder,
CharUnits offset) {
builder.add(CGM.getBuilder().getConstPtrAttr(CGM.getBuilder().getUInt8PtrTy(),
offset.getQuantity()));
}
static void AddRelativeLayoutOffset(CIRGenModule &CGM,
ConstantArrayBuilder &builder,
CharUnits offset) {
llvm_unreachable("NYI");
// builder.add(llvm::ConstantInt::get(CGM.Int32Ty, offset.getQuantity()));
}
void CIRGenVTables::addVTableComponent(ConstantArrayBuilder &builder,
const VTableLayout &layout,
unsigned componentIndex,
mlir::Attribute rtti,
unsigned &nextVTableThunkIndex,
unsigned vtableAddressPoint,
bool vtableHasLocalLinkage) {
auto &component = layout.vtable_components()[componentIndex];
auto addOffsetConstant =
useRelativeLayout() ? AddRelativeLayoutOffset : AddPointerLayoutOffset;
switch (component.getKind()) {
case VTableComponent::CK_VCallOffset:
return addOffsetConstant(CGM, builder, component.getVCallOffset());
case VTableComponent::CK_VBaseOffset:
return addOffsetConstant(CGM, builder, component.getVBaseOffset());
case VTableComponent::CK_OffsetToTop:
return addOffsetConstant(CGM, builder, component.getOffsetToTop());
case VTableComponent::CK_RTTI:
if (useRelativeLayout()) {
llvm_unreachable("NYI");
// return addRelativeComponent(builder, rtti, vtableAddressPoint,
// vtableHasLocalLinkage,
// /*isCompleteDtor=*/false);
} else {
assert((mlir::isa<cir::GlobalViewAttr>(rtti) ||
mlir::isa<cir::ConstPtrAttr>(rtti)) &&
"expected GlobalViewAttr or ConstPtrAttr");
return builder.add(rtti);
}
case VTableComponent::CK_FunctionPointer:
case VTableComponent::CK_CompleteDtorPointer:
case VTableComponent::CK_DeletingDtorPointer: {
GlobalDecl GD = component.getGlobalDecl();
if (CGM.getLangOpts().CUDA) {
llvm_unreachable("NYI");
}
auto getSpecialVirtualFn = [&](StringRef name) -> cir::FuncOp {
// FIXME(PR43094): When merging comdat groups, lld can select a local
// symbol as the signature symbol even though it cannot be accessed
// outside that symbol's TU. The relative vtables ABI would make
// __cxa_pure_virtual and __cxa_deleted_virtual local symbols, and
// depending on link order, the comdat groups could resolve to the one
// with the local symbol. As a temporary solution, fill these components
// with zero. We shouldn't be calling these in the first place anyway.
if (useRelativeLayout())
llvm_unreachable("NYI");
// For NVPTX devices in OpenMP emit special functon as null pointers,
// otherwise linking ends up with unresolved references.
if (CGM.getLangOpts().OpenMP && CGM.getLangOpts().OpenMPIsTargetDevice &&
CGM.getTriple().isNVPTX())
llvm_unreachable("NYI");
cir::FuncType fnTy =
CGM.getBuilder().getFuncType({}, CGM.getBuilder().getVoidTy());
cir::FuncOp fnPtr = CGM.createRuntimeFunction(fnTy, name);
// LLVM codegen handles unnamedAddr
assert(!cir::MissingFeatures::unnamedAddr());
return fnPtr;
};
cir::FuncOp fnPtr;
if (cast<CXXMethodDecl>(GD.getDecl())->isPureVirtual()) {
// Pure virtual member functions.
if (!PureVirtualFn)
PureVirtualFn =
getSpecialVirtualFn(CGM.getCXXABI().getPureVirtualCallName());
fnPtr = PureVirtualFn;
} else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) {
// Deleted virtual member functions.
if (!DeletedVirtualFn)
DeletedVirtualFn =
getSpecialVirtualFn(CGM.getCXXABI().getDeletedVirtualCallName());
fnPtr = DeletedVirtualFn;
} else if (nextVTableThunkIndex < layout.vtable_thunks().size() &&
layout.vtable_thunks()[nextVTableThunkIndex].first ==
componentIndex) {
// Thunks.
auto &thunkInfo = layout.vtable_thunks()[nextVTableThunkIndex].second;
nextVTableThunkIndex++;
fnPtr = maybeEmitThunk(GD, thunkInfo, /*ForVTable=*/true);
} else {
// Otherwise we can use the method definition directly.
auto fnTy = CGM.getTypes().GetFunctionTypeForVTable(GD);
fnPtr = CGM.GetAddrOfFunction(GD, fnTy, /*ForVTable=*/true);
}
if (useRelativeLayout()) {
llvm_unreachable("NYI");
} else {
return builder.add(cir::GlobalViewAttr::get(
CGM.getBuilder().getUInt8PtrTy(),
mlir::FlatSymbolRefAttr::get(fnPtr.getSymNameAttr())));
}
}
case VTableComponent::CK_UnusedFunctionPointer:
if (useRelativeLayout())
llvm_unreachable("NYI");
else {
llvm_unreachable("NYI");
// return builder.addNullPointer(CGM.Int8PtrTy);
}
}
llvm_unreachable("Unexpected vtable component kind");
}
void CIRGenVTables::createVTableInitializer(ConstantRecordBuilder &builder,
const VTableLayout &layout,
mlir::Attribute rtti,
bool vtableHasLocalLinkage) {
auto componentType = getVTableComponentType();
const auto &addressPoints = layout.getAddressPointIndices();
unsigned nextVTableThunkIndex = 0;
for (unsigned vtableIndex = 0, endIndex = layout.getNumVTables();
vtableIndex != endIndex; ++vtableIndex) {
auto vtableElem = builder.beginArray(componentType);
size_t vtableStart = layout.getVTableOffset(vtableIndex);
size_t vtableEnd = vtableStart + layout.getVTableSize(vtableIndex);
for (size_t componentIndex = vtableStart; componentIndex < vtableEnd;
++componentIndex) {
addVTableComponent(vtableElem, layout, componentIndex, rtti,
nextVTableThunkIndex, addressPoints[vtableIndex],
vtableHasLocalLinkage);
}
vtableElem.finishAndAddTo(rtti.getContext(), builder);
}
}
cir::GlobalOp CIRGenVTables::generateConstructionVTable(
const CXXRecordDecl *RD, const BaseSubobject &Base, bool BaseIsVirtual,
cir::GlobalLinkageKind Linkage, VTableAddressPointsMapTy &AddressPoints) {
if (CGM.getModuleDebugInfo())
llvm_unreachable("NYI");
std::unique_ptr<VTableLayout> VTLayout(
getItaniumVTableContext().createConstructionVTableLayout(
Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD));
// Add the address points.
AddressPoints = VTLayout->getAddressPoints();
// Get the mangled construction vtable name.
SmallString<256> OutName;
llvm::raw_svector_ostream Out(OutName);
cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
.mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(),
Base.getBase(), Out);
SmallString<256> Name(OutName);
bool UsingRelativeLayout = getItaniumVTableContext().isRelativeLayout();
assert(!UsingRelativeLayout && "NYI");
auto VTType = getVTableType(*VTLayout);
// Construction vtable symbols are not part of the Itanium ABI, so we cannot
// guarantee that they actually will be available externally. Instead, when
// emitting an available_externally VTT, we provide references to an internal
// linkage construction vtable. The ABI only requires complete-object vtables
// to be the same for all instances of a type, not construction vtables.
if (Linkage == cir::GlobalLinkageKind::AvailableExternallyLinkage)
Linkage = cir::GlobalLinkageKind::InternalLinkage;
auto Align = CGM.getDataLayout().getABITypeAlign(VTType);
auto Loc = CGM.getLoc(RD->getSourceRange());
// Create the variable that will hold the construction vtable.
auto VTable = CGM.createOrReplaceCXXRuntimeVariable(
Loc, Name, VTType, Linkage, CharUnits::fromQuantity(Align));
// V-tables are always unnamed_addr.
assert(!cir::MissingFeatures::unnamedAddr() && "NYI");
auto RTTI = CGM.getAddrOfRTTIDescriptor(
Loc, CGM.getASTContext().getCanonicalTagType(Base.getBase()));
// Create and set the initializer.
ConstantInitBuilder builder(CGM);
auto components = builder.beginRecord();
createVTableInitializer(components, *VTLayout, RTTI,
cir::isLocalLinkage(VTable.getLinkage()));
components.finishAndSetAsInitializer(VTable);
// Set properties only after the initializer has been set to ensure that the
// GV is treated as definition and not declaration.
assert(!VTable.isDeclaration() && "Shouldn't set properties on declaration");
CGM.setGVProperties(VTable, RD);
CGM.emitVTableTypeMetadata(RD, VTable, *VTLayout.get());
if (UsingRelativeLayout) {
llvm_unreachable("NYI");
}
return VTable;
}
/// Compute the required linkage of the vtable for the given class.
///
/// Note that we only call this at the end of the translation unit.
cir::GlobalLinkageKind CIRGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
if (!RD->isExternallyVisible())
return cir::GlobalLinkageKind::InternalLinkage;
// We're at the end of the translation unit, so the current key
// function is fully correct.
const CXXMethodDecl *keyFunction = astContext.getCurrentKeyFunction(RD);
if (keyFunction && !RD->hasAttr<DLLImportAttr>()) {
// If this class has a key function, use that to determine the
// linkage of the vtable.
const FunctionDecl *def = nullptr;
if (keyFunction->hasBody(def))
keyFunction = cast<CXXMethodDecl>(def);
switch (keyFunction->getTemplateSpecializationKind()) {
case TSK_Undeclared:
case TSK_ExplicitSpecialization:
assert(
(def || codeGenOpts.OptimizationLevel > 0 ||
codeGenOpts.getDebugInfo() != llvm::codegenoptions::NoDebugInfo) &&
"Shouldn't query vtable linkage without key function, "
"optimizations, or debug info");
if (!def && codeGenOpts.OptimizationLevel > 0)
return cir::GlobalLinkageKind::AvailableExternallyLinkage;
if (keyFunction->isInlined())
return !astContext.getLangOpts().AppleKext
? cir::GlobalLinkageKind::LinkOnceODRLinkage
: cir::GlobalLinkageKind::InternalLinkage;
return cir::GlobalLinkageKind::ExternalLinkage;
case TSK_ImplicitInstantiation:
return !astContext.getLangOpts().AppleKext
? cir::GlobalLinkageKind::LinkOnceODRLinkage
: cir::GlobalLinkageKind::InternalLinkage;
case TSK_ExplicitInstantiationDefinition:
return !astContext.getLangOpts().AppleKext
? cir::GlobalLinkageKind::WeakODRLinkage
: cir::GlobalLinkageKind::InternalLinkage;
case TSK_ExplicitInstantiationDeclaration:
llvm_unreachable("Should not have been asked to emit this");
}
}
// -fapple-kext mode does not support weak linkage, so we must use
// internal linkage.
if (astContext.getLangOpts().AppleKext)
return cir::GlobalLinkageKind::InternalLinkage;
auto DiscardableODRLinkage = cir::GlobalLinkageKind::LinkOnceODRLinkage;
auto NonDiscardableODRLinkage = cir::GlobalLinkageKind::WeakODRLinkage;
if (RD->hasAttr<DLLExportAttr>()) {
// Cannot discard exported vtables.
DiscardableODRLinkage = NonDiscardableODRLinkage;
} else if (RD->hasAttr<DLLImportAttr>()) {
// Imported vtables are available externally.
DiscardableODRLinkage = cir::GlobalLinkageKind::AvailableExternallyLinkage;
NonDiscardableODRLinkage =
cir::GlobalLinkageKind::AvailableExternallyLinkage;
}
switch (RD->getTemplateSpecializationKind()) {
case TSK_Undeclared:
case TSK_ExplicitSpecialization:
case TSK_ImplicitInstantiation:
return DiscardableODRLinkage;
case TSK_ExplicitInstantiationDeclaration: {
// Explicit instantiations in MSVC do not provide vtables, so we must emit
// our own.
if (getTarget().getCXXABI().isMicrosoft())
return DiscardableODRLinkage;
auto r = shouldEmitAvailableExternallyVTable(*this, RD)
? cir::GlobalLinkageKind::AvailableExternallyLinkage
: cir::GlobalLinkageKind::ExternalLinkage;
return r;
}
case TSK_ExplicitInstantiationDefinition:
return NonDiscardableODRLinkage;
}
llvm_unreachable("Invalid TemplateSpecializationKind!");
}
cir::GlobalOp
getAddrOfVTTVTable(CIRGenVTables &CGVT, CIRGenModule &CGM,
const CXXRecordDecl *MostDerivedClass,
const VTTVTable &vtable, cir::GlobalLinkageKind linkage,
VTableLayout::AddressPointsMapTy &addressPoints) {
if (vtable.getBase() == MostDerivedClass) {
assert(vtable.getBaseOffset().isZero() &&
"Most derived class vtable must have a zero offset!");
// This is a regular vtable.
return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits());
}
return CGVT.generateConstructionVTable(
MostDerivedClass, vtable.getBaseSubobject(), vtable.isVirtual(), linkage,
addressPoints);
}
cir::GlobalOp CIRGenVTables::getAddrOfVTT(const CXXRecordDecl *RD) {
assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
SmallString<256> OutName;
llvm::raw_svector_ostream Out(OutName);
cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
.mangleCXXVTT(RD, Out);
StringRef Name = OutName.str();
// This will also defer the definition of the VTT.
(void)CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
VTTBuilder Builder(CGM.getASTContext(), RD, /*GenerateDefinition=*/false);
auto ArrayType = cir::ArrayType::get(CGM.getBuilder().getUInt8PtrTy(),
Builder.getVTTComponents().size());
auto Align =
CGM.getDataLayout().getABITypeAlign(CGM.getBuilder().getUInt8PtrTy());
auto VTT = CGM.createOrReplaceCXXRuntimeVariable(
CGM.getLoc(RD->getSourceRange()), Name, ArrayType,
cir::GlobalLinkageKind::ExternalLinkage, CharUnits::fromQuantity(Align));
CGM.setGVProperties(VTT, RD);
return VTT;
}
uint64_t CIRGenVTables::getSubVTTIndex(const CXXRecordDecl *RD,
BaseSubobject Base) {
BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
if (I != SubVTTIndicies.end())
return I->second;
VTTBuilder Builder(CGM.getASTContext(), RD, /*GenerateDefinition=*/false);
for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator
I = Builder.getSubVTTIndices().begin(),
E = Builder.getSubVTTIndices().end();
I != E; ++I) {
// Insert all indices.
BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
}
I = SubVTTIndicies.find(ClassSubobjectPair);
assert(I != SubVTTIndicies.end() && "Did not find index!");
return I->second;
}
uint64_t CIRGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
BaseSubobject Base) {
SecondaryVirtualPointerIndicesMapTy::iterator I =
SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
if (I != SecondaryVirtualPointerIndices.end())
return I->second;
VTTBuilder Builder(CGM.getASTContext(), RD, /*GenerateDefinition=*/false);
// Insert all secondary vpointer indices.
for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator
I = Builder.getSecondaryVirtualPointerIndices().begin(),
E = Builder.getSecondaryVirtualPointerIndices().end();
I != E; ++I) {
std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
std::make_pair(RD, I->first);
SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
}
I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
return I->second;
}
/// Emit the definition of the given vtable.
void CIRGenVTables::emitVTTDefinition(cir::GlobalOp VTT,
cir::GlobalLinkageKind Linkage,
const CXXRecordDecl *RD) {
VTTBuilder Builder(CGM.getASTContext(), RD, /*GenerateDefinition=*/true);
auto ArrayType = cir::ArrayType::get(CGM.getBuilder().getUInt8PtrTy(),
Builder.getVTTComponents().size());
SmallVector<cir::GlobalOp, 8> VTables;
SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
for (const VTTVTable *i = Builder.getVTTVTables().begin(),
*e = Builder.getVTTVTables().end();
i != e; ++i) {
VTableAddressPoints.push_back(VTableAddressPointsMapTy());
VTables.push_back(getAddrOfVTTVTable(*this, CGM, RD, *i, Linkage,
VTableAddressPoints.back()));
}
SmallVector<mlir::Attribute, 8> VTTComponents;
for (const VTTComponent *i = Builder.getVTTComponents().begin(),
*e = Builder.getVTTComponents().end();
i != e; ++i) {
const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex];
cir::GlobalOp VTable = VTables[i->VTableIndex];
VTableLayout::AddressPointLocation AddressPoint;
if (VTTVT.getBase() == RD) {
// Just get the address point for the regular vtable.
AddressPoint =
getItaniumVTableContext().getVTableLayout(RD).getAddressPoint(
i->VTableBase);
} else {
AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase);
assert(AddressPoint.AddressPointIndex != 0 &&
"Did not find ctor vtable address point!");
}
mlir::Attribute Idxs[2] = {
CGM.getBuilder().getI32IntegerAttr(AddressPoint.VTableIndex),
CGM.getBuilder().getI32IntegerAttr(AddressPoint.AddressPointIndex),
};
auto Indices = mlir::ArrayAttr::get(CGM.getBuilder().getContext(), Idxs);
auto Init = CGM.getBuilder().getGlobalViewAttr(
CGM.getBuilder().getUInt8PtrTy(), VTable, Indices);
VTTComponents.push_back(Init);
}
auto Init = CGM.getBuilder().getConstArray(
mlir::ArrayAttr::get(CGM.getBuilder().getContext(), VTTComponents),
ArrayType);
VTT.setInitialValueAttr(Init);
// Set the correct linkage.
VTT.setLinkage(Linkage);
mlir::SymbolTable::setSymbolVisibility(VTT,
CIRGenModule::getMLIRVisibility(VTT));
if (CGM.supportsCOMDAT() && VTT.isWeakForLinker()) {
assert(!cir::MissingFeatures::setComdat());
}
}
static bool shouldEmitVTableThunk(CIRGenModule &CGM, const CXXMethodDecl *MD,
bool IsUnprototyped, bool ForVTable) {
// Always emit thunks in the MS C++ ABI. We cannot rely on other TUs to
// provide thunks for us.
if (CGM.getTarget().getCXXABI().isMicrosoft())
return true;
// In the Itanium C++ ABI, vtable thunks are provided by TUs that provide
// definitions of the main method. Therefore, emitting thunks with the vtable
// is purely an optimization. Emit the thunk if optimizations are enabled and
// all of the parameter types are complete.
if (ForVTable)
return CGM.getCodeGenOpts().OptimizationLevel && !IsUnprototyped;
// Always emit thunks along with the method definition.
return true;
}
cir::FuncOp CIRGenVTables::maybeEmitThunk(GlobalDecl GD,
const ThunkInfo &ThunkAdjustments,
bool ForVTable) {
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
SmallString<256> Name;
MangleContext &MCtx = CGM.getCXXABI().getMangleContext();
llvm::raw_svector_ostream Out(Name);
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
MCtx.mangleCXXDtorThunk(DD, GD.getDtorType(), ThunkAdjustments,
/* elideOverrideInfo */ false, Out);
} else
MCtx.mangleThunk(MD, ThunkAdjustments, /* elideOverrideInfo */ false, Out);
if (CGM.getASTContext().useAbbreviatedThunkName(GD, Name.str())) {
Name = "";
if (const CXXDestructorDecl *dd = dyn_cast<CXXDestructorDecl>(MD))
MCtx.mangleCXXDtorThunk(dd, GD.getDtorType(), ThunkAdjustments,
/* elideOverrideInfo */ true, Out);
else
MCtx.mangleThunk(MD, ThunkAdjustments, /* elideOverrideInfo */ true, Out);
}
cir::FuncType ThunkVTableTy = CGM.getTypes().GetFunctionTypeForVTable(GD);
cir::FuncOp Thunk = CGM.getAddrOfThunk(Name, ThunkVTableTy, GD);
// If we don't need to emit a definition, return this declaration as is.
bool IsUnprototyped = !CGM.getTypes().isFuncTypeConvertible(
MD->getType()->castAs<FunctionType>());
if (!shouldEmitVTableThunk(CGM, MD, IsUnprototyped, ForVTable))
return Thunk;
// Arrange a function prototype appropriate for a function definition. In some
// cases in the MS ABI, we may need to build an unprototyped musttail thunk.
const CIRGenFunctionInfo &FnInfo =
IsUnprototyped ? CGM.getTypes().arrangeUnprototypedMustTailThunk(MD)
: CGM.getTypes().arrangeGlobalDeclaration(GD);
cir::FuncType ThunkFnTy = CGM.getTypes().GetFunctionType(FnInfo);
// This is to replace OG's casting to a function, keeping it here to
// streamline the 1-to-1 mapping from OG starting below
cir::FuncOp ThunkFn = Thunk;
if (Thunk.getFunctionType() != ThunkFnTy) {
cir::FuncOp OldThunkFn = ThunkFn;
assert(OldThunkFn.isDeclaration() && "Shouldn't replace non-declaration");
// Remove the name from the old thunk function and get a new thunk.
OldThunkFn.setName(StringRef());
auto thunkFn =
cir::FuncOp::create(CGM.getBuilder(), Thunk->getLoc(), Name.str(),
ThunkFnTy, cir::GlobalLinkageKind::ExternalLinkage);
CGM.setCIRFunctionAttributes(MD, FnInfo, thunkFn, /*IsThunk=*/false);
if (!OldThunkFn->use_empty()) {
OldThunkFn->replaceAllUsesWith(thunkFn);
}
// Remove the old thunk.
OldThunkFn->erase();
}
bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions();
bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions;
// If the type of the underlying GlobalValue is wrong, we'll have to replace
// it. It should be a declaration.
if (!ThunkFn.isDeclaration()) {
if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) {
// There is already a thunk emitted for this function, do nothing.
return ThunkFn;
}
setThunkProperties(CGM, ThunkAdjustments, ThunkFn, ForVTable, GD);
return ThunkFn;
}
if (IsUnprototyped)
ThunkFn->setAttr("thunk", mlir::UnitAttr::get(&CGM.getMLIRContext()));
CGM.setCIRFunctionAttributesForDefinition(GD.getDecl(), ThunkFn);
//
// Thunks for variadic methods are special because in general variadic
// arguments cannot be perfectly forwarded. In the general case, clang
// implements such thunks by cloning the original function body. However, for
// thunks with no return adjustment on targets that support musttail, we can
// use musttail to perfectly forward the variadic arguments.
bool ShouldCloneVarArgs = false;
if (!IsUnprototyped && ThunkFn.getFunctionType().isVarArg()) {
ShouldCloneVarArgs = true;
if (ThunkAdjustments.Return.isEmpty()) {
switch (CGM.getTriple().getArch()) {
case llvm::Triple::x86_64:
case llvm::Triple::x86:
case llvm::Triple::aarch64:
ShouldCloneVarArgs = false;
break;
default:
break;
}
}
}
if (ShouldCloneVarArgs) {
if (UseAvailableExternallyLinkage)
return ThunkFn;
llvm_unreachable("NYI method, see OG GenerateVarArgsThunk");
} else {
// Generate the thunk body
CIRGenFunction CGF(CGM, CGM.getBuilder());
CGF.generateThunk(ThunkFn, FnInfo, GD, ThunkAdjustments, IsUnprototyped);
}
setThunkProperties(CGM, ThunkAdjustments, ThunkFn, ForVTable, GD);
return ThunkFn;
}
void CIRGenVTables::emitThunks(GlobalDecl GD) {
const CXXMethodDecl *MD =
cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
// We don't need to generate thunks for the base destructor.
if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
return;
const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector =
VTContext->getThunkInfo(GD);
if (!ThunkInfoVector)
return;
for (const ThunkInfo &Thunk : *ThunkInfoVector)
maybeEmitThunk(GD, Thunk, /*ForVTable=*/false);
}
bool CIRGenModule::AlwaysHasLTOVisibilityPublic(const CXXRecordDecl *RD) {
if (RD->hasAttr<LTOVisibilityPublicAttr>() || RD->hasAttr<UuidAttr>() ||
RD->hasAttr<DLLExportAttr>() || RD->hasAttr<DLLImportAttr>())
return true;
if (!getCodeGenOpts().LTOVisibilityPublicStd)
return false;
const DeclContext *DC = RD;
while (true) {
auto *D = cast<Decl>(DC);
DC = DC->getParent();
if (isa<TranslationUnitDecl>(DC->getRedeclContext())) {
if (auto *ND = dyn_cast<NamespaceDecl>(D))
if (const IdentifierInfo *II = ND->getIdentifier())
if (II->isStr("std") || II->isStr("stdext"))
return true;
break;
}
}
return false;
}
bool CIRGenModule::HasHiddenLTOVisibility(const CXXRecordDecl *RD) {
LinkageInfo LV = RD->getLinkageAndVisibility();
if (!isExternallyVisible(LV.getLinkage()))
return true;
if (!getTriple().isOSBinFormatCOFF() &&
LV.getVisibility() != HiddenVisibility)
return false;
return !AlwaysHasLTOVisibilityPublic(RD);
}