Commit 9aff7ae7 by Ben Clayton

Change for loops to use const refs, per performance-for-range-copy clang-tidy.

This CL optimizes C++11 range-based for loops where the variable is copied in each iteration but it would suffice to obtain it by const reference. This is only applied to loop variables of types that are expensive to copy which means they are not trivially copyable or have a non-trivial copy constructor or destructor. To ensure that it is safe to replace the copy with a const reference the following heuristic is employed: The loop variable is const qualified. The loop variable is not const, but only const methods or operators are invoked on it, or it is used as const reference or value argument in constructors or function calls. See cl/304001946 for more information. Bug: None. Change-Id: Idbb5a0dbf19c6dccb16a0d525900aa99419a8527 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/43368 Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com> Tested-by: 's avatarBen Clayton <bclayton@google.com>
parent 0774188d
......@@ -83,11 +83,11 @@ MacroExpander::ScopedMacroReenabler::ScopedMacroReenabler(MacroExpander *expande
MacroExpander::ScopedMacroReenabler::~ScopedMacroReenabler()
{
mExpander->mDeferReenablingMacros = false;
for (auto macro : mExpander->mMacrosToReenable)
for (const auto &macro : mExpander->mMacrosToReenable)
{
// Copying the string here by using substr is a check for use-after-free. It detects
// use-after-free more reliably than just toggling the disabled flag.
assert(macro->name.substr() != "");
assert(!macro->name.substr().empty());
macro->disabled = false;
}
mExpander->mMacrosToReenable.clear();
......
......@@ -472,7 +472,7 @@ void SpirvShader::EmitLoop(EmitState *state) const
// Continue emitting from the merge block.
Nucleus::setInsertBlock(mergeBasicBlock);
state->pending->push_back(mergeBlockId);
for(auto it : mergeActiveLaneMasks)
for(const auto &it : mergeActiveLaneMasks)
{
state->addActiveLaneMaskEdge(it.first, mergeBlockId, it.second);
}
......@@ -574,7 +574,7 @@ SpirvShader::EmitResult SpirvShader::EmitFunctionCall(InsnIterator insn, EmitSta
ASSERT(function.blocks.size() == 1);
spv::Op wrapOpKill[] = { spv::OpLabel, spv::OpKill };
for(auto block : function.blocks)
for(const auto &block : function.blocks)
{
int insnNumber = 0;
for(auto blockInsn : block.second)
......
......@@ -2078,6 +2078,7 @@ namespace sw
}
}
#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
// Assert that every function is reachable (these should have been
// stripped in earlier stages). Unreachable functions may be code
// generated, but their own limits are not considered below, potentially
......@@ -2085,7 +2086,8 @@ namespace sw
// If we ever find cases where there are unreachable functions, we can
// replace this assert with NO-OPing or stripping out the dead
// functions.
for (auto it : functions) { ASSERT(it.second.reachable); }
for (const auto &it : functions) { ASSERT(it.second.reachable); }
#endif
// We have now gathered all the information about each of the functions
// in the shader. Traverse these functions starting from the main
......
......@@ -679,7 +679,7 @@ public:
RandomNumberGeneratorWrapper RNGW(RNG);
// Shuffle the resulting equivalence classes.
for (auto I : EquivalenceClasses) {
for (const auto &I : EquivalenceClasses) {
const RegisterList &List = I.second;
RegisterList Shuffled(List);
RandomShuffle(Shuffled.begin(), Shuffled.end(), RNGW);
......@@ -695,7 +695,7 @@ public:
OstreamLocker L(Func->getContext());
Ostream &Str = Func->getContext()->getStrDump();
Str << "Register equivalence classes:\n";
for (auto I : EquivalenceClasses) {
for (const auto &I : EquivalenceClasses) {
Str << "{";
const RegisterList &List = I.second;
bool First = true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment