diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp --- a/flang/lib/Lower/ConvertExprToHLFIR.cpp +++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp @@ -1259,9 +1259,28 @@ hlfir::Entity entity, Fortran::lower::StatementContext &stmtCtx) { auto exv = Fortran::lower::translateToExtendedValue( loc, converter.getFirOpBuilder(), entity, stmtCtx); - if (fir::isa_trivial(fir::getBase(exv).getType())) - TODO(loc, "place trivial in memory"); - return fir::factory::createBoxValue(converter.getFirOpBuilder(), loc, exv); + mlir::Value base = fir::getBase(exv); + fir::FirOpBuilder &builder = converter.getFirOpBuilder(); + + if (fir::isa_trivial(base.getType())) { + // place trivial in memory + mlir::Type ty = base.getType(); + mlir::Operation *op = base.getDefiningOp(); + + // put alloca in entry block + auto insertPt = builder.saveInsertionPoint(); + if (auto func = op->getParentOfType()) { + mlir::Block *entryBlock = &func.getBlocks().front(); + builder.setInsertionPointToStart(entryBlock); + } + mlir::Value ref = builder.create(loc, ty); + builder.restoreInsertionPoint(insertPt); + + builder.create(loc, base, ref); + exv = fir::ExtendedValue{ref}; + } + + return fir::factory::createBoxValue(builder, loc, exv); } fir::BoxValue Fortran::lower::convertExprToBox( mlir::Location loc, Fortran::lower::AbstractConverter &converter, diff --git a/flang/test/Lower/HLFIR/intrinsic-subroutines.f90 b/flang/test/Lower/HLFIR/intrinsic-subroutines.f90 --- a/flang/test/Lower/HLFIR/intrinsic-subroutines.f90 +++ b/flang/test/Lower/HLFIR/intrinsic-subroutines.f90 @@ -12,3 +12,15 @@ ! CHECK: %[[VAL_2:.*]] = fir.call @_FortranACpuTime() fastmath : () -> f64 ! CHECK: %[[VAL_3:.*]] = fir.convert %[[VAL_2]] : (f64) -> f32 ! CHECK: fir.store %[[VAL_3]] to %[[VAL_1]]#1 : !fir.ref + +! check we can box a trivial value +subroutine sumMask(s, a) + integer :: s + integer :: a(:) + s = sum(a, mask=.true.) +endsubroutine +! CHECK-LABEL: func.func @_QPsummask( +! CHECK: %[[ALLOC:.*]] = fir.alloca i1 +! CHECK: %[[TRUE:.*]] = arith.constant true +! CHECK-NEXT: fir.store %[[TRUE]] to %[[ALLOC]] +! CHECK-NEXT: %[[BOX:.*]] = fir.embox %[[ALLOC]] : (!fir.ref) -> !fir.box