Skip to content

[IRGen] Use condbrs for big int switches. #42186

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
Apr 6, 2022
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
18 changes: 18 additions & 0 deletions lib/IRGen/EnumPayload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,24 @@ void EnumPayload::emitSwitch(IRGenFunction &IGF,
return;
}

if (mask.getBitWidth() > IGF.IGM.getPointerSize().getValueInBits() * 2) {
for (int index = 0, size = cases.size(); index < size; ++index) {
auto &c = cases[index];
auto *cmp = emitCompare(IGF, mask, c.first);
llvm::BasicBlock *elseBlock;
if (index < size - 1) {
elseBlock = IGF.createBasicBlock("");
} else {
elseBlock = dflt.getPointer();
}
IGF.Builder.CreateCondBr(cmp, c.second, elseBlock);
if (index < size - 1) {
IGF.Builder.emitBlock(elseBlock);
}
}
return;
}

// Otherwise emit a switch statement.
auto &C = IGF.IGM.getLLVMContext();
unsigned numBits = mask.countPopulation();
Expand Down
61 changes: 61 additions & 0 deletions test/IRGen/enum_large.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// RUN: %swift-target-frontend -emit-irgen %s | %FileCheck %s

// REQUIRES: PTRSIZE=64

public struct SpareBits {
var o: UInt64 = 0
var x: UInt8 = 0
var y: UInt64 = 0
var x_2: UInt8 = 0
var y_2: UInt64 = 0
var x_3: UInt8 = 0
var y_3: UInt64 = 0
var x_4: UInt8 = 0
var y_4: UInt64 = 0
var x_5: UInt8 = 0
var y_5: UInt64 = 0
var x_6: UInt8 = 0
var y_6: UInt64 = 0
}

public class MyClass {}

public enum Multipayload {
case a
case b(MyClass)
case c(SpareBits)
case e
case f
case g
}

@inline(never)
func dump<T>(_ t : T) {}
func dumpInt(_ i: Int) {}

// CHECK-LABEL: define{{.*}} @"$s10enum_large6testIt1eyAA12MultipayloadO_tF"{{.*}}{
// CHECK: switch i8 {{%[^,]+}}, label {{%[^,]+}} [
// CHECK: i8 0, label {{%[^,]+}}
// CHECK: i8 1, label {{%[^,]+}}
// CHECK: i8 2, label %[[REGISTER_41:[^,]+]]
// CHECK: [[REGISTER_41]]:
// CHECK: br i1 {{%[^,]+}}, label {{%[^,]+}}, label %[[REGISTER_67:[^,]+]]
// CHECK: [[REGISTER_67]]:
// CHECK: br i1 {{%[^,]+}}, label {{%[^,]+}}, label %[[REGISTER_93:[^,]+]]
// CHECK: [[REGISTER_93]]:
// CHECK: br i1 {{%[^,]+}}, label {{%[^,]+}}, label %[[REGISTER_119:[^,]+]]
// CHECK: [[REGISTER_119]]:
// CHECK: br i1 {{%[^,]+}}, label %[[REGISTER_149:[^,]+]], label {{%[^,]+}}
// CHECK: [[REGISTER_149]]
// CHECK: call swiftcc void @"$s10enum_large7dumpIntyySiF"(i64 8675309)
public func testIt(e : Multipayload) {
switch e {
case .a, .e, .f, .g:
dumpInt(8675309)
case .b(let c):
dump(c)
case .c(let s):
dump(s)
}
}

63 changes: 63 additions & 0 deletions validation-test/IRGen/rdar83158525.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test

public struct SpareBits {
var o: UInt64 = 0
var x: UInt8 = 0
var y: UInt64 = 0
var x_2: UInt8 = 0
var y_2: UInt64 = 0
var x_3: UInt8 = 0
var y_3: UInt64 = 0
var x_4: UInt8 = 0
var y_4: UInt64 = 0
var x_5: UInt8 = 0
var y_5: UInt64 = 0
var x_6: UInt8 = 0
var y_6: UInt64 = 0
}

public class MyClass {}

public enum Multipayload {
case a
case b(MyClass)
case c(SpareBits)
case e
case f
case g
}

public func testIt(_ e : Multipayload) {
switch e {
case .a:
print(".a (no payload)")
case .e:
print(".e (no payload)")
case .f:
print(".f (no payload)")
case .g:
print(".g (no payload)")
case .b(let s):
print(".b(\(s))")
case .c(let x):
print(".c(\(x))")
}
}

func doit() {
testIt(Multipayload.a)
testIt(Multipayload.e)
testIt(Multipayload.f)
testIt(Multipayload.g)
testIt(Multipayload.b(MyClass()))
testIt(Multipayload.c(SpareBits()))
}
doit()

// CHECK: .a (no payload)
// CHECK: .e (no payload)
// CHECK: .f (no payload)
// CHECK: .g (no payload)
// CHECK: .b(main.MyClass)
// CHECK: .c(SpareBits(o: 0, x: 0, y: 0, x_2: 0, y_2: 0, x_3: 0, y_3: 0, x_4: 0, y_4: 0, x_5: 0, y_5: 0, x_6: 0, y_6: 0))