1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
|
//===-- ConvertType.cpp ---------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Lower/ConvertType.h"
#include "flang/Lower/PFTBuilder.h"
#include "flang/Lower/Utils.h"
#include "flang/Optimizer/Dialect/FIRType.h"
#include "flang/Semantics/tools.h"
#include "flang/Semantics/type.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinTypes.h"
#undef QUOTE
#undef TODO
#define QUOTE(X) #X
#define TODO(S) \
{ \
emitError(__FILE__ ":" QUOTE(__LINE__) ": type lowering of " S \
" not implemented"); \
exit(1); \
}
template <typename A>
bool isConstant(const Fortran::evaluate::Expr<A> &e) {
return Fortran::evaluate::IsConstantExpr(Fortran::lower::SomeExpr{e});
}
template <typename A>
int64_t toConstant(const Fortran::evaluate::Expr<A> &e) {
auto opt = Fortran::evaluate::ToInt64(e);
assert(opt.has_value() && "expression didn't resolve to a constant");
return opt.value();
}
// one argument template, must be specialized
template <Fortran::common::TypeCategory TC>
mlir::Type genFIRType(mlir::MLIRContext *, int) {
return {};
}
// two argument template
template <Fortran::common::TypeCategory TC, int KIND>
mlir::Type genFIRType(mlir::MLIRContext *context) {
if constexpr (TC == Fortran::common::TypeCategory::Integer) {
auto bits{Fortran::evaluate::Type<Fortran::common::TypeCategory::Integer,
KIND>::Scalar::bits};
return mlir::IntegerType::get(context, bits);
} else if constexpr (TC == Fortran::common::TypeCategory::Logical ||
TC == Fortran::common::TypeCategory::Character ||
TC == Fortran::common::TypeCategory::Complex) {
return genFIRType<TC>(context, KIND);
} else {
return {};
}
}
template <>
mlir::Type
genFIRType<Fortran::common::TypeCategory::Real, 2>(mlir::MLIRContext *context) {
return mlir::FloatType::getF16(context);
}
template <>
mlir::Type
genFIRType<Fortran::common::TypeCategory::Real, 3>(mlir::MLIRContext *context) {
return mlir::FloatType::getBF16(context);
}
template <>
mlir::Type
genFIRType<Fortran::common::TypeCategory::Real, 4>(mlir::MLIRContext *context) {
return mlir::FloatType::getF32(context);
}
template <>
mlir::Type
genFIRType<Fortran::common::TypeCategory::Real, 8>(mlir::MLIRContext *context) {
return mlir::FloatType::getF64(context);
}
template <>
mlir::Type genFIRType<Fortran::common::TypeCategory::Real, 10>(
mlir::MLIRContext *context) {
return fir::RealType::get(context, 10);
}
template <>
mlir::Type genFIRType<Fortran::common::TypeCategory::Real, 16>(
mlir::MLIRContext *context) {
return fir::RealType::get(context, 16);
}
template <>
mlir::Type
genFIRType<Fortran::common::TypeCategory::Real>(mlir::MLIRContext *context,
int kind) {
if (Fortran::evaluate::IsValidKindOfIntrinsicType(
Fortran::common::TypeCategory::Real, kind)) {
switch (kind) {
case 2:
return genFIRType<Fortran::common::TypeCategory::Real, 2>(context);
case 3:
return genFIRType<Fortran::common::TypeCategory::Real, 3>(context);
case 4:
return genFIRType<Fortran::common::TypeCategory::Real, 4>(context);
case 8:
return genFIRType<Fortran::common::TypeCategory::Real, 8>(context);
case 10:
return genFIRType<Fortran::common::TypeCategory::Real, 10>(context);
case 16:
return genFIRType<Fortran::common::TypeCategory::Real, 16>(context);
}
}
llvm_unreachable("REAL type translation not implemented");
}
template <>
mlir::Type
genFIRType<Fortran::common::TypeCategory::Integer>(mlir::MLIRContext *context,
int kind) {
if (Fortran::evaluate::IsValidKindOfIntrinsicType(
Fortran::common::TypeCategory::Integer, kind)) {
switch (kind) {
case 1:
return genFIRType<Fortran::common::TypeCategory::Integer, 1>(context);
case 2:
return genFIRType<Fortran::common::TypeCategory::Integer, 2>(context);
case 4:
return genFIRType<Fortran::common::TypeCategory::Integer, 4>(context);
case 8:
return genFIRType<Fortran::common::TypeCategory::Integer, 8>(context);
case 16:
return genFIRType<Fortran::common::TypeCategory::Integer, 16>(context);
}
}
llvm_unreachable("INTEGER type translation not implemented");
}
template <>
mlir::Type
genFIRType<Fortran::common::TypeCategory::Logical>(mlir::MLIRContext *context,
int KIND) {
if (Fortran::evaluate::IsValidKindOfIntrinsicType(
Fortran::common::TypeCategory::Logical, KIND))
return fir::LogicalType::get(context, KIND);
return {};
}
template <>
mlir::Type
genFIRType<Fortran::common::TypeCategory::Character>(mlir::MLIRContext *context,
int KIND) {
if (Fortran::evaluate::IsValidKindOfIntrinsicType(
Fortran::common::TypeCategory::Character, KIND))
return fir::CharacterType::get(context, KIND, 1);
return {};
}
template <>
mlir::Type
genFIRType<Fortran::common::TypeCategory::Complex>(mlir::MLIRContext *context,
int KIND) {
if (Fortran::evaluate::IsValidKindOfIntrinsicType(
Fortran::common::TypeCategory::Complex, KIND))
return fir::ComplexType::get(context, KIND);
return {};
}
namespace {
/// Discover the type of an Fortran::evaluate::Expr<T> and convert it to an
/// mlir::Type. The type returned may be an MLIR standard or FIR type.
class TypeBuilder {
public:
/// Constructor.
explicit TypeBuilder(
mlir::MLIRContext *context,
const Fortran::common::IntrinsicTypeDefaultKinds &defaults)
: context{context}, defaults{defaults} {}
//===--------------------------------------------------------------------===//
// Generate type entry points
//===--------------------------------------------------------------------===//
template <template <typename> typename A, Fortran::common::TypeCategory TC>
mlir::Type gen(const A<Fortran::evaluate::SomeKind<TC>> &) {
return genFIRType<TC>(context, defaultKind<TC>());
}
template <template <typename> typename A, Fortran::common::TypeCategory TC,
int KIND>
mlir::Type gen(const A<Fortran::evaluate::Type<TC, KIND>> &) {
return genFIRType<TC, KIND>(context);
}
// breaks the conflict between A<Type<TC,KIND>> and Expr<B> deduction
template <Fortran::common::TypeCategory TC, int KIND>
mlir::Type
gen(const Fortran::evaluate::Expr<Fortran::evaluate::Type<TC, KIND>> &) {
return genFIRType<TC, KIND>(context);
}
// breaks the conflict between A<SomeKind<TC>> and Expr<B> deduction
template <Fortran::common::TypeCategory TC>
mlir::Type
gen(const Fortran::evaluate::Expr<Fortran::evaluate::SomeKind<TC>> &expr) {
return genVariant(expr);
}
template <typename A>
mlir::Type gen(const Fortran::evaluate::Expr<A> &expr) {
return genVariant(expr);
}
mlir::Type gen(const Fortran::evaluate::DataRef &dref) {
return genVariant(dref);
}
mlir::Type gen(const Fortran::lower::pft::Variable &var) {
return genSymbolHelper(var.getSymbol(), var.isHeapAlloc(), var.isPointer());
}
/// Type consing from a symbol. A symbol's type must be created from the type
/// discovered by the front-end at runtime.
mlir::Type gen(Fortran::semantics::SymbolRef symbol) {
return genSymbolHelper(symbol);
}
// non-template, category is runtime values, kind is defaulted
mlir::Type genFIRTy(Fortran::common::TypeCategory tc) {
return genFIRTy(tc, defaultKind(tc));
}
// non-template, arguments are runtime values
mlir::Type genFIRTy(Fortran::common::TypeCategory tc, int kind) {
switch (tc) {
case Fortran::common::TypeCategory::Real:
return genFIRType<Fortran::common::TypeCategory::Real>(context, kind);
case Fortran::common::TypeCategory::Integer:
return genFIRType<Fortran::common::TypeCategory::Integer>(context, kind);
case Fortran::common::TypeCategory::Complex:
return genFIRType<Fortran::common::TypeCategory::Complex>(context, kind);
case Fortran::common::TypeCategory::Logical:
return genFIRType<Fortran::common::TypeCategory::Logical>(context, kind);
case Fortran::common::TypeCategory::Character:
return genFIRType<Fortran::common::TypeCategory::Character>(context,
kind);
default:
break;
}
llvm_unreachable("unhandled type category");
}
private:
//===--------------------------------------------------------------------===//
// Generate type helpers
//===--------------------------------------------------------------------===//
mlir::Type gen(const Fortran::evaluate::ImpliedDoIndex &) {
return genFIRType<Fortran::evaluate::ImpliedDoIndex::Result::category>(
context, Fortran::evaluate::ImpliedDoIndex::Result::kind);
}
mlir::Type gen(const Fortran::evaluate::TypeParamInquiry &) {
return genFIRType<Fortran::evaluate::TypeParamInquiry::Result::category>(
context, Fortran::evaluate::TypeParamInquiry::Result::kind);
}
template <typename A>
mlir::Type gen(const Fortran::evaluate::Relational<A> &) {
return genFIRType<Fortran::common::TypeCategory::Logical, 1>(context);
}
// some sequence of `n` bytes
mlir::Type gen(const Fortran::evaluate::StaticDataObject::Pointer &ptr) {
mlir::Type byteTy{mlir::IntegerType::get(context, 8)};
return fir::SequenceType::get(trivialShape(ptr->itemBytes()), byteTy);
}
mlir::Type gen(const Fortran::evaluate::Substring &ss) {
return genVariant(ss.GetBaseObject());
}
mlir::Type gen(const Fortran::evaluate::NullPointer &) {
return genTypelessPtr();
}
mlir::Type gen(const Fortran::evaluate::ProcedureRef &) {
return genTypelessPtr();
}
mlir::Type gen(const Fortran::evaluate::ProcedureDesignator &) {
return genTypelessPtr();
}
mlir::Type gen(const Fortran::evaluate::BOZLiteralConstant &) {
return genTypelessPtr();
}
mlir::Type gen(const Fortran::evaluate::ArrayRef &) { TODO("array ref"); }
mlir::Type gen(const Fortran::evaluate::CoarrayRef &) { TODO("coarray ref"); }
mlir::Type gen(const Fortran::evaluate::Component &) { TODO("component"); }
mlir::Type gen(const Fortran::evaluate::ComplexPart &) {
TODO("complex part");
}
mlir::Type gen(const Fortran::evaluate::DescriptorInquiry &) {
TODO("descriptor inquiry");
}
mlir::Type gen(const Fortran::evaluate::StructureConstructor &) {
TODO("structure constructor");
}
fir::SequenceType::Shape genSeqShape(Fortran::semantics::SymbolRef symbol) {
assert(symbol->IsObjectArray() && "unexpected symbol type");
fir::SequenceType::Shape bounds;
return seqShapeHelper(symbol, bounds);
}
fir::SequenceType::Shape genSeqShape(Fortran::semantics::SymbolRef symbol,
fir::SequenceType::Extent charLen) {
assert(symbol->IsObjectArray() && "unexpected symbol type");
fir::SequenceType::Shape bounds;
bounds.push_back(charLen);
return seqShapeHelper(symbol, bounds);
}
mlir::Type genSymbolHelper(const Fortran::semantics::Symbol &symbol,
bool isAlloc = false, bool isPtr = false) {
mlir::Type ty;
if (auto *type{symbol.GetType()}) {
if (auto *tySpec{type->AsIntrinsic()}) {
int kind = toConstant(tySpec->kind());
switch (tySpec->category()) {
case Fortran::common::TypeCategory::Integer:
ty =
genFIRType<Fortran::common::TypeCategory::Integer>(context, kind);
break;
case Fortran::common::TypeCategory::Real:
ty = genFIRType<Fortran::common::TypeCategory::Real>(context, kind);
break;
case Fortran::common::TypeCategory::Complex:
ty =
genFIRType<Fortran::common::TypeCategory::Complex>(context, kind);
break;
case Fortran::common::TypeCategory::Character:
ty = genFIRType<Fortran::common::TypeCategory::Character>(context,
kind);
break;
case Fortran::common::TypeCategory::Logical:
ty =
genFIRType<Fortran::common::TypeCategory::Logical>(context, kind);
break;
default:
emitError("symbol has unknown intrinsic type");
return {};
}
} else if (auto *tySpec = type->AsDerived()) {
std::vector<std::pair<std::string, mlir::Type>> ps;
std::vector<std::pair<std::string, mlir::Type>> cs;
auto &symbol = tySpec->typeSymbol();
// FIXME: don't want to recurse forever here, but this won't happen
// since we don't know the components at this time
auto rec = fir::RecordType::get(context, toStringRef(symbol.name()));
auto &details = symbol.get<Fortran::semantics::DerivedTypeDetails>();
for (auto ¶m : details.paramDecls()) {
auto &p{*param};
ps.push_back(std::pair{p.name().ToString(), gen(p)});
}
emitError("the front-end returns symbols of derived type that have "
"components that are simple names and not symbols, so cannot "
"construct the type '" +
toStringRef(symbol.name()) + "'");
rec.finalize(ps, cs);
ty = rec;
} else {
emitError("symbol's type must have a type spec");
return {};
}
} else {
emitError("symbol must have a type");
return {};
}
if (symbol.IsObjectArray()) {
if (symbol.GetType()->category() ==
Fortran::semantics::DeclTypeSpec::Character) {
auto charLen = fir::SequenceType::getUnknownExtent();
const auto &lenParam = symbol.GetType()->characterTypeSpec().length();
if (auto expr = lenParam.GetExplicit()) {
auto len = Fortran::evaluate::AsGenericExpr(std::move(*expr));
auto asInt = Fortran::evaluate::ToInt64(len);
if (asInt)
charLen = *asInt;
}
return fir::SequenceType::get(genSeqShape(symbol, charLen), ty);
}
return fir::SequenceType::get(genSeqShape(symbol), ty);
}
if (isPtr || Fortran::semantics::IsPointer(symbol))
ty = fir::PointerType::get(ty);
else if (isAlloc || Fortran::semantics::IsAllocatable(symbol))
ty = fir::HeapType::get(ty);
return ty;
}
//===--------------------------------------------------------------------===//
// Other helper functions
//===--------------------------------------------------------------------===//
fir::SequenceType::Shape trivialShape(int size) {
fir::SequenceType::Shape bounds;
bounds.emplace_back(size);
return bounds;
}
mlir::Type mkVoid() { return mlir::TupleType::get(context); }
mlir::Type genTypelessPtr() { return fir::ReferenceType::get(mkVoid()); }
template <typename A>
mlir::Type genVariant(const A &variant) {
return std::visit([&](const auto &x) { return gen(x); }, variant.u);
}
template <Fortran::common::TypeCategory TC>
int defaultKind() {
return defaultKind(TC);
}
int defaultKind(Fortran::common::TypeCategory TC) {
return defaults.GetDefaultKind(TC);
}
fir::SequenceType::Shape seqShapeHelper(Fortran::semantics::SymbolRef symbol,
fir::SequenceType::Shape &bounds) {
auto &details = symbol->get<Fortran::semantics::ObjectEntityDetails>();
const auto size = details.shape().size();
for (auto &ss : details.shape()) {
auto lb = ss.lbound();
auto ub = ss.ubound();
if (lb.isAssumed() && ub.isAssumed() && size == 1)
return {};
if (lb.isExplicit() && ub.isExplicit()) {
auto &lbv = lb.GetExplicit();
auto &ubv = ub.GetExplicit();
if (lbv.has_value() && ubv.has_value() && isConstant(lbv.value()) &&
isConstant(ubv.value())) {
bounds.emplace_back(toConstant(ubv.value()) -
toConstant(lbv.value()) + 1);
} else {
bounds.emplace_back(fir::SequenceType::getUnknownExtent());
}
} else {
bounds.emplace_back(fir::SequenceType::getUnknownExtent());
}
}
return bounds;
}
//===--------------------------------------------------------------------===//
// Emit errors and warnings.
//===--------------------------------------------------------------------===//
mlir::InFlightDiagnostic emitError(const llvm::Twine &message) {
return mlir::emitError(mlir::UnknownLoc::get(context), message);
}
mlir::InFlightDiagnostic emitWarning(const llvm::Twine &message) {
return mlir::emitWarning(mlir::UnknownLoc::get(context), message);
}
//===--------------------------------------------------------------------===//
mlir::MLIRContext *context;
const Fortran::common::IntrinsicTypeDefaultKinds &defaults;
};
} // namespace
mlir::Type Fortran::lower::getFIRType(
mlir::MLIRContext *context,
const Fortran::common::IntrinsicTypeDefaultKinds &defaults,
Fortran::common::TypeCategory tc, int kind) {
return TypeBuilder{context, defaults}.genFIRTy(tc, kind);
}
mlir::Type Fortran::lower::getFIRType(
mlir::MLIRContext *context,
const Fortran::common::IntrinsicTypeDefaultKinds &defaults,
Fortran::common::TypeCategory tc) {
return TypeBuilder{context, defaults}.genFIRTy(tc);
}
mlir::Type Fortran::lower::translateDataRefToFIRType(
mlir::MLIRContext *context,
const Fortran::common::IntrinsicTypeDefaultKinds &defaults,
const Fortran::evaluate::DataRef &dataRef) {
return TypeBuilder{context, defaults}.gen(dataRef);
}
mlir::Type Fortran::lower::translateSomeExprToFIRType(
mlir::MLIRContext *context,
const Fortran::common::IntrinsicTypeDefaultKinds &defaults,
const SomeExpr *expr) {
return TypeBuilder{context, defaults}.gen(*expr);
}
mlir::Type Fortran::lower::translateSymbolToFIRType(
mlir::MLIRContext *context,
const Fortran::common::IntrinsicTypeDefaultKinds &defaults,
const SymbolRef symbol) {
return TypeBuilder{context, defaults}.gen(symbol);
}
mlir::Type Fortran::lower::translateVariableToFIRType(
mlir::MLIRContext *context,
const Fortran::common::IntrinsicTypeDefaultKinds &defaults,
const Fortran::lower::pft::Variable &var) {
return TypeBuilder{context, defaults}.gen(var);
}
mlir::Type Fortran::lower::convertReal(mlir::MLIRContext *context, int kind) {
return genFIRType<Fortran::common::TypeCategory::Real>(context, kind);
}
mlir::Type Fortran::lower::getSequenceRefType(mlir::Type refType) {
auto type{refType.dyn_cast<fir::ReferenceType>()};
assert(type && "expected a reference type");
auto elementType{type.getEleTy()};
fir::SequenceType::Shape shape{fir::SequenceType::getUnknownExtent()};
return fir::ReferenceType::get(fir::SequenceType::get(shape, elementType));
}
|