1//===-- Inquiry.h - generate inquiry 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/Inquiry.h"
10#include "flang/Optimizer/Builder/FIRBuilder.h"
11#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
12#include "flang/Runtime/inquiry.h"
13#include "flang/Runtime/support.h"
14
15using namespace Fortran::runtime;
16
17/// Generate call to `Lbound` runtime routine when the DIM argument is present.
18mlir::Value fir::runtime::genLboundDim(fir::FirOpBuilder &builder,
19 mlir::Location loc, mlir::Value array,
20 mlir::Value dim) {
21 mlir::func::FuncOp lboundFunc =
22 fir::runtime::getRuntimeFunc<mkRTKey(LboundDim)>(loc, builder);
23 auto fTy = lboundFunc.getFunctionType();
24 auto sourceFile = fir::factory::locationToFilename(builder, loc);
25 auto sourceLine =
26 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
27 auto args = fir::runtime::createArguments(builder, loc, fTy, array, dim,
28 sourceFile, sourceLine);
29 return builder.create<fir::CallOp>(loc, lboundFunc, args).getResult(0);
30}
31
32/// Generate call to `Ubound` runtime routine. Calls to UBOUND with a DIM
33/// argument get transformed into an expression equivalent to
34/// SIZE() + LBOUND() - 1, so they don't have an intrinsic in the runtime.
35void fir::runtime::genUbound(fir::FirOpBuilder &builder, mlir::Location loc,
36 mlir::Value resultBox, mlir::Value array,
37 mlir::Value kind) {
38 mlir::func::FuncOp uboundFunc =
39 fir::runtime::getRuntimeFunc<mkRTKey(Ubound)>(loc, builder);
40 auto fTy = uboundFunc.getFunctionType();
41 auto sourceFile = fir::factory::locationToFilename(builder, loc);
42 auto sourceLine =
43 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
44 auto args = fir::runtime::createArguments(builder, loc, fTy, resultBox, array,
45 kind, sourceFile, sourceLine);
46 builder.create<fir::CallOp>(loc, uboundFunc, args).getResult(0);
47}
48
49/// Generate call to `Size` runtime routine. This routine is a version when
50/// the DIM argument is present.
51mlir::Value fir::runtime::genSizeDim(fir::FirOpBuilder &builder,
52 mlir::Location loc, mlir::Value array,
53 mlir::Value dim) {
54 mlir::func::FuncOp sizeFunc =
55 fir::runtime::getRuntimeFunc<mkRTKey(SizeDim)>(loc, builder);
56 auto fTy = sizeFunc.getFunctionType();
57 auto sourceFile = fir::factory::locationToFilename(builder, loc);
58 auto sourceLine =
59 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
60 auto args = fir::runtime::createArguments(builder, loc, fTy, array, dim,
61 sourceFile, sourceLine);
62 return builder.create<fir::CallOp>(loc, sizeFunc, args).getResult(0);
63}
64
65/// Generate call to `Size` runtime routine. This routine is a version when
66/// the DIM argument is absent.
67mlir::Value fir::runtime::genSize(fir::FirOpBuilder &builder,
68 mlir::Location loc, mlir::Value array) {
69 mlir::func::FuncOp sizeFunc =
70 fir::runtime::getRuntimeFunc<mkRTKey(Size)>(loc, builder);
71 auto fTy = sizeFunc.getFunctionType();
72 auto sourceFile = fir::factory::locationToFilename(builder, loc);
73 auto sourceLine =
74 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
75 auto args = fir::runtime::createArguments(builder, loc, fTy, array,
76 sourceFile, sourceLine);
77 return builder.create<fir::CallOp>(loc, sizeFunc, args).getResult(0);
78}
79
80/// Generate call to `Is_contiguous` runtime routine.
81mlir::Value fir::runtime::genIsContiguous(fir::FirOpBuilder &builder,
82 mlir::Location loc,
83 mlir::Value array) {
84 mlir::func::FuncOp isContiguousFunc =
85 fir::runtime::getRuntimeFunc<mkRTKey(IsContiguous)>(loc, builder);
86 auto fTy = isContiguousFunc.getFunctionType();
87 auto args = fir::runtime::createArguments(builder, loc, fTy, array);
88 return builder.create<fir::CallOp>(loc, isContiguousFunc, args).getResult(0);
89}
90

source code of flang/lib/Optimizer/Builder/Runtime/Inquiry.cpp