Skip to content

Commit 0ec12fa

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

3 files changed

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

0 commit comments

Comments
 (0)