[flang-commits] [flang] [llvm] [flang][cuda] Allow host variables to be defined in device subprograms under -gpu=unified (PR #192118)
Zhen Wang via flang-commits
flang-commits at lists.llvm.org
Tue Apr 14 12:56:14 PDT 2026
https://github.com/wangzpgi updated https://github.com/llvm/llvm-project/pull/192118
>From e044d0acbee4eed3b27b45c043fc12602c552c79 Mon Sep 17 00:00:00 2001
From: Zhen Wang <zhenw at nvidia.com>
Date: Tue, 14 Apr 2026 12:40:22 -0700
Subject: [PATCH 1/2] Allow host variables to be defined in device subprograms
under -gpu=unified
---
device_data.mod | 24 ++++++++++++++++++++++++
flang/lib/Semantics/definable.cpp | 4 +++-
flang/test/Semantics/cuf26.cuf | 15 +++++++++++++++
3 files changed, 42 insertions(+), 1 deletion(-)
create mode 100644 device_data.mod
create mode 100644 flang/test/Semantics/cuf26.cuf
diff --git a/device_data.mod b/device_data.mod
new file mode 100644
index 0000000000000..9e77d622d2fdb
--- /dev/null
+++ b/device_data.mod
@@ -0,0 +1,24 @@
+!mod$ v1 sum:4742aa75f60f1f4e
+!need$ c8dda17ea6314235 i __cuda_builtins
+!need$ 3537609d44c937af i cudadevice
+module device_data
+real(4),constant::d1(1_8:10_8)
+real(4),constant::d2(1_8:10_8,1_8:10_8)
+real(4),constant::d3(1_8:10_8)
+real(4),constant::d4(1_8:10_8)
+real(4),constant::d5
+real(4),constant::d11(1_8:10_8)
+real(4),constant::d12(1_8:10_8,1_8:10_8)
+real(4),constant::d13(1_8:10_8)
+real(4),constant::d14(1_8:10_8)
+real(4),constant::d15(1_8:10_8)
+real(4),constant::d16
+real(4),constant::d21(1_8:10_8)
+real(4),constant::d22(1_8:10_8,1_8:10_8)
+real(4),constant::d23(1_8:10_8)
+real(4),constant::d24(1_8:10_8)
+real(4),constant::d25
+contains
+attributes(global) subroutine init()
+end
+end
diff --git a/flang/lib/Semantics/definable.cpp b/flang/lib/Semantics/definable.cpp
index a82ca80ddd1b8..d6a654683001f 100644
--- a/flang/lib/Semantics/definable.cpp
+++ b/flang/lib/Semantics/definable.cpp
@@ -171,7 +171,9 @@ static std::optional<parser::Message> WhyNotDefinableBase(parser::CharBlock at,
"'%s' is not device or managed or shared data and is not definable in a device subprogram"_err_en_US,
original);
}
- } else if (!isOwnedByDeviceCode) {
+ } else if (!isOwnedByDeviceCode &&
+ !scope.context().languageFeatures().IsEnabled(
+ common::LanguageFeature::CudaUnified)) {
return BlameSymbol(at,
"'%s' is a host variable and is not definable in a device subprogram"_err_en_US,
original);
diff --git a/flang/test/Semantics/cuf26.cuf b/flang/test/Semantics/cuf26.cuf
new file mode 100644
index 0000000000000..5fb1c5808da75
--- /dev/null
+++ b/flang/test/Semantics/cuf26.cuf
@@ -0,0 +1,15 @@
+! RUN: bbc -emit-hlfir -fcuda -gpu=unified %s -o /dev/null
+
+! Test that host module variables are definable in device subprograms
+! when -gpu=unified is enabled (unified memory).
+
+module mtests
+ integer :: mx
+contains
+ attributes(global) subroutine g1(a, m)
+ integer :: a(*), m, i
+ i = threadIdx%x
+ a(i) = m + i
+ if (i .eq. 1) mx = a(i)
+ end subroutine
+end module mtests
>From 72b749f0345f0cdedc40d797c17ef9804e936506 Mon Sep 17 00:00:00 2001
From: Zhen Wang <zhenw at nvidia.com>
Date: Tue, 14 Apr 2026 12:44:00 -0700
Subject: [PATCH 2/2] remove unnecessary .mod file
---
device_data.mod | 24 ------------------------
1 file changed, 24 deletions(-)
delete mode 100644 device_data.mod
diff --git a/device_data.mod b/device_data.mod
deleted file mode 100644
index 9e77d622d2fdb..0000000000000
--- a/device_data.mod
+++ /dev/null
@@ -1,24 +0,0 @@
-!mod$ v1 sum:4742aa75f60f1f4e
-!need$ c8dda17ea6314235 i __cuda_builtins
-!need$ 3537609d44c937af i cudadevice
-module device_data
-real(4),constant::d1(1_8:10_8)
-real(4),constant::d2(1_8:10_8,1_8:10_8)
-real(4),constant::d3(1_8:10_8)
-real(4),constant::d4(1_8:10_8)
-real(4),constant::d5
-real(4),constant::d11(1_8:10_8)
-real(4),constant::d12(1_8:10_8,1_8:10_8)
-real(4),constant::d13(1_8:10_8)
-real(4),constant::d14(1_8:10_8)
-real(4),constant::d15(1_8:10_8)
-real(4),constant::d16
-real(4),constant::d21(1_8:10_8)
-real(4),constant::d22(1_8:10_8,1_8:10_8)
-real(4),constant::d23(1_8:10_8)
-real(4),constant::d24(1_8:10_8)
-real(4),constant::d25
-contains
-attributes(global) subroutine init()
-end
-end
More information about the flang-commits
mailing list