[flang-commits] [flang] [flang][debug] Improve check for global variable detection. (PR #118326)

Abid Qadeer via flang-commits flang-commits at lists.llvm.org
Mon Dec 2 09:29:26 PST 2024


https://github.com/abidh created https://github.com/llvm/llvm-project/pull/118326

When a global variable is used in the OpenMP target region, it is passed as an argument to the function that implements target region. But the `DeclareOp` for this incarnation still have the original name of the variable. As some of our checks to decide if a variable is global or nor are based on the name, this can result in a local variable being treated as global. This PR hardens the check a bit. We now also check that memory ref is actually an `AddrOfOp` before looking at the name.

>From 7eddf851bc584db314eefa42e19d3404022df8bb Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Mon, 2 Dec 2024 16:53:12 +0000
Subject: [PATCH] [flang][debug] Improve check for global variable.

When a global variable is used in the OpenMP target region, it is
passed as an argument to the function that implements target region.
But the DeclareOp for this incarnation still have the original name of
the variable. As some of our checks to decide if a variable is global
or nor are based on the name, this can result in a local variable being
treated as global. This PR hardens the check a bit. We now also check
that memory ref is actually `an AddrOfOp` before looking at the name.
---
 flang/lib/Optimizer/Transforms/AddDebugInfo.cpp | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp b/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
index 3a437c7a0f0137..db223e99b6c59c 100644
--- a/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
+++ b/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
@@ -102,15 +102,15 @@ void AddDebugInfoPass::handleDeclareOp(fir::cg::XDeclareOp declOp,
   if (result.first != fir::NameUniquer::NameKind::VARIABLE)
     return;
   // If this DeclareOp actually represents a global then treat it as such.
-  if (auto global = symbolTable->lookup<fir::GlobalOp>(declOp.getUniqName())) {
-    handleGlobalOp(global, fileAttr, scopeAttr, typeGen, symbolTable, declOp);
-    return;
+  mlir::Operation *defOp = declOp.getMemref().getDefiningOp();
+  if (defOp && llvm::isa<fir::AddrOfOp>(defOp)) {
+    if (auto global =
+            symbolTable->lookup<fir::GlobalOp>(declOp.getUniqName())) {
+      handleGlobalOp(global, fileAttr, scopeAttr, typeGen, symbolTable, declOp);
+      return;
+    }
   }
 
-  // Only accept local variables.
-  if (result.second.procs.empty())
-    return;
-
   // FIXME: There may be cases where an argument is processed a bit before
   // DeclareOp is generated. In that case, DeclareOp may point to an
   // intermediate op and not to BlockArgument.



More information about the flang-commits mailing list