xref: /llvm-project/flang/lib/Optimizer/Builder/Runtime/Support.cpp (revision 539dbfcfcf5705cf100999ad2483318192418e21)
1 //===-- Support.cpp - generate support runtime API calls --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "flang/Optimizer/Builder/Runtime/Support.h"
10 #include "flang/Optimizer/Builder/FIRBuilder.h"
11 #include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
12 #include "flang/Runtime/support.h"
13 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
14 
15 using namespace Fortran::runtime;
16 
17 template <>
18 constexpr fir::runtime::TypeBuilderFunc
getModel()19 fir::runtime::getModel<Fortran::runtime::LowerBoundModifier>() {
20   return [](mlir::MLIRContext *context) -> mlir::Type {
21     return mlir::IntegerType::get(
22         context, sizeof(Fortran::runtime::LowerBoundModifier) * 8);
23   };
24 }
25 
genCopyAndUpdateDescriptor(fir::FirOpBuilder & builder,mlir::Location loc,mlir::Value to,mlir::Value from,mlir::Value newDynamicType,mlir::Value newAttribute,mlir::Value newLowerBounds)26 void fir::runtime::genCopyAndUpdateDescriptor(fir::FirOpBuilder &builder,
27                                               mlir::Location loc,
28                                               mlir::Value to, mlir::Value from,
29                                               mlir::Value newDynamicType,
30                                               mlir::Value newAttribute,
31                                               mlir::Value newLowerBounds) {
32   mlir::func::FuncOp func =
33       fir::runtime::getRuntimeFunc<mkRTKey(CopyAndUpdateDescriptor)>(loc,
34                                                                      builder);
35   auto fTy = func.getFunctionType();
36   auto args =
37       fir::runtime::createArguments(builder, loc, fTy, to, from, newDynamicType,
38                                     newAttribute, newLowerBounds);
39   llvm::StringRef noCapture = mlir::LLVM::LLVMDialect::getNoCaptureAttrName();
40   if (!func.getArgAttr(0, noCapture)) {
41     mlir::UnitAttr unitAttr = mlir::UnitAttr::get(func.getContext());
42     func.setArgAttr(0, noCapture, unitAttr);
43     func.setArgAttr(1, noCapture, unitAttr);
44   }
45   builder.create<fir::CallOp>(loc, func, args);
46 }
47 
genIsAssumedSize(fir::FirOpBuilder & builder,mlir::Location loc,mlir::Value box)48 mlir::Value fir::runtime::genIsAssumedSize(fir::FirOpBuilder &builder,
49                                            mlir::Location loc,
50                                            mlir::Value box) {
51   mlir::func::FuncOp func =
52       fir::runtime::getRuntimeFunc<mkRTKey(IsAssumedSize)>(loc, builder);
53   auto fTy = func.getFunctionType();
54   auto args = fir::runtime::createArguments(builder, loc, fTy, box);
55   return builder.create<fir::CallOp>(loc, func, args).getResult(0);
56 }
57