diff --git a/src/glow/Optimizer/IROptimizer.cpp b/src/glow/Optimizer/IROptimizer.cpp index 6d833cad71..fcbc17d74c 100644 --- a/src/glow/Optimizer/IROptimizer.cpp +++ b/src/glow/Optimizer/IROptimizer.cpp @@ -221,10 +221,15 @@ static void shareBuffers(Module &M) { liveBuffers.insert(W); } + // Output buffers of the current instruction. + std::unordered_set outBuffers; + // For each instruction, in reverse order. for (auto it = instrs.rbegin(), e = instrs.rend(); it != e; ++it) { Instruction *I = *it; + outBuffers.clear(); + // Remove dependencies from the live set, because this instruction // writes into them. This means that the buffer is unused before the write // point. @@ -241,6 +246,7 @@ static void shareBuffers(Module &M) { auto it = liveBuffers.find(ai); if (it != liveBuffers.end()) { liveBuffers.erase(it); + outBuffers.insert(ai); } continue; } @@ -249,6 +255,12 @@ static void shareBuffers(Module &M) { if (ai && O.second == OperandKind::InOut) { liveBuffers.insert(ai); } + // The use of a buffer that is also used as an means that the + // value of the buffer is being consumed, which means that it is alive. + // Add to the live set. + if (ai && O.second == OperandKind::In && outBuffers.count(ai) > 0) { + liveBuffers.insert(ai); + } } // Now that we've calculated the liveness for the exact location of the