File tree Expand file tree Collapse file tree
include/clad/Differentiator Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -210,6 +210,10 @@ namespace clad {
210210 // / For an expr E, decides if we should recompute it or store it.
211211 // / This is the central point for checkpointing.
212212 bool ShouldRecompute (const clang::Expr* E);
213+ // / Called in ShouldRecompute. In CUDA, to access a current thread/block id
214+ // / we use functions that do not change the state of any variable, since no
215+ // / point to store the value.
216+ static bool isCUDABuiltInIndex (const clang::Expr* E);
213217
214218 // / Builds a variable declaration and stores it in the function
215219 // / global scope.
Original file line number Diff line number Diff line change @@ -3228,7 +3228,22 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) {
32283228 }
32293229
32303230 bool ReverseModeVisitor::ShouldRecompute (const Expr* E) {
3231- return !(utils::ContainsFunctionCalls (E) || E->HasSideEffects (m_Context));
3231+ return !(utils::ContainsFunctionCalls (E) || E->HasSideEffects (m_Context)) ||
3232+ isCUDABuiltInIndex (E);
3233+ }
3234+
3235+ bool ReverseModeVisitor::isCUDABuiltInIndex (const Expr* E) {
3236+ const clang::Expr* B = E->IgnoreImplicit ();
3237+ if (const auto * pseudoE = llvm::dyn_cast<PseudoObjectExpr>(B)) {
3238+ if (const auto * opaqueE =
3239+ llvm::dyn_cast<OpaqueValueExpr>(pseudoE->getSemanticExpr (0 ))) {
3240+ const Expr* innerE = opaqueE->getSourceExpr ()->IgnoreImplicit ();
3241+ QualType innerT = innerE->getType ();
3242+ if (innerT.isConstQualified ())
3243+ return true ;
3244+ }
3245+ }
3246+ return false ;
32323247 }
32333248
32343249 bool ReverseModeVisitor::UsefulToStoreGlobal (Expr* E) {
You can’t perform that action at this time.
0 commit comments