Skip to content

Commit 7d3134f

Browse files
authored
Revert "[flang][cuda] Set the allocator of derived type component after allocation" (#152402)
Reverts #152379 Buildbot failure https://lab.llvm.org/buildbot/#/builders/207/builds/4905
1 parent e83abd7 commit 7d3134f

File tree

7 files changed

+89
-205
lines changed

7 files changed

+89
-205
lines changed

flang/include/flang/Lower/CUDA.h renamed to flang/include/flang/Lower/Cuda.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- Lower/CUDA.h -- CUDA Fortran utilities ------------------*- C++ -*-===//
1+
//===-- Lower/Cuda.h -- Cuda Fortran utilities ------------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -15,7 +15,6 @@
1515

1616
#include "flang/Optimizer/Builder/FIRBuilder.h"
1717
#include "flang/Optimizer/Dialect/CUF/CUFOps.h"
18-
#include "flang/Runtime/allocator-registry-consts.h"
1918
#include "flang/Semantics/tools.h"
2019
#include "mlir/Dialect/Func/IR/FuncOps.h"
2120
#include "mlir/Dialect/OpenACC/OpenACC.h"
@@ -38,15 +37,6 @@ static inline unsigned getAllocatorIdx(const Fortran::semantics::Symbol &sym) {
3837
return kDefaultAllocator;
3938
}
4039

41-
void initializeDeviceComponentAllocator(
42-
Fortran::lower::AbstractConverter &converter,
43-
const Fortran::semantics::Symbol &sym, const fir::MutableBoxValue &box);
44-
45-
mlir::Type gatherDeviceComponentCoordinatesAndType(
46-
fir::FirOpBuilder &builder, mlir::Location loc,
47-
const Fortran::semantics::Symbol &sym, fir::RecordType recTy,
48-
llvm::SmallVector<mlir::Value> &coordinates);
49-
5040
} // end namespace Fortran::lower
5141

5242
#endif // FORTRAN_LOWER_CUDA_H

flang/lib/Lower/Allocatable.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
#include "flang/Lower/Allocatable.h"
1414
#include "flang/Evaluate/tools.h"
1515
#include "flang/Lower/AbstractConverter.h"
16-
#include "flang/Lower/CUDA.h"
1716
#include "flang/Lower/ConvertType.h"
1817
#include "flang/Lower/ConvertVariable.h"
18+
#include "flang/Lower/Cuda.h"
1919
#include "flang/Lower/IterationSpace.h"
2020
#include "flang/Lower/Mangler.h"
2121
#include "flang/Lower/OpenACC.h"
@@ -445,14 +445,10 @@ class AllocateStmtHelper {
445445
/*mustBeHeap=*/true);
446446
}
447447

448-
void postAllocationAction(const Allocation &alloc,
449-
const fir::MutableBoxValue &box) {
448+
void postAllocationAction(const Allocation &alloc) {
450449
if (alloc.getSymbol().test(Fortran::semantics::Symbol::Flag::AccDeclare))
451450
Fortran::lower::attachDeclarePostAllocAction(converter, builder,
452451
alloc.getSymbol());
453-
if (Fortran::semantics::HasCUDAComponent(alloc.getSymbol()))
454-
Fortran::lower::initializeDeviceComponentAllocator(
455-
converter, alloc.getSymbol(), box);
456452
}
457453

458454
void setPinnedToFalse() {
@@ -485,7 +481,7 @@ class AllocateStmtHelper {
485481
// Pointers must use PointerAllocate so that their deallocations
486482
// can be validated.
487483
genInlinedAllocation(alloc, box);
488-
postAllocationAction(alloc, box);
484+
postAllocationAction(alloc);
489485
setPinnedToFalse();
490486
return;
491487
}
@@ -508,7 +504,7 @@ class AllocateStmtHelper {
508504
genCudaAllocate(builder, loc, box, errorManager, alloc.getSymbol());
509505
}
510506
fir::factory::syncMutableBoxFromIRBox(builder, loc, box);
511-
postAllocationAction(alloc, box);
507+
postAllocationAction(alloc);
512508
errorManager.assignStat(builder, loc, stat);
513509
}
514510

@@ -651,7 +647,7 @@ class AllocateStmtHelper {
651647
setPinnedToFalse();
652648
}
653649
fir::factory::syncMutableBoxFromIRBox(builder, loc, box);
654-
postAllocationAction(alloc, box);
650+
postAllocationAction(alloc);
655651
errorManager.assignStat(builder, loc, stat);
656652
}
657653

flang/lib/Lower/Bridge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
#include "flang/Lower/Bridge.h"
1414

1515
#include "flang/Lower/Allocatable.h"
16-
#include "flang/Lower/CUDA.h"
1716
#include "flang/Lower/CallInterface.h"
1817
#include "flang/Lower/Coarray.h"
1918
#include "flang/Lower/ConvertCall.h"
2019
#include "flang/Lower/ConvertExpr.h"
2120
#include "flang/Lower/ConvertExprToHLFIR.h"
2221
#include "flang/Lower/ConvertType.h"
2322
#include "flang/Lower/ConvertVariable.h"
23+
#include "flang/Lower/Cuda.h"
2424
#include "flang/Lower/DirectivesCommon.h"
2525
#include "flang/Lower/HostAssociations.h"
2626
#include "flang/Lower/IO.h"

flang/lib/Lower/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ add_flang_library(FortranLower
1515
ConvertProcedureDesignator.cpp
1616
ConvertType.cpp
1717
ConvertVariable.cpp
18-
CUDA.cpp
1918
CustomIntrinsicCall.cpp
2019
HlfirIntrinsics.cpp
2120
HostAssociations.cpp

flang/lib/Lower/CUDA.cpp

Lines changed: 0 additions & 148 deletions
This file was deleted.

flang/lib/Lower/ConvertVariable.cpp

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
#include "flang/Lower/AbstractConverter.h"
1515
#include "flang/Lower/Allocatable.h"
1616
#include "flang/Lower/BoxAnalyzer.h"
17-
#include "flang/Lower/CUDA.h"
1817
#include "flang/Lower/CallInterface.h"
1918
#include "flang/Lower/ConvertConstant.h"
2019
#include "flang/Lower/ConvertExpr.h"
2120
#include "flang/Lower/ConvertExprToHLFIR.h"
2221
#include "flang/Lower/ConvertProcedureDesignator.h"
22+
#include "flang/Lower/Cuda.h"
2323
#include "flang/Lower/Mangler.h"
2424
#include "flang/Lower/PFTBuilder.h"
2525
#include "flang/Lower/StatementContext.h"
@@ -814,24 +814,81 @@ initializeDeviceComponentAllocator(Fortran::lower::AbstractConverter &converter,
814814
baseTy = boxTy.getEleTy();
815815
baseTy = fir::unwrapRefType(baseTy);
816816

817-
if (fir::isAllocatableType(fir::getBase(exv).getType()) ||
818-
fir::isPointerType(fir::getBase(exv).getType()))
817+
if (mlir::isa<fir::SequenceType>(baseTy) &&
818+
(fir::isAllocatableType(fir::getBase(exv).getType()) ||
819+
fir::isPointerType(fir::getBase(exv).getType())))
819820
return; // Allocator index need to be set after allocation.
820821

821822
auto recTy =
822823
mlir::dyn_cast<fir::RecordType>(fir::unwrapSequenceType(baseTy));
823824
assert(recTy && "expected fir::RecordType");
824825

826+
llvm::SmallVector<mlir::Value> coordinates;
825827
Fortran::semantics::UltimateComponentIterator components{*derived};
826828
for (const auto &sym : components) {
827829
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);
830+
unsigned fieldIdx = recTy.getFieldIndex(sym.name().ToString());
831+
mlir::Type fieldTy;
832+
llvm::SmallVector<mlir::Value> coordinates;
833+
834+
if (fieldIdx != std::numeric_limits<unsigned>::max()) {
835+
// Field found in the base record type.
836+
auto fieldName = recTy.getTypeList()[fieldIdx].first;
837+
fieldTy = recTy.getTypeList()[fieldIdx].second;
838+
mlir::Value fieldIndex = fir::FieldIndexOp::create(
839+
builder, loc, fir::FieldType::get(fieldTy.getContext()),
840+
fieldName, recTy,
841+
/*typeParams=*/mlir::ValueRange{});
842+
coordinates.push_back(fieldIndex);
843+
} else {
844+
// Field not found in base record type, search in potential
845+
// record type components.
846+
for (auto component : recTy.getTypeList()) {
847+
if (auto childRecTy =
848+
mlir::dyn_cast<fir::RecordType>(component.second)) {
849+
fieldIdx = childRecTy.getFieldIndex(sym.name().ToString());
850+
if (fieldIdx != std::numeric_limits<unsigned>::max()) {
851+
mlir::Value parentFieldIndex = fir::FieldIndexOp::create(
852+
builder, loc,
853+
fir::FieldType::get(childRecTy.getContext()),
854+
component.first, recTy,
855+
/*typeParams=*/mlir::ValueRange{});
856+
coordinates.push_back(parentFieldIndex);
857+
auto fieldName = childRecTy.getTypeList()[fieldIdx].first;
858+
fieldTy = childRecTy.getTypeList()[fieldIdx].second;
859+
mlir::Value childFieldIndex = fir::FieldIndexOp::create(
860+
builder, loc, fir::FieldType::get(fieldTy.getContext()),
861+
fieldName, childRecTy,
862+
/*typeParams=*/mlir::ValueRange{});
863+
coordinates.push_back(childFieldIndex);
864+
break;
865+
}
866+
}
867+
}
868+
}
869+
870+
if (coordinates.empty())
871+
TODO(loc, "device resident component in complex derived-type "
872+
"hierarchy");
873+
832874
mlir::Value base = fir::getBase(exv);
833-
mlir::Value comp = fir::CoordinateOp::create(
834-
builder, loc, builder.getRefType(fieldTy), base, coord);
875+
mlir::Value comp;
876+
if (mlir::isa<fir::BaseBoxType>(fir::unwrapRefType(base.getType()))) {
877+
mlir::Value box = fir::LoadOp::create(builder, loc, base);
878+
mlir::Value addr = fir::BoxAddrOp::create(builder, loc, box);
879+
llvm::SmallVector<mlir::Value> lenParams;
880+
assert(coordinates.size() == 1 && "expect one coordinate");
881+
auto field = mlir::dyn_cast<fir::FieldIndexOp>(
882+
coordinates[0].getDefiningOp());
883+
comp = hlfir::DesignateOp::create(
884+
builder, loc, builder.getRefType(fieldTy), addr,
885+
/*component=*/field.getFieldName(),
886+
/*componentShape=*/mlir::Value{},
887+
hlfir::DesignateOp::Subscripts{});
888+
} else {
889+
comp = fir::CoordinateOp::create(
890+
builder, loc, builder.getRefType(fieldTy), base, coordinates);
891+
}
835892
cuf::DataAttributeAttr dataAttr =
836893
Fortran::lower::translateSymbolCUFDataAttribute(
837894
builder.getContext(), sym);

0 commit comments

Comments
 (0)