This is an archive of the discontinued LLVM Phabricator instance.

Fix lack of cc1 flag in llvmcmd sections when assertions are enabled
ClosedPublic

Authored by aidengrossman on Jul 27 2022, 2:41 AM.

Details

Summary

Currently when assertions are enabled, the cc1 flag is not
inserted into the llvmcmd section of object files with embedded
bitcode. This deviates from the normal behavior where this is
the first flag that is inserted. This error stems from incorrect
use of the function generateCC1CommandLine() which requires
manually adding in the -cc1 flag which is currently not done.

Diff Detail

Event Timeline

aidengrossman created this revision.Jul 27 2022, 2:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2022, 2:41 AM
aidengrossman requested review of this revision.Jul 27 2022, 2:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2022, 2:41 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Thanks for the fix! Left one suggestion.

clang/lib/Frontend/CompilerInvocation.cpp
4549

This will shift all generated arguments in the vector. Could you do something like this instead?

Args.push_back("-cc1");
Invocation.generateCC1CommandLine(Args, SA);

I think generateCC1CommandLine() appends to Args, so this should be safe to do.

Moved insertion of cc1 flag to prevent shifting entire array

aidengrossman marked an inline comment as done.Jul 27 2022, 12:44 PM
aidengrossman added inline comments.
clang/lib/Frontend/CompilerInvocation.cpp
4549

Fixed. Thanks for the suggestion. Just tested it and everything looks good. For some reason when I was working on it this morning I didn't think it would be that easy.

The code change looks good to me, could you also include a test case for this?

aidengrossman marked an inline comment as done.

Added two unit tests to ensure functionality is correct in both
the assertion case and the default case. Should be sufficient
to ensure correct functionality without utilizing regression tests.
However, if regression tests are also desired, let me know and I can
add one in.

Thanks for adding the tests. They are failing in CI though, and I don't think they're entirely correct.

My understanding is that the goal is to make sure Clang doesn't drop the -cc1 flag when embedding command-lines into object files.

During normal compilation, the arguments given by the driver to CompilerInvocation::CreateFromArgs() already begin with -cc1, so I suggest setting up the tests to do so as well. We could use {"-cc1", "-round-trip-args"} in the first test case and {"-cc1", "-no-round-trip-args"} in the second.

We probably want to verify that the CompilerInvocation member used to store the command-line looks correctly. My guess is that the bitcode is using CodeGenOptions::CommandLineArgs, so I suggest checking that instead of GeneratedArgs in the final assertion.

(I feel like GeneratedArgs are an implementation detail in this particular test case. Moreover, the vector will be empty in the current revision, since you don't call Invocation.generateCC1CommandLine(GeneratedArgs, *this) like the surrounding test cases do.)

Rebased against main and adjusted unit tests to check directly
for the cc1 flag in CmdArgs which is directly consumed by the
bitcode writer while creating the .llvmcmd section. Also changed
the flags to better match what is already getting passed in.

jansvoboda11 accepted this revision.Jul 28 2022, 5:53 PM

LGTM, thanks for the patch!

This revision is now accepted and ready to land.Jul 28 2022, 5:53 PM

Thanks for the review and your suggestions. Do you mind pushing this commit? I don't currently have commit access to LLVM. Thanks.

Thanks for the review and your suggestions. Do you mind pushing this commit? I don't currently have commit access to LLVM. Thanks.

I can do it.

This revision was landed with ongoing or failed builds.Jul 29 2022, 6:52 PM
This revision was automatically updated to reflect the committed changes.