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