Skip to content

Commit 272e7c0

Browse files
author
ovdiiuv
committed
Recompute the values of CUDA built-in index functions
1 parent 2805a42 commit 272e7c0

3 files changed

Lines changed: 257 additions & 256 deletions

File tree

include/clad/Differentiator/ReverseModeVisitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ 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 we use functions that do not change the state of any variable, since no point to store the value.
214+
bool isCUDABuiltInIndex(const clang::Expr* E);
213215

214216
/// Builds a variable declaration and stores it in the function
215217
/// global scope.

lib/Differentiator/ReverseModeVisitor.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3228,9 +3228,31 @@ 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)) || isCUDABuiltInIndex(E);
32323232
}
32333233

3234+
bool ReverseModeVisitor::isCUDABuiltInIndex(const Expr* E) {
3235+
const clang::Expr* B = E->IgnoreImplicit();
3236+
if (const auto* pseudoE = llvm::dyn_cast<PseudoObjectExpr>(B)) {
3237+
for (const Expr* subE : pseudoE->semantics()) {
3238+
subE = subE->IgnoreImplicit();
3239+
if (const auto* call = llvm::dyn_cast<CallExpr>(subE)) {
3240+
if (const auto* memberE = llvm::dyn_cast<MemberExpr>(call->getCallee()->IgnoreParenImpCasts())) {
3241+
const Expr* base = memberE->getBase()->IgnoreImplicit();
3242+
if (const auto* opaqueE = llvm::dyn_cast<OpaqueValueExpr>(base)) {
3243+
const Expr* innerE = opaqueE->getSourceExpr()->IgnoreImplicit();
3244+
if (const auto* DRE = llvm::dyn_cast<DeclRefExpr>(innerE)) {
3245+
std::string Name = DRE->getNameInfo().getAsString();
3246+
return Name == "blockDim" || Name == "threadIdx" || Name == "blockIdx";
3247+
}
3248+
}
3249+
}
3250+
}
3251+
}
3252+
}
3253+
return false;
3254+
}
3255+
32343256
bool ReverseModeVisitor::UsefulToStoreGlobal(Expr* E) {
32353257
if (!E)
32363258
return false;

0 commit comments

Comments
 (0)