Skip to content

Commit b17af42

Browse files
ovdiiuvvgvassilev
authored andcommitted
Recompute the values of CUDA built-in index functions
1 parent 6d35b89 commit b17af42

2 files changed

Lines changed: 249 additions & 256 deletions

File tree

lib/Differentiator/ReverseModeVisitor.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3207,8 +3207,26 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) {
32073207
return Visit(EWC->getSubExpr(), dfdx());
32083208
}
32093209

3210+
/// Called in ShouldRecompute. In CUDA, to access a current thread/block id
3211+
/// we use functions that do not change the state of any variable, since no
3212+
/// point to store the value.
3213+
static bool isCUDABuiltInIndex(const Expr* E) {
3214+
const clang::Expr* B = E->IgnoreImplicit();
3215+
if (const auto* pseudoE = llvm::dyn_cast<PseudoObjectExpr>(B)) {
3216+
if (const auto* opaqueE =
3217+
llvm::dyn_cast<OpaqueValueExpr>(pseudoE->getSemanticExpr(0))) {
3218+
const Expr* innerE = opaqueE->getSourceExpr()->IgnoreImplicit();
3219+
QualType innerT = innerE->getType();
3220+
if (innerT.isConstQualified())
3221+
return true;
3222+
}
3223+
}
3224+
return false;
3225+
}
3226+
32103227
bool ReverseModeVisitor::ShouldRecompute(const Expr* E) {
3211-
return !(utils::ContainsFunctionCalls(E) || E->HasSideEffects(m_Context));
3228+
return !(utils::ContainsFunctionCalls(E) || E->HasSideEffects(m_Context)) ||
3229+
isCUDABuiltInIndex(E);
32123230
}
32133231

32143232
bool ReverseModeVisitor::UsefulToStoreGlobal(Expr* E) {

0 commit comments

Comments
 (0)