Skip to content

[IRGen] Always load extra tag bits as full bytes in CVW to avoid elem… #74952

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
Jul 3, 2024
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
15 changes: 13 additions & 2 deletions lib/IRGen/GenEnum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1688,11 +1688,22 @@ namespace {
auto payload = EnumPayload::load(IGF, projectPayload(IGF, addr),
PayloadSchema);
if (ExtraTagBitCount > 0) {
extraTag = IGF.Builder.CreateLoad(projectExtraTagBits(IGF, addr));
if (maskExtraTagBits) {
auto maskBits = llvm::NextPowerOf2(NumExtraTagValues) - 1;
auto projectedBits = projectExtraTagBits(IGF, addr);
// LLVM assumes that loads of fractional byte sizes have been stored
// with the same type, so all unused bits would be 0. Since we are
// re-using spare bits for tag storage, that assumption is wrong here.
// In CVW we have to mask the extra bits, which requires us to make
// this cast here, otherwise LLVM would optimize away the bit mask.
if (projectedBits.getElementType()->getIntegerBitWidth() < 8) {
projectedBits = IGF.Builder.CreateElementBitCast(addr, IGM.Int8Ty);
}
extraTag = IGF.Builder.CreateLoad(projectedBits);
auto maskBits = llvm::PowerOf2Ceil(NumExtraTagValues) - 1;
auto mask = llvm::ConstantInt::get(extraTag->getType(), maskBits);
extraTag = IGF.Builder.CreateAnd(extraTag, mask);
} else {
extraTag = IGF.Builder.CreateLoad(projectExtraTagBits(IGF, addr));
}
}
return {std::move(payload), extraTag};
Expand Down
10 changes: 10 additions & 0 deletions test/Interpreter/Inputs/layout_string_witnesses_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,16 @@ public enum MultiPayloadError {
case error2(Int, Error)
}

public enum TwoPayloadInner {
case x(AnyObject)
case y(Int)
}

public enum TwoPayloadOuter {
case x(Int)
case y(TwoPayloadInner)
}

@inline(never)
public func consume<T>(_ x: T.Type) {
withExtendedLifetime(x) {}
Expand Down
39 changes: 34 additions & 5 deletions test/Interpreter/layout_string_witnesses_static.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -I %S/Inputs/CTypes -enable-experimental-feature LayoutStringValueWitnesses -enable-layout-string-value-witnesses -parse-stdlib -emit-module -emit-module-path=%t/layout_string_witnesses_types.swiftmodule %S/Inputs/layout_string_witnesses_types.swift
// RUN: %target-swift-frontend -O -I %S/Inputs/CTypes -enable-experimental-feature LayoutStringValueWitnesses -enable-layout-string-value-witnesses -parse-stdlib -emit-module -emit-module-path=%t/layout_string_witnesses_types.swiftmodule %S/Inputs/layout_string_witnesses_types.swift

// NOTE: We have to build this as dylib to turn private external symbols into local symbols, so we can observe potential issues with linkage
// RUN: %target-build-swift-dylib(%t/%target-library-name(layout_string_witnesses_types)) -I %S/Inputs/CTypes -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -parse-stdlib -parse-as-library %S/Inputs/layout_string_witnesses_types.swift
// RUN: %target-build-swift-dylib(%t/%target-library-name(layout_string_witnesses_types)) -O -I %S/Inputs/CTypes -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -parse-stdlib -parse-as-library %S/Inputs/layout_string_witnesses_types.swift
// RUN: %target-codesign %t/%target-library-name(layout_string_witnesses_types)
// RUN: %target-swift-frontend -enable-experimental-feature LayoutStringValueWitnesses -enable-layout-string-value-witnesses -enable-library-evolution -emit-module -emit-module-path=%t/layout_string_witnesses_types_resilient.swiftmodule %S/Inputs/layout_string_witnesses_types_resilient.swift
// RUN: %target-build-swift -g -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-library-evolution -c -parse-as-library -o %t/layout_string_witnesses_types_resilient.o %S/Inputs/layout_string_witnesses_types_resilient.swift
// RUN: %target-build-swift -g -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-type-layout -Xfrontend -parse-stdlib -module-name layout_string_witnesses_static -llayout_string_witnesses_types -L%t %t/layout_string_witnesses_types_resilient.o -I %t -o %t/main %s %target-rpath(%t)
// RUN: %target-swift-frontend -O -enable-experimental-feature LayoutStringValueWitnesses -enable-layout-string-value-witnesses -enable-library-evolution -emit-module -emit-module-path=%t/layout_string_witnesses_types_resilient.swiftmodule %S/Inputs/layout_string_witnesses_types_resilient.swift
// RUN: %target-build-swift -O -g -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-library-evolution -c -parse-as-library -o %t/layout_string_witnesses_types_resilient.o %S/Inputs/layout_string_witnesses_types_resilient.swift
// RUN: %target-build-swift -O -g -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-type-layout -Xfrontend -parse-stdlib -module-name layout_string_witnesses_static -llayout_string_witnesses_types -L%t %t/layout_string_witnesses_types_resilient.o -I %t -o %t/main %s %target-rpath(%t)
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main %t/%target-library-name(layout_string_witnesses_types) | %FileCheck %s --check-prefix=CHECK -check-prefix=CHECK-%target-os

Expand Down Expand Up @@ -1290,6 +1290,35 @@ func testCTypeUnderAligned() {

testCTypeUnderAligned()

func testNestedTwoPayload() {
let ptr = UnsafeMutablePointer<TwoPayloadOuter>.allocate(capacity: 1)

do {
let x = TwoPayloadOuter.y(TwoPayloadInner.x(SimpleClass(x: 23)))
testInit(ptr, to: x)
}

do {
let y = TwoPayloadOuter.y(TwoPayloadInner.x(SimpleClass(x: 1)))

// CHECK: Before deinit
print("Before deinit")

// CHECK-NEXT: SimpleClass deinitialized!
testAssign(ptr, from: y)
}

// CHECK-NEXT: Before deinit
print("Before deinit")

// CHECK-NEXT: SimpleClass deinitialized!
testDestroy(ptr)

ptr.deallocate()
}

testNestedTwoPayload()

#if os(macOS)
func testObjc() {
let ptr = UnsafeMutablePointer<ObjcWrapper>.allocate(capacity: 1)
Expand Down