llvm-project/clang/test/Profile/cxx-throws.cpp
Jan Svoboda 839b91c229
[clang] Move -fprofile-instrument-use-path= check to driver (#159667)
The frontend currently opens the path provided via
`-fprofile-instrument-use-path=` to learn the kind of the
instrumentation data and set the `CodeGenOptions::ProfileUse` value.
This happens during command-line parsing, where we don't have a
correctly configured VFS yet, so the behavior is quite different from
all other frontend inputs. We need to move this logic out of the
frontend command line parsing logic somewhere where we do have the
configured VFS.

The complication is that the `ProfileUse` flag is being used to set
preprocessor macros, and there isn't a great place between command line
parsing and preprocessor initialization to perform this logic.

This PR solves the issue by deducing the kind of instrumentation data
right in the driver and then passing it via a new flag to the frontend.
This shouldn't change observable behavior of Clang on the driver level,
and only affects the frontend command line interface, which is an
implementation detail anyway.
2025-10-06 10:41:46 -07:00

94 lines
3.6 KiB
C++

// Test instrumentation of C++ exception handling constructs.
// FIXME: Don't seek bb labels, like "if.else"
// REQUIRES: asserts
// RUN: %clang_cc1 %s -o - -emit-llvm -fprofile-instrument=clang -fexceptions -fcxx-exceptions -triple %itanium_abi_triple | FileCheck -check-prefix=PGOGEN %s
// RUN: %clang_cc1 %s -o - -emit-llvm -fprofile-instrument=clang -fexceptions -fcxx-exceptions -triple %itanium_abi_triple | FileCheck -check-prefix=PGOGEN-EXC %s
// RUN: llvm-profdata merge %S/Inputs/cxx-throws.proftext -o %t.profdata
// RUN: %clang_cc1 %s -o - -emit-llvm -fprofile-instrument-use=clang -fprofile-instrument-use-path=%t.profdata -fexceptions -fcxx-exceptions -triple %itanium_abi_triple | FileCheck -check-prefix=PGOUSE %s
// RUN: %clang_cc1 %s -o - -emit-llvm -fprofile-instrument-use=clang -fprofile-instrument-use-path=%t.profdata -fexceptions -fcxx-exceptions -triple %itanium_abi_triple | FileCheck -check-prefix=PGOUSE-EXC %s
// PGOGEN: @[[THC:__profc__Z6throwsv]] = {{(private|internal)}} global [9 x i64] zeroinitializer
// PGOGEN-EXC: @[[THC:__profc__Z6throwsv]] = {{(private|internal)}} global [9 x i64] zeroinitializer
// PGOGEN: @[[UNC:__profc__Z11unreachablei]] = {{(private|internal)}} global [3 x i64] zeroinitializer
// PGOGEN-LABEL: @_Z6throwsv()
// PGOUSE-LABEL: @_Z6throwsv()
// PGOGEN: store {{.*}} @[[THC]]
void throws() {
// PGOGEN: store {{.*}} @[[THC]], i32 0, i32 1
// PGOUSE: br {{.*}} !prof ![[TH1:[0-9]+]]
for (int i = 0; i < 100; ++i) {
try {
// PGOGEN: store {{.*}} @[[THC]], i32 0, i32 3
// PGOUSE: br {{.*}} !prof ![[TH2:[0-9]+]]
if (i % 3) {
// PGOGEN: store {{.*}} @[[THC]], i32 0, i32 4
// PGOUSE: br {{.*}} !prof ![[TH3:[0-9]+]]
if (i < 50)
throw 1;
} else {
// The catch block may be emitted after the throw above, we can skip it
// by looking for an else block, but this will break if anyone puts an
// else in the catch
// PGOUSE: if.else{{.*}}:
// PGOGEN: if.else{{.*}}:
// PGOGEN: store {{.*}} @[[THC]], i32 0, i32 5
// PGOUSE: br {{.*}} !prof ![[TH4:[0-9]+]]
if (i >= 50)
throw 0;
}
} catch (int e) {
// PGOUSE-EXC: catch{{.*}}:
// PGOGEN-EXC: catch{{.*}}:
// PGOGEN-EXC: store {{.*}} @[[THC]], i32 0, i32 6
// PGOGEN-EXC: store {{.*}} @[[THC]], i32 0, i32 7
// PGOUSE-EXC: br {{.*}} !prof ![[TH5:[0-9]+]]
if (e) {}
}
// PGOGEN: store {{.*}} @[[THC]], i32 0, i32 2
// PGOGEN: store {{.*}} @[[THC]], i32 0, i32 8
// PGOUSE: br {{.*}} !prof ![[TH6:[0-9]+]]
if (i < 100) {}
}
// PGOUSE-NOT: br {{.*}} !prof ![0-9]+
// PGOUSE: ret void
}
// PGOGEN-LABEL: @_Z11unreachablei(i32
// PGOUSE-LABEL: @_Z11unreachablei(i32
// PGOGEN: store {{.*}} @[[UNC]]
void unreachable(int i) {
// PGOGEN: store {{.*}} @[[UNC]], i32 0, i32 1
// PGOUSE: br {{.*}} !prof ![[UN1:[0-9]+]]
if (i)
throw i;
// PGOGEN: store {{.*}} @[[UNC]], i32 0, i32 2
// Since we never reach here, the weights should all be zero (and skipped)
// PGOUSE-NOT: br {{.*}} !prof !{{.*}}
if (i) {}
}
// PGOUSE-DAG: ![[TH1]] = !{!"branch_weights", i32 101, i32 2}
// PGOUSE-DAG: ![[TH2]] = !{!"branch_weights", i32 67, i32 35}
// PGOUSE-DAG: ![[TH3]] = !{!"branch_weights", i32 34, i32 34}
// PGOUSE-DAG: ![[TH4]] = !{!"branch_weights", i32 18, i32 18}
// PGOUSE-EXC: ![[TH5]] = !{!"branch_weights", i32 34, i32 18}
// PGOUSE-DAG: ![[TH6]] = !{!"branch_weights", i32 101, i32 1}
// PGOUSE-DAG: ![[UN1]] = !{!"branch_weights", i32 2, i32 1}
int main(int argc, const char *argv[]) {
throws();
try {
unreachable(1);
} catch (int) {}
return 0;
}