This is an archive of the discontinued LLVM Phabricator instance.

[NFC][clang] Fix Coverity static analyzer tool concerns about auto_causes_copy
AbandonedPublic

Authored by Manna on Apr 4 2023, 11:39 AM.

Details

Summary

Reported by Coverity:

AUTO_CAUSES_COPY
Unnecessary object copies can affect performance

  1. Inside FrontendActions.cpp file,

In clang::​DumpModuleInfoAction::​ExecuteAction(): Using the auto keyword without an & causes the copy of an object of type pair.

  1. Inside ComputeDependence.cpp file,

In clang::​computeDependence(clang::​OverloadExpr *, bool, bool, bool): Using the auto keyword without an & causes the copy of an object of type TemplateArgumentLoc.

Diff Detail

Event Timeline

Manna created this revision.Apr 4 2023, 11:39 AM
Herald added a project: Restricted Project. · View Herald Transcript
Manna requested review of this revision.Apr 4 2023, 11:39 AM

Did an analysis over each of these, only 2 I think needs to not be made, 2 are questionable, other one looks fine. I'll leave it to @aaron.ballman/others to interpret my thoughts below.

clang/include/clang/Analysis/FlowSensitive/DataflowWorklist.h
89

So succs and pred are both of type AdjacentBlock. This type is 2 ptrs long, which is roughly the 'decision point' for me. That is, a 2 pointer copy is cheap, and forming a reference requires a series of indirections, so I could go either way on this one. However, in both cases, only a single indirection is done (since enqueueBlock is being called with a CFGBlock via the conversion operator), so the indirection cost is low.

So I think these are OK to me.

clang/lib/AST/ComputeDependence.cpp
753

Ends up being pretty sizable, TemplateArgumentLoc has a TemplateArgument (which we typically pass around by value), but it also has a TemplateArgumentLocInfo, which is a pair of pointers + a pair of source locations.

clang/lib/AST/Type.cpp
748

So QualType is usually a type we pass around by value, since it is a pointer/int pair. So the copy here is intentional IMO.

clang/lib/Frontend/FrontendActions.cpp
885

A pair of a string and the other type, definitely worth saving the copies.

clang/lib/Sema/SemaCoroutine.cpp
1149

This ends up being a pair of pointers(ParmVarDecl* and Stmt*). so again I'm on the fence here :/ Probably OK doing anyway, since there is again only a single indirection, so the change is probably a wash.

clang/lib/Sema/SemaDeclCXX.cpp
804

This is just a SourceLocation, so it is tiny. Definitely not worth doing a reference here.

Manna added a comment.Apr 4 2023, 12:25 PM

Thanks @erichkeane for reviews and feedbacks.

In general, I agree with the comments from @erichkeane. We don't typically want to pass around const references to tiny objects, so QualType and SourceLocation uses should be left alone. The changes in ComputeDependence.cpp and FrontendActions.cpp seem reasonably well-motivated, but the rest feel like changes just to silence a static analyzer diagnostic. In general, I think we usually take the view that these sort of speculative perf changes aren't worth the churn unless there's a measurable performance issue being addressed. Did you do a performance analysis and find that there were concerns in any of these places? If not, then I'd say we should take just the two changes and drop the rest. WDYT?

Manna updated this revision to Diff 510907.Apr 4 2023, 1:14 PM

Thanks @aaron.ballman for reviews. I have updated patch.

Hi Soumi-
I think you've managed to do the inverse of what we suggested, at least that is how it looks! Aaron suggested keeping: ComputeDependence.cpp and FrontendActions.cpp, and not doing the rest.

Manna updated this revision to Diff 510924.Apr 4 2023, 2:00 PM
Manna edited the summary of this revision. (Show Details)

Thanks @erichkeane and @aaron.ballman for the reviews and feedbacks. I have updated diff that contains changes from ComputeDependence.cpp and FrontendActions.cpp files.

Manna added a comment.Apr 4 2023, 2:03 PM

Hi Soumi-
I think you've managed to do the inverse of what we suggested, at least that is how it looks! Aaron suggested keeping: ComputeDependence.cpp and FrontendActions.cpp, and not doing the rest.

Sorry @erichkeane. I uploaded wrong patch.

Manna abandoned this revision.Apr 4 2023, 2:15 PM