| 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 | |
| 15 | using namespace Fortran::runtime; |
| 16 | |
| 17 | /// Generate call to `Lbound` runtime routine when the DIM argument is present. |
| 18 | mlir::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 | void fir::runtime::genLbound(fir::FirOpBuilder &builder, mlir::Location loc, |
| 33 | mlir::Value resultAddr, mlir::Value array, |
| 34 | mlir::Value kind) { |
| 35 | mlir::func::FuncOp func = |
| 36 | fir::runtime::getRuntimeFunc<mkRTKey(Lbound)>(loc, builder); |
| 37 | auto fTy = func.getFunctionType(); |
| 38 | auto sourceFile = fir::factory::locationToFilename(builder, loc); |
| 39 | auto sourceLine = |
| 40 | fir::factory::locationToLineNo(builder, loc, fTy.getInput(4)); |
| 41 | auto args = fir::runtime::createArguments( |
| 42 | builder, loc, fTy, resultAddr, array, kind, sourceFile, sourceLine); |
| 43 | builder.create<fir::CallOp>(loc, func, args); |
| 44 | } |
| 45 | |
| 46 | /// Generate call to `Ubound` runtime routine. Calls to UBOUND with a DIM |
| 47 | /// argument get transformed into an expression equivalent to |
| 48 | /// SIZE() + LBOUND() - 1, so they don't have an intrinsic in the runtime. |
| 49 | void fir::runtime::genUbound(fir::FirOpBuilder &builder, mlir::Location loc, |
| 50 | mlir::Value resultBox, mlir::Value array, |
| 51 | mlir::Value kind) { |
| 52 | mlir::func::FuncOp uboundFunc = |
| 53 | fir::runtime::getRuntimeFunc<mkRTKey(Ubound)>(loc, builder); |
| 54 | auto fTy = uboundFunc.getFunctionType(); |
| 55 | auto sourceFile = fir::factory::locationToFilename(builder, loc); |
| 56 | auto sourceLine = |
| 57 | fir::factory::locationToLineNo(builder, loc, fTy.getInput(2)); |
| 58 | auto args = fir::runtime::createArguments(builder, loc, fTy, resultBox, array, |
| 59 | kind, sourceFile, sourceLine); |
| 60 | builder.create<fir::CallOp>(loc, uboundFunc, args); |
| 61 | } |
| 62 | |
| 63 | /// Generate call to `Size` runtime routine. This routine is a version when |
| 64 | /// the DIM argument is present. |
| 65 | mlir::Value fir::runtime::genSizeDim(fir::FirOpBuilder &builder, |
| 66 | mlir::Location loc, mlir::Value array, |
| 67 | mlir::Value dim) { |
| 68 | mlir::func::FuncOp sizeFunc = |
| 69 | fir::runtime::getRuntimeFunc<mkRTKey(SizeDim)>(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(3)); |
| 74 | auto args = fir::runtime::createArguments(builder, loc, fTy, array, dim, |
| 75 | sourceFile, sourceLine); |
| 76 | return builder.create<fir::CallOp>(loc, sizeFunc, args).getResult(0); |
| 77 | } |
| 78 | |
| 79 | /// Generate call to `Size` runtime routine. This routine is a version when |
| 80 | /// the DIM argument is absent. |
| 81 | mlir::Value fir::runtime::genSize(fir::FirOpBuilder &builder, |
| 82 | mlir::Location loc, mlir::Value array) { |
| 83 | mlir::func::FuncOp sizeFunc = |
| 84 | fir::runtime::getRuntimeFunc<mkRTKey(Size)>(loc, builder); |
| 85 | auto fTy = sizeFunc.getFunctionType(); |
| 86 | auto sourceFile = fir::factory::locationToFilename(builder, loc); |
| 87 | auto sourceLine = |
| 88 | fir::factory::locationToLineNo(builder, loc, fTy.getInput(2)); |
| 89 | auto args = fir::runtime::createArguments(builder, loc, fTy, array, |
| 90 | sourceFile, sourceLine); |
| 91 | return builder.create<fir::CallOp>(loc, sizeFunc, args).getResult(0); |
| 92 | } |
| 93 | |
| 94 | /// Generate call to `IsContiguous` runtime routine. |
| 95 | mlir::Value fir::runtime::genIsContiguous(fir::FirOpBuilder &builder, |
| 96 | mlir::Location loc, |
| 97 | mlir::Value array) { |
| 98 | mlir::func::FuncOp isContiguousFunc = |
| 99 | fir::runtime::getRuntimeFunc<mkRTKey(IsContiguous)>(loc, builder); |
| 100 | auto fTy = isContiguousFunc.getFunctionType(); |
| 101 | auto args = fir::runtime::createArguments(builder, loc, fTy, array); |
| 102 | return builder.create<fir::CallOp>(loc, isContiguousFunc, args).getResult(0); |
| 103 | } |
| 104 | |
| 105 | /// Generate call to `IsContiguousUpTo` runtime routine. |
| 106 | mlir::Value fir::runtime::genIsContiguousUpTo(fir::FirOpBuilder &builder, |
| 107 | mlir::Location loc, |
| 108 | mlir::Value array, |
| 109 | mlir::Value dim) { |
| 110 | mlir::func::FuncOp isContiguousFunc = |
| 111 | fir::runtime::getRuntimeFunc<mkRTKey(IsContiguousUpTo)>(loc, builder); |
| 112 | auto fTy = isContiguousFunc.getFunctionType(); |
| 113 | auto args = fir::runtime::createArguments(builder, loc, fTy, array, dim); |
| 114 | return builder.create<fir::CallOp>(loc, isContiguousFunc, args).getResult(0); |
| 115 | } |
| 116 | |
| 117 | void fir::runtime::genShape(fir::FirOpBuilder &builder, mlir::Location loc, |
| 118 | mlir::Value resultAddr, mlir::Value array, |
| 119 | mlir::Value kind) { |
| 120 | mlir::func::FuncOp func = |
| 121 | fir::runtime::getRuntimeFunc<mkRTKey(Shape)>(loc, builder); |
| 122 | auto fTy = func.getFunctionType(); |
| 123 | auto sourceFile = fir::factory::locationToFilename(builder, loc); |
| 124 | auto sourceLine = |
| 125 | fir::factory::locationToLineNo(builder, loc, fTy.getInput(4)); |
| 126 | auto args = fir::runtime::createArguments( |
| 127 | builder, loc, fTy, resultAddr, array, kind, sourceFile, sourceLine); |
| 128 | builder.create<fir::CallOp>(loc, func, args); |
| 129 | } |
| 130 | |