Files
clang-p2996/flang/lib/Optimizer/Builder/Runtime/Assign.cpp
Slava Zakharin da60b9e7dc [flang] Fixed managing copy-in/copy-out temps.
There are several observations regarding the copy-in/copy-out:
  * Actual argument associated with INTENT(OUT) dummy argument that
    requires finalization (7.5.6.3 p. 7) may be read by the finalization
    function, so a copy-in is required.
  * A temporary created for the copy-in/copy-out must be destroyed
    without finalization after the call (or after the corresponding copy-out),
    otherwise, memory leaks may occur.
  * The copy-out assignment must not perform finalization for the LHS.
  * The copy-out assignment from the temporary to the actual argument
    may or may not need to initialize the LHS.

This change-set introduces new runtime methods: CopyOutAssign and
DestroyWithoutFinalization. They are called by the compiler generated
code to match the behavior described above.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D151135
2023-05-23 09:35:17 -07:00

88 lines
4.2 KiB
C++

//===-- Assign.cpp -- generate assignment runtime API calls ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Optimizer/Builder/Runtime/Assign.h"
#include "flang/Optimizer/Builder/FIRBuilder.h"
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
#include "flang/Runtime/assign.h"
using namespace Fortran::runtime;
void fir::runtime::genAssign(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::Value destBox, mlir::Value sourceBox) {
auto func = fir::runtime::getRuntimeFunc<mkRTKey(Assign)>(loc, builder);
auto fTy = func.getFunctionType();
auto sourceFile = fir::factory::locationToFilename(builder, loc);
auto sourceLine =
fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
auto args = fir::runtime::createArguments(builder, loc, fTy, destBox,
sourceBox, sourceFile, sourceLine);
builder.create<fir::CallOp>(loc, func, args);
}
void fir::runtime::genAssignPolymorphic(fir::FirOpBuilder &builder,
mlir::Location loc, mlir::Value destBox,
mlir::Value sourceBox) {
auto func =
fir::runtime::getRuntimeFunc<mkRTKey(AssignPolymorphic)>(loc, builder);
auto fTy = func.getFunctionType();
auto sourceFile = fir::factory::locationToFilename(builder, loc);
auto sourceLine =
fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
auto args = fir::runtime::createArguments(builder, loc, fTy, destBox,
sourceBox, sourceFile, sourceLine);
builder.create<fir::CallOp>(loc, func, args);
}
void fir::runtime::genAssignExplicitLengthCharacter(fir::FirOpBuilder &builder,
mlir::Location loc,
mlir::Value destBox,
mlir::Value sourceBox) {
auto func =
fir::runtime::getRuntimeFunc<mkRTKey(AssignExplicitLengthCharacter)>(
loc, builder);
auto fTy = func.getFunctionType();
auto sourceFile = fir::factory::locationToFilename(builder, loc);
auto sourceLine =
fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
auto args = fir::runtime::createArguments(builder, loc, fTy, destBox,
sourceBox, sourceFile, sourceLine);
builder.create<fir::CallOp>(loc, func, args);
}
void fir::runtime::genAssignTemporary(fir::FirOpBuilder &builder,
mlir::Location loc, mlir::Value destBox,
mlir::Value sourceBox) {
auto func =
fir::runtime::getRuntimeFunc<mkRTKey(AssignTemporary)>(loc, builder);
auto fTy = func.getFunctionType();
auto sourceFile = fir::factory::locationToFilename(builder, loc);
auto sourceLine =
fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
auto args = fir::runtime::createArguments(builder, loc, fTy, destBox,
sourceBox, sourceFile, sourceLine);
builder.create<fir::CallOp>(loc, func, args);
}
void fir::runtime::genCopyOutAssign(fir::FirOpBuilder &builder,
mlir::Location loc, mlir::Value destBox,
mlir::Value sourceBox, bool skipToInit) {
auto func =
fir::runtime::getRuntimeFunc<mkRTKey(CopyOutAssign)>(loc, builder);
auto fTy = func.getFunctionType();
auto sourceFile = fir::factory::locationToFilename(builder, loc);
auto sourceLine =
fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
auto i1Ty = builder.getIntegerType(1);
auto skipToInitVal = builder.createIntegerConstant(loc, i1Ty, skipToInit);
auto args =
fir::runtime::createArguments(builder, loc, fTy, destBox, sourceBox,
skipToInitVal, sourceFile, sourceLine);
builder.create<fir::CallOp>(loc, func, args);
}