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,30 @@ 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+ for (const Expr* subE : pseudoE->semantics ()) {
3239+ subE = subE->IgnoreImplicit ();
3240+ if (const auto * call = llvm::dyn_cast<CallExpr>(subE)) {
3241+ if (const auto * memberE = llvm::dyn_cast<MemberExpr>(
3242+ call->getCallee ()->IgnoreParenImpCasts ())) {
3243+ const Expr* base = memberE->getBase ()->IgnoreImplicit ();
3244+ if (const auto * opaqueE = llvm::dyn_cast<OpaqueValueExpr>(base)) {
3245+ const Expr* innerE = opaqueE->getSourceExpr ()->IgnoreImplicit ();
3246+ QualType innerT = innerE->getType ();
3247+ if (innerT.isConstQualified ())
3248+ return true ;
3249+ }
3250+ }
3251+ }
3252+ }
3253+ }
3254+ return false ;
32323255 }
32333256
32343257 bool ReverseModeVisitor::UsefulToStoreGlobal (Expr* E) {
You can’t perform that action at this time.
0 commit comments