Skip to content

Integrate LLVM IR VFE, WME and conditional records with LLVM changes, un-XFAIL tests #39727

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

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,29 +334,56 @@ void IRGenModule::emitNonoverriddenMethodDescriptor(const SILVTable *VTable,
getAddrOfLLVMVariable(entity, init, DebugTypeInfo());
}

void IRGenModule::setVCallVisibility(llvm::GlobalVariable *var,
llvm::GlobalObject::VCallVisibility vis,
std::pair<uint64_t, uint64_t> range) {
// Insert attachment of !vcall_visibility !{ vis, range.first, range.second }
var->addMetadata(
llvm::LLVMContext::MD_vcall_visibility,
*llvm::MDNode::get(getLLVMContext(),
{
llvm::ConstantAsMetadata::get(
llvm::ConstantInt::get(Int64Ty, vis)),
llvm::ConstantAsMetadata::get(
llvm::ConstantInt::get(Int64Ty, range.first)),
llvm::ConstantAsMetadata::get(
llvm::ConstantInt::get(Int64Ty, range.second)),
}));
// Insert attachment of !typed_global_not_for_cfi !{}
var->addMetadata("typed_global_not_for_cfi",
*llvm::MDNode::get(getLLVMContext(), {}));
}

void IRGenModule::addVTableTypeMetadata(
ClassDecl *decl, llvm::GlobalVariable *var,
SmallVector<std::pair<Size, SILDeclRef>, 8> vtableEntries) {
if (vtableEntries.empty())
return;

uint64_t minOffset = UINT64_MAX;
uint64_t maxOffset = 0;
for (auto ventry : vtableEntries) {
auto method = ventry.second;
auto offset = ventry.first.getValue();
var->addTypeMetadata(offset, typeIdForMethod(*this, method));
minOffset = std::min(minOffset, offset);
maxOffset = std::max(maxOffset, offset);
}

using VCallVisibility = llvm::GlobalObject::VCallVisibility;
VCallVisibility vis = VCallVisibility::VCallVisibilityPublic;
auto AS = decl->getFormalAccessScope();
if (AS.isFileScope()) {
var->setVCallVisibilityMetadata(
llvm::GlobalObject::VCallVisibility::VCallVisibilityTranslationUnit);
vis = VCallVisibility::VCallVisibilityTranslationUnit;
} else if (AS.isPrivate() || AS.isInternal()) {
var->setVCallVisibilityMetadata(
llvm::GlobalObject::VCallVisibility::VCallVisibilityLinkageUnit);
vis = VCallVisibility::VCallVisibilityLinkageUnit;
} else if (getOptions().InternalizeAtLink) {
var->setVCallVisibilityMetadata(
llvm::GlobalObject::VCallVisibility::VCallVisibilityLinkageUnit);
} else {
var->setVCallVisibilityMetadata(
llvm::GlobalObject::VCallVisibility::VCallVisibilityPublic);
vis = VCallVisibility::VCallVisibilityLinkageUnit;
}

auto relptrSize = DataLayout.getTypeAllocSize(Int32Ty).getValue();
setVCallVisibility(var, vis,
std::make_pair(minOffset, maxOffset + relptrSize));
}

namespace {
Expand Down
27 changes: 18 additions & 9 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,9 @@ static void addWTableTypeMetadata(IRGenModule &IGM,
llvm::GlobalVariable *global,
SILWitnessTable *wt) {
auto conf = wt->getConformance();

uint64_t minOffset = UINT64_MAX;
uint64_t maxOffset = 0;
for (auto entry : wt->getEntries()) {
if (entry.getKind() != SILWitnessTable::WitnessKind::Method)
continue;
Expand All @@ -2178,30 +2181,36 @@ static void addWTableTypeMetadata(IRGenModule &IGM,
auto index = fnProtoInfo.getFunctionIndex(member).forProtocolWitnessTable();
auto offset = index.getValue() * IGM.getPointerSize().getValue();
global->addTypeMetadata(offset, typeIdForMethod(IGM, member));

minOffset = std::min(minOffset, offset);
maxOffset = std::max(maxOffset, offset);
}

if (minOffset == UINT64_MAX)
return;

using VCallVisibility = llvm::GlobalObject::VCallVisibility;
VCallVisibility vis = VCallVisibility::VCallVisibilityPublic;
auto linkage = stripExternalFromLinkage(wt->getLinkage());
switch (linkage) {
case SILLinkage::Private:
global->setVCallVisibilityMetadata(
llvm::GlobalObject::VCallVisibility::VCallVisibilityTranslationUnit);
vis = VCallVisibility::VCallVisibilityTranslationUnit;
break;
case SILLinkage::Hidden:
case SILLinkage::Shared:
global->setVCallVisibilityMetadata(
llvm::GlobalObject::VCallVisibility::VCallVisibilityLinkageUnit);
vis = VCallVisibility::VCallVisibilityLinkageUnit;
break;
case SILLinkage::Public:
default:
if (IGM.getOptions().InternalizeAtLink) {
global->setVCallVisibilityMetadata(
llvm::GlobalObject::VCallVisibility::VCallVisibilityLinkageUnit);
} else {
global->setVCallVisibilityMetadata(
llvm::GlobalObject::VCallVisibility::VCallVisibilityPublic);
vis = VCallVisibility::VCallVisibilityLinkageUnit;
}
break;
}

auto relptrSize = IGM.DataLayout.getTypeAllocSize(IGM.Int32Ty).getValue();
IGM.setVCallVisibility(global, vis,
std::make_pair(minOffset, maxOffset + relptrSize));
}

void IRGenModule::emitSILWitnessTable(SILWitnessTable *wt) {
Expand Down
5 changes: 5 additions & 0 deletions lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,11 @@ private: \
bool isDestroyer,
bool isForeign,
ForDefinition_t forDefinition);

void setVCallVisibility(llvm::GlobalVariable *var,
llvm::GlobalObject::VCallVisibility visibility,
std::pair<uint64_t, uint64_t> range);

void addVTableTypeMetadata(
ClassDecl *decl, llvm::GlobalVariable *var,
SmallVector<std::pair<Size, SILDeclRef>, 8> vtableEntries);
Expand Down
4 changes: 2 additions & 2 deletions test/IRGen/conditional-dead-strip-exec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

// REQUIRES: executable_test

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

// (1) used
@inline(never) func func1_used() { print("func1_used") }
Expand Down
4 changes: 2 additions & 2 deletions test/IRGen/virtual-function-elimination-exec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

// REQUIRES: executable_test

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

class MyClass {
func foo() { print("MyClass.foo") }
Expand Down
2 changes: 2 additions & 0 deletions test/IRGen/virtual-function-elimination-ir-thunks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// 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
// 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

// UNSUPPORTED: OS=windows-msvc

#if LIBRARY

public class MyLibraryClass {
Expand Down
42 changes: 24 additions & 18 deletions test/IRGen/virtual-function-elimination-ir.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ class MyDerivedClass: MyClass {

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

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

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

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


func func1() {
Expand All @@ -56,19 +56,25 @@ func func2() {
// CHECK-64: !0 = !{i64 56, !"$s4main7MyClassC3fooyyFTq"}
// CHECK-64: !1 = !{i64 64, !"$s4main7MyClassC3baryyFTq"}
// CHECK-64: !2 = !{i64 72, !"$s4main7MyClassCACycfCTq"}
// CHECK-64: !3 = !{i64 1}
// CHECK-64: !4 = !{i64 72, !"$s4main7MyClassC3fooyyFTq"}
// CHECK-64: !5 = !{i64 80, !"$s4main7MyClassC3baryyFTq"}
// CHECK-64: !6 = !{i64 88, !"$s4main7MyClassCACycfCTq"}
// CHECK-64: !7 = !{i64 68, !"$s4main7MyClassC3baryyFTq"}
// CHECK-64: !8 = !{i64 80, !"$s4main7MyClassCACycfCTq"}
// CHECK-64: !3 = !{i64 1, i64 56, i64 76}
// CHECK-64: !4 = !{}
// CHECK-64: !5 = !{i64 72, !"$s4main7MyClassC3fooyyFTq"}
// CHECK-64: !6 = !{i64 80, !"$s4main7MyClassC3baryyFTq"}
// CHECK-64: !7 = !{i64 88, !"$s4main7MyClassCACycfCTq"}
// CHECK-64: !8 = !{i64 1, i64 72, i64 92}
// CHECK-64: !9 = !{i64 68, !"$s4main7MyClassC3baryyFTq"}
// CHECK-64: !10 = !{i64 80, !"$s4main7MyClassCACycfCTq"}
// CHECK-64: !11 = !{i64 1, i64 56, i64 84}

// CHECK-32: !0 = !{i64 56, !"$s4main7MyClassC3fooyyFTq"}
// CHECK-32: !1 = !{i64 64, !"$s4main7MyClassC3baryyFTq"}
// CHECK-32: !2 = !{i64 72, !"$s4main7MyClassCACycfCTq"}
// CHECK-32: !3 = !{i64 1}
// CHECK-32: !4 = !{i64 48, !"$s4main7MyClassC3fooyyFTq"}
// CHECK-32: !5 = !{i64 52, !"$s4main7MyClassC3baryyFTq"}
// CHECK-32: !6 = !{i64 56, !"$s4main7MyClassCACycfCTq"}
// CHECK-32: !7 = !{i64 68, !"$s4main7MyClassC3baryyFTq"}
// CHECK-32: !8 = !{i64 80, !"$s4main7MyClassCACycfCTq"}
// CHECK-32: !3 = !{i64 1, i64 56, i64 76}
// CHECK-32: !4 = !{}
// CHECK-32: !5 = !{i64 48, !"$s4main7MyClassC3fooyyFTq"}
// CHECK-32: !6 = !{i64 52, !"$s4main7MyClassC3baryyFTq"}
// CHECK-32: !7 = !{i64 56, !"$s4main7MyClassCACycfCTq"}
// CHECK-32: !8 = !{i64 1, i64 48, i64 60}
// CHECK-32: !9 = !{i64 68, !"$s4main7MyClassC3baryyFTq"}
// CHECK-32: !10 = !{i64 80, !"$s4main7MyClassCACycfCTq"}
// CHECK-32: !11 = !{i64 1, i64 56, i64 84}
4 changes: 2 additions & 2 deletions test/IRGen/virtual-function-elimination-two-modules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

// REQUIRES: executable_test

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

#if LIBRARY

Expand Down
4 changes: 2 additions & 2 deletions test/IRGen/witness-method-elimination-exec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

// REQUIRES: executable_test

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

protocol TheProtocol {
func func1_live()
Expand Down
10 changes: 6 additions & 4 deletions test/IRGen/witness-method-elimination-ir.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ struct MyStruct : TheProtocol {
// CHECK-SAME: i8* bitcast ({{.*}} @"$s4main8MyStructVAA11TheProtocolAAMc{{(.ptrauth)?}}" to i8*)
// CHECK-SAME: i8* bitcast ({{.*}} @"$s4main8MyStructVAA11TheProtocolA2aDP10func1_liveyyFTW{{(.ptrauth)?}}" to i8*)
// CHECK-SAME: i8* bitcast ({{.*}} @"$s4main8MyStructVAA11TheProtocolA2aDP10func2_deadyyFTW{{(.ptrauth)?}}" to i8*)
// CHECK-64-SAME: align 8, !type !0, !type !1, !vcall_visibility !2
// CHECK-32-SAME: align 4, !type !0, !type !1, !vcall_visibility !2
// CHECK-64-SAME: align 8, !type !0, !type !1, !vcall_visibility !2, !typed_global_not_for_cfi !3
// CHECK-32-SAME: align 4, !type !0, !type !1, !vcall_visibility !2, !typed_global_not_for_cfi !3

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

// CHECK-64: !0 = !{i64 8, !"$s4main11TheProtocolP10func1_liveyyFTq"}
// CHECK-64: !1 = !{i64 16, !"$s4main11TheProtocolP10func2_deadyyFTq"}
// CHECK-64: !2 = !{i64 1}
// CHECK-64: !2 = !{i64 1, i64 8, i64 20}
// CHECK-64: !3 = !{}

// CHECK-32: !0 = !{i64 4, !"$s4main11TheProtocolP10func1_liveyyFTq"}
// CHECK-32: !1 = !{i64 8, !"$s4main11TheProtocolP10func2_deadyyFTq"}
// CHECK-32: !2 = !{i64 1}
// CHECK-32: !2 = !{i64 1, i64 4, i64 12}
// CHECK-32: !3 = !{}
4 changes: 2 additions & 2 deletions test/IRGen/witness-method-elimination-two-modules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@

// REQUIRES: executable_test

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

#if LIBRARY

Expand Down