| 1 |
//===-- Numeric.cpp -- runtime API for numeric intrinsics -----------------===// |
| 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/Numeric.h" |
| 10 |
#include "flang/Optimizer/Builder/BoxValue.h" |
| 11 |
#include "flang/Optimizer/Builder/Character.h" |
| 12 |
#include "flang/Optimizer/Builder/FIRBuilder.h" |
| 13 |
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h" |
| 14 |
#include "flang/Optimizer/Builder/Todo.h" |
| 15 |
#include "flang/Runtime/numeric.h" |
| 16 |
#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 17 |
|
| 18 |
using namespace Fortran::runtime; |
| 19 |
|
| 20 |
// The real*10 and real*16 placeholders below are used to force the |
| 21 |
// compilation of the real*10 and real*16 method names on systems that |
| 22 |
// may not have them in their runtime library. This can occur in the |
| 23 |
// case of cross compilation, for example. |
| 24 |
|
| 25 |
/// Placeholder for real*10 version of Exponent Intrinsic |
| 26 |
struct ForcedExponent10_4 { |
| 27 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Exponent10_4)); |
| 28 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 29 |
return [](mlir::MLIRContext *ctx) { |
| 30 |
auto fltTy = mlir::FloatType::getF80(ctx); |
| 31 |
auto intTy = mlir::IntegerType::get(ctx, 32); |
| 32 |
return mlir::FunctionType::get(ctx, fltTy, intTy); |
| 33 |
}; |
| 34 |
} |
| 35 |
}; |
| 36 |
|
| 37 |
struct ForcedExponent10_8 { |
| 38 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Exponent10_8)); |
| 39 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 40 |
return [](mlir::MLIRContext *ctx) { |
| 41 |
auto fltTy = mlir::FloatType::getF80(ctx); |
| 42 |
auto intTy = mlir::IntegerType::get(ctx, 64); |
| 43 |
return mlir::FunctionType::get(ctx, fltTy, intTy); |
| 44 |
}; |
| 45 |
} |
| 46 |
}; |
| 47 |
|
| 48 |
/// Placeholder for real*16 version of Exponent Intrinsic |
| 49 |
struct ForcedExponent16_4 { |
| 50 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Exponent16_4)); |
| 51 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 52 |
return [](mlir::MLIRContext *ctx) { |
| 53 |
auto fltTy = mlir::FloatType::getF128(ctx); |
| 54 |
auto intTy = mlir::IntegerType::get(ctx, 32); |
| 55 |
return mlir::FunctionType::get(ctx, fltTy, intTy); |
| 56 |
}; |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
struct ForcedExponent16_8 { |
| 61 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Exponent16_8)); |
| 62 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 63 |
return [](mlir::MLIRContext *ctx) { |
| 64 |
auto fltTy = mlir::FloatType::getF128(ctx); |
| 65 |
auto intTy = mlir::IntegerType::get(ctx, 64); |
| 66 |
return mlir::FunctionType::get(ctx, fltTy, intTy); |
| 67 |
}; |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
/// Placeholder for real*10 version of Fraction Intrinsic |
| 72 |
struct ForcedFraction10 { |
| 73 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Fraction10)); |
| 74 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 75 |
return [](mlir::MLIRContext *ctx) { |
| 76 |
auto ty = mlir::FloatType::getF80(ctx); |
| 77 |
return mlir::FunctionType::get(ctx, {ty}, {ty}); |
| 78 |
}; |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
/// Placeholder for real*16 version of Fraction Intrinsic |
| 83 |
struct ForcedFraction16 { |
| 84 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Fraction16)); |
| 85 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 86 |
return [](mlir::MLIRContext *ctx) { |
| 87 |
auto ty = mlir::FloatType::getF128(ctx); |
| 88 |
return mlir::FunctionType::get(ctx, {ty}, {ty}); |
| 89 |
}; |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
/// Placeholder for real*10 version of Mod Intrinsic |
| 94 |
struct ForcedMod10 { |
| 95 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(ModReal10)); |
| 96 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 97 |
return [](mlir::MLIRContext *ctx) { |
| 98 |
auto fltTy = mlir::FloatType::getF80(ctx); |
| 99 |
auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); |
| 100 |
auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); |
| 101 |
return mlir::FunctionType::get(ctx, {fltTy, fltTy, strTy, intTy}, |
| 102 |
{fltTy}); |
| 103 |
}; |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
/// Placeholder for real*16 version of Mod Intrinsic |
| 108 |
struct ForcedMod16 { |
| 109 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(ModReal16)); |
| 110 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 111 |
return [](mlir::MLIRContext *ctx) { |
| 112 |
auto fltTy = mlir::FloatType::getF128(ctx); |
| 113 |
auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); |
| 114 |
auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); |
| 115 |
return mlir::FunctionType::get(ctx, {fltTy, fltTy, strTy, intTy}, |
| 116 |
{fltTy}); |
| 117 |
}; |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
/// Placeholder for real*10 version of Nearest Intrinsic |
| 122 |
struct ForcedNearest10 { |
| 123 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Nearest10)); |
| 124 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 125 |
return [](mlir::MLIRContext *ctx) { |
| 126 |
auto fltTy = mlir::FloatType::getF80(ctx); |
| 127 |
auto boolTy = mlir::IntegerType::get(ctx, 1); |
| 128 |
return mlir::FunctionType::get(ctx, {fltTy, boolTy}, {fltTy}); |
| 129 |
}; |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
/// Placeholder for real*16 version of Nearest Intrinsic |
| 134 |
struct ForcedNearest16 { |
| 135 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Nearest16)); |
| 136 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 137 |
return [](mlir::MLIRContext *ctx) { |
| 138 |
auto fltTy = mlir::FloatType::getF128(ctx); |
| 139 |
auto boolTy = mlir::IntegerType::get(ctx, 1); |
| 140 |
return mlir::FunctionType::get(ctx, {fltTy, boolTy}, {fltTy}); |
| 141 |
}; |
| 142 |
} |
| 143 |
}; |
| 144 |
|
| 145 |
/// Placeholder for real*10 version of RRSpacing Intrinsic |
| 146 |
struct ForcedRRSpacing10 { |
| 147 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(RRSpacing10)); |
| 148 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 149 |
return [](mlir::MLIRContext *ctx) { |
| 150 |
auto ty = mlir::FloatType::getF80(ctx); |
| 151 |
return mlir::FunctionType::get(ctx, {ty}, {ty}); |
| 152 |
}; |
| 153 |
} |
| 154 |
}; |
| 155 |
|
| 156 |
/// Placeholder for real*16 version of RRSpacing Intrinsic |
| 157 |
struct ForcedRRSpacing16 { |
| 158 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(RRSpacing16)); |
| 159 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 160 |
return [](mlir::MLIRContext *ctx) { |
| 161 |
auto ty = mlir::FloatType::getF128(ctx); |
| 162 |
return mlir::FunctionType::get(ctx, {ty}, {ty}); |
| 163 |
}; |
| 164 |
} |
| 165 |
}; |
| 166 |
|
| 167 |
/// Placeholder for real*10 version of Scale Intrinsic |
| 168 |
struct ForcedScale10 { |
| 169 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Scale10)); |
| 170 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 171 |
return [](mlir::MLIRContext *ctx) { |
| 172 |
auto fltTy = mlir::FloatType::getF80(ctx); |
| 173 |
auto intTy = mlir::IntegerType::get(ctx, 64); |
| 174 |
return mlir::FunctionType::get(ctx, {fltTy, intTy}, {fltTy}); |
| 175 |
}; |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
/// Placeholder for real*16 version of Scale Intrinsic |
| 180 |
struct ForcedScale16 { |
| 181 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Scale16)); |
| 182 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 183 |
return [](mlir::MLIRContext *ctx) { |
| 184 |
auto fltTy = mlir::FloatType::getF128(ctx); |
| 185 |
auto intTy = mlir::IntegerType::get(ctx, 64); |
| 186 |
return mlir::FunctionType::get(ctx, {fltTy, intTy}, {fltTy}); |
| 187 |
}; |
| 188 |
} |
| 189 |
}; |
| 190 |
|
| 191 |
/// Placeholder for real*10 version of RRSpacing Intrinsic |
| 192 |
struct ForcedSetExponent10 { |
| 193 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(SetExponent10)); |
| 194 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 195 |
return [](mlir::MLIRContext *ctx) { |
| 196 |
auto fltTy = mlir::FloatType::getF80(ctx); |
| 197 |
auto intTy = mlir::IntegerType::get(ctx, 64); |
| 198 |
return mlir::FunctionType::get(ctx, {fltTy, intTy}, {fltTy}); |
| 199 |
}; |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
/// Placeholder for real*10 version of RRSpacing Intrinsic |
| 204 |
struct ForcedSetExponent16 { |
| 205 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(SetExponent16)); |
| 206 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 207 |
return [](mlir::MLIRContext *ctx) { |
| 208 |
auto fltTy = mlir::FloatType::getF128(ctx); |
| 209 |
auto intTy = mlir::IntegerType::get(ctx, 64); |
| 210 |
return mlir::FunctionType::get(ctx, {fltTy, intTy}, {fltTy}); |
| 211 |
}; |
| 212 |
} |
| 213 |
}; |
| 214 |
|
| 215 |
/// Placeholder for real*10 version of Spacing Intrinsic |
| 216 |
struct ForcedSpacing10 { |
| 217 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Spacing10)); |
| 218 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 219 |
return [](mlir::MLIRContext *ctx) { |
| 220 |
auto ty = mlir::FloatType::getF80(ctx); |
| 221 |
return mlir::FunctionType::get(ctx, {ty}, {ty}); |
| 222 |
}; |
| 223 |
} |
| 224 |
}; |
| 225 |
|
| 226 |
/// Placeholder for real*16 version of Spacing Intrinsic |
| 227 |
struct ForcedSpacing16 { |
| 228 |
static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Spacing16)); |
| 229 |
static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { |
| 230 |
return [](mlir::MLIRContext *ctx) { |
| 231 |
auto ty = mlir::FloatType::getF128(ctx); |
| 232 |
return mlir::FunctionType::get(ctx, {ty}, {ty}); |
| 233 |
}; |
| 234 |
} |
| 235 |
}; |
| 236 |
|
| 237 |
/// Generate call to Exponent instrinsic runtime routine. |
| 238 |
mlir::Value fir::runtime::genExponent(fir::FirOpBuilder &builder, |
| 239 |
mlir::Location loc, mlir::Type resultType, |
| 240 |
mlir::Value x) { |
| 241 |
mlir::func::FuncOp func; |
| 242 |
mlir::Type fltTy = x.getType(); |
| 243 |
|
| 244 |
if (fltTy.isF16()) { |
| 245 |
TODO(loc, "support for REAL with KIND = 2 in EXPONENT"); |
| 246 |
} else if (fltTy.isF32()) { |
| 247 |
if (resultType.isInteger(32)) |
| 248 |
func = fir::runtime::getRuntimeFunc<mkRTKey(Exponent4_4)>(loc, builder); |
| 249 |
else if (resultType.isInteger(64)) |
| 250 |
func = fir::runtime::getRuntimeFunc<mkRTKey(Exponent4_8)>(loc, builder); |
| 251 |
} else if (fltTy.isF64()) { |
| 252 |
if (resultType.isInteger(32)) |
| 253 |
func = fir::runtime::getRuntimeFunc<mkRTKey(Exponent8_4)>(loc, builder); |
| 254 |
else if (resultType.isInteger(64)) |
| 255 |
func = fir::runtime::getRuntimeFunc<mkRTKey(Exponent8_8)>(loc, builder); |
| 256 |
} else if (fltTy.isF80()) { |
| 257 |
if (resultType.isInteger(32)) |
| 258 |
func = fir::runtime::getRuntimeFunc<ForcedExponent10_4>(loc, builder); |
| 259 |
else if (resultType.isInteger(64)) |
| 260 |
func = fir::runtime::getRuntimeFunc<ForcedExponent10_8>(loc, builder); |
| 261 |
} else if (fltTy.isF128()) { |
| 262 |
if (resultType.isInteger(32)) |
| 263 |
func = fir::runtime::getRuntimeFunc<ForcedExponent16_4>(loc, builder); |
| 264 |
else if (resultType.isInteger(64)) |
| 265 |
func = fir::runtime::getRuntimeFunc<ForcedExponent16_8>(loc, builder); |
| 266 |
} else |
| 267 |
fir::emitFatalError(loc, "unsupported REAL kind in EXPONENT"); |
| 268 |
|
| 269 |
auto funcTy = func.getFunctionType(); |
| 270 |
llvm::SmallVector<mlir::Value> args = { |
| 271 |
builder.createConvert(loc, funcTy.getInput(0), x)}; |
| 272 |
|
| 273 |
return builder.create<fir::CallOp>(loc, func, args).getResult(0); |
| 274 |
} |
| 275 |
|
| 276 |
/// Generate call to Fraction instrinsic runtime routine. |
| 277 |
mlir::Value fir::runtime::genFraction(fir::FirOpBuilder &builder, |
| 278 |
mlir::Location loc, mlir::Value x) { |
| 279 |
mlir::func::FuncOp func; |
| 280 |
mlir::Type fltTy = x.getType(); |
| 281 |
if (fltTy.isF16()) |
| 282 |
TODO(loc, "support for REAL with KIND = 2 in FRACTION"); |
| 283 |
else if (fltTy.isF32()) |
| 284 |
func = fir::runtime::getRuntimeFunc<mkRTKey(Fraction4)>(loc, builder); |
| 285 |
else if (fltTy.isF64()) |
| 286 |
func = fir::runtime::getRuntimeFunc<mkRTKey(Fraction8)>(loc, builder); |
| 287 |
else if (fltTy.isF80()) |
| 288 |
func = fir::runtime::getRuntimeFunc<ForcedFraction10>(loc, builder); |
| 289 |
else if (fltTy.isF128()) |
| 290 |
func = fir::runtime::getRuntimeFunc<ForcedFraction16>(loc, builder); |
| 291 |
else |
| 292 |
fir::emitFatalError(loc, "unsupported REAL kind in FRACTION"); |
| 293 |
|
| 294 |
auto funcTy = func.getFunctionType(); |
| 295 |
llvm::SmallVector<mlir::Value> args = { |
| 296 |
builder.createConvert(loc, funcTy.getInput(0), x)}; |
| 297 |
|
| 298 |
return builder.create<fir::CallOp>(loc, func, args).getResult(0); |
| 299 |
} |
| 300 |
|
| 301 |
/// Generate call to Mod intrinsic runtime routine. |
| 302 |
mlir::Value fir::runtime::genMod(fir::FirOpBuilder &builder, mlir::Location loc, |
| 303 |
mlir::Value a, mlir::Value p) { |
| 304 |
mlir::func::FuncOp func; |
| 305 |
mlir::Type fltTy = a.getType(); |
| 306 |
|
| 307 |
if (fltTy != p.getType()) |
| 308 |
fir::emitFatalError(loc, "arguments type mismatch in MOD"); |
| 309 |
|
| 310 |
if (fltTy.isF16()) |
| 311 |
TODO(loc, "support for REAL with KIND = 2 in MOD"); |
| 312 |
else if (fltTy.isF32()) |
| 313 |
func = fir::runtime::getRuntimeFunc<mkRTKey(ModReal4)>(loc, builder); |
| 314 |
else if (fltTy.isF64()) |
| 315 |
func = fir::runtime::getRuntimeFunc<mkRTKey(ModReal8)>(loc, builder); |
| 316 |
else if (fltTy.isF80()) |
| 317 |
func = fir::runtime::getRuntimeFunc<ForcedMod10>(loc, builder); |
| 318 |
else if (fltTy.isF128()) |
| 319 |
func = fir::runtime::getRuntimeFunc<ForcedMod16>(loc, builder); |
| 320 |
else |
| 321 |
fir::emitFatalError(loc, "unsupported REAL kind in MOD"); |
| 322 |
|
| 323 |
auto funcTy = func.getFunctionType(); |
| 324 |
auto sourceFile = fir::factory::locationToFilename(builder, loc); |
| 325 |
auto sourceLine = |
| 326 |
fir::factory::locationToLineNo(builder, loc, funcTy.getInput(3)); |
| 327 |
auto args = fir::runtime::createArguments(builder, loc, funcTy, a, p, |
| 328 |
sourceFile, sourceLine); |
| 329 |
|
| 330 |
return builder.create<fir::CallOp>(loc, func, args).getResult(0); |
| 331 |
} |
| 332 |
|
| 333 |
/// Generate call to Nearest intrinsic runtime routine. |
| 334 |
mlir::Value fir::runtime::genNearest(fir::FirOpBuilder &builder, |
| 335 |
mlir::Location loc, mlir::Value x, |
| 336 |
mlir::Value s) { |
| 337 |
mlir::func::FuncOp func; |
| 338 |
mlir::Type fltTy = x.getType(); |
| 339 |
|
| 340 |
if (fltTy.isF16()) |
| 341 |
TODO(loc, "support for REAL with KIND = 2 in NEAREST"); |
| 342 |
else if (fltTy.isF32()) |
| 343 |
func = fir::runtime::getRuntimeFunc<mkRTKey(Nearest4)>(loc, builder); |
| 344 |
else if (fltTy.isF64()) |
| 345 |
func = fir::runtime::getRuntimeFunc<mkRTKey(Nearest8)>(loc, builder); |
| 346 |
else if (fltTy.isF80()) |
| 347 |
func = fir::runtime::getRuntimeFunc<ForcedNearest10>(loc, builder); |
| 348 |
else if (fltTy.isF128()) |
| 349 |
func = fir::runtime::getRuntimeFunc<ForcedNearest16>(loc, builder); |
| 350 |
else |
| 351 |
fir::emitFatalError(loc, "unsupported REAL kind in NEAREST"); |
| 352 |
|
| 353 |
auto funcTy = func.getFunctionType(); |
| 354 |
|
| 355 |
mlir::Type sTy = s.getType(); |
| 356 |
mlir::Value zero = builder.createRealZeroConstant(loc, sTy); |
| 357 |
auto cmp = builder.create<mlir::arith::CmpFOp>( |
| 358 |
loc, mlir::arith::CmpFPredicate::OGT, s, zero); |
| 359 |
|
| 360 |
mlir::Type boolTy = mlir::IntegerType::get(builder.getContext(), 1); |
| 361 |
mlir::Value False = builder.createIntegerConstant(loc, boolTy, 0); |
| 362 |
mlir::Value True = builder.createIntegerConstant(loc, boolTy, 1); |
| 363 |
|
| 364 |
mlir::Value positive = |
| 365 |
builder.create<mlir::arith::SelectOp>(loc, cmp, True, False); |
| 366 |
auto args = fir::runtime::createArguments(builder, loc, funcTy, x, positive); |
| 367 |
|
| 368 |
return builder.create<fir::CallOp>(loc, func, args).getResult(0); |
| 369 |
} |
| 370 |
|
| 371 |
/// Generate call to RRSpacing intrinsic runtime routine. |
| 372 |
mlir::Value fir::runtime::genRRSpacing(fir::FirOpBuilder &builder, |
| 373 |
mlir::Location loc, mlir::Value x) { |
| 374 |
mlir::func::FuncOp func; |
| 375 |
mlir::Type fltTy = x.getType(); |
| 376 |
|
| 377 |
if (fltTy.isF16()) |
| 378 |
TODO(loc, "support for REAL with KIND = 2 in RRSPACING"); |
| 379 |
else if (fltTy.isF32()) |
| 380 |
func = fir::runtime::getRuntimeFunc<mkRTKey(RRSpacing4)>(loc, builder); |
| 381 |
else if (fltTy.isF64()) |
| 382 |
func = fir::runtime::getRuntimeFunc<mkRTKey(RRSpacing8)>(loc, builder); |
| 383 |
else if (fltTy.isF80()) |
| 384 |
func = fir::runtime::getRuntimeFunc<ForcedRRSpacing10>(loc, builder); |
| 385 |
else if (fltTy.isF128()) |
| 386 |
func = fir::runtime::getRuntimeFunc<ForcedRRSpacing16>(loc, builder); |
| 387 |
else |
| 388 |
fir::emitFatalError(loc, "unsupported REAL kind in RRSPACING"); |
| 389 |
|
| 390 |
auto funcTy = func.getFunctionType(); |
| 391 |
llvm::SmallVector<mlir::Value> args = { |
| 392 |
builder.createConvert(loc, funcTy.getInput(0), x)}; |
| 393 |
|
| 394 |
return builder.create<fir::CallOp>(loc, func, args).getResult(0); |
| 395 |
} |
| 396 |
|
| 397 |
/// Generate call to Scale intrinsic runtime routine. |
| 398 |
mlir::Value fir::runtime::genScale(fir::FirOpBuilder &builder, |
| 399 |
mlir::Location loc, mlir::Value x, |
| 400 |
mlir::Value i) { |
| 401 |
mlir::func::FuncOp func; |
| 402 |
mlir::Type fltTy = x.getType(); |
| 403 |
|
| 404 |
if (fltTy.isF16()) |
| 405 |
TODO(loc, "support for REAL with KIND = 2 in SCALE"); |
| 406 |
else if (fltTy.isF32()) |
| 407 |
func = fir::runtime::getRuntimeFunc<mkRTKey(Scale4)>(loc, builder); |
| 408 |
else if (fltTy.isF64()) |
| 409 |
func = fir::runtime::getRuntimeFunc<mkRTKey(Scale8)>(loc, builder); |
| 410 |
else if (fltTy.isF80()) |
| 411 |
func = fir::runtime::getRuntimeFunc<ForcedScale10>(loc, builder); |
| 412 |
else if (fltTy.isF128()) |
| 413 |
func = fir::runtime::getRuntimeFunc<ForcedScale16>(loc, builder); |
| 414 |
else |
| 415 |
fir::emitFatalError(loc, "unsupported REAL kind in SCALE"); |
| 416 |
|
| 417 |
auto funcTy = func.getFunctionType(); |
| 418 |
auto args = fir::runtime::createArguments(builder, loc, funcTy, x, i); |
| 419 |
|
| 420 |
return builder.create<fir::CallOp>(loc, func, args).getResult(0); |
| 421 |
} |
| 422 |
|
| 423 |
/// Generate call to Selected_int_kind intrinsic runtime routine. |
| 424 |
mlir::Value fir::runtime::genSelectedIntKind(fir::FirOpBuilder &builder, |
| 425 |
mlir::Location loc, |
| 426 |
mlir::Value x) { |
| 427 |
mlir::func::FuncOp func = |
| 428 |
fir::runtime::getRuntimeFunc<mkRTKey(SelectedIntKind)>(loc, builder); |
| 429 |
auto fTy = func.getFunctionType(); |
| 430 |
auto sourceFile = fir::factory::locationToFilename(builder, loc); |
| 431 |
auto sourceLine = |
| 432 |
fir::factory::locationToLineNo(builder, loc, fTy.getInput(1)); |
| 433 |
if (!fir::isa_ref_type(x.getType())) |
| 434 |
fir::emitFatalError(loc, "argument address for runtime not found"); |
| 435 |
mlir::Type eleTy = fir::unwrapRefType(x.getType()); |
| 436 |
mlir::Value xKind = builder.createIntegerConstant( |
| 437 |
loc, fTy.getInput(3), eleTy.getIntOrFloatBitWidth() / 8); |
| 438 |
auto args = fir::runtime::createArguments(builder, loc, fTy, sourceFile, |
| 439 |
sourceLine, x, xKind); |
| 440 |
|
| 441 |
return builder.create<fir::CallOp>(loc, func, args).getResult(0); |
| 442 |
} |
| 443 |
|
| 444 |
/// Generate call to Selected_real_kind intrinsic runtime routine. |
| 445 |
mlir::Value fir::runtime::genSelectedRealKind(fir::FirOpBuilder &builder, |
| 446 |
mlir::Location loc, |
| 447 |
mlir::Value precision, |
| 448 |
mlir::Value range, |
| 449 |
mlir::Value radix) { |
| 450 |
mlir::func::FuncOp func = |
| 451 |
fir::runtime::getRuntimeFunc<mkRTKey(SelectedRealKind)>(loc, builder); |
| 452 |
auto fTy = func.getFunctionType(); |
| 453 |
auto getArgKinds = [&](mlir::Value arg, int argKindIndex) -> mlir::Value { |
| 454 |
if (fir::isa_ref_type(arg.getType())) { |
| 455 |
mlir::Type eleTy = fir::unwrapRefType(arg.getType()); |
| 456 |
return builder.createIntegerConstant(loc, fTy.getInput(argKindIndex), |
| 457 |
eleTy.getIntOrFloatBitWidth() / 8); |
| 458 |
} else { |
| 459 |
return builder.createIntegerConstant(loc, fTy.getInput(argKindIndex), 0); |
| 460 |
} |
| 461 |
}; |
| 462 |
|
| 463 |
auto sourceFile = fir::factory::locationToFilename(builder, loc); |
| 464 |
auto sourceLine = |
| 465 |
fir::factory::locationToLineNo(builder, loc, fTy.getInput(1)); |
| 466 |
mlir::Value pKind = getArgKinds(precision, 3); |
| 467 |
mlir::Value rKind = getArgKinds(range, 5); |
| 468 |
mlir::Value dKind = getArgKinds(radix, 7); |
| 469 |
auto args = fir::runtime::createArguments(builder, loc, fTy, sourceFile, |
| 470 |
sourceLine, precision, pKind, range, |
| 471 |
rKind, radix, dKind); |
| 472 |
|
| 473 |
return builder.create<fir::CallOp>(loc, func, args).getResult(0); |
| 474 |
} |
| 475 |
|
| 476 |
/// Generate call to Set_exponent instrinsic runtime routine. |
| 477 |
mlir::Value fir::runtime::genSetExponent(fir::FirOpBuilder &builder, |
| 478 |
mlir::Location loc, mlir::Value x, |
| 479 |
mlir::Value i) { |
| 480 |
mlir::func::FuncOp func; |
| 481 |
mlir::Type fltTy = x.getType(); |
| 482 |
|
| 483 |
if (fltTy.isF16()) |
| 484 |
TODO(loc, "support for REAL with KIND = 2 in FRACTION"); |
| 485 |
else if (fltTy.isF32()) |
| 486 |
func = fir::runtime::getRuntimeFunc<mkRTKey(SetExponent4)>(loc, builder); |
| 487 |
else if (fltTy.isF64()) |
| 488 |
func = fir::runtime::getRuntimeFunc<mkRTKey(SetExponent8)>(loc, builder); |
| 489 |
else if (fltTy.isF80()) |
| 490 |
func = fir::runtime::getRuntimeFunc<ForcedSetExponent10>(loc, builder); |
| 491 |
else if (fltTy.isF128()) |
| 492 |
func = fir::runtime::getRuntimeFunc<ForcedSetExponent16>(loc, builder); |
| 493 |
else |
| 494 |
fir::emitFatalError(loc, "unsupported REAL kind in FRACTION"); |
| 495 |
|
| 496 |
auto funcTy = func.getFunctionType(); |
| 497 |
auto args = fir::runtime::createArguments(builder, loc, funcTy, x, i); |
| 498 |
|
| 499 |
return builder.create<fir::CallOp>(loc, func, args).getResult(0); |
| 500 |
} |
| 501 |
|
| 502 |
/// Generate call to Spacing intrinsic runtime routine. |
| 503 |
mlir::Value fir::runtime::genSpacing(fir::FirOpBuilder &builder, |
| 504 |
mlir::Location loc, mlir::Value x) { |
| 505 |
mlir::func::FuncOp func; |
| 506 |
mlir::Type fltTy = x.getType(); |
| 507 |
|
| 508 |
if (fltTy.isF16()) |
| 509 |
TODO(loc, "support for REAL with KIND = 2 in SPACING"); |
| 510 |
else if (fltTy.isF32()) |
| 511 |
func = fir::runtime::getRuntimeFunc<mkRTKey(Spacing4)>(loc, builder); |
| 512 |
else if (fltTy.isF64()) |
| 513 |
func = fir::runtime::getRuntimeFunc<mkRTKey(Spacing8)>(loc, builder); |
| 514 |
else if (fltTy.isF80()) |
| 515 |
func = fir::runtime::getRuntimeFunc<ForcedSpacing10>(loc, builder); |
| 516 |
else if (fltTy.isF128()) |
| 517 |
func = fir::runtime::getRuntimeFunc<ForcedSpacing16>(loc, builder); |
| 518 |
else |
| 519 |
fir::emitFatalError(loc, "unsupported REAL kind in SPACING"); |
| 520 |
|
| 521 |
auto funcTy = func.getFunctionType(); |
| 522 |
llvm::SmallVector<mlir::Value> args = { |
| 523 |
builder.createConvert(loc, funcTy.getInput(0), x)}; |
| 524 |
|
| 525 |
return builder.create<fir::CallOp>(loc, func, args).getResult(0); |
| 526 |
} |