@@ -4539,26 +4539,16 @@ SpirvEmitter::incDecRWACSBufferCounter(const CXXMemberCallExpr *expr,
45394539 (void)doExpr(object);
45404540 }
45414541
4542- const auto *counterPair = getFinalACSBufferCounter (object);
4543- if (!counterPair ) {
4542+ auto *counter = getFinalACSBufferCounterInstruction (object);
4543+ if (!counter ) {
45444544 emitFatalError("cannot find the associated counter variable",
45454545 object->getExprLoc());
45464546 return nullptr;
45474547 }
45484548
4549- llvm::SmallVector<SpirvInstruction *, 2> indexes;
4550- if(const auto *arraySubscriptExpr = dyn_cast<ArraySubscriptExpr>(object)) {
4551- // TODO(5440): This codes does not handle multi-dimensional arrays. We need
4552- // to look at specific example to determine the best way to do it.
4553- indexes.push_back(doExpr(arraySubscriptExpr->getIdx()));
4554- }
4555-
45564549 // Add an extra 0 because the counter is wrapped in a struct.
4557- indexes.push_back(zero);
4558-
4559- auto *counterPtr = spvBuilder.createAccessChain(
4560- astContext.IntTy, counterPair->get(spvBuilder, spvContext), indexes,
4561- srcLoc, srcRange);
4550+ auto *counterPtr = spvBuilder.createAccessChain(astContext.IntTy, counter,
4551+ {zero}, srcLoc, srcRange);
45624552
45634553 SpirvInstruction *index = nullptr;
45644554 if (isInc) {
@@ -4596,13 +4586,13 @@ bool SpirvEmitter::tryToAssignCounterVar(const DeclaratorDecl *dstDecl,
45964586 // Handle AssocCounter#1 (see CounterVarFields comment)
45974587 if (const auto *dstPair =
45984588 declIdMapper.createOrGetCounterIdAliasPair(dstDecl)) {
4599- const auto *srcPair = getFinalACSBufferCounter (srcExpr);
4600- if (!srcPair ) {
4589+ auto *srcCounter = getFinalACSBufferCounterInstruction (srcExpr);
4590+ if (!srcCounter ) {
46014591 emitFatalError("cannot find the associated counter variable",
46024592 srcExpr->getExprLoc());
46034593 return false;
46044594 }
4605- dstPair->assign(*srcPair , spvBuilder, spvContext );
4595+ dstPair->assign(srcCounter , spvBuilder);
46064596 return true;
46074597 }
46084598
@@ -4633,18 +4623,18 @@ bool SpirvEmitter::tryToAssignCounterVar(const Expr *dstExpr,
46334623 dstExpr = dstExpr->IgnoreParenCasts();
46344624 srcExpr = srcExpr->IgnoreParenCasts();
46354625
4636- const auto *dstPair = getFinalACSBufferCounter (dstExpr);
4637- const auto *srcPair = getFinalACSBufferCounter (srcExpr);
4626+ auto *dstCounter = getFinalACSBufferCounterAliasAddressInstruction (dstExpr);
4627+ auto *srcCounter = getFinalACSBufferCounterInstruction (srcExpr);
46384628
4639- if ((dstPair == nullptr) != (srcPair == nullptr)) {
4629+ if ((dstCounter == nullptr) != (srcCounter == nullptr)) {
46404630 emitFatalError("cannot handle associated counter variable assignment",
46414631 srcExpr->getExprLoc());
46424632 return false;
46434633 }
46444634
46454635 // Handle AssocCounter#1 & AssocCounter#2
4646- if (dstPair && srcPair ) {
4647- dstPair->assign(*srcPair, spvBuilder, spvContext );
4636+ if (dstCounter && srcCounter ) {
4637+ spvBuilder.createStore(dstCounter, srcCounter, /* SourceLocation */ {} );
46484638 return true;
46494639 }
46504640
@@ -4662,6 +4652,37 @@ bool SpirvEmitter::tryToAssignCounterVar(const Expr *dstExpr,
46624652 return false;
46634653}
46644654
4655+ SpirvInstruction *SpirvEmitter::getFinalACSBufferCounterAliasAddressInstruction(
4656+ const Expr *expr) {
4657+ const CounterIdAliasPair *counter = getFinalACSBufferCounter(expr);
4658+ return (counter ? counter->getAliasAddress() : nullptr);
4659+ }
4660+
4661+ SpirvInstruction *
4662+ SpirvEmitter::getFinalACSBufferCounterInstruction(const Expr *expr) {
4663+ const CounterIdAliasPair *counterPair = getFinalACSBufferCounter(expr);
4664+ if (!counterPair)
4665+ return nullptr;
4666+
4667+ SpirvInstruction *counter =
4668+ counterPair->getCounterVariable(spvBuilder, spvContext);
4669+ const auto srcLoc = expr->getExprLoc();
4670+
4671+ // TODO(5440): This codes does not handle multi-dimensional arrays. We need
4672+ // to look at specific example to determine the best way to do it. Could a
4673+ // call to collectArrayStructIndices handle that for us?
4674+ llvm::SmallVector<SpirvInstruction *, 2> indexes;
4675+ if (const auto *arraySubscriptExpr = dyn_cast<ArraySubscriptExpr>(expr)) {
4676+ indexes.push_back(doExpr(arraySubscriptExpr->getIdx()));
4677+ }
4678+
4679+ if (!indexes.empty()) {
4680+ counter = spvBuilder.createAccessChain(spvContext.getACSBufferCounterType(),
4681+ counter, indexes, srcLoc);
4682+ }
4683+ return counter;
4684+ }
4685+
46654686const CounterIdAliasPair *
46664687SpirvEmitter::getFinalACSBufferCounter(const Expr *expr) {
46674688 // AssocCounter#1: referencing some stand-alone variable
0 commit comments