-
Notifications
You must be signed in to change notification settings - Fork 200
Reverse switches on the stored condition instead of a control-flow tape #1918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4412,6 +4412,8 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) { | |
| // statement body will be processed in both the forward and the reverse | ||
| // pass. Thus, we do not need to add them in the differentiated function. | ||
| if (!(SSData->cases.empty())) { | ||
| // The forward sweep is a clone of the original switch: control flow is | ||
| // recorded implicitly by the stored condition, so no tape is needed. | ||
| Sema::ConditionResult condRes = | ||
| m_Sema.ActOnCondition(getCurrentScope(), noLoc, CloneNode(condExpr), | ||
| Sema::ConditionKind::Switch); | ||
|
|
@@ -4421,18 +4423,40 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) { | |
| /*LParenLoc=*/noLoc, nullptr, condRes, | ||
| /*RParenLoc=*/noLoc) | ||
| .getAs<SwitchStmt>(); | ||
| activeBreakContHandler->UpdateForwAndRevBlocks(bodyDiff); | ||
|
|
||
| // Registers all the cases to the switch statement. | ||
| for (auto* SC : SSData->cases) | ||
| forwardSS->addSwitchCase(SC); | ||
|
|
||
| forwardSS = | ||
| m_Sema.ActOnFinishSwitchStmt(noLoc, forwardSS, bodyDiff.getStmt()) | ||
| .getAs<SwitchStmt>(); | ||
|
|
||
| // The reverse sweep re-switches on the stored condition. Each | ||
| // fall-through group's adjoint replay is entered through the original | ||
| // case values (the per-case `if (v == _cond) break` guards emitted by | ||
| // VisitCaseStmt peel off the cases that did not run). The trailing group | ||
| // is closed by the switch end rather than a break, so label it here; it | ||
| // is the topmost group in the bottom-up reverse block. | ||
| Stmt* revBody = bodyDiff.getStmt_dx(); | ||
| if (SSData->groupStart < SSData->cases.size()) { | ||
| Stmt* finalEntry = CloseReverseSwitchCaseGroup(*SSData); | ||
| revBody = | ||
| utils::PrependAndCreateCompoundStmt(m_Context, revBody, finalEntry); | ||
| } | ||
| Sema::ConditionResult revCondRes = | ||
| m_Sema.ActOnCondition(getCurrentScope(), noLoc, CloneNode(condExpr), | ||
| Sema::ConditionKind::Switch); | ||
| SwitchStmt* reverseSS = | ||
| m_Sema | ||
| .ActOnStartOfSwitchStmt(/*SwitchLoc=*/noLoc, | ||
| /*LParenLoc=*/noLoc, nullptr, revCondRes, | ||
| /*RParenLoc=*/noLoc) | ||
| .getAs<SwitchStmt>(); | ||
| for (auto* SC : SSData->reverseEntryCases) | ||
| reverseSS->addSwitchCase(SC); | ||
| reverseSS = m_Sema.ActOnFinishSwitchStmt(noLoc, reverseSS, revBody) | ||
| .getAs<SwitchStmt>(); | ||
|
|
||
| addToCurrentBlock(forwardSS, direction::forward); | ||
| addToCurrentBlock(bodyDiff.getStmt_dx(), direction::reverse); | ||
| addToCurrentBlock(reverseSS, direction::reverse); | ||
| } | ||
|
|
||
| PopBreakContStmtHandler(); | ||
|
|
@@ -4492,6 +4516,36 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) { | |
| return {endBlock(direction::forward), endBlock(direction::reverse)}; | ||
| } | ||
|
|
||
| Stmt* | ||
| ReverseModeVisitor::CloseReverseSwitchCaseGroup(SwitchStmtInfo& SSData) { | ||
| // Build `case v1: case v2: ... [default:] ;` for the still-open group's | ||
| // original labels, innermost first. The order of the labels is irrelevant | ||
| // (they all fall into the same reverse replay); the shared null | ||
| // substatement lets the group's adjoints follow as siblings in the switch | ||
| // body. | ||
| Stmt* inner = m_Sema.ActOnNullStmt(noLoc).get(); | ||
| for (std::size_t i = SSData.groupStart, e = SSData.cases.size(); i != e; | ||
| ++i) { | ||
| SwitchCase* rev = nullptr; | ||
| if (isa<DefaultStmt>(SSData.cases[i])) { | ||
| rev = new (m_Context) DefaultStmt(noLoc, noLoc, inner); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: assigning newly created 'gsl::owner<>' to non-owner 'SwitchCase *' [cppcoreguidelines-owning-memory] rev = new (m_Context) DefaultStmt(noLoc, noLoc, inner);
^ |
||
| } else { | ||
| auto* fwd = cast<CaseStmt>(SSData.cases[i]); | ||
| // Clone the range high end too, matching the forward case, so a GNU | ||
| // `case a ... b:` keeps its extent in the reverse switch. | ||
| Expr* rhs = fwd->getRHS() ? CloneNode(fwd->getRHS()) : nullptr; | ||
| auto* caseStmt = CaseStmt::Create(m_Context, CloneNode(fwd->getLHS()), | ||
| rhs, noLoc, noLoc, noLoc); | ||
| caseStmt->setSubStmt(inner); | ||
| rev = caseStmt; | ||
| } | ||
| SSData.reverseEntryCases.push_back(rev); | ||
| inner = rev; | ||
| } | ||
| SSData.groupStart = SSData.cases.size(); | ||
| return inner; | ||
| } | ||
|
|
||
| static bool hasCheckpointingPragma(ASTContext& C, SourceLocation loopLoc, | ||
| const DiffRequest& request) { | ||
| if (!loopLoc.isValid()) | ||
|
|
@@ -4640,10 +4694,18 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) { | |
| Stmt* newBS = | ||
| clad_compat::ActOnBreakStmt(m_Sema, noLoc, getCurrentScope()).get(); | ||
| auto* activeBreakContHandler = GetActiveBreakContStmtHandler(); | ||
| // A break in a source-level switch only closes the current fall-through | ||
| // group (VisitSwitchStmt turns each group into a reverse-switch entry). | ||
| // Unlike the loop path below, it records nothing on a control-flow tape. | ||
| if (activeBreakContHandler->m_IsInvokedBySwitchStmt) { | ||
| addToCurrentBlock(newBS); | ||
| Stmt* revEntry = CloseReverseSwitchCaseGroup(*GetActiveSwitchStmtInfo()); | ||
| return {endBlock(direction::forward), revEntry}; | ||
| } | ||
| Stmt* CFCaseStmt = activeBreakContHandler->GetNextCFCaseStmt(); | ||
| Stmt* pushExprToCurrentCase = activeBreakContHandler | ||
| ->CreateCFTapePushExprToCurrentCase(); | ||
| if (isInsideLoop && !activeBreakContHandler->m_IsInvokedBySwitchStmt) { | ||
| if (isInsideLoop) { | ||
| Expr* tapeBackExprForCurrentCase = | ||
| activeBreakContHandler->CreateCFTapeBackExprForCurrentCase(); | ||
| if (m_CurrentBreakFlagExpr) { | ||
|
|
@@ -4733,7 +4795,9 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) { | |
|
|
||
| void ReverseModeVisitor::BreakContStmtHandler::UpdateForwAndRevBlocks( | ||
| StmtDiff& bodyDiff) { | ||
| if (m_SwitchCases.empty() && !m_IsInvokedBySwitchStmt) | ||
| // Only loops reach here; a loop with no break/continue needs no | ||
| // control-flow switch in its reverse body. | ||
| if (m_SwitchCases.empty()) | ||
| return; | ||
|
|
||
| // Add case statement in the beginning of the reverse block | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: use auto when initializing with a template cast to avoid duplicating the type name [modernize-use-auto]