Skip to content

[flang][cuda] Change induction variable from i32 to index for doconcurrent inside cuf kernel directive #129924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3150,10 +3150,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
loc, 1); // Use index type directly

// Ensure lb, ub, and step are of index type using fir.convert
mlir::Type indexType = builder->getIndexType();
lb = builder->create<fir::ConvertOp>(loc, indexType, lb);
ub = builder->create<fir::ConvertOp>(loc, indexType, ub);
step = builder->create<fir::ConvertOp>(loc, indexType, step);
lb = builder->create<fir::ConvertOp>(loc, idxTy, lb);
ub = builder->create<fir::ConvertOp>(loc, idxTy, ub);
step = builder->create<fir::ConvertOp>(loc, idxTy, step);

lbs.push_back(lb);
ubs.push_back(ub);
Expand All @@ -3163,18 +3162,14 @@ class FirConverter : public Fortran::lower::AbstractConverter {

// Handle induction variable
mlir::Value ivValue = getSymbolAddress(*name.symbol);
std::size_t ivTypeSize = name.symbol->size();
if (ivTypeSize == 0)
llvm::report_fatal_error("unexpected induction variable size");
mlir::Type ivTy = builder->getIntegerType(ivTypeSize * 8);

if (!ivValue) {
// DO CONCURRENT induction variables are not mapped yet since they are
// local to the DO CONCURRENT scope.
mlir::OpBuilder::InsertPoint insPt = builder->saveInsertionPoint();
builder->setInsertionPointToStart(builder->getAllocaBlock());
ivValue = builder->createTemporaryAlloc(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should map the iv symbol to the block argument directly so we don't need a temporary. This can be done in a separate patch.

loc, ivTy, toStringRef(name.symbol->name()));
loc, idxTy, toStringRef(name.symbol->name()));
builder->restoreInsertionPoint(insPt);
}

Expand All @@ -3186,7 +3181,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
// Bind the symbol to the declared variable
bindSymbol(*name.symbol, ivValue);
ivValues.push_back(ivValue);
ivTypes.push_back(ivTy);
ivTypes.push_back(idxTy);
ivLocs.push_back(loc);
}
} else {
Expand Down
14 changes: 7 additions & 7 deletions flang/test/Lower/CUDA/cuda-doconc.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ subroutine doconc1
end

! CHECK: func.func @_QPdoconc1() {
! CHECK: %[[DECL:.*]]:2 = hlfir.declare %{{.*}}#0 {uniq_name = "_QFdoconc1Ei"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
! CHECK: %[[DECL:.*]]:2 = hlfir.declare %{{.*}}#0 {uniq_name = "_QFdoconc1Ei"} : (!fir.ref<index>) -> (!fir.ref<index>, !fir.ref<index>)
! CHECK: cuf.kernel<<<*, *>>>
! CHECK: %{{.*}} = fir.load %[[DECL]]#0 : !fir.ref<i32>
! CHECK: %{{.*}} = fir.load %[[DECL]]#0 : !fir.ref<index>

subroutine doconc2
integer :: i, j, m, n
Expand All @@ -32,8 +32,8 @@ subroutine doconc2
end

! CHECK: func.func @_QPdoconc2() {
! CHECK: %[[DECLI:.*]]:2 = hlfir.declare %{{.*}}#0 {uniq_name = "_QFdoconc2Ei"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
! CHECK: %[[DECLJ:.*]]:2 = hlfir.declare %{{.*}}#0 {uniq_name = "_QFdoconc2Ej"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
! CHECK: cuf.kernel<<<*, *>>> (%arg0 : i32, %arg1 : i32) = (%{{.*}}, %{{.*}} : index, index) to (%{{.*}}, %{{.*}} : index, index) step (%{{.*}}, %{{.*}} : index, index) {
! CHECK: %{{.*}} = fir.load %[[DECLI]]#0 : !fir.ref<i32>
! CHECK: %{{.*}} = fir.load %[[DECLJ]]#0 : !fir.ref<i32>
! CHECK: %[[DECLI:.*]]:2 = hlfir.declare %{{.*}}#0 {uniq_name = "_QFdoconc2Ei"} : (!fir.ref<index>) -> (!fir.ref<index>, !fir.ref<index>)
! CHECK: %[[DECLJ:.*]]:2 = hlfir.declare %{{.*}}#0 {uniq_name = "_QFdoconc2Ej"} : (!fir.ref<index>) -> (!fir.ref<index>, !fir.ref<index>)
! CHECK: cuf.kernel<<<*, *>>> (%arg0 : index, %arg1 : index) = (%{{.*}}, %{{.*}} : index, index) to (%{{.*}}, %{{.*}} : index, index) step (%{{.*}}, %{{.*}} : index, index) {
! CHECK: %{{.*}} = fir.load %[[DECLI]]#0 : !fir.ref<index>
! CHECK: %{{.*}} = fir.load %[[DECLJ]]#0 : !fir.ref<index>