Skip to content

Commit 982f8bb

Browse files
committed
Integrate LLVM IR VFE, WME and conditional records with LLVM changes, un-XFAIL tests
1 parent b3f7c8d commit 982f8bb

11 files changed

+101
-50
lines changed

lib/IRGen/GenMeta.cpp

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,29 +334,56 @@ void IRGenModule::emitNonoverriddenMethodDescriptor(const SILVTable *VTable,
334334
getAddrOfLLVMVariable(entity, init, DebugTypeInfo());
335335
}
336336

337+
void IRGenModule::setVCallVisibility(llvm::GlobalVariable *var,
338+
llvm::GlobalObject::VCallVisibility vis,
339+
std::pair<uint64_t, uint64_t> range) {
340+
// Insert attachment of !vcall_visibility !{ vis, range.first, range.second }
341+
var->addMetadata(
342+
llvm::LLVMContext::MD_vcall_visibility,
343+
*llvm::MDNode::get(getLLVMContext(),
344+
{
345+
llvm::ConstantAsMetadata::get(
346+
llvm::ConstantInt::get(Int64Ty, vis)),
347+
llvm::ConstantAsMetadata::get(
348+
llvm::ConstantInt::get(Int64Ty, range.first)),
349+
llvm::ConstantAsMetadata::get(
350+
llvm::ConstantInt::get(Int64Ty, range.second)),
351+
}));
352+
// Insert attachment of !typed_global_not_for_cfi !{}
353+
var->addMetadata("typed_global_not_for_cfi",
354+
*llvm::MDNode::get(getLLVMContext(), {}));
355+
}
356+
337357
void IRGenModule::addVTableTypeMetadata(
338358
ClassDecl *decl, llvm::GlobalVariable *var,
339359
SmallVector<std::pair<Size, SILDeclRef>, 8> vtableEntries) {
360+
if (vtableEntries.empty())
361+
return;
362+
363+
uint64_t minOffset = UINT64_MAX;
364+
uint64_t maxOffset = 0;
340365
for (auto ventry : vtableEntries) {
341366
auto method = ventry.second;
342367
auto offset = ventry.first.getValue();
343368
var->addTypeMetadata(offset, typeIdForMethod(*this, method));
369+
minOffset = std::min(minOffset, offset);
370+
maxOffset = std::max(maxOffset, offset);
344371
}
345372

373+
using VCallVisibility = llvm::GlobalObject::VCallVisibility;
374+
VCallVisibility vis = VCallVisibility::VCallVisibilityPublic;
346375
auto AS = decl->getFormalAccessScope();
347376
if (AS.isFileScope()) {
348-
var->setVCallVisibilityMetadata(
349-
llvm::GlobalObject::VCallVisibility::VCallVisibilityTranslationUnit);
377+
vis = VCallVisibility::VCallVisibilityTranslationUnit;
350378
} else if (AS.isPrivate() || AS.isInternal()) {
351-
var->setVCallVisibilityMetadata(
352-
llvm::GlobalObject::VCallVisibility::VCallVisibilityLinkageUnit);
379+
vis = VCallVisibility::VCallVisibilityLinkageUnit;
353380
} else if (getOptions().InternalizeAtLink) {
354-
var->setVCallVisibilityMetadata(
355-
llvm::GlobalObject::VCallVisibility::VCallVisibilityLinkageUnit);
356-
} else {
357-
var->setVCallVisibilityMetadata(
358-
llvm::GlobalObject::VCallVisibility::VCallVisibilityPublic);
381+
vis = VCallVisibility::VCallVisibilityLinkageUnit;
359382
}
383+
384+
auto relptrSize = DataLayout.getTypeAllocSize(Int32Ty).getValue();
385+
setVCallVisibility(var, vis,
386+
std::make_pair(minOffset, maxOffset + relptrSize));
360387
}
361388

362389
namespace {

lib/IRGen/GenProto.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,9 @@ static void addWTableTypeMetadata(IRGenModule &IGM,
21672167
llvm::GlobalVariable *global,
21682168
SILWitnessTable *wt) {
21692169
auto conf = wt->getConformance();
2170+
2171+
uint64_t minOffset = UINT64_MAX;
2172+
uint64_t maxOffset = 0;
21702173
for (auto entry : wt->getEntries()) {
21712174
if (entry.getKind() != SILWitnessTable::WitnessKind::Method)
21722175
continue;
@@ -2178,30 +2181,36 @@ static void addWTableTypeMetadata(IRGenModule &IGM,
21782181
auto index = fnProtoInfo.getFunctionIndex(member).forProtocolWitnessTable();
21792182
auto offset = index.getValue() * IGM.getPointerSize().getValue();
21802183
global->addTypeMetadata(offset, typeIdForMethod(IGM, member));
2184+
2185+
minOffset = std::min(minOffset, offset);
2186+
maxOffset = std::max(maxOffset, offset);
21812187
}
21822188

2189+
if (minOffset == UINT64_MAX)
2190+
return;
2191+
2192+
using VCallVisibility = llvm::GlobalObject::VCallVisibility;
2193+
VCallVisibility vis = VCallVisibility::VCallVisibilityPublic;
21832194
auto linkage = stripExternalFromLinkage(wt->getLinkage());
21842195
switch (linkage) {
21852196
case SILLinkage::Private:
2186-
global->setVCallVisibilityMetadata(
2187-
llvm::GlobalObject::VCallVisibility::VCallVisibilityTranslationUnit);
2197+
vis = VCallVisibility::VCallVisibilityTranslationUnit;
21882198
break;
21892199
case SILLinkage::Hidden:
21902200
case SILLinkage::Shared:
2191-
global->setVCallVisibilityMetadata(
2192-
llvm::GlobalObject::VCallVisibility::VCallVisibilityLinkageUnit);
2201+
vis = VCallVisibility::VCallVisibilityLinkageUnit;
21932202
break;
21942203
case SILLinkage::Public:
21952204
default:
21962205
if (IGM.getOptions().InternalizeAtLink) {
2197-
global->setVCallVisibilityMetadata(
2198-
llvm::GlobalObject::VCallVisibility::VCallVisibilityLinkageUnit);
2199-
} else {
2200-
global->setVCallVisibilityMetadata(
2201-
llvm::GlobalObject::VCallVisibility::VCallVisibilityPublic);
2206+
vis = VCallVisibility::VCallVisibilityLinkageUnit;
22022207
}
22032208
break;
22042209
}
2210+
2211+
auto relptrSize = IGM.DataLayout.getTypeAllocSize(IGM.Int32Ty).getValue();
2212+
IGM.setVCallVisibility(global, vis,
2213+
std::make_pair(minOffset, maxOffset + relptrSize));
22052214
}
22062215

22072216
void IRGenModule::emitSILWitnessTable(SILWitnessTable *wt) {

lib/IRGen/IRGenModule.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,11 @@ private: \
14511451
bool isDestroyer,
14521452
bool isForeign,
14531453
ForDefinition_t forDefinition);
1454+
1455+
void setVCallVisibility(llvm::GlobalVariable *var,
1456+
llvm::GlobalObject::VCallVisibility visibility,
1457+
std::pair<uint64_t, uint64_t> range);
1458+
14541459
void addVTableTypeMetadata(
14551460
ClassDecl *decl, llvm::GlobalVariable *var,
14561461
SmallVector<std::pair<Size, SILDeclRef>, 8> vtableEntries);

test/IRGen/conditional-dead-strip-exec.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
// REQUIRES: executable_test
1414

15-
// Test disabled until LLVM GlobalDCE supports conditional references.
16-
// REQUIRES: rdar81868900
15+
// FIXME(mracek): More work needed to get this to work on non-Apple platforms.
16+
// REQUIRES: VENDOR=apple
1717

1818
// (1) used
1919
@inline(never) func func1_used() { print("func1_used") }

test/IRGen/virtual-function-elimination-exec.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
// REQUIRES: executable_test
1313

14-
// Test disabled until LLVM GlobalDCE supports Swift vtables.
15-
// REQUIRES: rdar81868930
14+
// FIXME(mracek): More work needed to get this to work on non-Apple platforms.
15+
// REQUIRES: VENDOR=apple
1616

1717
class MyClass {
1818
func foo() { print("MyClass.foo") }

test/IRGen/virtual-function-elimination-ir-thunks.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// RUN: %target-build-swift -Xfrontend -disable-objc-interop -Xfrontend -enable-llvm-vfe -parse-as-library %s -DLIBRARY -module-name Library -emit-module -o %t/Library.swiftmodule
66
// RUN: %target-build-swift -Xfrontend -disable-objc-interop -Xfrontend -enable-llvm-vfe -parse-as-library %s -DCLIENT -module-name Main -I%t -emit-ir -o - | %FileCheck %s
77

8+
// UNSUPPORTED: OS=windows-msvc
9+
810
#if LIBRARY
911

1012
public class MyLibraryClass {

test/IRGen/virtual-function-elimination-ir.swift

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ class MyDerivedClass: MyClass {
1818

1919
// Has vfunc slots at offsets 56, 64, 72 (in swift.method_descriptor structs).
2020
// CHECK: @"$s4main7MyClassCMn" =
21-
// CHECK-SAME: align 4, !type !0, !type !1, !type !2, !vcall_visibility !3
21+
// CHECK-SAME: align 4, !type !0, !type !1, !type !2, !vcall_visibility !3, !typed_global_not_for_cfi !4
2222

2323
// Has vfunc slots at offsets 72, 80, 88 (on 64-bit) / 48, 52, 56 (on 32-bit).
2424
// CHECK: @"$s4main7MyClassCMf" =
25-
// CHECK-64-SAME: align 8, !type !4, !type !5, !type !6, !vcall_visibility !3
26-
// CHECK-32-SAME: align 4, !type !4, !type !5, !type !6, !vcall_visibility !3
25+
// CHECK-64-SAME: align 8, !type !5, !type !6, !type !7, !vcall_visibility !8, !typed_global_not_for_cfi !4
26+
// CHECK-32-SAME: align 4, !type !5, !type !6, !type !7, !vcall_visibility !8, !typed_global_not_for_cfi !4
2727

2828
// Has vfunc slots at offsets 56, 68, 80 (in swift.method_override_descriptor structs).
2929
// CHECK: @"$s4main14MyDerivedClassCMn" =
30-
// CHECK-SAME: align 4, !type !0, !type !7, !type !8, !vcall_visibility !3
30+
// CHECK-SAME: align 4, !type !0, !type !9, !type !10, !vcall_visibility !11, !typed_global_not_for_cfi !4
3131

3232
// Has vfunc slots at offsets 72, 80, 88 (on 64-bit) / 48, 52, 56 (on 32-bit)
3333
// CHECK: @"$s4main14MyDerivedClassCMf" =
34-
// CHECK-64-SAME: align 8, !type !4, !type !5, !type !6, !vcall_visibility !3
35-
// CHECK-32-SAME: align 4, !type !4, !type !5, !type !6, !vcall_visibility !3
34+
// CHECK-64-SAME: align 8, !type !5, !type !6, !type !7, !vcall_visibility !8, !typed_global_not_for_cfi !4
35+
// CHECK-32-SAME: align 4, !type !5, !type !6, !type !7, !vcall_visibility !8, !typed_global_not_for_cfi !4
3636

3737

3838
func func1() {
@@ -56,19 +56,25 @@ func func2() {
5656
// CHECK-64: !0 = !{i64 56, !"$s4main7MyClassC3fooyyFTq"}
5757
// CHECK-64: !1 = !{i64 64, !"$s4main7MyClassC3baryyFTq"}
5858
// CHECK-64: !2 = !{i64 72, !"$s4main7MyClassCACycfCTq"}
59-
// CHECK-64: !3 = !{i64 1}
60-
// CHECK-64: !4 = !{i64 72, !"$s4main7MyClassC3fooyyFTq"}
61-
// CHECK-64: !5 = !{i64 80, !"$s4main7MyClassC3baryyFTq"}
62-
// CHECK-64: !6 = !{i64 88, !"$s4main7MyClassCACycfCTq"}
63-
// CHECK-64: !7 = !{i64 68, !"$s4main7MyClassC3baryyFTq"}
64-
// CHECK-64: !8 = !{i64 80, !"$s4main7MyClassCACycfCTq"}
59+
// CHECK-64: !3 = !{i64 1, i64 56, i64 76}
60+
// CHECK-64: !4 = !{}
61+
// CHECK-64: !5 = !{i64 72, !"$s4main7MyClassC3fooyyFTq"}
62+
// CHECK-64: !6 = !{i64 80, !"$s4main7MyClassC3baryyFTq"}
63+
// CHECK-64: !7 = !{i64 88, !"$s4main7MyClassCACycfCTq"}
64+
// CHECK-64: !8 = !{i64 1, i64 72, i64 92}
65+
// CHECK-64: !9 = !{i64 68, !"$s4main7MyClassC3baryyFTq"}
66+
// CHECK-64: !10 = !{i64 80, !"$s4main7MyClassCACycfCTq"}
67+
// CHECK-64: !11 = !{i64 1, i64 56, i64 84}
6568

6669
// CHECK-32: !0 = !{i64 56, !"$s4main7MyClassC3fooyyFTq"}
6770
// CHECK-32: !1 = !{i64 64, !"$s4main7MyClassC3baryyFTq"}
6871
// CHECK-32: !2 = !{i64 72, !"$s4main7MyClassCACycfCTq"}
69-
// CHECK-32: !3 = !{i64 1}
70-
// CHECK-32: !4 = !{i64 48, !"$s4main7MyClassC3fooyyFTq"}
71-
// CHECK-32: !5 = !{i64 52, !"$s4main7MyClassC3baryyFTq"}
72-
// CHECK-32: !6 = !{i64 56, !"$s4main7MyClassCACycfCTq"}
73-
// CHECK-32: !7 = !{i64 68, !"$s4main7MyClassC3baryyFTq"}
74-
// CHECK-32: !8 = !{i64 80, !"$s4main7MyClassCACycfCTq"}
72+
// CHECK-32: !3 = !{i64 1, i64 56, i64 76}
73+
// CHECK-32: !4 = !{}
74+
// CHECK-32: !5 = !{i64 48, !"$s4main7MyClassC3fooyyFTq"}
75+
// CHECK-32: !6 = !{i64 52, !"$s4main7MyClassC3baryyFTq"}
76+
// CHECK-32: !7 = !{i64 56, !"$s4main7MyClassCACycfCTq"}
77+
// CHECK-32: !8 = !{i64 1, i64 48, i64 60}
78+
// CHECK-32: !9 = !{i64 68, !"$s4main7MyClassC3baryyFTq"}
79+
// CHECK-32: !10 = !{i64 80, !"$s4main7MyClassCACycfCTq"}
80+
// CHECK-32: !11 = !{i64 1, i64 56, i64 84}

test/IRGen/virtual-function-elimination-two-modules.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131

3232
// REQUIRES: executable_test
3333

34-
// Test disabled until LLVM GlobalDCE supports Swift vtables.
35-
// REQUIRES: rdar81868930
34+
// FIXME(mracek): More work needed to get this to work on non-Apple platforms.
35+
// REQUIRES: VENDOR=apple
3636

3737
#if LIBRARY
3838

test/IRGen/witness-method-elimination-exec.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
// REQUIRES: executable_test
1313

14-
// Test disabled until LLVM GlobalDCE supports Swift vtables.
15-
// REQUIRES: rdar81868930
14+
// FIXME(mracek): More work needed to get this to work on non-Apple platforms.
15+
// REQUIRES: VENDOR=apple
1616

1717
protocol TheProtocol {
1818
func func1_live()

test/IRGen/witness-method-elimination-ir.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ struct MyStruct : TheProtocol {
1818
// CHECK-SAME: i8* bitcast ({{.*}} @"$s4main8MyStructVAA11TheProtocolAAMc{{(.ptrauth)?}}" to i8*)
1919
// CHECK-SAME: i8* bitcast ({{.*}} @"$s4main8MyStructVAA11TheProtocolA2aDP10func1_liveyyFTW{{(.ptrauth)?}}" to i8*)
2020
// CHECK-SAME: i8* bitcast ({{.*}} @"$s4main8MyStructVAA11TheProtocolA2aDP10func2_deadyyFTW{{(.ptrauth)?}}" to i8*)
21-
// CHECK-64-SAME: align 8, !type !0, !type !1, !vcall_visibility !2
22-
// CHECK-32-SAME: align 4, !type !0, !type !1, !vcall_visibility !2
21+
// CHECK-64-SAME: align 8, !type !0, !type !1, !vcall_visibility !2, !typed_global_not_for_cfi !3
22+
// CHECK-32-SAME: align 4, !type !0, !type !1, !vcall_visibility !2, !typed_global_not_for_cfi !3
2323

2424
func test1() {
2525
// CHECK: define hidden swiftcc void @"$s4main5test1yyF"()
@@ -42,8 +42,10 @@ func test2() {
4242

4343
// CHECK-64: !0 = !{i64 8, !"$s4main11TheProtocolP10func1_liveyyFTq"}
4444
// CHECK-64: !1 = !{i64 16, !"$s4main11TheProtocolP10func2_deadyyFTq"}
45-
// CHECK-64: !2 = !{i64 1}
45+
// CHECK-64: !2 = !{i64 1, i64 8, i64 20}
46+
// CHECK-64: !3 = !{}
4647

4748
// CHECK-32: !0 = !{i64 4, !"$s4main11TheProtocolP10func1_liveyyFTq"}
4849
// CHECK-32: !1 = !{i64 8, !"$s4main11TheProtocolP10func2_deadyyFTq"}
49-
// CHECK-32: !2 = !{i64 1}
50+
// CHECK-32: !2 = !{i64 1, i64 4, i64 12}
51+
// CHECK-32: !3 = !{}

test/IRGen/witness-method-elimination-two-modules.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232

3333
// REQUIRES: executable_test
3434

35-
// Test disabled until LLVM GlobalDCE supports Swift wtables.
36-
// REQUIRES: rdar81868930
35+
// FIXME(mracek): More work needed to get this to work on non-Apple platforms.
36+
// REQUIRES: VENDOR=apple
3737

3838
#if LIBRARY
3939

0 commit comments

Comments
 (0)