Skip to content

Commit 4803413

Browse files
author
ovdiiuv
committed
Recompute the values of CUDA built-in index functions
1 parent a40fa2c commit 4803413

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
@@ -3232,8 +3232,26 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) {
32323232
return Visit(EWC->getSubExpr(), dfdx());
32333233
}
32343234

3235+
/// Called in ShouldRecompute. In CUDA, to access a current thread/block id
3236+
/// we use functions that do not change the state of any variable, since no
3237+
/// point to store the value.
3238+
static bool isCUDABuiltInIndex(const Expr* E) {
3239+
const clang::Expr* B = E->IgnoreImplicit();
3240+
if (const auto* pseudoE = llvm::dyn_cast<PseudoObjectExpr>(B)) {
3241+
if (const auto* opaqueE =
3242+
llvm::dyn_cast<OpaqueValueExpr>(pseudoE->getSemanticExpr(0))) {
3243+
const Expr* innerE = opaqueE->getSourceExpr()->IgnoreImplicit();
3244+
QualType innerT = innerE->getType();
3245+
if (innerT.isConstQualified())
3246+
return true;
3247+
}
3248+
}
3249+
return false;
3250+
}
3251+
32353252
bool ReverseModeVisitor::ShouldRecompute(const Expr* E) {
3236-
return !(utils::ContainsFunctionCalls(E) || E->HasSideEffects(m_Context));
3253+
return !(utils::ContainsFunctionCalls(E) || E->HasSideEffects(m_Context)) ||
3254+
isCUDABuiltInIndex(E);
32373255
}
32383256

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

0 commit comments

Comments
 (0)