@@ -16,7 +16,7 @@ using namespace clang;
1616namespace clad {
1717VectorForwardModeVisitor::VectorForwardModeVisitor (DerivativeBuilder& builder,
1818 const DiffRequest& request)
19- : BaseForwardModeVisitor(builder, request), m_IndVarCountExpr( nullptr ) {}
19+ : BaseForwardModeVisitor(builder, request) {}
2020
2121VectorForwardModeVisitor::~VectorForwardModeVisitor () {}
2222
@@ -28,8 +28,8 @@ DiffMode VectorForwardModeVisitor::GetPushForwardMode() {
2828 return DiffMode::vector_pushforward;
2929}
3030
31- void VectorForwardModeVisitor::SetIndependentVarsExpr (Expr* IndVarCountExpr ) {
32- m_IndVarCountExpr = IndVarCountExpr ;
31+ void VectorForwardModeVisitor::SetIndependentVarCountDecl (VarDecl* VD ) {
32+ m_IndVarCountDecl = VD ;
3333}
3434
3535DerivativeAndOverload VectorForwardModeVisitor::Derive () {
@@ -70,7 +70,10 @@ DerivativeAndOverload VectorForwardModeVisitor::Derive() {
7070 DiffParams args{};
7171 for (const auto & dParam : m_DiffReq.DVI )
7272 args.push_back (dParam.param );
73- auto params = BuildVectorModeParams (args);
73+ // Running sum of independent-variable counts, filled in by
74+ // BuildVectorModeParams and materialized into m_IndVarCountDecl below.
75+ Expr* indVarCountExpr = nullptr ;
76+ auto params = BuildVectorModeParams (args, indVarCountExpr);
7477 vectorDiffFD->setParams (
7578 clad_compat::makeArrayRef (params.data (), params.size ()));
7679 vectorDiffFD->setBody (nullptr );
@@ -82,11 +85,11 @@ DerivativeAndOverload VectorForwardModeVisitor::Derive() {
8285
8386 // Instantiate a variable indepVarCount to store the total number of
8487 // independent variables requested.
85- // size_t indepVarCount = m_IndVarCountExpr ;
86- auto * totalIndVars = BuildVarDecl (m_Context. UnsignedLongTy , " indepVarCount " ,
87- m_IndVarCountExpr );
88+ // size_t indepVarCount = indVarCountExpr ;
89+ auto * totalIndVars =
90+ BuildVarDecl (m_Context. UnsignedLongTy , " indepVarCount " , indVarCountExpr );
8891 addToCurrentBlock (BuildDeclStmt (totalIndVars));
89- m_IndVarCountExpr = BuildDeclRef ( totalIndVars) ;
92+ m_IndVarCountDecl = totalIndVars;
9093
9194 // Expression for maintaining the number of independent variables processed
9295 // till now present as array elements. This will be sum of sizes of all such
@@ -128,8 +131,8 @@ DerivativeAndOverload VectorForwardModeVisitor::Derive() {
128131 // Create an identity matrix for the parameter,
129132 // with number of rows equal to the size of the array,
130133 // and number of columns equal to the number of independent variables
131- llvm::SmallVector<Expr*, 3 > args = {
132- getSize, CloneNode (m_IndVarCountExpr), offsetExpr};
134+ llvm::SmallVector<Expr*, 3 > args = {getSize, buildIndVarCountRef (),
135+ offsetExpr};
133136 dVectorParam = BuildIdentityMatrixExpr (dParamType, args, loc);
134137
135138 // Update the array independent expression. getSize is already used by
@@ -144,8 +147,7 @@ DerivativeAndOverload VectorForwardModeVisitor::Derive() {
144147 }
145148 } else {
146149 // Create a one hot vector for the parameter.
147- llvm::SmallVector<Expr*, 2 > args = {CloneNode (m_IndVarCountExpr),
148- offsetExpr};
150+ llvm::SmallVector<Expr*, 2 > args = {buildIndVarCountRef (), offsetExpr};
149151 dVectorParam = BuildCallExprToCladFunction (" one_hot_vector" , args,
150152 {dParamType}, loc);
151153 ++nonArrayIndVarCount;
@@ -158,7 +160,7 @@ DerivativeAndOverload VectorForwardModeVisitor::Derive() {
158160 continue ;
159161 // This parameter is not an independent variable.
160162 // Initialize by all zeros.
161- Expr* dCount = CloneNode (m_IndVarCountExpr );
163+ Expr* dCount = buildIndVarCountRef ( );
162164 dVectorParam = BuildCallExprToCladFunction (" zero_vector" , {dCount},
163165 {dParamType}, loc);
164166 }
@@ -356,7 +358,8 @@ VectorForwardModeVisitor::CreateVectorModeOverload(FunctionDecl* derivative) {
356358}
357359
358360llvm::SmallVector<clang::ParmVarDecl*, 8 >
359- VectorForwardModeVisitor::BuildVectorModeParams (DiffParams& diffParams) {
361+ VectorForwardModeVisitor::BuildVectorModeParams (DiffParams& diffParams,
362+ Expr*& indVarCountExpr) {
360363 llvm::SmallVector<clang::ParmVarDecl*, 8 > params, paramDerivatives;
361364 params.reserve (m_DiffReq->getNumParams () + diffParams.size ());
362365 auto derivativeFnType = cast<FunctionProtoType>(m_Derivative->getType ());
@@ -395,14 +398,14 @@ VectorForwardModeVisitor::BuildVectorModeParams(DiffParams& diffParams) {
395398 if (utils::isArrayOrPointerType (PVD ->getType ())) {
396399 m_ParamVariables[*it] = (Expr*)BuildDeclRef (dPVD);
397400 // dPVD will be a clad::array or clad::array_ref, both have size() method.
398- // If m_IndVarCountExpr is null, initialize it with dPVD.size().
401+ // If indVarCountExpr is null, initialize it with dPVD.size().
399402 // Otherwise, increment it by dPVD.size().
400403 Expr* getSize = BuildArrayRefSizeExpr (m_ParamVariables[*it]);
401- if (!m_IndVarCountExpr ) {
402- m_IndVarCountExpr = getSize;
404+ if (!indVarCountExpr ) {
405+ indVarCountExpr = getSize;
403406 } else {
404- m_IndVarCountExpr =
405- BuildOp (BinaryOperatorKind::BO_Add, m_IndVarCountExpr , getSize);
407+ indVarCountExpr =
408+ BuildOp (BinaryOperatorKind::BO_Add, indVarCountExpr , getSize);
406409 }
407410 } else {
408411 m_ParamVariables[*it] = BuildOp (UO_Deref, BuildDeclRef (dPVD), noLoc);
@@ -415,11 +418,11 @@ VectorForwardModeVisitor::BuildVectorModeParams(DiffParams& diffParams) {
415418 // of non-array parameters.
416419 Expr* nonArrayIndVarCountExpr = ConstantFolder::synthesizeLiteral (
417420 m_Context.UnsignedLongTy , m_Context, nonArrayIndVarCount);
418- if (!m_IndVarCountExpr ) {
419- m_IndVarCountExpr = nonArrayIndVarCountExpr;
421+ if (!indVarCountExpr ) {
422+ indVarCountExpr = nonArrayIndVarCountExpr;
420423 } else if (nonArrayIndVarCount != 0 ) {
421- m_IndVarCountExpr = BuildOp (BinaryOperatorKind::BO_Add, m_IndVarCountExpr ,
422- nonArrayIndVarCountExpr);
424+ indVarCountExpr = BuildOp (BinaryOperatorKind::BO_Add, indVarCountExpr ,
425+ nonArrayIndVarCountExpr);
423426 }
424427
425428 // insert the derivative parameters at the end of the parameter list.
@@ -564,7 +567,7 @@ VectorForwardModeVisitor::DifferentiateVarDecl(const VarDecl* VD) {
564567StmtDiff VectorForwardModeVisitor::VisitFloatingLiteral (
565568 const clang::FloatingLiteral* FL ) {
566569 SourceLocation fakeLoc = utils::GetValidSLoc (m_Sema);
567- Expr* dCount = CloneNode (m_IndVarCountExpr );
570+ Expr* dCount = buildIndVarCountRef ( );
568571 auto * zero_vec = BuildCallExprToCladFunction (" zero_vector" , {dCount},
569572 {FL ->getType ()}, fakeLoc);
570573 return StmtDiff (Clone (FL ), zero_vec);
@@ -573,7 +576,7 @@ StmtDiff VectorForwardModeVisitor::VisitFloatingLiteral(
573576StmtDiff
574577VectorForwardModeVisitor::VisitIntegerLiteral (const clang::IntegerLiteral* IL ) {
575578 SourceLocation fakeLoc = utils::GetValidSLoc (m_Sema);
576- Expr* dCount = CloneNode (m_IndVarCountExpr );
579+ Expr* dCount = buildIndVarCountRef ( );
577580 auto * zero_vec = BuildCallExprToCladFunction (" zero_vector" , {dCount},
578581 {IL ->getType ()}, fakeLoc);
579582 return StmtDiff (Clone (IL ), zero_vec);
0 commit comments