Skip to content

Commit bee3114

Browse files
aschwaighofermeg-gupta
authored andcommitted
IRGen: Fix shift amount in typelayout lowering of mulipayload enums to bits
Transfer the fix in swiftlang#42131 to the typelayout lowering code. Noticed as part of investigating the fix for swiftlang#60590.
1 parent 9d49533 commit bee3114

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

lib/IRGen/TypeLayout.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,7 +2405,7 @@ void EnumTypeLayoutEntry::storeEnumTagMultipayload(IRGenFunction &IGF,
24052405
// } else {
24062406
// unsigned numPayloadBits = layout.payloadSize * CHAR_BIT;
24072407
// whichTag = numPayloads + (whichEmptyCase >> numPayloadBits);
2408-
// whichPayloadValue = whichEmptyCase & ((1U << numPayloads) - 1U);
2408+
// whichPayloadValue = whichEmptyCase & ((1U << numPayloadBits) - 1U);
24092409
// }
24102410
// storeMultiPayloadTag(value, layout, whichTag);
24112411
// storeMultiPayloadValue(value, layout, whichPayloadValue);
@@ -2451,7 +2451,7 @@ void EnumTypeLayoutEntry::storeEnumTagMultipayload(IRGenFunction &IGF,
24512451
whichTag->addIncoming(tmp2, Builder.GetInsertBlock());
24522452

24532453
auto tmp3 = Builder.CreateSub(
2454-
Builder.CreateShl(IGM.getInt32(1), numPayloads), IGM.getInt32(1));
2454+
Builder.CreateShl(IGM.getInt32(1), numPayloadBits), IGM.getInt32(1));
24552455
auto tmp4 = Builder.CreateAnd(whichEmptyCase, tmp3);
24562456
whichPayloadValue->addIncoming(tmp4, Builder.GetInsertBlock());
24572457
Builder.CreateBr(storeBB);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public enum ProblematicEnumeration<T> {
2+
case zero(T)
3+
case one(Bool)
4+
case two
5+
case three
6+
case four
7+
case five
8+
case six
9+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Build unoptimized library
2+
// RUN: %empty-directory(%t)
3+
// 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
4+
// RUN: %target-codesign %t/%target-library-name(ResilientEnum)
5+
// RUN: %target-build-swift %s -lResilientEnum -I %t -L %t -o %t/main %target-rpath(%t)
6+
// RUN: %target-codesign %t/main
7+
// RUN: %target-run %t/main %t/%target-library-name(ResilientEnum) | %FileCheck %s
8+
9+
// Build optimized library (this exercises a different value witness generation path)
10+
// RUN: %empty-directory(%t)
11+
// 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
12+
// RUN: %target-codesign %t/%target-library-name(ResilientEnum)
13+
// RUN: %target-build-swift %s -lResilientEnum -I %t -L %t -o %t/main %target-rpath(%t)
14+
// RUN: %target-codesign %t/main
15+
// RUN: %target-run %t/main %t/%target-library-name(ResilientEnum) | %FileCheck %s
16+
17+
import ResilientEnum
18+
19+
@inline(never)
20+
@_semantics("optimize.sil.specialize.generic.never")
21+
func dump<T>(_ t: T) {
22+
print(t)
23+
}
24+
25+
@inline(never)
26+
func doit() {
27+
let e = ProblematicEnumeration<Bool>.six
28+
// CHECK: six
29+
dump(e)
30+
}
31+
32+
doit()

0 commit comments

Comments
 (0)