2323#include " flang/Optimizer/Support/InternalNames.h"
2424#include " flang/Optimizer/Support/TypeCode.h"
2525#include " flang/Optimizer/Support/Utils.h"
26+ #include " flang/Runtime/allocator-registry.h"
27+ #include " flang/Runtime/descriptor.h"
2628#include " flang/Semantics/runtime-type-info.h"
2729#include " mlir/Conversion/ArithCommon/AttrToLLVMConverter.h"
2830#include " mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
@@ -1224,8 +1226,8 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> {
12241226 fir::BaseBoxType boxTy, mlir::Type inputType,
12251227 mlir::ConversionPatternRewriter &rewriter,
12261228 unsigned rank, mlir::Value eleSize,
1227- mlir::Value cfiTy,
1228- mlir::Value typeDesc ) const {
1229+ mlir::Value cfiTy, mlir::Value typeDesc,
1230+ int allocatorIdx = kDefaultAllocator ) const {
12291231 auto llvmBoxTy = this ->lowerTy ().convertBoxTypeAsStruct (boxTy, rank);
12301232 bool isUnlimitedPolymorphic = fir::isUnlimitedPolymorphicType (boxTy);
12311233 bool useInputType = fir::isPolymorphicType (boxTy) || isUnlimitedPolymorphic;
@@ -1243,9 +1245,17 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> {
12431245 this ->genI32Constant (loc, rewriter, getCFIAttr (boxTy)));
12441246
12451247 const bool hasAddendum = fir::boxHasAddendum (boxTy);
1248+
1249+ // Descriptor used to set the correct value of the extra field.
1250+ Fortran::runtime::StaticDescriptor<0 > staticDescriptor;
1251+ Fortran::runtime::Descriptor &desc{staticDescriptor.descriptor ()};
1252+ desc.raw ().extra = 0 ;
1253+ desc.SetAllocIdx (allocatorIdx);
1254+ if (hasAddendum)
1255+ desc.SetHasAddendum ();
12461256 descriptor =
12471257 insertField (rewriter, loc, descriptor, {kExtraPosInBox },
1248- this ->genI32Constant (loc, rewriter, hasAddendum ? 1 : 0 ));
1258+ this ->genI32Constant (loc, rewriter, desc. raw (). extra ));
12491259
12501260 if (hasAddendum) {
12511261 unsigned typeDescFieldId = getTypeDescFieldId (boxTy);
@@ -1300,6 +1310,13 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> {
13001310 typeparams.push_back (substrParams[1 ]);
13011311 }
13021312
1313+ int allocatorIdx = 0 ;
1314+ if constexpr (std::is_same_v<BOX, fir::EmboxOp> ||
1315+ std::is_same_v<BOX, fir::cg::XEmboxOp>) {
1316+ if (box.getAllocatorIdx ())
1317+ allocatorIdx = *box.getAllocatorIdx ();
1318+ }
1319+
13031320 // Write each of the fields with the appropriate values.
13041321 // When emboxing an element to a polymorphic descriptor, use the
13051322 // input type since the destination descriptor type has not the exact
@@ -1321,8 +1338,9 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> {
13211338 cfiTy.getType (), rewriter, kTypePosInBox );
13221339 }
13231340 auto mod = box->template getParentOfType <mlir::ModuleOp>();
1324- mlir::Value descriptor = populateDescriptor (
1325- loc, mod, boxTy, inputType, rewriter, rank, eleSize, cfiTy, typeDesc);
1341+ mlir::Value descriptor =
1342+ populateDescriptor (loc, mod, boxTy, inputType, rewriter, rank, eleSize,
1343+ cfiTy, typeDesc, allocatorIdx);
13261344
13271345 return {boxTy, descriptor, eleSize};
13281346 }
0 commit comments