[flang-commits] [flang] [flang][cuda] Update c_loc with device variable to get host address (PR #164317)

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Mon Oct 20 13:50:25 PDT 2025


https://github.com/clementval created https://github.com/llvm/llvm-project/pull/164317

Bypass the declare op because it is rewritten in CUFOpConversion and will only provide the device address. c_loc is expected to have the host address of a device address to be used in API like `cudaMemcpyToSymbol` so we need to provide the address of op directly. 

>From 8582750159b469e0deb673d03c0da81ac7f07022 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Mon, 20 Oct 2025 13:47:30 -0700
Subject: [PATCH] [flang][cuda] Update c_loc with device variable to get host
 address

---
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index ec0c802fb209c..29eedfb0ce9cd 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -3516,11 +3516,23 @@ static mlir::Value getAddrFromBox(fir::FirOpBuilder &builder,
   return addr;
 }
 
+static void clocDeviceArgRewrite(fir::ExtendedValue arg) {
+  // Special case for device address in c_loc.
+  if (auto emboxOp = mlir::dyn_cast_or_null<fir::EmboxOp>(
+          fir::getBase(arg).getDefiningOp()))
+    if (auto declareOp = mlir::dyn_cast_or_null<hlfir::DeclareOp>(
+            emboxOp.getMemref().getDefiningOp()))
+      if (declareOp.getDataAttr() &&
+          declareOp.getDataAttr() == cuf::DataAttribute::Device)
+        emboxOp.getMemrefMutable().assign(declareOp.getMemref());
+}
+
 static fir::ExtendedValue
 genCLocOrCFunLoc(fir::FirOpBuilder &builder, mlir::Location loc,
                  mlir::Type resultType, llvm::ArrayRef<fir::ExtendedValue> args,
                  bool isFunc = false, bool isDevLoc = false) {
   assert(args.size() == 1);
+  clocDeviceArgRewrite(args[0]);
   mlir::Value res = fir::AllocaOp::create(builder, loc, resultType);
   mlir::Value resAddr;
   if (isDevLoc)



More information about the flang-commits mailing list