Skip to content

IRGen: Fix shift amount in typelayout lowering of mulipayload enums to bits #60609

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
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
4 changes: 2 additions & 2 deletions lib/IRGen/TypeLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2398,7 +2398,7 @@ void EnumTypeLayoutEntry::storeEnumTagMultipayload(IRGenFunction &IGF,
// } else {
// unsigned numPayloadBits = layout.payloadSize * CHAR_BIT;
// whichTag = numPayloads + (whichEmptyCase >> numPayloadBits);
// whichPayloadValue = whichEmptyCase & ((1U << numPayloads) - 1U);
// whichPayloadValue = whichEmptyCase & ((1U << numPayloadBits) - 1U);
// }
// storeMultiPayloadTag(value, layout, whichTag);
// storeMultiPayloadValue(value, layout, whichPayloadValue);
Expand Down Expand Up @@ -2444,7 +2444,7 @@ void EnumTypeLayoutEntry::storeEnumTagMultipayload(IRGenFunction &IGF,
whichTag->addIncoming(tmp2, Builder.GetInsertBlock());

auto tmp3 = Builder.CreateSub(
Builder.CreateShl(IGM.getInt32(1), numPayloads), IGM.getInt32(1));
Builder.CreateShl(IGM.getInt32(1), numPayloadBits), IGM.getInt32(1));
auto tmp4 = Builder.CreateAnd(whichEmptyCase, tmp3);
whichPayloadValue->addIncoming(tmp4, Builder.GetInsertBlock());
Builder.CreateBr(storeBB);
Expand Down
9 changes: 9 additions & 0 deletions test/Interpreter/Inputs/resilient_multipayload_enum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public enum ProblematicEnumeration<T> {
case zero(T)
case one(Bool)
case two
case three
case four
case five
case six
}
32 changes: 32 additions & 0 deletions test/Interpreter/resilient_multipayload_enum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Build unoptimized library
// RUN: %empty-directory(%t)
// RUN: %target-build-swift-dylib(%t/%target-library-name(ResilientEnum)) -enable-library-evolution %S/Inputs/resilient_multipayload_enum.swift -emit-module -emit-module-path %t/ResilientEnum.swiftmodule -module-name ResilientEnum
// RUN: %target-codesign %t/%target-library-name(ResilientEnum)
// RUN: %target-build-swift %s -lResilientEnum -I %t -L %t -o %t/main %target-rpath(%t)
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main %t/%target-library-name(ResilientEnum) | %FileCheck %s

// Build optimized library (this exercises a different value witness generation path)
// RUN: %empty-directory(%t)
// RUN: %target-build-swift-dylib(%t/%target-library-name(ResilientEnum)) -enable-library-evolution %S/Inputs/resilient_multipayload_enum.swift -emit-module -emit-module-path %t/ResilientEnum.swiftmodule -module-name ResilientEnum -O
// RUN: %target-codesign %t/%target-library-name(ResilientEnum)
// RUN: %target-build-swift %s -lResilientEnum -I %t -L %t -o %t/main %target-rpath(%t)
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main %t/%target-library-name(ResilientEnum) | %FileCheck %s

import ResilientEnum

@inline(never)
@_semantics("optimize.sil.specialize.generic.never")
func dump<T>(_ t: T) {
print(t)
}

@inline(never)
func doit() {
let e = ProblematicEnumeration<Bool>.six
// CHECK: six
dump(e)
}

doit()