@@ -2729,7 +2729,8 @@ static void genAtomicUpdateStatement(
2729
2729
const parser::Expr &assignmentStmtExpr,
2730
2730
const parser::OmpAtomicClauseList *leftHandClauseList,
2731
2731
const parser::OmpAtomicClauseList *rightHandClauseList, mlir::Location loc,
2732
- mlir::Operation *atomicCaptureOp = nullptr ) {
2732
+ mlir::Operation *atomicCaptureOp = nullptr ,
2733
+ lower::StatementContext *atomicCaptureStmtCtx = nullptr ) {
2733
2734
// Generate `atomic.update` operation for atomic assignment statements
2734
2735
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder ();
2735
2736
mlir::Location currentLocation = converter.getCurrentLocation ();
@@ -2803,15 +2804,24 @@ static void genAtomicUpdateStatement(
2803
2804
},
2804
2805
assignmentStmtExpr.u );
2805
2806
lower::StatementContext nonAtomicStmtCtx;
2807
+ lower::StatementContext *stmtCtxPtr = &nonAtomicStmtCtx;
2806
2808
if (!nonAtomicSubExprs.empty ()) {
2807
2809
// Generate non atomic part before all the atomic operations.
2808
2810
auto insertionPoint = firOpBuilder.saveInsertionPoint ();
2809
- if (atomicCaptureOp)
2811
+ if (atomicCaptureOp) {
2812
+ assert (atomicCaptureStmtCtx && " must specify statement context" );
2810
2813
firOpBuilder.setInsertionPoint (atomicCaptureOp);
2814
+ // Any clean-ups associated with the expression lowering
2815
+ // must also be generated outside of the atomic update operation
2816
+ // and after the atomic capture operation.
2817
+ // The atomicCaptureStmtCtx will be finalized at the end
2818
+ // of the atomic capture operation generation.
2819
+ stmtCtxPtr = atomicCaptureStmtCtx;
2820
+ }
2811
2821
mlir::Value nonAtomicVal;
2812
2822
for (auto *nonAtomicSubExpr : nonAtomicSubExprs) {
2813
2823
nonAtomicVal = fir::getBase (converter.genExprValue (
2814
- currentLocation, *nonAtomicSubExpr, nonAtomicStmtCtx ));
2824
+ currentLocation, *nonAtomicSubExpr, *stmtCtxPtr ));
2815
2825
exprValueOverrides.try_emplace (nonAtomicSubExpr, nonAtomicVal);
2816
2826
}
2817
2827
if (atomicCaptureOp)
@@ -3097,7 +3107,7 @@ static void genAtomicCapture(lower::AbstractConverter &converter,
3097
3107
genAtomicUpdateStatement (
3098
3108
converter, stmt2LHSArg, stmt2VarType, stmt2Var, stmt2Expr,
3099
3109
/* leftHandClauseList=*/ nullptr ,
3100
- /* rightHandClauseList=*/ nullptr , loc, atomicCaptureOp);
3110
+ /* rightHandClauseList=*/ nullptr , loc, atomicCaptureOp, &stmtCtx );
3101
3111
} else {
3102
3112
// Atomic capture construct is of the form [capture-stmt, write-stmt]
3103
3113
firOpBuilder.setInsertionPoint (atomicCaptureOp);
@@ -3121,7 +3131,7 @@ static void genAtomicCapture(lower::AbstractConverter &converter,
3121
3131
genAtomicUpdateStatement (
3122
3132
converter, stmt1LHSArg, stmt1VarType, stmt1Var, stmt1Expr,
3123
3133
/* leftHandClauseList=*/ nullptr ,
3124
- /* rightHandClauseList=*/ nullptr , loc, atomicCaptureOp);
3134
+ /* rightHandClauseList=*/ nullptr , loc, atomicCaptureOp, &stmtCtx );
3125
3135
genAtomicCaptureStatement (converter, stmt1LHSArg, stmt2LHSArg,
3126
3136
/* leftHandClauseList=*/ nullptr ,
3127
3137
/* rightHandClauseList=*/ nullptr , elementType,
0 commit comments