Skip to content

[NFC][CLANG] Fix static analyzer bugs about unnecessary object copies with auto keyword #75082

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

Merged
merged 1 commit into from
Dec 23, 2023

Conversation

smanna12
Copy link
Contributor

@smanna12 smanna12 commented Dec 11, 2023

Reported by Static Analyzer Tool:

In ​EmitAssemblyHelper::​RunOptimizationPipeline(): Using the auto keyword without an & causes the copy of an object of type function.

/// List of pass builder callbacks ("CodeGenOptions.h").
std::vector<std::function<void(llvm::PassBuilder &)>> PassBuilderCallbacks;

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Dec 11, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 11, 2023

@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: None (smanna12)

Changes

… with auto


Full diff: https://github.com/llvm/llvm-project/pull/75082.diff

1 Files Affected:

  • (modified) clang/lib/CodeGen/BackendUtil.cpp (+1-1)
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 77455c075cab0..1d77fbe016307 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -876,7 +876,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
           << PluginFN << toString(PassPlugin.takeError());
     }
   }
-  for (auto PassCallback : CodeGenOpts.PassBuilderCallbacks)
+  for (const auto &PassCallback : CodeGenOpts.PassBuilderCallbacks)
     PassCallback(PB);
 #define HANDLE_EXTENSION(Ext)                                                  \
   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);

@smanna12 smanna12 requested a review from tahonermann December 11, 2023 18:43
@smanna12 smanna12 changed the title [NFC][CLANG] Fix static analyzer bugs about unnecessary object copies… [NFC][CLANG] Fix static analyzer bugs about unnecessary object copies with auto keyword Dec 11, 2023
Copy link
Contributor

@tahonermann tahonermann left a comment

Choose a reason for hiding this comment

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

Looks good to me!

for (auto PassCallback : CodeGenOpts.PassBuilderCallbacks)
for (const auto &PassCallback : CodeGenOpts.PassBuilderCallbacks)
Copy link
Contributor

Choose a reason for hiding this comment

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

PassBuilderCallbacks is defined as:

clang/include/clang/Basic/CodeGenOptions.h:
404   /// List of pass builder callbacks.
405   std::vector<std::function<void(llvm::PassBuilder &)>> PassBuilderCallbacks;

In the case where a std::function element wraps a lambda, copies can be expensive, so such avoidance makes sense. The operator() member of std::function is already const qualified, so the addition of const doesn't effect the behavior, at least not in the current language standards. Such calls are valid even when a mutable lambda is wrapped. That arguably results in a const violation, so there have been some papers that argued for changes (N4348, P0045) but to my knowledge, the standard has not been changed.

This looks like a good change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you @tahonermann for reviews and information!

@smanna12 smanna12 merged commit bbe1b06 into llvm:main Dec 23, 2023
@smanna12 smanna12 deleted the Fix_Static_Analyzer_Auto branch December 23, 2023 02:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants