[clang] [OpenMP] Do not GEP 'inbounds' for multi-dimensional OpenMP device maps (PR #218436)

2026-10-02 Thread Joseph Huber via cfe-commits

jhuber6 wrote:

Merged in 
https://github.com/llvm/llvm-project/commit/06fb2cbb4e57137c07742aa1f6457a05007661f5

https://github.com/llvm/llvm-project/pull/218436
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [OpenMP] Do not GEP 'inbounds' for multi-dimensional OpenMP device maps (PR #218436)

2026-10-02 Thread Joseph Huber via cfe-commits

https://github.com/jhuber6 closed 
https://github.com/llvm/llvm-project/pull/218436
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [OpenMP] Do not GEP 'inbounds' for multi-dimensional OpenMP device maps (PR #218436)

2026-10-02 Thread Joseph Huber via cfe-commits

https://github.com/jhuber6 updated 
https://github.com/llvm/llvm-project/pull/218436

>From 3309f46ff171692856e281d4b2bf57ed9a89db08 Mon Sep 17 00:00:00 2001
From: Joseph Huber 
Date: Sun, 23 Aug 2026 20:14:54 -0500
Subject: [PATCH 1/3] [OpenMP] Do not GEP 'inbounds' for multi-dimensional
 OpenMP device maps

The C standard treats array subscripts as inbounds of the object, but
OpenMP section maps intentionally index from a host-relative base that
is not the mapped allocation. Drop inbounds on those GEPs so later
passes do not assume the slice lives inside the object.
---
 clang/lib/CodeGen/CGExprScalar.cpp| 30 ++-
 .../OpenMP/target_map_section_inbounds.cpp| 18 +++
 2 files changed, 41 insertions(+), 7 deletions(-)
 create mode 100644 clang/test/OpenMP/target_map_section_inbounds.cpp

diff --git a/clang/lib/CodeGen/CGExprScalar.cpp 
b/clang/lib/CodeGen/CGExprScalar.cpp
index 173931213ce68..7d848feed4eda 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -35,6 +35,7 @@
 #include "clang/Basic/TargetInfo.h"
 #include "llvm/ADT/APFixedPoint.h"
 #include "llvm/ADT/ScopeExit.h"
+#include "llvm/Analysis/ValueTracking.h"
 #include "llvm/IR/Argument.h"
 #include "llvm/IR/CFG.h"
 #include "llvm/IR/Constants.h"
@@ -6434,6 +6435,23 @@ static GEPOffsetAndOverflow EmitGEPOffsetInBytes(Value 
*BasePtr, Value *GEPVal,
   return {TotalOffset, OffsetOverflows};
 }
 
+// OpenMP section maps pass `section - host_offset`. A GEP of an 
array-of-arrays
+// from that pointer is not inbounds of the allocation.
+static llvm::GEPNoWrapFlags
+inBoundsGEPFlags(const CodeGenFunction &CGF, const llvm::Value *Ptr,
+ llvm::Type *SrcTy, bool SignedIndices, bool IsSubtraction) {
+  llvm::GEPNoWrapFlags NW;
+  auto *AT = dyn_cast(SrcTy);
+  bool SectionGEP = CGF.getLangOpts().OpenMPIsTargetDevice && AT &&
+AT->getElementType()->isArrayTy() &&
+!isa(llvm::getUnderlyingObject(Ptr));
+  if (!SectionGEP)
+NW = llvm::GEPNoWrapFlags::inBounds();
+  if (!SignedIndices && !IsSubtraction)
+NW |= llvm::GEPNoWrapFlags::noUnsignedWrap();
+  return NW;
+}
+
 Value *
 CodeGenFunction::EmitCheckedInBoundsGEP(llvm::Type *ElemTy, Value *Ptr,
 ArrayRef IdxList,
@@ -6441,9 +6459,8 @@ CodeGenFunction::EmitCheckedInBoundsGEP(llvm::Type 
*ElemTy, Value *Ptr,
 SourceLocation Loc, const Twine &Name) 
{
   llvm::Type *PtrTy = Ptr->getType();
 
-  llvm::GEPNoWrapFlags NWFlags = llvm::GEPNoWrapFlags::inBounds();
-  if (!SignedIndices && !IsSubtraction)
-NWFlags |= llvm::GEPNoWrapFlags::noUnsignedWrap();
+  llvm::GEPNoWrapFlags NWFlags =
+  inBoundsGEPFlags(*this, Ptr, ElemTy, SignedIndices, IsSubtraction);
 
   Value *GEPVal = Builder.CreateGEP(ElemTy, Ptr, IdxList, Name, NWFlags);
 
@@ -6554,10 +6571,9 @@ Address CodeGenFunction::EmitCheckedInBoundsGEP(
 bool SignedIndices, bool IsSubtraction, SourceLocation Loc, CharUnits 
Align,
 const Twine &Name) {
   if (!SanOpts.has(SanitizerKind::PointerOverflow)) {
-llvm::GEPNoWrapFlags NWFlags = llvm::GEPNoWrapFlags::inBounds();
-if (!SignedIndices && !IsSubtraction)
-  NWFlags |= llvm::GEPNoWrapFlags::noUnsignedWrap();
-
+llvm::GEPNoWrapFlags NWFlags =
+inBoundsGEPFlags(*this, Addr.getBasePointer(), Addr.getElementType(),
+ SignedIndices, IsSubtraction);
 return Builder.CreateGEP(Addr, IdxList, elementType, Align, Name, NWFlags);
   }
 
diff --git a/clang/test/OpenMP/target_map_section_inbounds.cpp 
b/clang/test/OpenMP/target_map_section_inbounds.cpp
new file mode 100644
index 0..f3f380ef597bc
--- /dev/null
+++ b/clang/test/OpenMP/target_map_section_inbounds.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple amdgcn-amd-amdhsa \
+// RUN:   -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-is-target-device \
+// RUN:   -fopenmp-host-ir-file-path %t-host.bc -emit-llvm %s -o - \
+// RUN:   | FileCheck %s
+
+int Data[8][4];
+
+void section_2d(int P, int I) {
+#pragma omp target map(tofrom : Data[P : 1][0 : 4]) firstprivate(P, I)
+  {
+Data[P][I] = 1;
+  }
+}
+
+// CHECK-LABEL: define {{.*}}@{{__omp_offloading_.+}}section_2d
+// CHECK-NOT: getelementptr inbounds{{.*}}[8 x [4 x i32]]
+// CHECK: getelementptr{{.*}}[8 x [4 x i32]]
+// CHECK: getelementptr inbounds{{.*}}[4 x i32]

>From c7ef2f7fb264666e4bdef5ff36cae04e13ca05e6 Mon Sep 17 00:00:00 2001
From: Joseph Huber 
Date: Tue, 25 Aug 2026 17:10:37 -0500
Subject: [PATCH 2/3] Update clang/lib/CodeGen/CGExprScalar.cpp

Co-authored-by: Alexey Bataev 
---
 clang/lib/CodeGen/CGExprScalar.cpp | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CGExprScalar.cpp 
b/clang/lib/CodeGen/CGExprScalar.cpp
index 7d848feed4eda..538efafc4a44f 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@