Index: flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp =================================================================== --- flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp +++ flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp @@ -459,9 +459,10 @@ mlir::LogicalResult BroadcastAssignBufferization::matchAndRewrite( hlfir::AssignOp assign, mlir::PatternRewriter &rewriter) const { - if (assign.isAllocatableAssignment()) - return rewriter.notifyMatchFailure(assign, "AssignOp may imply allocation"); - + // Since RHS is a scalar and LHS is an array, LHS must be allocated + // in a conforming Fortran program, and LHS cannot be reallocated + // as a result of the assignment. So we can ignore isAllocatableAssignment + // and do the transformation always. mlir::Value rhs = assign.getRhs(); if (!fir::isa_trivial(rhs.getType())) return rewriter.notifyMatchFailure( Index: flang/test/HLFIR/opt-scalar-assign.fir =================================================================== --- flang/test/HLFIR/opt-scalar-assign.fir +++ flang/test/HLFIR/opt-scalar-assign.fir @@ -86,9 +86,19 @@ } // CHECK-LABEL: func.func @_QPtest3( // CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>>> {fir.bindc_name = "x"}) { -// CHECK: %[[VAL_1:.*]] = arith.constant 0 : i32 -// CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %[[VAL_0]] {fortran_attrs = #fir.var_attrs, uniq_name = "_QFtest3Ex"} : (!fir.ref>>>) -> (!fir.ref>>>, !fir.ref>>>) -// CHECK: hlfir.assign %[[VAL_1]] to %[[VAL_2]]#0 realloc : i32, !fir.ref>>> +// CHECK: %[[VAL_1:.*]] = arith.constant 1 : index +// CHECK: %[[VAL_2:.*]] = arith.constant 0 : index +// CHECK: %[[VAL_3:.*]] = arith.constant 0 : i32 +// CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_0]] {fortran_attrs = #fir.var_attrs, uniq_name = "_QFtest3Ex"} : (!fir.ref>>>) -> (!fir.ref>>>, !fir.ref>>>) +// CHECK: %[[VAL_5:.*]] = fir.load %[[VAL_4]]#0 : !fir.ref>>> +// CHECK: %[[VAL_6:.*]]:3 = fir.box_dims %[[VAL_5]], %[[VAL_2]] : (!fir.box>>, index) -> (index, index, index) +// CHECK: fir.do_loop %[[VAL_7:.*]] = %[[VAL_1]] to %[[VAL_6]]#1 step %[[VAL_1]] unordered { +// CHECK: %[[VAL_8:.*]]:3 = fir.box_dims %[[VAL_5]], %[[VAL_2]] : (!fir.box>>, index) -> (index, index, index) +// CHECK: %[[VAL_9:.*]] = arith.subi %[[VAL_8]]#0, %[[VAL_1]] : index +// CHECK: %[[VAL_10:.*]] = arith.addi %[[VAL_7]], %[[VAL_9]] : index +// CHECK: %[[VAL_11:.*]] = hlfir.designate %[[VAL_5]] (%[[VAL_10]]) : (!fir.box>>, index) -> !fir.ref +// CHECK: hlfir.assign %[[VAL_3]] to %[[VAL_11]] : i32, !fir.ref +// CHECK: } // CHECK: return // CHECK: }