Skip to content

Commit 5aa9625

Browse files
author
ovdiiuv
committed
Recompute the values of CUDA built-in index functions
1 parent a1352d1 commit 5aa9625

3 files changed

Lines changed: 258 additions & 256 deletions

File tree

include/clad/Differentiator/ReverseModeVisitor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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.

lib/Differentiator/ReverseModeVisitor.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff 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) {

0 commit comments

Comments
 (0)