Skip to content
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

Fix PGO instrumentation crash for final switch(enum) statements together with -release #3512

Merged
merged 1 commit into from
Jul 22, 2020
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
5 changes: 3 additions & 2 deletions gen/statements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,8 @@ class ToIRVisitor : public Visitor {
llvm::BasicBlock *defaultcntr =
irs->insertBBBefore(defaultTargetBB, "defaultcntr");
irs->scope() = IRScope(defaultcntr);
PGO.emitCounterIncrement(stmt->sdefault);
if (stmt->sdefault)
PGO.emitCounterIncrement(stmt->sdefault);
llvm::BranchInst::Create(defaultTargetBB, defaultcntr);
// Create switch
si = llvm::SwitchInst::Create(condVal, defaultcntr, caseCount,
Expand Down Expand Up @@ -1063,7 +1064,7 @@ class ToIRVisitor : public Visitor {
llvm::BasicBlock *nextbb = irs->insertBBBefore(endbb, "checkcase");
llvm::BranchInst::Create(nextbb, irs->scopebb());

if (PGO.emitsInstrumentation()) {
if (stmt->sdefault && PGO.emitsInstrumentation()) {
// Prepend extra BB to "default:" to increment profiling counter.
llvm::BasicBlock *defaultcntr =
irs->insertBBBefore(defaultTargetBB, "defaultcntr");
Expand Down
43 changes: 43 additions & 0 deletions tests/PGO/final_switch_release.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// See GH issue 3375

// Test instrumentation for final switches without default case.
// The frontend creates hidden default case to catch runtime errors. We disable
// that using `-release`.

// RUN: %ldc -release -fprofile-instr-generate=%t.profraw -run %s \
// RUN: && %profdata merge %t.profraw -o %t.profdata \
// RUN: && %ldc -release -c -output-ll -of=%t.ll -fprofile-instr-use=%t.profdata %s \
// RUN: && FileCheck %s < %t.ll

extern (C): // Simplify matching by disabling function name mangling

enum A
{
Start,
End
}

// CHECK-LABEL: @final_switch(
// CHECK-SAME: !prof ![[SW0:[0-9]+]]
void final_switch(A state)
{
// CHECK: switch {{.*}} [
// CHECK: ], !prof ![[SW1:[0-9]+]]
final switch (state)
{
case A.Start:
break;
case A.End:
break;
}
}

void main()
{
final_switch(A.Start);
final_switch(A.End);
final_switch(A.Start);
}

// CHECK-DAG: ![[SW0]] = !{!"function_entry_count", i64 3}
// CHECK-DAG: ![[SW1]] = !{!"branch_weights", i32 1, i32 3, i32 2}