Skip to content

Commit f295cbe

Browse files
committed
IRGen: Fix shift amount in typelayout lowering of mulipayload enums to bits
Transfer the fix in #42131 to the typelayout lowering code. Noticed as part of investigating the fix for #60590.
1 parent 0cc2ee1 commit f295cbe

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
@@ -2398,7 +2398,7 @@ void EnumTypeLayoutEntry::storeEnumTagMultipayload(IRGenFunction &IGF,
23982398
// } else {
23992399
// unsigned numPayloadBits = layout.payloadSize * CHAR_BIT;
24002400
// whichTag = numPayloads + (whichEmptyCase >> numPayloadBits);
2401-
// whichPayloadValue = whichEmptyCase & ((1U << numPayloads) - 1U);
2401+
// whichPayloadValue = whichEmptyCase & ((1U << numPayloadBits) - 1U);
24022402
// }
24032403
// storeMultiPayloadTag(value, layout, whichTag);
24042404
// storeMultiPayloadValue(value, layout, whichPayloadValue);
@@ -2444,7 +2444,7 @@ void EnumTypeLayoutEntry::storeEnumTagMultipayload(IRGenFunction &IGF,
24442444
whichTag->addIncoming(tmp2, Builder.GetInsertBlock());
24452445

24462446
auto tmp3 = Builder.CreateSub(
2447-
Builder.CreateShl(IGM.getInt32(1), numPayloads), IGM.getInt32(1));
2447+
Builder.CreateShl(IGM.getInt32(1), numPayloadBits), IGM.getInt32(1));
24482448
auto tmp4 = Builder.CreateAnd(whichEmptyCase, tmp3);
24492449
whichPayloadValue->addIncoming(tmp4, Builder.GetInsertBlock());
24502450
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)