Skip to content

__llvm_profile_raw_version doesn't prevent version mismatches hard enough #52683

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

Open
glandium opened this issue Dec 14, 2021 · 2 comments
Open
Labels
PGO Profile Guided Optimizations

Comments

@glandium
Copy link
Contributor

The profile runtime checks __llvm_profile_raw_version at runtime so that if you compile something with a version of LLVM that doesn't match that of the profile runtime, the runtime aborts. This is fine if all your compiled code comes from the same compiler, but falls apart when combining them.

For example, as of writing, rustc stable through nightly use LLVM 13. We used in combination of clang trunk (currently 14), things can go pretty badly without noticing until it's too late:

$ cat >lib.rs <<EOF
#[no_mangle]
unsafe extern "C" fn foo() {
    println!("Foo");
}
EOF
$ rustc --crate-type=staticlib lib.rs -C panic=abort -C profile-generate -O
$ cat >foo.c <<EOF
extern void foo();

int main() {
	foo();
	return 1;
}
EOF
$ clang -o foo.o -c foo.c -fprofile-generate -O2
$ clang -o foo foo.o liblib.a  -lpthread -ldl -lm -fprofile-generate
$ ./foo
Foo

Here, the profile runtime found __llvm_profile_raw_version from the C object file rather than the one from the rust library. (Ideally, the linker would complain that all the linked in __llvm_profile_raw_version symbols don't have the same content... but it probably isn't allowed to).
The result seemingly worked... until you try to actually use the resulting profile:

$ llvm-profdata show default_15853215676058847363_0.profraw 
error: default_15853215676058847363_0.profraw: malformed instrumentation profile data: counter offset 666194 is greater than the maximum number of counters 2
@zamazan4ik
Copy link

zamazan4ik commented Feb 19, 2023

Any updates? I got the same issue during ClickHouse PGO optimization with Clang. Is there a way to avoid this problem?

Error:

llvm-profdata merge -o converted_profile.profdata profile_with_out_of_memory.profraw

warning: profile_with_out_of_memory.profraw: malformed instrumentation profile data: counter offset 42403480 is greater than the maximum counter offset 41111143
error: no profile can be merged

Clang version:

clang --version
Homebrew clang version 15.0.7
Target: arm64-apple-darwin22.2.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin

@Endilll Endilll added PGO Profile Guided Optimizations and removed new issue labels Aug 15, 2023
@ZequanWu
Copy link
Contributor

(Ideally, the linker would complain that all the linked in __llvm_profile_raw_version symbols don't have the same content... but it probably isn't allowed to).

Agree. The upper 32 bits of __llvm_profile_raw_version are reserved for other variant modes of profile, only the lower 32 bits indicate the real version number. I'm not sure when different modes are used if it's valid to mix them together. If it's valid, maybe we need another symbol to indicate the real version number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PGO Profile Guided Optimizations
Projects
None yet
Development

No branches or pull requests

4 participants