From 90c123a177b6282e797ee4c90b17bee867876c1a Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Tue, 17 May 2022 13:12:59 -0400 Subject: Liveness tracking with phis (#2233) * #include an absolute path didn't work - because paths were taken to always be relative. * Refactor Liveness pass, such that locations can be found independently of setting up ranges. * Refactor around different stages of liveness span analysis. * WIP Take into account PHI temporaries in liveness tracking. * WIP First pass of PHI liveness refactor. * Add BlockIndex. * WIP Refactor phi liveness around inst runs. * More improvements around liveness tracking. * Bug fixes. Special handling to not add multiple ends, at starts of blocks and after accesses. * Fix test output. * Use IRInsertLoc to track insertion point. * Liveness markers don't have side effects. * Fix typo in liveness test. * Small improvements around setting SuccessorResult. * Fix memory issue around reallocation and RAIIStackArray. Update test output. * Update test output for liveness.slang. * Fix typo in SuccessorResult blockIndex. * Small tidy up. * Handle the root start block, correctly scoping the run. * Split BlockInfo into 'Root' and 'Function'. Store successors as BlockIndices. * Tidy up around liveness tracking. * Add head/tail support to ArrayViews. Use Count where appropriate. Use head/tail in liveness impl. --- source/slang/slang-emit.cpp | 65 ++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 22 deletions(-) (limited to 'source/slang/slang-emit.cpp') diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index dd176d859..baf0949bb 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -734,33 +734,54 @@ Result linkAndOptimizeIR( lowerBitCast(targetRequest, irModule); simplifyIR(irModule); - // - // Downstream targets may benefit from having live-range information for - // local variables, and our IR currently encodes a reasonably good version - // of that information. At this point we will insert live-range markers - // for local variables, on when such markers are requested. - // - // After this point in optimization, any passes that introduce new - // temporary variables into the IR module should take responsibility for - // producing their own live-range information. - // - if (codeGenContext->shouldTrackLiveness()) { - addLivenessTrackingToModule(irModule); + // Storage for liveness information + List livenessLocations; + const bool shouldTrackLiveness = codeGenContext->shouldTrackLiveness(); - dumpIRIfEnabled(codeGenContext, irModule, "LIVENESS"); - } + // + // Downstream targets may benefit from having live-range information for + // local variables, and our IR currently encodes a reasonably good version + // of that information. At this point we will insert live-range markers + // for local variables, on when such markers are requested. + // + // After this point in optimization, any passes that introduce new + // temporary variables into the IR module should take responsibility for + // producing their own live-range information. + // + if (shouldTrackLiveness) + { + LivenessUtil::locateVariables(irModule, livenessLocations); + } + + // As a late step, we need to take the SSA-form IR and move things *out* + // of SSA form, by eliminating all "phi nodes" (block parameters) and + // introducing explicit temporaries instead. Doing this at the IR level + // means that subsequent emit logic doesn't need to contend with the + // complexities of blocks with parameters. + // + + { + // We only want to accumulate locations if liveness tracking is enabled. + List* locsPtr = shouldTrackLiveness ? &livenessLocations : nullptr; + + eliminatePhis(codeGenContext, locsPtr, irModule); +#if 0 + dumpIRIfEnabled(codeGenContext, irModule, "PHIS ELIMINATED"); +#endif + } + + // If liveness is enabled add liveness ranges based on the accumulated liveness locations + + if (shouldTrackLiveness) + { + LivenessUtil::addLivenessRanges(irModule, livenessLocations); - // As a late step, we need to take the SSA-form IR and move things *out* - // of SSA form, by eliminating all "phi nodes" (block parameters) and - // introducing explicit temporaries instead. Doing this at the IR level - // means that subsequent emit logic doesn't need to contend with the - // complexities of blocks with parameters. - // - eliminatePhis(codeGenContext, irModule); #if 0 - dumpIRIfEnabled(codeGenContext, irModule, "PHIS ELIMINATED"); + dumpIRIfEnabled(codeGenContext, irModule, "LIVENESS"); #endif + } + } // TODO: We need to insert the logic that fixes variable scoping issues // here (rather than doing it very late in the emit process), because -- cgit v1.2.3