Skip to content

Commit 526ec14

Browse files
clementvalgithub-actions[bot]
authored andcommitted
Automerge: [flang][cuda] Remove set_allocator_idx operation (#157747)
The allocator index is set from the component genre #157731 . There is no more need of an operation to set it at a later point.
2 parents b6069ec + 3e98021 commit 526ec14

File tree

14 files changed

+2
-344
lines changed

14 files changed

+2
-344
lines changed

flang-rt/lib/cuda/descriptor.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,6 @@ void RTDEF(CUFDescriptorCheckSection)(
6262
}
6363
}
6464

65-
void RTDEF(CUFSetAllocatorIndex)(
66-
Descriptor *desc, int index, const char *sourceFile, int sourceLine) {
67-
if (!desc) {
68-
Terminator terminator{sourceFile, sourceLine};
69-
terminator.Crash("descriptor is null");
70-
}
71-
desc->SetAllocIdx(index);
72-
}
73-
7465
RT_EXT_API_GROUP_END
7566
}
7667
} // namespace Fortran::runtime::cuda

flang-rt/unittests/Runtime/CUDA/AllocatorCUF.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,3 @@ TEST(AllocatableCUFTest, DescriptorAllocationTest) {
7272
EXPECT_TRUE(desc != nullptr);
7373
RTNAME(CUFFreeDescriptor)(desc);
7474
}
75-
76-
TEST(AllocatableCUFTest, CUFSetAllocatorIndex) {
77-
using Fortran::common::TypeCategory;
78-
RTNAME(CUFRegisterAllocator)();
79-
// REAL(4), DEVICE, ALLOCATABLE :: a(:)
80-
auto a{createAllocatable(TypeCategory::Real, 4)};
81-
EXPECT_EQ((int)kDefaultAllocator, a->GetAllocIdx());
82-
RTNAME(CUFSetAllocatorIndex)(
83-
a.get(), kDeviceAllocatorPos, __FILE__, __LINE__);
84-
EXPECT_EQ((int)kDeviceAllocatorPos, a->GetAllocIdx());
85-
}

flang/include/flang/Lower/CUDA.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ static inline unsigned getAllocatorIdx(const Fortran::semantics::Symbol &sym) {
4747
return kDefaultAllocator;
4848
}
4949

50-
void initializeDeviceComponentAllocator(
51-
Fortran::lower::AbstractConverter &converter,
52-
const Fortran::semantics::Symbol &sym, const fir::MutableBoxValue &box);
53-
5450
mlir::Type gatherDeviceComponentCoordinatesAndType(
5551
fir::FirOpBuilder &builder, mlir::Location loc,
5652
const Fortran::semantics::Symbol &sym, fir::RecordType recTy,

flang/include/flang/Optimizer/Builder/Runtime/CUDA/Descriptor.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ void genSyncGlobalDescriptor(fir::FirOpBuilder &builder, mlir::Location loc,
3131
void genDescriptorCheckSection(fir::FirOpBuilder &builder, mlir::Location loc,
3232
mlir::Value desc);
3333

34-
/// Generate runtime call to set the allocator index in the descriptor.
35-
void genSetAllocatorIndex(fir::FirOpBuilder &builder, mlir::Location loc,
36-
mlir::Value desc, mlir::Value index);
37-
3834
} // namespace fir::runtime::cuda
3935

4036
#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_CUDA_DESCRIPTOR_H_

flang/include/flang/Optimizer/Dialect/CUF/CUFOps.td

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -388,25 +388,4 @@ def cuf_StreamCastOp : cuf_Op<"stream_cast", [NoMemoryEffect]> {
388388
let hasVerifier = 1;
389389
}
390390

391-
def cuf_SetAllocatorIndexOp : cuf_Op<"set_allocator_idx", []> {
392-
let summary = "Set the allocator index in a descriptor";
393-
394-
let description = [{
395-
Allocator index in the Fortran descriptor is used to retrived the correct
396-
CUDA allocator to allocate the memory on the device.
397-
In many cases the allocator index is set when the descriptor is created. For
398-
device components, the descriptor is part of the derived-type itself and
399-
needs to be set after the derived-type is allocated in managed memory.
400-
}];
401-
402-
let arguments = (ins Arg<fir_ReferenceType, "", [MemRead, MemWrite]>:$box,
403-
cuf_DataAttributeAttr:$data_attr);
404-
405-
let assemblyFormat = [{
406-
$box `:` qualified(type($box)) attr-dict
407-
}];
408-
409-
let hasVerifier = 1;
410-
}
411-
412391
#endif // FORTRAN_DIALECT_CUF_CUF_OPS

flang/include/flang/Runtime/CUDA/descriptor.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ void RTDECL(CUFSyncGlobalDescriptor)(
4141
void RTDECL(CUFDescriptorCheckSection)(
4242
const Descriptor *, const char *sourceFile = nullptr, int sourceLine = 0);
4343

44-
/// Set the allocator index with the provided value.
45-
void RTDECL(CUFSetAllocatorIndex)(Descriptor *, int index,
46-
const char *sourceFile = nullptr, int sourceLine = 0);
47-
4844
} // extern "C"
4945

5046
} // namespace Fortran::runtime::cuda

flang/lib/Lower/Allocatable.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,6 @@ class AllocateStmtHelper {
450450
if (alloc.getSymbol().test(Fortran::semantics::Symbol::Flag::AccDeclare))
451451
Fortran::lower::attachDeclarePostAllocAction(converter, builder,
452452
alloc.getSymbol());
453-
if (Fortran::semantics::HasCUDAComponent(alloc.getSymbol()))
454-
Fortran::lower::initializeDeviceComponentAllocator(
455-
converter, alloc.getSymbol(), box);
456453
}
457454

458455
void setPinnedToFalse() {

flang/lib/Lower/CUDA.cpp

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -17,95 +17,6 @@
1717

1818
#define DEBUG_TYPE "flang-lower-cuda"
1919

20-
void Fortran::lower::initializeDeviceComponentAllocator(
21-
Fortran::lower::AbstractConverter &converter,
22-
const Fortran::semantics::Symbol &sym, const fir::MutableBoxValue &box) {
23-
if (const auto *details{
24-
sym.GetUltimate()
25-
.detailsIf<Fortran::semantics::ObjectEntityDetails>()}) {
26-
const Fortran::semantics::DeclTypeSpec *type{details->type()};
27-
const Fortran::semantics::DerivedTypeSpec *derived{type ? type->AsDerived()
28-
: nullptr};
29-
if (derived) {
30-
if (!FindCUDADeviceAllocatableUltimateComponent(*derived))
31-
return; // No device components.
32-
33-
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
34-
mlir::Location loc = converter.getCurrentLocation();
35-
36-
mlir::Type baseTy = fir::unwrapRefType(box.getAddr().getType());
37-
38-
// Only pointer and allocatable needs post allocation initialization
39-
// of components descriptors.
40-
if (!fir::isAllocatableType(baseTy) && !fir::isPointerType(baseTy))
41-
return;
42-
43-
// Extract the derived type.
44-
mlir::Type ty = fir::getDerivedType(baseTy);
45-
auto recTy = mlir::dyn_cast<fir::RecordType>(ty);
46-
assert(recTy && "expected fir::RecordType");
47-
48-
if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(baseTy))
49-
baseTy = boxTy.getEleTy();
50-
baseTy = fir::unwrapRefType(baseTy);
51-
52-
Fortran::semantics::UltimateComponentIterator components{*derived};
53-
mlir::Value loadedBox = fir::LoadOp::create(builder, loc, box.getAddr());
54-
mlir::Value addr;
55-
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(baseTy)) {
56-
mlir::Type idxTy = builder.getIndexType();
57-
mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
58-
mlir::Value zero = builder.createIntegerConstant(loc, idxTy, 0);
59-
llvm::SmallVector<fir::DoLoopOp> loops;
60-
llvm::SmallVector<mlir::Value> indices;
61-
llvm::SmallVector<mlir::Value> extents;
62-
for (unsigned i = 0; i < seqTy.getDimension(); ++i) {
63-
mlir::Value dim = builder.createIntegerConstant(loc, idxTy, i);
64-
auto dimInfo = fir::BoxDimsOp::create(builder, loc, idxTy, idxTy,
65-
idxTy, loadedBox, dim);
66-
mlir::Value lbub = mlir::arith::AddIOp::create(
67-
builder, loc, dimInfo.getResult(0), dimInfo.getResult(1));
68-
mlir::Value ext =
69-
mlir::arith::SubIOp::create(builder, loc, lbub, one);
70-
mlir::Value cmp = mlir::arith::CmpIOp::create(
71-
builder, loc, mlir::arith::CmpIPredicate::sgt, ext, zero);
72-
ext = mlir::arith::SelectOp::create(builder, loc, cmp, ext, zero);
73-
extents.push_back(ext);
74-
75-
auto loop = fir::DoLoopOp::create(
76-
builder, loc, dimInfo.getResult(0), dimInfo.getResult(1),
77-
dimInfo.getResult(2), /*isUnordered=*/true,
78-
/*finalCount=*/false, mlir::ValueRange{});
79-
loops.push_back(loop);
80-
indices.push_back(loop.getInductionVar());
81-
builder.setInsertionPointToStart(loop.getBody());
82-
}
83-
mlir::Value boxAddr = fir::BoxAddrOp::create(builder, loc, loadedBox);
84-
auto shape = fir::ShapeOp::create(builder, loc, extents);
85-
addr = fir::ArrayCoorOp::create(
86-
builder, loc, fir::ReferenceType::get(recTy), boxAddr, shape,
87-
/*slice=*/mlir::Value{}, indices, /*typeparms=*/mlir::ValueRange{});
88-
} else {
89-
addr = fir::BoxAddrOp::create(builder, loc, loadedBox);
90-
}
91-
for (const auto &compSym : components) {
92-
if (Fortran::semantics::IsDeviceAllocatable(compSym)) {
93-
llvm::SmallVector<mlir::Value> coord;
94-
mlir::Type fieldTy = gatherDeviceComponentCoordinatesAndType(
95-
builder, loc, compSym, recTy, coord);
96-
assert(coord.size() == 1 && "expect one coordinate");
97-
mlir::Value comp = fir::CoordinateOp::create(
98-
builder, loc, builder.getRefType(fieldTy), addr, coord[0]);
99-
cuf::DataAttributeAttr dataAttr =
100-
Fortran::lower::translateSymbolCUFDataAttribute(
101-
builder.getContext(), compSym);
102-
cuf::SetAllocatorIndexOp::create(builder, loc, comp, dataAttr);
103-
}
104-
}
105-
}
106-
}
107-
}
108-
10920
mlir::Type Fortran::lower::gatherDeviceComponentCoordinatesAndType(
11021
fir::FirOpBuilder &builder, mlir::Location loc,
11122
const Fortran::semantics::Symbol &sym, fir::RecordType recTy,

flang/lib/Lower/ConvertVariable.cpp

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -786,62 +786,6 @@ static mlir::Value createNewLocal(Fortran::lower::AbstractConverter &converter,
786786
return res;
787787
}
788788

789-
/// Device allocatable components in a derived-type don't have the correct
790-
/// allocator index in their descriptor when they are created. After
791-
/// initialization, cuf.set_allocator_idx operations are inserted to set the
792-
/// correct allocator index for each device component.
793-
static void
794-
initializeDeviceComponentAllocator(Fortran::lower::AbstractConverter &converter,
795-
const Fortran::semantics::Symbol &symbol,
796-
Fortran::lower::SymMap &symMap) {
797-
if (const auto *details{
798-
symbol.GetUltimate()
799-
.detailsIf<Fortran::semantics::ObjectEntityDetails>()}) {
800-
const Fortran::semantics::DeclTypeSpec *type{details->type()};
801-
const Fortran::semantics::DerivedTypeSpec *derived{type ? type->AsDerived()
802-
: nullptr};
803-
if (derived) {
804-
if (!FindCUDADeviceAllocatableUltimateComponent(*derived))
805-
return; // No device components.
806-
807-
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
808-
mlir::Location loc = converter.getCurrentLocation();
809-
810-
fir::ExtendedValue exv =
811-
converter.getSymbolExtendedValue(symbol.GetUltimate(), &symMap);
812-
mlir::Type baseTy = fir::unwrapRefType(fir::getBase(exv).getType());
813-
if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(baseTy))
814-
baseTy = boxTy.getEleTy();
815-
baseTy = fir::unwrapRefType(baseTy);
816-
817-
if (fir::isAllocatableType(fir::getBase(exv).getType()) ||
818-
fir::isPointerType(fir::getBase(exv).getType()))
819-
return; // Allocator index need to be set after allocation.
820-
821-
auto recTy =
822-
mlir::dyn_cast<fir::RecordType>(fir::unwrapSequenceType(baseTy));
823-
assert(recTy && "expected fir::RecordType");
824-
825-
Fortran::semantics::UltimateComponentIterator components{*derived};
826-
for (const auto &sym : components) {
827-
if (Fortran::semantics::IsDeviceAllocatable(sym)) {
828-
llvm::SmallVector<mlir::Value> coord;
829-
mlir::Type fieldTy =
830-
Fortran::lower::gatherDeviceComponentCoordinatesAndType(
831-
builder, loc, sym, recTy, coord);
832-
mlir::Value base = fir::getBase(exv);
833-
mlir::Value comp = fir::CoordinateOp::create(
834-
builder, loc, builder.getRefType(fieldTy), base, coord);
835-
cuf::DataAttributeAttr dataAttr =
836-
Fortran::lower::translateSymbolCUFDataAttribute(
837-
builder.getContext(), sym);
838-
cuf::SetAllocatorIndexOp::create(builder, loc, comp, dataAttr);
839-
}
840-
}
841-
}
842-
}
843-
}
844-
845789
/// Must \p var be default initialized at runtime when entering its scope.
846790
static bool
847791
mustBeDefaultInitializedAtRuntime(const Fortran::lower::pft::Variable &var) {
@@ -1164,9 +1108,6 @@ static void instantiateLocal(Fortran::lower::AbstractConverter &converter,
11641108
if (mustBeDefaultInitializedAtRuntime(var))
11651109
Fortran::lower::defaultInitializeAtRuntime(converter, var.getSymbol(),
11661110
symMap);
1167-
if (converter.getFoldingContext().languageFeatures().IsEnabled(
1168-
Fortran::common::LanguageFeature::CUDA))
1169-
initializeDeviceComponentAllocator(converter, var.getSymbol(), symMap);
11701111
auto *builder = &converter.getFirOpBuilder();
11711112
if (needCUDAAlloc(var.getSymbol()) &&
11721113
!cuf::isCUDADeviceContext(builder->getRegion())) {
@@ -1426,9 +1367,6 @@ static void instantiateAlias(Fortran::lower::AbstractConverter &converter,
14261367
if (mustBeDefaultInitializedAtRuntime(var))
14271368
Fortran::lower::defaultInitializeAtRuntime(converter, var.getSymbol(),
14281369
symMap);
1429-
if (converter.getFoldingContext().languageFeatures().IsEnabled(
1430-
Fortran::common::LanguageFeature::CUDA))
1431-
initializeDeviceComponentAllocator(converter, var.getSymbol(), symMap);
14321370
}
14331371

14341372
//===--------------------------------------------------------------===//

flang/lib/Optimizer/Builder/Runtime/CUDA/Descriptor.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,3 @@ void fir::runtime::cuda::genDescriptorCheckSection(fir::FirOpBuilder &builder,
4747
builder, loc, fTy, desc, sourceFile, sourceLine)};
4848
fir::CallOp::create(builder, loc, func, args);
4949
}
50-
51-
void fir::runtime::cuda::genSetAllocatorIndex(fir::FirOpBuilder &builder,
52-
mlir::Location loc,
53-
mlir::Value desc,
54-
mlir::Value index) {
55-
mlir::func::FuncOp func =
56-
fir::runtime::getRuntimeFunc<mkRTKey(CUFSetAllocatorIndex)>(loc, builder);
57-
auto fTy = func.getFunctionType();
58-
mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
59-
mlir::Value sourceLine =
60-
fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
61-
llvm::SmallVector<mlir::Value> args{fir::runtime::createArguments(
62-
builder, loc, fTy, desc, index, sourceFile, sourceLine)};
63-
fir::CallOp::create(builder, loc, func, args);
64-
}

0 commit comments

Comments
 (0)