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

[analyze] [CTU] Add support for pch paramteres of clang-extdef-mapping #3707

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

martong
Copy link
Contributor

@martong martong commented Jul 13, 2022

This patch is about supporting https://reviews.llvm.org/D128704 from Clang.

When doing CTU analysis setup we parse the .cpp to get an .ast and then
we run clang-extdef-mapping on the .cpp file later. This is
sub-optimal since we have to re-parse the file again.

With this patch we can now run clang-extdef-mapping directly on
the .ast file. That saves some time.

Fixes #3703

"""
try:
return bool(util.strtobool(
os.environ['CC_TEST_FORCE_EXTDEF_MAPPING_CAN_READ_PCH']))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bruntib TODO set a JIRA ticket to turn this variable on before our next internal release, when we rebase Clang.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done before the PR is merged.

@martong
Copy link
Contributor Author

martong commented Jul 14, 2022

The overall CSA runtime (with the default profile) improved roughly with 1-2 % in case of C++, and with 0-1% in case of C.
image

@bruntib bruntib added this to the release 6.20.0 milestone Jul 18, 2022
@martong
Copy link
Contributor Author

martong commented Jul 22, 2022

Ping

Copy link
Contributor

@bruntib bruntib left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically it looks good to me, though, it's not clear why relative/absolute paths matter. Maybe it would be worth for a comment about its reasons.

@martong
Copy link
Contributor Author

martong commented Jul 27, 2022

Technically it looks good to me, though, it's not clear why relative/absolute paths matter. Maybe it would be worth for a comment about its reasons.

Thanks for the review! Relative/absolute paths matter because the "on-demand" and the "pch" based CTU uses the paths differently. This is documented in ctu_manager.py at the function func_map_list_src_to_ast:

    """ Turns textual function map list with source files into a
    mapping from mangled names to mapped paths, which can be absolute paths to
    the original source files if ctu_on_demand is True, or relative path
    segments to AST-dump files that reside in CTU-DIR directory otherwise. """

@martong
Copy link
Contributor Author

martong commented Aug 2, 2022

Ping

@Szelethus Szelethus self-requested a review August 17, 2022 07:39
@whisperity whisperity changed the title Add support for pch paramteres of clang-extdef-mapping [analyze] [CTU] Add support for pch paramteres of clang-extdef-mapping Aug 19, 2022
@whisperity whisperity added enhancement 🌟 analyzer 📈 Related to the analyze commands (analysis driver) clang sa 🐉 The Clang Static Analyzer is a source code analysis tool that finds bugs in C-family programs. labels Aug 19, 2022
@martong
Copy link
Contributor Author

martong commented Sep 26, 2022

Ping @Szelethus

Copy link
Collaborator

@Szelethus Szelethus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This absolutely cannot go in without a proper PR description and commit message. Please make the review, and the subsequent archeology easier.

Does this have a corresponding LLVM commit and/or phabricator revision? Is there an issue or something for this? Why do we need this? Whats to gain?

@martong
Copy link
Contributor Author

martong commented Oct 17, 2022

This absolutely cannot go in without a proper PR description and commit message. Please make the review, and the subsequent archeology easier.

Does this have a corresponding LLVM commit and/or phabricator revision? Is there an issue or something for this? Why do we need this? Whats to gain?

This patch is about supporting https://reviews.llvm.org/D128704 from Clang.

When doing CTU analysis setup we parse the .cpp to get an .ast and then
we run clang-extdef-mapping on the .cpp file later. This is
sub-optimal since we have to re-parse the file again.

With this patch we can now run clang-extdef-mapping directly on
the .ast file. That saves some time.

@martong
Copy link
Contributor Author

martong commented Oct 17, 2022

BTW, the commit message refers to an issue that describes the meaning of this PR:
Fixes #3703

# Make relative path out of absolute.
path = path[1:] if path[0] == os.sep else path

# If clang-extdef-mapping can read a pch then the file entry already has
# the .ast suffix.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something worth assertin then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because there is another case we want to support: when the clang-extdef-mapping cannot read a pch (i.e. older Clang).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not what the comment says. It says "Can read pch => entry already has the .ast suffix". That is assertable, not that the entry has an .ast suffix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand. In this line, we add a the ".ast" suffix for the path, but only when necessary.

@@ -138,8 +138,12 @@ def __get_ctu_pre_analysis_cmds(self, action):
cmds.append(' '.join(cmd))

# Get command to create CTU index file.
# FIXME Even if clang-extdef-mapping supports pch files as parameters,
# feed the source files rather. This way, the below 'sed' command is
# still meaningful.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You lost me here a little. What is the context?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, there is a legacy technical debt here: The makefile generation implementation in makefile.py diverges and uses its own implementation for generating the ext-def-mapping file. E.g., Instead of using a generic version of ast-dump_path, it uses sed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FIXME states the current hack, or what the fix should be? I'm not in the clear. Shouldn't the FIXME state that we should use the ast_dump_path method? What is preventing us from doing it? Why does this divergence exist?

Also, what you wrote here could be in the code as well, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, maybe this fixme should simply be in the documentation for get_extdef_mapping_cmd.

Copy link
Contributor Author

@martong martong Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FIXME states the current hack, or what the fix should be? I'm not in the clear.

makefile.py had been architectured with a technical debt.

Shouldn't the FIXME state that we should use the ast_dump_path method?

Ok, I am updating the comment with that.

What is preventing us from doing it? Why does this divergence exist?

That would require further changes in makefile.py which is out of the scope of this patch, in my opinion. Addressing the mentioned technical debt should be done in a follow up patch.

Also, what you wrote here could be in the code as well, right?

Could be, yes.

Oh, maybe this fixme should simply be in the documentation for get_extdef_mapping_cmd.

I think, this problem relates to makefile.py and not of the ctu_manager.

""" AST-dump based analysis uses preprocessed paths, here the path prefix
'ast' and the filename suffix '.ast' is added to the name of the original
source file. """

# If clang-extdef-mapping can read a pch then the parameter 'path' is an
# absolute path to the ast file. Let's transform that to a relative path
# from the ctu-dir; that is the same format we have in the other cases.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was kind of missing the "otherwise" part. "If [....] path is [...]. Otherwise, path is [...], which is what we want".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I tried to reword this.

@@ -138,8 +138,12 @@ def __get_ctu_pre_analysis_cmds(self, action):
cmds.append(' '.join(cmd))

# Get command to create CTU index file.
# FIXME Even if clang-extdef-mapping supports pch files as parameters,
# feed the source files rather. This way, the below 'sed' command is
# still meaningful.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FIXME states the current hack, or what the fix should be? I'm not in the clear. Shouldn't the FIXME state that we should use the ast_dump_path method? What is preventing us from doing it? Why does this divergence exist?

Also, what you wrote here could be in the code as well, right?

# Make relative path out of absolute.
path = path[1:] if path[0] == os.sep else path

# If clang-extdef-mapping can read a pch then the file entry already has
# the .ast suffix.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not what the comment says. It says "Can read pch => entry already has the .ast suffix". That is assertable, not that the entry has an .ast suffix.

def get_extdef_mapping_cmd(action, config, source, func_map_cmd):
def get_extdef_mapping_cmd(
action, config, source, func_map_cmd, triple_arch,
for_makefile_generation):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain this parameter in the comments. I think I get it now -- we need to act a little different if this function is called from makefile.py -- explain why.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, added. Plus I've added some more explanatory comments to the command assembly.

@@ -138,8 +138,12 @@ def __get_ctu_pre_analysis_cmds(self, action):
cmds.append(' '.join(cmd))

# Get command to create CTU index file.
# FIXME Even if clang-extdef-mapping supports pch files as parameters,
# feed the source files rather. This way, the below 'sed' command is
# still meaningful.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, maybe this fixme should simply be in the documentation for get_extdef_mapping_cmd.

Copy link
Collaborator

@Szelethus Szelethus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Very nice! 🥲

# still meaningful. Unfortunately, there is a legacy technical debt
# here: The makefile generation implementation in makefile.py diverges
# and uses its own implementation for generating the ext-def-mapping
# file. E.g., Instead of using a generic version of ast-dump_path, it
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# file. E.g., Instead of using a generic version of ast-dump_path, it
# file. E.g., Instead of using a generic version of ast_dump_path, it

"""
try:
return bool(util.strtobool(
os.environ['CC_TEST_FORCE_EXTDEF_MAPPING_CAN_READ_PCH']))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done before the PR is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer 📈 Related to the analyze commands (analysis driver) clang sa 🐉 The Clang Static Analyzer is a source code analysis tool that finds bugs in C-family programs. enhancement 🌟
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Run clang-extdef-mapping on the .ast files instead of the source
5 participants