@@ -1294,51 +1294,6 @@ 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
-
1342
1297
// / Common base class for embox to descriptor conversion.
1343
1298
template <typename OP>
1344
1299
struct EmboxCommonConversion : public fir ::FIROpConversion<OP> {
@@ -1451,6 +1406,36 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> {
1451
1406
stride);
1452
1407
}
1453
1408
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
+
1454
1439
template <typename ModOpTy>
1455
1440
mlir::Value populateDescriptor (mlir::Location loc, ModOpTy mod,
1456
1441
fir::BaseBoxType boxTy, mlir::Type inputType,
@@ -1515,17 +1500,16 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> {
1515
1500
mlir::Type innerType = fir::unwrapInnerType (inputType);
1516
1501
if (innerType && mlir::isa<fir::RecordType>(innerType)) {
1517
1502
auto recTy = mlir::dyn_cast<fir::RecordType>(innerType);
1518
- typeDesc =
1519
- getTypeDescriptor (mod, rewriter, loc, recTy, this ->options );
1503
+ typeDesc = getTypeDescriptor (mod, rewriter, loc, recTy);
1520
1504
} else {
1521
1505
// Unlimited polymorphic type descriptor with no record type. Set
1522
1506
// type descriptor address to a clean state.
1523
1507
typeDesc = rewriter.create <mlir::LLVM::ZeroOp>(
1524
1508
loc, ::getLlvmPtrType (mod.getContext ()));
1525
1509
}
1526
1510
} else {
1527
- typeDesc = getTypeDescriptor (
1528
- mod, rewriter, loc, fir::unwrapIfDerived (boxTy), this -> options );
1511
+ typeDesc = getTypeDescriptor (mod, rewriter, loc,
1512
+ fir::unwrapIfDerived (boxTy));
1529
1513
}
1530
1514
}
1531
1515
if (typeDesc)
@@ -3037,10 +3021,22 @@ struct TypeDescOpConversion : public fir::FIROpConversion<fir::TypeDescOp> {
3037
3021
assert (mlir::isa<fir::RecordType>(inTy) && " expecting fir.type" );
3038
3022
auto recordType = mlir::dyn_cast<fir::RecordType>(inTy);
3039
3023
auto module = typeDescOp.getOperation ()->getParentOfType <mlir::ModuleOp>();
3040
- mlir::Value typeDesc = getTypeDescriptor (
3041
- module , rewriter, typeDescOp.getLoc (), recordType, this ->options );
3042
- rewriter.replaceOp (typeDescOp, typeDesc);
3043
- return mlir::success ();
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 ();
3044
3040
}
3045
3041
};
3046
3042
0 commit comments