Skip to content

Conversation

@wangzpgi
Copy link
Contributor

Fixed an issue in genCUDAImplicitDataTransfer where creating an hlfir::Entity from a symbol address could fail when the address comes from a hlfir.declare operation. Fix is to check if the address comes from a hlfir.declare operation. If so, use the base value from the declare op when available. Falling back to the original address otherwise.

@wangzpgi wangzpgi requested a review from clementval May 10, 2025 22:47
@wangzpgi wangzpgi self-assigned this May 10, 2025
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels May 10, 2025
@llvmbot
Copy link
Member

llvmbot commented May 10, 2025

@llvm/pr-subscribers-flang-fir-hlfir

Author: Zhen Wang (wangzpgi)

Changes

Fixed an issue in genCUDAImplicitDataTransfer where creating an hlfir::Entity from a symbol address could fail when the address comes from a hlfir.declare operation. Fix is to check if the address comes from a hlfir.declare operation. If so, use the base value from the declare op when available. Falling back to the original address otherwise.


Full diff: https://github.com/llvm/llvm-project/pull/139414.diff

1 Files Affected:

  • (modified) flang/lib/Lower/Bridge.cpp (+7-1)
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 43375e84f21fa..bfe8898ebff3d 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -4778,7 +4778,13 @@ class FirConverter : public Fortran::lower::AbstractConverter {
               nbDeviceResidentObject <= 1 &&
               "Only one reference to the device resident object is supported");
           auto addr = getSymbolAddress(sym);
-          hlfir::Entity entity{addr};
+          mlir::Value baseValue;
+          if (auto declareOp = llvm::dyn_cast<hlfir::DeclareOp>(addr.getDefiningOp()))
+            baseValue = declareOp.getBase();
+          else
+            baseValue = addr;
+
+          hlfir::Entity entity{baseValue};
           auto [temp, cleanup] =
               hlfir::createTempFromMold(loc, builder, entity);
           auto needCleanup = fir::getIntIfConstant(cleanup);

@github-actions
Copy link

github-actions bot commented May 10, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

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

Can you make sure your new test check the actual change?

end subroutine

!CHECK-LABEL: func.func @_QPtestr2
!CHECK: %{{.*}} = cuf.alloc !fir.array<?x?xf32>, %{{.*}}, %{{.*}} : index, index {bindc_name = "ai4", data_attr = #cuf.cuda<managed>, uniq_name = "_QFtestr2Eai4"} -> !fir.ref<!fir.array<?x?xf32>>
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you check the actual change?

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

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

LGTM

@wangzpgi wangzpgi merged commit eef4b5a into llvm:main May 12, 2025
7 of 9 checks passed
@wangzpgi wangzpgi deleted the entity branch May 12, 2025 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:fir-hlfir flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants