This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Give error message for invalid profile path when compiling IR
ClosedPublic

Authored by aidengrossman on Aug 30 2022, 9:31 PM.

Details

Summary

Before this patch, when compiling an IR file (eg the .llvmbc section
from an object file compiled with -Xclang -fembed-bitcode=all) and
profile data was passed in using the -fprofile-instrument-use-path
flag, there would be no error printed (as the previous implementation
relied on the error getting caught again in the constructor of
CodeGenModule which isn't called when -x ir is set). This patch
moves the error checking directly to where the error is caught
originally rather than failing silently in setPGOUseInstrumentor and
waiting to catch it in CodeGenModule to print diagnostic information to
the user.

Regression test added.

Diff Detail

Event Timeline

aidengrossman created this revision.Aug 30 2022, 9:31 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 30 2022, 9:31 PM
Herald added a subscriber: wenlei. · View Herald Transcript
aidengrossman requested review of this revision.Aug 30 2022, 9:31 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 30 2022, 9:31 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aidengrossman added a subscriber: mtrofin.

Pinging reviewers based on previous commits in setPGOUseInstrumentor.

If there is an alternative method of writing this patch (eg placing the check in an equivalent of CodeGenModule for the IR side), let me know and I can adjust it. I'm not very familiar with the clang codebase, so there might be some inefficiencies in how I've implemented things.

xur added a comment.Aug 31 2022, 10:23 AM

I'm fine with this change.

clang/lib/Frontend/CompilerInvocation.cpp
1301

Should we use "Error in reading profile ..."?
Reader still can return error even if the profile can be read.

Changed error message from "Could not read profile" to "Error in reading profile"
to better reflect the fact that profile reading can fail for multiple reasons, not just
when the file path cannot be read/does not exist.

aidengrossman marked an inline comment as done.Aug 31 2022, 1:21 PM
xur accepted this revision.Aug 31 2022, 1:49 PM
This revision is now accepted and ready to land.Aug 31 2022, 1:49 PM

Fix tests and replace error message in CodeGenModule with assertion since we're
now capturing the error message in CompileInvocation.

@xur I've modified the patch slightly (mainly fixing tests and changing the error message printing in CodeGenModule to an assert as we should be capturing everything in CompilerInvocation. Do you mind looking over these changes, specifically making sure that going through CompilerInvocation is always guaranteed if we end up hitting CodeGenModule?

@xur I've modified the patch slightly (mainly fixing tests and changing the error message printing in CodeGenModule to an assert as we should be capturing everything in CompilerInvocation. Do you mind looking over these changes, specifically making sure that going through CompilerInvocation is always guaranteed if we end up hitting CodeGenModule?

How come the old error checking in CodeGenModule wasn't tripped before?

CodeGenModule doesn't get invoked when compiling IR. The error would trip when compiling other languages (eg c), but when passing IR to clang, the function doing error checking would never get called so no error was ever thrown.

In regards to the review, I have checked most cases, and I'm pretty confident this patch should cover all cases (given that CompilerInvocation is used directly from clang's main()), but if there are any edge cases that somehow pop up due to this patch, it would be due to switching to an assert within CodeGenModule.

@xur Gentle reminder. Just want to make sure I'm not missing anything obvious since I've made some somewhat substantial changes since your sign off.

mtrofin accepted this revision.Sep 16 2022, 10:40 AM