Skip to content

Commit 49bcd66

Browse files
committed
[flang] Add allocator_idx attribute on fir.embox and fircg.ext_embox
1 parent 6df4e7c commit 49bcd66

File tree

8 files changed

+64
-16
lines changed

8 files changed

+64
-16
lines changed

flang/include/flang/Optimizer/CodeGen/CGOps.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def fircg_XEmboxOp : fircg_Op<"ext_embox", [AttrSizedOperandSegments]> {
4848
- substring: A substring operator (offset, length) for CHARACTER.
4949
- LEN type parameters: A vector of runtime LEN type parameters that
5050
describe an correspond to the elemental derived type.
51+
- allocator_idx: specify special allocator to use.
5152

5253
The memref and shape arguments are mandatory. The rest are optional.
5354
}];
@@ -60,7 +61,8 @@ def fircg_XEmboxOp : fircg_Op<"ext_embox", [AttrSizedOperandSegments]> {
6061
Variadic<AnyCoordinateType>:$subcomponent,
6162
Variadic<AnyIntegerType>:$substr,
6263
Variadic<AnyIntegerType>:$lenParams,
63-
Optional<fir_ClassType>:$sourceBox
64+
Optional<fir_ClassType>:$sourceBox,
65+
OptionalAttr<I32Attr>:$allocator_idx
6466
);
6567
let results = (outs BoxOrClassType);
6668

flang/include/flang/Optimizer/Dialect/FIROps.td

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ def fir_EmboxOp : fir_Op<"embox", [NoMemoryEffect, AttrSizedOperandSegments]> {
783783
- slice: an array section can be described with a slice triple,
784784
- typeparams: for emboxing a derived type with LEN type parameters,
785785
- accessMap: unused/experimental.
786+
- allocator_idx: specify special allocator to use.
786787
}];
787788

788789
let arguments = (ins
@@ -791,7 +792,8 @@ def fir_EmboxOp : fir_Op<"embox", [NoMemoryEffect, AttrSizedOperandSegments]> {
791792
Optional<fir_SliceType>:$slice,
792793
Variadic<AnyIntegerType>:$typeparams,
793794
Optional<fir_ClassType>:$sourceBox,
794-
OptionalAttr<AffineMapAttr>:$accessMap
795+
OptionalAttr<AffineMapAttr>:$accessMap,
796+
OptionalAttr<I32Attr>:$allocator_idx
795797
);
796798

797799
let results = (outs BoxOrClassType);
@@ -801,9 +803,11 @@ def fir_EmboxOp : fir_Op<"embox", [NoMemoryEffect, AttrSizedOperandSegments]> {
801803
"mlir::Value":$memref, CArg<"mlir::Value", "{}">:$shape,
802804
CArg<"mlir::Value", "{}">:$slice,
803805
CArg<"mlir::ValueRange", "{}">:$typeparams,
804-
CArg<"mlir::Value", "{}">:$sourceBox),
806+
CArg<"mlir::Value", "{}">:$sourceBox,
807+
CArg<"mlir::IntegerAttr", "{}">:$allocator_idx),
805808
[{ return build($_builder, $_state, resultTypes, memref, shape, slice,
806-
typeparams, sourceBox, mlir::AffineMapAttr{}); }]>
809+
typeparams, sourceBox, mlir::AffineMapAttr{},
810+
allocator_idx); }]>
807811
];
808812

809813
let assemblyFormat = [{

flang/runtime/allocator-registry.h renamed to flang/include/flang/Runtime/allocator-registry.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <cstdlib>
1414
#include <vector>
1515

16+
static constexpr unsigned kDefaultAllocator = 0;
17+
1618
#define MAX_ALLOCATOR 5
1719

1820
namespace Fortran::runtime {
@@ -37,7 +39,9 @@ struct AllocatorRegistry {
3739
RT_API_ATTRS constexpr AllocatorRegistry()
3840
: allocators{{&MallocWrapper, &FreeWrapper}} {}
3941
#else
40-
constexpr AllocatorRegistry() { allocators[0] = {&std::malloc, &std::free}; };
42+
constexpr AllocatorRegistry() {
43+
allocators[kDefaultAllocator] = {&std::malloc, &std::free};
44+
};
4145
#endif
4246
RT_API_ATTRS void Register(int, Allocator_t);
4347
RT_API_ATTRS AllocFct GetAllocator(int pos);

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
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"
2627
#include "flang/Semantics/runtime-type-info.h"
2728
#include "mlir/Conversion/ArithCommon/AttrToLLVMConverter.h"
2829
#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
@@ -1224,8 +1225,8 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> {
12241225
fir::BaseBoxType boxTy, mlir::Type inputType,
12251226
mlir::ConversionPatternRewriter &rewriter,
12261227
unsigned rank, mlir::Value eleSize,
1227-
mlir::Value cfiTy,
1228-
mlir::Value typeDesc) const {
1228+
mlir::Value cfiTy, mlir::Value typeDesc,
1229+
int allocatorIdx = 0) const {
12291230
auto llvmBoxTy = this->lowerTy().convertBoxTypeAsStruct(boxTy, rank);
12301231
bool isUnlimitedPolymorphic = fir::isUnlimitedPolymorphicType(boxTy);
12311232
bool useInputType = fir::isPolymorphicType(boxTy) || isUnlimitedPolymorphic;
@@ -1243,9 +1244,14 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> {
12431244
this->genI32Constant(loc, rewriter, getCFIAttr(boxTy)));
12441245

12451246
const bool hasAddendum = fir::boxHasAddendum(boxTy);
1246-
descriptor =
1247-
insertField(rewriter, loc, descriptor, {kExtraPosInBox},
1248-
this->genI32Constant(loc, rewriter, hasAddendum ? 1 : 0));
1247+
1248+
Fortran::ISO::CFI_cdesc_t desc;
1249+
desc.extra = 0;
1250+
desc.SetAllocIdx(allocatorIdx);
1251+
if (hasAddendum)
1252+
desc.SetHasAddendum();
1253+
descriptor = insertField(rewriter, loc, descriptor, {kExtraPosInBox},
1254+
this->genI32Constant(loc, rewriter, desc.extra));
12491255

12501256
if (hasAddendum) {
12511257
unsigned typeDescFieldId = getTypeDescFieldId(boxTy);
@@ -1300,6 +1306,13 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> {
13001306
typeparams.push_back(substrParams[1]);
13011307
}
13021308

1309+
int allocatorIdx = 0;
1310+
if constexpr (std::is_same_v<BOX, fir::EmboxOp> ||
1311+
std::is_same_v<BOX, fir::cg::XEmboxOp>) {
1312+
if (box.getAllocatorIdx())
1313+
allocatorIdx = *box.getAllocatorIdx();
1314+
}
1315+
13031316
// Write each of the fields with the appropriate values.
13041317
// When emboxing an element to a polymorphic descriptor, use the
13051318
// input type since the destination descriptor type has not the exact
@@ -1321,8 +1334,9 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> {
13211334
cfiTy.getType(), rewriter, kTypePosInBox);
13221335
}
13231336
auto mod = box->template getParentOfType<mlir::ModuleOp>();
1324-
mlir::Value descriptor = populateDescriptor(
1325-
loc, mod, boxTy, inputType, rewriter, rank, eleSize, cfiTy, typeDesc);
1337+
mlir::Value descriptor =
1338+
populateDescriptor(loc, mod, boxTy, inputType, rewriter, rank, eleSize,
1339+
cfiTy, typeDesc, allocatorIdx);
13261340

13271341
return {boxTy, descriptor, eleSize};
13281342
}

flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class EmboxConversion : public mlir::OpRewritePattern<fir::EmboxOp> {
109109
auto xbox = rewriter.create<fir::cg::XEmboxOp>(
110110
loc, embox.getType(), embox.getMemref(), shapeOpers, std::nullopt,
111111
std::nullopt, std::nullopt, std::nullopt, embox.getTypeparams(),
112-
embox.getSourceBox());
112+
embox.getSourceBox(), embox.getAllocatorIdxAttr());
113113
LLVM_DEBUG(llvm::dbgs() << "rewriting " << embox << " to " << xbox << '\n');
114114
rewriter.replaceOp(embox, xbox.getOperation()->getResults());
115115
return mlir::success();
@@ -145,7 +145,7 @@ class EmboxConversion : public mlir::OpRewritePattern<fir::EmboxOp> {
145145
auto xbox = rewriter.create<fir::cg::XEmboxOp>(
146146
loc, embox.getType(), embox.getMemref(), shapeOpers, shiftOpers,
147147
sliceOpers, subcompOpers, substrOpers, embox.getTypeparams(),
148-
embox.getSourceBox());
148+
embox.getSourceBox(), embox.getAllocatorIdxAttr());
149149
LLVM_DEBUG(llvm::dbgs() << "rewriting " << embox << " to " << xbox << '\n');
150150
rewriter.replaceOp(embox, xbox.getOperation()->getResults());
151151
return mlir::success();

flang/runtime/allocator-registry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "allocator-registry.h"
9+
#include "flang/Runtime/allocator-registry.h"
1010
#include "terminator.h"
1111

1212
namespace Fortran::runtime {

flang/runtime/descriptor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
#include "flang/Runtime/descriptor.h"
1010
#include "ISO_Fortran_util.h"
11-
#include "allocator-registry.h"
1211
#include "derived.h"
1312
#include "memory.h"
1413
#include "stat.h"
1514
#include "terminator.h"
1615
#include "tools.h"
1716
#include "type-info.h"
17+
#include "flang/Runtime/allocator-registry.h"
1818
#include <cassert>
1919
#include <cstdlib>
2020
#include <cstring>

flang/test/Fir/embox.fir

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,27 @@ func.func @fir_dev_issue_1416(%arg0: !fir.ref<!fir.array<40x?xf32>>, %low: index
103103
fir.call @do_something(%3) : (!fir.box<!fir.array<?xf32>>) -> ()
104104
return
105105
}
106+
107+
// CHECK-LABEL: define void @_QPtest_allocator1()
108+
func.func @_QPtest_allocator1() {
109+
%c20 = arith.constant 20 : index
110+
%0 = fir.alloca !fir.array<20xi32> {bindc_name = "x", uniq_name = "_QFtest_sliceEx"}
111+
%1 = fir.shape %c20 : (index) -> !fir.shape<1>
112+
%3 = fir.embox %0(%1) {allocator_idx = 1 : i32} : (!fir.ref<!fir.array<20xi32>>, !fir.shape<1>) -> !fir.box<!fir.array<?xi32>>
113+
fir.call @_QPtest_callee(%3) : (!fir.box<!fir.array<?xi32>>) -> ()
114+
return
115+
}
116+
117+
// %{{.*}} = insertvalue { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } { ptr undef, i64 ptrtoint (ptr getelementptr (i32, ptr null, i32 1) to i64), i32 20240719, i8 1, i8 9, i8 0, i8 2, [1 x [3 x i64]] [[3 x i64] [i64 1, i64 20, i64 ptrtoint (ptr getelementptr (i32, ptr null, i32 1) to i64)]] }
118+
119+
// CHECK-LABEL: define void @_QPtest_allocator2()
120+
func.func @_QPtest_allocator2() {
121+
%c20 = arith.constant 20 : index
122+
%0 = fir.alloca !fir.array<20xi32> {bindc_name = "x", uniq_name = "_QFtest_sliceEx"}
123+
%1 = fir.shape %c20 : (index) -> !fir.shape<1>
124+
%3 = fir.embox %0(%1) {allocator_idx = 3 : i32} : (!fir.ref<!fir.array<20xi32>>, !fir.shape<1>) -> !fir.box<!fir.array<?xi32>>
125+
fir.call @_QPtest_callee(%3) : (!fir.box<!fir.array<?xi32>>) -> ()
126+
return
127+
}
128+
129+
// %{{.*}} = insertvalue { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } { ptr undef, i64 ptrtoint (ptr getelementptr (i32, ptr null, i32 1) to i64), i32 20240719, i8 1, i8 9, i8 0, i8 6, [1 x [3 x i64]] [[3 x i64] [i64 1, i64 20, i64 ptrtoint (ptr getelementptr (i32, ptr null, i32 1) to i64)]] }

0 commit comments

Comments
 (0)