Skip to content

[flang][cuda] Avoid hlfir.declare verifier error when creating temps #89984

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
Apr 25, 2024

Conversation

clementval
Copy link
Contributor

When creating temporaries for implicit transfer, the newly create hlfir.declare operation was missing some information like the shape and the verifier was throwing an error. Fix it by making sure we have an ExtendedValue when calling addSymbol to register the temp.

error: loc("cuda-data-transfer.cuf":67:22): 'hlfir.declare' op of array entity
with a raw address base must have a shape operand that is a shape or shapeshift

Thanks @jeanPerier for the advice!

FYI @ImanHosseini

When creating temporaries for implicit transfer, the newly create hlfir.declare
operation was missing some information like the shape and the verifier was
throwing an error. Fix it by making sure we have an ExtendedValue when calling
addSymbol to register the temp.

```
error: loc("cuda-data-transfer.cuf":67:22): 'hlfir.declare' op of array entity
with a raw address base must have a shape operand that is a shape or shapeshift
```
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Apr 24, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 24, 2024

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

Author: Valentin Clement (バレンタイン クレメン) (clementval)

Changes

When creating temporaries for implicit transfer, the newly create hlfir.declare operation was missing some information like the shape and the verifier was throwing an error. Fix it by making sure we have an ExtendedValue when calling addSymbol to register the temp.

error: loc("cuda-data-transfer.cuf":67:22): 'hlfir.declare' op of array entity
with a raw address base must have a shape operand that is a shape or shapeshift

Thanks @jeanPerier for the advice!

FYI @ImanHosseini


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

2 Files Affected:

  • (modified) flang/lib/Lower/Bridge.cpp (+3-1)
  • (modified) flang/test/Lower/CUDA/cuda-data-transfer.cuf (+1-1)
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 8b62fe8c022f80..7fd42807e19f61 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -3794,7 +3794,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
           auto needCleanup = fir::getIntIfConstant(cleanup);
           if (needCleanup && *needCleanup)
             temps.push_back(temp);
-          addSymbol(sym, temp, /*forced=*/true);
+          addSymbol(sym,
+                    hlfir::translateToExtendedValue(loc, builder, temp).first,
+                    /*forced=*/true);
           builder.create<fir::CUDADataTransferOp>(loc, addr, temp,
                                                   transferKindAttr);
           ++nbDeviceResidentObject;
diff --git a/flang/test/Lower/CUDA/cuda-data-transfer.cuf b/flang/test/Lower/CUDA/cuda-data-transfer.cuf
index 4ebd736315bcbc..2ae3a89d60b0b7 100644
--- a/flang/test/Lower/CUDA/cuda-data-transfer.cuf
+++ b/flang/test/Lower/CUDA/cuda-data-transfer.cuf
@@ -98,7 +98,7 @@ end
 
 ! CHECK: %[[TEMP:.*]] = fir.allocmem !fir.array<10xi32> {bindc_name = ".tmp", uniq_name = ""}
 ! CHECK: %[[DECL_TEMP:.*]]:2 = hlfir.declare %[[TEMP]](%{{.*}}) {uniq_name = ".tmp"} : (!fir.heap<!fir.array<10xi32>>, !fir.shape<1>) -> (!fir.heap<!fir.array<10xi32>>, !fir.heap<!fir.array<10xi32>>)
-! CHECK: %[[ADEV_TEMP:.*]]:2 = hlfir.declare %21#0 {cuda_attr = #fir.cuda<device>, uniq_name = "_QFsub2Eadev"} : (!fir.heap<!fir.array<10xi32>>) -> (!fir.heap<!fir.array<10xi32>>, !fir.heap<!fir.array<10xi32>>)
+! CHECK: %[[ADEV_TEMP:.*]]:2 = hlfir.declare %[[DECL_TEMP]]#1(%{{.*}}) {cuda_attr = #fir.cuda<device>, uniq_name = "_QFsub2Eadev"} : (!fir.heap<!fir.array<10xi32>>, !fir.shape<1>) -> (!fir.heap<!fir.array<10xi32>>, !fir.heap<!fir.array<10xi32>>)
 ! CHECK: fir.cuda_data_transfer %[[ADEV]]#1 to %[[DECL_TEMP]]#0 {transfer_kind = #fir.cuda_transfer<device_host>} : !fir.ref<!fir.array<10xi32>>, !fir.heap<!fir.array<10xi32>>
 ! CHECK: %[[ELEMENTAL:.*]] = hlfir.elemental %{{.*}} unordered : (!fir.shape<1>) -> !hlfir.expr<10xi32>
 ! CHECK: hlfir.assign %[[ELEMENTAL]] to %[[BHOST]]#0 : !hlfir.expr<10xi32>, !fir.ref<!fir.array<10xi32>>

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

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

LGTM, the implicit mlir::Value conversion to fir::ExtendedValue is a double edged sword....

@clementval clementval merged commit 09cdfd6 into llvm:main Apr 25, 2024
@clementval clementval deleted the cuda_extra_declare branch April 25, 2024 15:50
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