@@ -1294,6 +1294,51 @@ genCUFAllocDescriptor(mlir::Location loc,
1294
1294
.getResult ();
1295
1295
}
1296
1296
1297
+ // / Get the address of the type descriptor global variable that was created by
1298
+ // / lowering for derived type \p recType.
1299
+ template <typename ModOpTy>
1300
+ static mlir::Value
1301
+ getTypeDescriptor (ModOpTy mod, mlir::ConversionPatternRewriter &rewriter,
1302
+ mlir::Location loc, fir::RecordType recType,
1303
+ const fir::FIRToLLVMPassOptions &options) {
1304
+ std::string name =
1305
+ options.typeDescriptorsRenamedForAssembly
1306
+ ? fir::NameUniquer::getTypeDescriptorAssemblyName (recType.getName ())
1307
+ : fir::NameUniquer::getTypeDescriptorName (recType.getName ());
1308
+ mlir::Type llvmPtrTy = ::getLlvmPtrType (mod.getContext ());
1309
+ if (auto global = mod.template lookupSymbol <fir::GlobalOp>(name))
1310
+ return rewriter.create <mlir::LLVM::AddressOfOp>(loc, llvmPtrTy,
1311
+ global.getSymName ());
1312
+ // The global may have already been translated to LLVM.
1313
+ if (auto global = mod.template lookupSymbol <mlir::LLVM::GlobalOp>(name))
1314
+ return rewriter.create <mlir::LLVM::AddressOfOp>(loc, llvmPtrTy,
1315
+ global.getSymName ());
1316
+ // Type info derived types do not have type descriptors since they are the
1317
+ // types defining type descriptors.
1318
+ if (options.ignoreMissingTypeDescriptors ||
1319
+ fir::NameUniquer::belongsToModule (
1320
+ name, Fortran::semantics::typeInfoBuiltinModule))
1321
+ return rewriter.create <mlir::LLVM::ZeroOp>(loc, llvmPtrTy);
1322
+
1323
+ if (!options.skipExternalRttiDefinition )
1324
+ fir::emitFatalError (loc,
1325
+ " runtime derived type info descriptor was not "
1326
+ " generated and skipExternalRttiDefinition and "
1327
+ " ignoreMissingTypeDescriptors options are not set" );
1328
+
1329
+ // Rtti for a derived type defined in another compilation unit and for which
1330
+ // rtti was not defined in lowering because of the skipExternalRttiDefinition
1331
+ // option. Generate the object declaration now.
1332
+ auto insertPt = rewriter.saveInsertionPoint ();
1333
+ rewriter.setInsertionPoint (mod.getBody (), mod.getBody ()->end ());
1334
+ mlir::LLVM::GlobalOp global = rewriter.create <mlir::LLVM::GlobalOp>(
1335
+ loc, llvmPtrTy, /* constant=*/ true , mlir::LLVM::Linkage::External, name,
1336
+ mlir::Attribute ());
1337
+ rewriter.restoreInsertionPoint (insertPt);
1338
+ return rewriter.create <mlir::LLVM::AddressOfOp>(loc, llvmPtrTy,
1339
+ global.getSymName ());
1340
+ }
1341
+
1297
1342
// / Common base class for embox to descriptor conversion.
1298
1343
template <typename OP>
1299
1344
struct EmboxCommonConversion : public fir ::FIROpConversion<OP> {
@@ -1406,36 +1451,6 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> {
1406
1451
stride);
1407
1452
}
1408
1453
1409
- // / Get the address of the type descriptor global variable that was created by
1410
- // / lowering for derived type \p recType.
1411
- template <typename ModOpTy>
1412
- mlir::Value
1413
- getTypeDescriptor (ModOpTy mod, mlir::ConversionPatternRewriter &rewriter,
1414
- mlir::Location loc, fir::RecordType recType) const {
1415
- std::string name =
1416
- this ->options .typeDescriptorsRenamedForAssembly
1417
- ? fir::NameUniquer::getTypeDescriptorAssemblyName (recType.getName ())
1418
- : fir::NameUniquer::getTypeDescriptorName (recType.getName ());
1419
- mlir::Type llvmPtrTy = ::getLlvmPtrType (mod.getContext ());
1420
- if (auto global = mod.template lookupSymbol <fir::GlobalOp>(name)) {
1421
- return rewriter.create <mlir::LLVM::AddressOfOp>(loc, llvmPtrTy,
1422
- global.getSymName ());
1423
- }
1424
- if (auto global = mod.template lookupSymbol <mlir::LLVM::GlobalOp>(name)) {
1425
- // The global may have already been translated to LLVM.
1426
- return rewriter.create <mlir::LLVM::AddressOfOp>(loc, llvmPtrTy,
1427
- global.getSymName ());
1428
- }
1429
- // Type info derived types do not have type descriptors since they are the
1430
- // types defining type descriptors.
1431
- if (!this ->options .ignoreMissingTypeDescriptors &&
1432
- !fir::NameUniquer::belongsToModule (
1433
- name, Fortran::semantics::typeInfoBuiltinModule))
1434
- fir::emitFatalError (
1435
- loc, " runtime derived type info descriptor was not generated" );
1436
- return rewriter.create <mlir::LLVM::ZeroOp>(loc, llvmPtrTy);
1437
- }
1438
-
1439
1454
template <typename ModOpTy>
1440
1455
mlir::Value populateDescriptor (mlir::Location loc, ModOpTy mod,
1441
1456
fir::BaseBoxType boxTy, mlir::Type inputType,
@@ -1500,16 +1515,17 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> {
1500
1515
mlir::Type innerType = fir::unwrapInnerType (inputType);
1501
1516
if (innerType && mlir::isa<fir::RecordType>(innerType)) {
1502
1517
auto recTy = mlir::dyn_cast<fir::RecordType>(innerType);
1503
- typeDesc = getTypeDescriptor (mod, rewriter, loc, recTy);
1518
+ typeDesc =
1519
+ getTypeDescriptor (mod, rewriter, loc, recTy, this ->options );
1504
1520
} else {
1505
1521
// Unlimited polymorphic type descriptor with no record type. Set
1506
1522
// type descriptor address to a clean state.
1507
1523
typeDesc = rewriter.create <mlir::LLVM::ZeroOp>(
1508
1524
loc, ::getLlvmPtrType (mod.getContext ()));
1509
1525
}
1510
1526
} else {
1511
- typeDesc = getTypeDescriptor (mod, rewriter, loc,
1512
- fir::unwrapIfDerived (boxTy));
1527
+ typeDesc = getTypeDescriptor (
1528
+ mod, rewriter, loc, fir::unwrapIfDerived (boxTy), this -> options );
1513
1529
}
1514
1530
}
1515
1531
if (typeDesc)
@@ -3021,22 +3037,10 @@ struct TypeDescOpConversion : public fir::FIROpConversion<fir::TypeDescOp> {
3021
3037
assert (mlir::isa<fir::RecordType>(inTy) && " expecting fir.type" );
3022
3038
auto recordType = mlir::dyn_cast<fir::RecordType>(inTy);
3023
3039
auto module = typeDescOp.getOperation ()->getParentOfType <mlir::ModuleOp>();
3024
- std::string typeDescName =
3025
- this ->options .typeDescriptorsRenamedForAssembly
3026
- ? fir::NameUniquer::getTypeDescriptorAssemblyName (
3027
- recordType.getName ())
3028
- : fir::NameUniquer::getTypeDescriptorName (recordType.getName ());
3029
- auto llvmPtrTy = ::getLlvmPtrType (typeDescOp.getContext ());
3030
- if (auto global = module .lookupSymbol <mlir::LLVM::GlobalOp>(typeDescName)) {
3031
- rewriter.replaceOpWithNewOp <mlir::LLVM::AddressOfOp>(
3032
- typeDescOp, llvmPtrTy, global.getSymName ());
3033
- return mlir::success ();
3034
- } else if (auto global = module .lookupSymbol <fir::GlobalOp>(typeDescName)) {
3035
- rewriter.replaceOpWithNewOp <mlir::LLVM::AddressOfOp>(
3036
- typeDescOp, llvmPtrTy, global.getSymName ());
3037
- return mlir::success ();
3038
- }
3039
- return mlir::failure ();
3040
+ mlir::Value typeDesc = getTypeDescriptor (
3041
+ module , rewriter, typeDescOp.getLoc (), recordType, this ->options );
3042
+ rewriter.replaceOp (typeDescOp, typeDesc);
3043
+ return mlir::success ();
3040
3044
}
3041
3045
};
3042
3046
0 commit comments