From 6290ef14dc6f4dbe38248b3826a0c3d32b1b27a0 Mon Sep 17 00:00:00 2001 From: Roman Levenstein Date: Thu, 28 Dec 2017 20:16:04 -0800 Subject: [PATCH] Fix a bug in shareBuffers If an @out parameter is also used as @in by an instruction, add it the the live buffers set. This bug was detected by the liveness verifier. --- src/glow/Optimizer/IROptimizer.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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