[clang] 1388887 - Enable dynamic-sized VLAs for data sharing in OpenMP offloaded target regions.

2023-07-06 Thread Doru Bercea via cfe-commits

Author: Doru Bercea
Date: 2023-07-06T10:57:10-04:00
New Revision: 1370e568dea84c4ea65fe5c01ef4f4ccc751

URL: 
https://github.com/llvm/llvm-project/commit/1370e568dea84c4ea65fe5c01ef4f4ccc751
DIFF: 
https://github.com/llvm/llvm-project/commit/1370e568dea84c4ea65fe5c01ef4f4ccc751.diff

LOG: Enable dynamic-sized VLAs for data sharing in OpenMP offloaded target 
regions.

Review: https://reviews.llvm.org/D153883

Added: 
clang/test/OpenMP/amdgcn_target_device_vla.cpp

Modified: 
clang/lib/CodeGen/CGDecl.cpp
clang/lib/CodeGen/CGOpenMPRuntime.h
clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
clang/lib/CodeGen/CGOpenMPRuntimeGPU.h
clang/lib/CodeGen/CodeGenFunction.h

Removed: 




diff  --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index e143687479ee3e..f19006f7b291e5 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -581,6 +581,16 @@ namespace {
 }
   };
 
+  struct KmpcAllocFree final : EHScopeStack::Cleanup {
+std::pair AddrSizePair;
+KmpcAllocFree(const std::pair )
+: AddrSizePair(AddrSizePair) {}
+void Emit(CodeGenFunction , Flags EmissionFlags) override {
+  auto  = CGF.CGM.getOpenMPRuntime();
+  RT.getKmpcFreeShared(CGF, AddrSizePair);
+}
+  };
+
   struct ExtendGCLifetime final : EHScopeStack::Cleanup {
 const VarDecl 
 ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
@@ -1583,28 +1593,59 @@ CodeGenFunction::EmitAutoVarAlloca(const VarDecl ) {
   } else {
 EnsureInsertPoint();
 
-if (!DidCallStackSave) {
-  // Save the stack.
-  Address Stack =
-CreateTempAlloca(Int8PtrTy, getPointerAlign(), "saved_stack");
+// Delayed globalization for variable length declarations. This ensures 
that
+// the expression representing the length has been emitted and can be used
+// by the definition of the VLA. Since this is an escaped declaration, in
+// OpenMP we have to use a call to __kmpc_alloc_shared(). The matching
+// deallocation call to __kmpc_free_shared() is emitted later.
+bool VarAllocated = false;
+if (getLangOpts().OpenMPIsDevice) {
+  auto  = CGM.getOpenMPRuntime();
+  if (RT.isDelayedVariableLengthDecl(*this, )) {
+// Emit call to __kmpc_alloc_shared() instead of the alloca.
+std::pair AddrSizePair =
+RT.getKmpcAllocShared(*this, );
+
+// Save the address of the allocation:
+LValue Base = MakeAddrLValue(AddrSizePair.first, D.getType(),
+ CGM.getContext().getDeclAlign(),
+ AlignmentSource::Decl);
+address = Base.getAddress(*this);
+
+// Push a cleanup block to emit the call to __kmpc_free_shared in the
+// appropriate location at the end of the scope of the
+// __kmpc_alloc_shared functions:
+pushKmpcAllocFree(NormalCleanup, AddrSizePair);
+
+// Mark variable as allocated:
+VarAllocated = true;
+  }
+}
 
-  llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
-  llvm::Value *V = Builder.CreateCall(F);
-  Builder.CreateStore(V, Stack);
+if (!VarAllocated) {
+  if (!DidCallStackSave) {
+// Save the stack.
+Address Stack =
+CreateTempAlloca(Int8PtrTy, getPointerAlign(), "saved_stack");
 
-  DidCallStackSave = true;
+llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
+llvm::Value *V = Builder.CreateCall(F);
+Builder.CreateStore(V, Stack);
 
-  // Push a cleanup block and restore the stack there.
-  // FIXME: in general circumstances, this should be an EH cleanup.
-  pushStackRestore(NormalCleanup, Stack);
-}
+DidCallStackSave = true;
 
-auto VlaSize = getVLASize(Ty);
-llvm::Type *llvmTy = ConvertTypeForMem(VlaSize.Type);
+// Push a cleanup block and restore the stack there.
+// FIXME: in general circumstances, this should be an EH cleanup.
+pushStackRestore(NormalCleanup, Stack);
+  }
+
+  auto VlaSize = getVLASize(Ty);
+  llvm::Type *llvmTy = ConvertTypeForMem(VlaSize.Type);
 
-// Allocate memory for the array.
-address = CreateTempAlloca(llvmTy, alignment, "vla", VlaSize.NumElts,
-   );
+  // Allocate memory for the array.
+  address = CreateTempAlloca(llvmTy, alignment, "vla", VlaSize.NumElts,
+ );
+}
 
 // If we have debug info enabled, properly describe the VLA dimensions for
 // this type by registering the vla size expression for each of the
@@ -2141,6 +2182,11 @@ void CodeGenFunction::pushStackRestore(CleanupKind Kind, 
Address SPMem) {
   EHStack.pushCleanup(Kind, SPMem);
 }
 
+void CodeGenFunction::pushKmpcAllocFree(
+CleanupKind Kind, std::pair AddrSizePair) {
+  EHStack.pushCleanup(Kind, 

[clang] 0191078 - Fix failure with team-wide allocated variable

2023-04-20 Thread Doru Bercea via cfe-commits

Author: Doru Bercea
Date: 2023-04-20T14:40:35-04:00
New Revision: 01910787d386584ea5a3d5dc317a908423ba39ed

URL: 
https://github.com/llvm/llvm-project/commit/01910787d386584ea5a3d5dc317a908423ba39ed
DIFF: 
https://github.com/llvm/llvm-project/commit/01910787d386584ea5a3d5dc317a908423ba39ed.diff

LOG: Fix failure with team-wide allocated variable

Review: https://reviews.llvm.org/D147572

Added: 
clang/test/OpenMP/target_team_variable_codegen.cpp

Modified: 
clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp

Removed: 




diff  --git a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
index 2fea9c219f358..3f231703aa651 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
@@ -3353,7 +3353,7 @@ Address 
CGOpenMPRuntimeGPU::getAddressOfLocalVariable(CodeGenFunction ,
 llvm::Type *VarTy = CGF.ConvertTypeForMem(VD->getType());
 auto *GV = new llvm::GlobalVariable(
 CGM.getModule(), VarTy, /*isConstant=*/false,
-llvm::GlobalValue::InternalLinkage, 
llvm::Constant::getNullValue(VarTy),
+llvm::GlobalValue::InternalLinkage, llvm::PoisonValue::get(VarTy),
 VD->getName(),
 /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal,
 CGM.getContext().getTargetAddressSpace(AS));

diff  --git a/clang/test/OpenMP/target_team_variable_codegen.cpp 
b/clang/test/OpenMP/target_team_variable_codegen.cpp
new file mode 100644
index 0..cc82503a52d2a
--- /dev/null
+++ b/clang/test/OpenMP/target_team_variable_codegen.cpp
@@ -0,0 +1,57 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --check-globals --prefix-filecheck-ir-name _ --global-value-regex 
"llvm.compiler.used" "_[0-9a-zA-Z]+A[0-9a-zA-Z]+pi[0-9a-zA-Z]+" 
"_[0-9a-zA-Z]+anotherPi" --version 2
+// REQUIRES: amdgpu-registered-target
+
+
+// Test target codegen - host bc file has to be created first.
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple powerpc64le-unknown-unknown 
-fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm-bc %s -o %t-ppc-host-amd.bc
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple amdgcn-amd-amdhsa 
-fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -fopenmp-target-debug 
-fopenmp-is-device -fopenmp-host-ir-file-path %t-ppc-host-amd.bc -o - | 
FileCheck %s --check-prefix=CHECK-AMD
+
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple powerpc64le-unknown-unknown 
-fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm-bc %s -o %t-ppc-host-nvidia.bc
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple nvptx64-unknown-unknown 
-fopenmp-targets=nvptx64-unknown-unknown -emit-llvm %s -fopenmp-target-debug 
-fopenmp-is-device -fopenmp-host-ir-file-path %t-ppc-host-nvidia.bc -o - | 
FileCheck %s --check-prefix=CHECK-NVIDIA
+
+// expected-no-diagnostics
+
+#ifndef HEADER
+#define HEADER
+
+typedef enum omp_allocator_handle_t {
+  omp_null_allocator = 0,
+  omp_default_mem_alloc = 1,
+  omp_large_cap_mem_alloc = 2,
+  omp_const_mem_alloc = 3,
+  omp_high_bw_mem_alloc = 4,
+  omp_low_lat_mem_alloc = 5,
+  omp_cgroup_mem_alloc = 6,
+  omp_pteam_mem_alloc = 7,
+  omp_thread_mem_alloc = 8,
+  KMP_ALLOCATOR_MAX_HANDLE = __UINTPTR_MAX__
+} omp_allocator_handle_t;
+
+//.
+// CHECK-AMD: @local_a = internal addrspace(3) global [10 x i32] poison, align 
4
+//.
+// CHECK-NVIDIA: @local_a = internal addrspace(3) global [10 x i32] poison, 
align 4
+//.
+int main()
+{
+   int N = 1;
+   int *a = new int[N];
+#pragma omp target data map(tofrom:a[:N])
+   {
+#pragma omp target teams distribute parallel for
+for(int i = 0; i < N; i++)
+{
+  int local_a[10];
+#pragma omp allocate(local_a) allocator(omp_pteam_mem_alloc)
+  for(int j = 0; j < 10; j++)
+   local_a[j] = a[(i + j) % N];
+  a[i] = local_a[0];
+}
+   }
+  return a[17];
+}
+
+#endif
+ NOTE: These prefixes are unused and the list is autogenerated. Do not add 
tests below this line:
+// CHECK-AMD: {{.*}}
+// CHECK-NVIDIA: {{.*}}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 0eabf59 - Enable constexpr class members that are device-mapped to not be optimized out.

2023-03-23 Thread Doru Bercea via cfe-commits

Author: Doru Bercea
Date: 2023-03-23T10:17:25-04:00
New Revision: 0eabf59528f3c3f64923900cae740d9f26c45ae8

URL: 
https://github.com/llvm/llvm-project/commit/0eabf59528f3c3f64923900cae740d9f26c45ae8
DIFF: 
https://github.com/llvm/llvm-project/commit/0eabf59528f3c3f64923900cae740d9f26c45ae8.diff

LOG: Enable constexpr class members that are device-mapped to not be optimized 
out.

This patch fixes an issue whereby a constexpr class member which is
mapped to the device is being optimized out thus leading to a runtime
error.

Patch: https://reviews.llvm.org/D146552

Added: 
clang/test/OpenMP/declare_target_constexpr_codegen.cpp
openmp/libomptarget/test/offloading/target_constexpr_mapping.cpp

Modified: 
clang/lib/CodeGen/CGOpenMPRuntime.cpp

Removed: 




diff  --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 5f21cfca66bb8..58a95d64ac50e 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -10387,7 +10387,9 @@ void 
CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD,
 }
 Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false);
 // Temp solution to prevent optimizations of the internal variables.
-if (CGM.getLangOpts().OpenMPIsDevice && !VD->isExternallyVisible()) {
+if (CGM.getLangOpts().OpenMPIsDevice &&
+(!VD->isExternallyVisible() ||
+ Linkage == llvm::GlobalValue::LinkOnceODRLinkage)) {
   // Do not create a "ref-variable" if the original is not also available
   // on the host.
   if (!OffloadEntriesInfoManager.hasDeviceGlobalVarEntryInfo(VarName))

diff  --git a/clang/test/OpenMP/declare_target_constexpr_codegen.cpp 
b/clang/test/OpenMP/declare_target_constexpr_codegen.cpp
new file mode 100644
index 0..27161feef05e0
--- /dev/null
+++ b/clang/test/OpenMP/declare_target_constexpr_codegen.cpp
@@ -0,0 +1,40 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --check-globals --prefix-filecheck-ir-name _ --global-value-regex 
"llvm.compiler.used" "_[0-9a-zA-Z]+A[0-9a-zA-Z]+pi[0-9a-zA-Z]+" 
"_[0-9a-zA-Z]+anotherPi" --version 2
+// REQUIRES: amdgpu-registered-target
+
+
+// Test target codegen - host bc file has to be created first.
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple powerpc64le-unknown-unknown 
-fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm-bc %s -o %t-ppc-host.bc
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple nvptx64-unknown-unknown 
-fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm %s -fopenmp-target-debug 
-fopenmp-is-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - | FileCheck 
%s --check-prefix=CHECK
+
+// expected-no-diagnostics
+
+#ifndef HEADER
+#define HEADER
+
+#pragma omp declare target
+class A {
+public:
+  static constexpr double pi = 3.141592653589793116;
+//.
+// CHECK: @_ZN1A2piE = linkonce_odr constant double 0x400921FB54442D18, 
comdat, align 8
+// CHECK: @_ZL9anotherPi = internal constant double 3.14e+00, align 8
+// CHECK: @llvm.compiler.used = appending global [2 x ptr] [ptr 
@"__ZN1A2piE$ref", ptr @"__ZL9anotherPi$ref"], section "llvm.metadata"
+//.
+  A() { ; }
+  ~A() { ; }
+};
+#pragma omp end declare target
+
+void F(const double &);
+void Test() { F(A::pi); }
+
+#pragma omp declare target
+constexpr static double anotherPi = 3.14;
+#pragma omp end declare target
+
+#endif
+
+
+//
+ NOTE: These prefixes are unused and the list is autogenerated. Do not add 
tests below this line:
+// CHECK: {{.*}}

diff  --git a/openmp/libomptarget/test/offloading/target_constexpr_mapping.cpp 
b/openmp/libomptarget/test/offloading/target_constexpr_mapping.cpp
new file mode 100644
index 0..14cf92a7cc26e
--- /dev/null
+++ b/openmp/libomptarget/test/offloading/target_constexpr_mapping.cpp
@@ -0,0 +1,34 @@
+// RUN: %libomptarget-compileoptxx-run-and-check-generic
+
+#include 
+#include 
+
+#pragma omp declare target
+class A {
+public:
+  constexpr static double pi = 3.141592653589793116;
+  A() { ; }
+  ~A() { ; }
+};
+#pragma omp end declare target
+
+#pragma omp declare target
+constexpr static double anotherPi = 3.14;
+#pragma omp end declare target
+
+int main() {
+  double a[2];
+#pragma omp target map(tofrom : a[:2])
+  {
+a[0] = A::pi;
+a[1] = anotherPi;
+  }
+
+  // CHECK: pi = 3.141592653589793116
+  printf("pi = %.18f\n", a[0]);
+
+  // CHECK: anotherPi = 3.14
+  printf("anotherPi = %.2f\n", a[1]);
+
+  return 0;
+}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 1407dbe - Allow a target loop to be used inside a parallel.

2023-01-20 Thread Doru Bercea via cfe-commits

Author: Doru Bercea
Date: 2023-01-20T14:10:43-06:00
New Revision: 1407dbeabcfed114f0918b022365d03713dac028

URL: 
https://github.com/llvm/llvm-project/commit/1407dbeabcfed114f0918b022365d03713dac028
DIFF: 
https://github.com/llvm/llvm-project/commit/1407dbeabcfed114f0918b022365d03713dac028.diff

LOG: Allow a target loop to be used inside a parallel.

Added: 
clang/test/OpenMP/nested_loop_codegen.cpp

Modified: 
clang/lib/CodeGen/CGStmtOpenMP.cpp

Removed: 




diff  --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp 
b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 87bab57ba5d6..4ba2c4b59991 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -7862,7 +7862,19 @@ void CodeGenFunction::EmitOMPGenericLoopDirective(
 const OMPGenericLoopDirective ) {
   // Unimplemented, just inline the underlying statement for now.
   auto & = [](CodeGenFunction , PrePostActionTy ) {
-CGF.EmitStmt(cast(S.getAssociatedStmt())->getCapturedStmt());
+// Emit the loop iteration variable.
+const Stmt *CS =
+cast(S.getAssociatedStmt())->getCapturedStmt();
+const auto *ForS = dyn_cast(CS);
+if (ForS && !isa(ForS->getInit())) {
+  OMPPrivateScope LoopScope(CGF);
+  CGF.EmitOMPPrivateLoopCounters(S, LoopScope);
+  (void)LoopScope.Privatize();
+  CGF.EmitStmt(CS);
+  LoopScope.restoreMap();
+} else {
+  CGF.EmitStmt(CS);
+}
   };
   OMPLexicalScope Scope(*this, S, OMPD_unknown);
   CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_loop, CodeGen);

diff  --git a/clang/test/OpenMP/nested_loop_codegen.cpp 
b/clang/test/OpenMP/nested_loop_codegen.cpp
new file mode 100644
index ..e38d9db29b8e
--- /dev/null
+++ b/clang/test/OpenMP/nested_loop_codegen.cpp
@@ -0,0 +1,950 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --function-signature --include-generated-funcs --replace-value-regex 
"__omp_offloading_[0-9a-z]+_[0-9a-z]+" "reduction_size[.].+[.]" 
"pl_cond[.].+[.|,]" --prefix-filecheck-ir-name _
+
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -emit-llvm %s -triple 
x86_64-unknown-linux -fexceptions -fcxx-exceptions -o - | FileCheck %s 
--check-prefix=CHECK1
+// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown 
-fexceptions -fcxx-exceptions -emit-pch -o %t %s
+// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-unknown-unknown -fexceptions 
-fcxx-exceptions -debug-info-kind=limited -std=c++11 -include-pch %t -verify %s 
-emit-llvm -o - | FileCheck %s --check-prefix=CHECK2
+// RUN: %clang_cc1 -verify -fopenmp -fopenmp-enable-irbuilder -DIRBUILDER -x 
c++ -emit-llvm %s -triple x86_64-unknown-linux -fexceptions -fcxx-exceptions -o 
- | FileCheck %s --check-prefix=CHECK3
+// RUN: %clang_cc1 -fopenmp -fopenmp-enable-irbuilder -DIRBUILDER -x c++ 
-std=c++11 -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions 
-emit-pch -o %t %s
+// RUN: %clang_cc1 -fopenmp -fopenmp-enable-irbuilder -DIRBUILDER -x c++ 
-triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions 
-debug-info-kind=limited -gno-column-info -std=c++11 -include-pch %t -verify %s 
-emit-llvm -o - | FileCheck %s --check-prefix=CHECK4
+
+// RUN: %clang_cc1 -verify -fopenmp-simd -x c++ -emit-llvm %s -triple 
x86_64-unknown-linux -fexceptions -fcxx-exceptions -o - | FileCheck %s 
--implicit-check-not="{{__kmpc|__tgt}}"
+// RUN: %clang_cc1 -fopenmp-simd -x c++ -std=c++11 -triple 
x86_64-unknown-unknown -fexceptions -fcxx-exceptions -emit-pch -o %t %s
+// RUN: %clang_cc1 -fopenmp-simd -x c++ -triple x86_64-unknown-unknown 
-fexceptions -fcxx-exceptions -debug-info-kind=limited -std=c++11 -include-pch 
%t -verify %s -emit-llvm -o - | FileCheck %s 
--implicit-check-not="{{__kmpc|__tgt}}"
+// RUN: %clang_cc1 -verify -fopenmp-simd -fopenmp-enable-irbuilder -x c++ 
-emit-llvm %s -triple x86_64-unknown-linux -fexceptions -fcxx-exceptions -o - | 
FileCheck %s --implicit-check-not="{{__kmpc|__tgt}}"
+// RUN: %clang_cc1 -fopenmp-simd -fopenmp-enable-irbuilder -x c++ -std=c++11 
-triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -emit-pch -o %t %s
+// RUN: %clang_cc1 -fopenmp-simd -fopenmp-enable-irbuilder -x c++ -triple 
x86_64-unknown-unknown -fexceptions -fcxx-exceptions -debug-info-kind=limited 
-std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s 
--implicit-check-not="{{__kmpc|__tgt}}"
+// expected-no-diagnostics
+#ifndef HEADER
+#define HEADER
+
+int outline_decl() {
+  int i, k;
+  #pragma omp parallel
+  for(i=0; i<10; i++) {
+#pragma omp loop
+for(k=0; k<5; k++) {
+  k++;
+}
+  }
+  return k;
+}
+
+int inline_decl() {
+  int i, res;
+  #pragma omp parallel
+  for(i=0; i<10; i++) {
+#pragma omp loop
+for(int k=0; k<5; k++) {
+  res++;
+}
+  }
+  return res;
+}
+
+#endif
+// CHECK1-LABEL: define {{[^@]+}}@_Z12outline_declv
+// CHECK1-SAME: () #[[ATTR0:[0-9]+]] {
+// 

[clang] 49d47c4 - Add Parse/Sema for iterator for map clause.

2023-01-20 Thread Doru Bercea via cfe-commits

Author: Doru Bercea
Date: 2023-01-20T12:54:49-06:00
New Revision: 49d47c4d2f280d15d1de94c53b72b6ab3c127b35

URL: 
https://github.com/llvm/llvm-project/commit/49d47c4d2f280d15d1de94c53b72b6ab3c127b35
DIFF: 
https://github.com/llvm/llvm-project/commit/49d47c4d2f280d15d1de94c53b72b6ab3c127b35.diff

LOG: Add Parse/Sema for iterator for map clause.

Added: 


Modified: 
clang/include/clang/AST/OpenMPClause.h
clang/include/clang/Basic/DiagnosticParseKinds.td
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/include/clang/Basic/OpenMPKinds.def
clang/include/clang/Basic/OpenMPKinds.h
clang/include/clang/Sema/Sema.h
clang/lib/AST/OpenMPClause.cpp
clang/lib/Parse/ParseOpenMP.cpp
clang/lib/Sema/SemaExpr.cpp
clang/lib/Sema/SemaOpenMP.cpp
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
clang/lib/Sema/TreeTransform.h
clang/lib/Serialization/ASTReader.cpp
clang/lib/Serialization/ASTWriter.cpp
clang/test/OpenMP/declare_mapper_ast_print.c
clang/test/OpenMP/declare_mapper_messages.c
clang/test/OpenMP/target_ast_print.cpp
clang/test/OpenMP/target_map_messages.cpp

Removed: 




diff  --git a/clang/include/clang/AST/OpenMPClause.h 
b/clang/include/clang/AST/OpenMPClause.h
index 47644c93bd426..352e60f9c3658 100644
--- a/clang/include/clang/AST/OpenMPClause.h
+++ b/clang/include/clang/AST/OpenMPClause.h
@@ -5745,7 +5745,7 @@ class OMPMapClause final : public 
OMPMappableExprListClause,
   size_t numTrailingObjects(OverloadToken) const {
 // There are varlist_size() of expressions, and varlist_size() of
 // user-defined mappers.
-return 2 * varlist_size();
+return 2 * varlist_size() + 1;
   }
   size_t numTrailingObjects(OverloadToken) const {
 return getUniqueDeclarationsNum();
@@ -5759,7 +5759,7 @@ class OMPMapClause final : public 
OMPMappableExprListClause,
   OpenMPMapModifierKind MapTypeModifiers[NumberOfOMPMapClauseModifiers] = {
   OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
   OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
-  OMPC_MAP_MODIFIER_unknown};
+  OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown};
 
   /// Location of map-type-modifiers for the 'map' clause.
   SourceLocation MapTypeModifiersLoc[NumberOfOMPMapClauseModifiers];
@@ -5860,6 +5860,11 @@ class OMPMapClause final : public 
OMPMappableExprListClause,
   /// Set colon location.
   void setColonLoc(SourceLocation Loc) { ColonLoc = Loc; }
 
+  /// Set iterator modifier.
+  void setIteratorModifier(Expr *IteratorModifier) {
+getTrailingObjects()[2 * varlist_size()] = IteratorModifier;
+  }
+
 public:
   /// Creates clause with a list of variables \a VL.
   ///
@@ -5872,6 +5877,7 @@ class OMPMapClause final : public 
OMPMappableExprListClause,
   /// \param ComponentLists Component lists used in the clause.
   /// \param UDMapperRefs References to user-defined mappers associated with
   /// expressions used in the clause.
+  /// \param IteratorModifier Iterator modifier.
   /// \param MapModifiers Map-type-modifiers.
   /// \param MapModifiersLoc Location of map-type-modifiers.
   /// \param UDMQualifierLoc C++ nested name specifier for the associated
@@ -5884,7 +5890,7 @@ class OMPMapClause final : public 
OMPMappableExprListClause,
   Create(const ASTContext , const OMPVarListLocTy ,
  ArrayRef Vars, ArrayRef Declarations,
  MappableExprComponentListsRef ComponentLists,
- ArrayRef UDMapperRefs,
+ ArrayRef UDMapperRefs, Expr *IteratorModifier,
  ArrayRef MapModifiers,
  ArrayRef MapModifiersLoc,
  NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId,
@@ -5903,6 +5909,11 @@ class OMPMapClause final : public 
OMPMappableExprListClause,
   static OMPMapClause *CreateEmpty(const ASTContext ,
const OMPMappableExprListSizeTy );
 
+  /// Fetches Expr * of iterator modifier.
+  Expr *getIteratorModifier() {
+return getTrailingObjects()[2 * varlist_size()];
+  }
+
   /// Fetches mapping kind for the clause.
   OpenMPMapClauseKind getMapType() const LLVM_READONLY { return MapType; }
 

diff  --git a/clang/include/clang/Basic/DiagnosticParseKinds.td 
b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 6bc35fadbf7e0..c367a34b762b7 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1362,7 +1362,7 @@ def err_omp_unknown_map_type : Error<
   "incorrect map type, expected one of 'to', 'from', 'tofrom', 'alloc', 
'release', or 'delete'">;
 def err_omp_unknown_map_type_modifier : Error<
   "incorrect map type modifier, expected one of: 'always', 'close', 'mapper'"
-  "%select{|, 'present'}0%select{|, 'ompx_hold'}1">;
+  "%select{|, 'present'|, 'present', 'iterator'}0%select{|, 'ompx_hold'}1">;
 def err_omp_map_type_missing : Error<
   "missing map type">;
 def 

[clang] b5c809a - Fix tests for commit 658ed9547cdd6657895339a6c390c31aa77a5698.

2022-12-19 Thread Doru Bercea via cfe-commits

Author: Doru Bercea
Date: 2022-12-19T07:46:34-06:00
New Revision: b5c809acd34c2489679300eb0b8a8b824aeb

URL: 
https://github.com/llvm/llvm-project/commit/b5c809acd34c2489679300eb0b8a8b824aeb
DIFF: 
https://github.com/llvm/llvm-project/commit/b5c809acd34c2489679300eb0b8a8b824aeb.diff

LOG: Fix tests for commit 658ed9547cdd6657895339a6c390c31aa77a5698.

Added: 


Modified: 
clang/test/OpenMP/declare_target_messages.cpp
clang/test/OpenMP/declare_target_nohost_variant_messages.cpp

Removed: 




diff  --git a/clang/test/OpenMP/declare_target_messages.cpp 
b/clang/test/OpenMP/declare_target_messages.cpp
index bf23813999119..ed011a8c3a593 100644
--- a/clang/test/OpenMP/declare_target_messages.cpp
+++ b/clang/test/OpenMP/declare_target_messages.cpp
@@ -11,7 +11,7 @@
 // RUN: %clang_cc1 -triple x86_64-apple-macos10.7.0 -verify=expected,omp51 
-fopenmp-version=51 -fopenmp-simd -fnoopenmp-use-tls -ferror-limit 100 -o - %s
 // RUN: %clang_cc1 -triple x86_64-apple-macos10.7.0 -verify=expected,omp51 
-fopenmp-version=51 -fopenmp-simd -fnoopenmp-use-tls -ferror-limit 100 -o - %s
 
-// RUN: %clang_cc1 -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa 
-fopenmp-version=52 -DVERBOSE_MODE=1 -verify=expected,omp52 -fnoopenmp-use-tls 
-ferror-limit 100 -o - %s
+// RUN: %clang_cc1 -triple x86_64-apple-macos10.7.0 -verify=expected,omp52 
-fopenmp -fopenmp-version=52 -DVERBOSE_MODE=1 -fnoopenmp-use-tls -ferror-limit 
100 -o - %s
 
 // RUN: %clang_cc1 -triple x86_64-apple-macos10.7.0 -verify=expected,omp5 
-fopenmp -fnoopenmp-use-tls -ferror-limit 100 -o - %s
 #pragma omp end declare target // expected-error {{unexpected OpenMP directive 
'#pragma omp end declare target'}}
@@ -242,11 +242,3 @@ int MultiDevTy;
 // expected-warning@+1 {{expected '#pragma omp end declare target' at end of 
file to match '#pragma omp begin declare target'}}
 #pragma omp begin declare target
 #endif
-
-void fun();
-void host_function();
-#pragma omp declare target enter(fun) device_type(nohost) // omp45-error 
{{unexpected 'enter' clause, use 'to' instead}} omp45-error {{expected at least 
one 'to' or 'link' clause}} omp5-error {{unexpected 'enter' clause, use 'to' 
instead}} omp5-error {{expected at least one 'to' or 'link' clause}} 
omp51-error {{expected at least one 'to', 'link' or 'indirect' clause}} 
omp51-error {{unexpected 'enter' clause, use 'to' instead}}
-#pragma omp declare variant(host_function) match(device={kind(host)})
-void fun() {}
-void host_function() {}
-void call_host_function() { fun(); }

diff  --git a/clang/test/OpenMP/declare_target_nohost_variant_messages.cpp 
b/clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
index b54f864a926b2..190c1387cb099 100644
--- a/clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
+++ b/clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
@@ -1,21 +1,31 @@
+// REQUIRES: amdgpu-registered-target
+
 // RUN: %clang_cc1 -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa 
-fopenmp-version=52 -DVERBOSE_MODE=1 -verify=omp52 -fnoopenmp-use-tls 
-ferror-limit 100 -fopenmp-targets=amdgcn-amd-amdhsa -o - %s
 
 void fun();
+void host_function();
+#pragma omp declare target enter(fun) device_type(nohost)
+#pragma omp declare variant(host_function) match(device={kind(host)})
+void fun() {}
+void host_function() {}
+void call_host_function() { fun(); }
+
+void fun1();
 void not_a_host_function();
-#pragma omp declare target enter(fun) device_type(nohost) // omp52-note 
{{marked as 'device_type(nohost)' here}}
+#pragma omp declare target enter(fun1) device_type(nohost) // omp52-note 
{{marked as 'device_type(nohost)' here}}
 #pragma omp declare variant(not_a_host_function) match(device={kind(host)}) // 
omp52-error {{function with 'device_type(nohost)' is not available on host}}
-void fun() {}
+void fun1() {}
 #pragma omp begin declare target device_type(nohost) // omp52-note {{marked as 
'device_type(nohost)' here}}
 void not_a_host_function() {}
 #pragma omp end declare target
-void failed_call_to_host_function() { fun(); } // omp52-error {{function with 
'device_type(nohost)' is not available on host}}
+void failed_call_to_host_function() { fun1(); } // omp52-error {{function with 
'device_type(nohost)' is not available on host}}
 
 void fun2();
-void host_function();
+void host_function2();
 #pragma omp declare target enter(fun2) device_type(nohost)
-#pragma omp declare variant(host_function) match(device={kind(host)})
+#pragma omp declare variant(host_function2) match(device={kind(host)})
 void fun2() {}
 #pragma omp begin declare target device_type(host)
-void host_function() {}
+void host_function2() {}
 #pragma omp end declare target
 void call_to_host_function() { fun2(); }



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 07ff3c5 - Fix abs labs and llabs to work in C code.

2022-12-19 Thread Doru Bercea via cfe-commits

Author: Doru Bercea
Date: 2022-12-19T06:28:15-06:00
New Revision: 07ff3c5ccce68aed6c1a270b3f89ea14de7aa250

URL: 
https://github.com/llvm/llvm-project/commit/07ff3c5ccce68aed6c1a270b3f89ea14de7aa250
DIFF: 
https://github.com/llvm/llvm-project/commit/07ff3c5ccce68aed6c1a270b3f89ea14de7aa250.diff

LOG: Fix abs labs and llabs to work in C code.

Added: 
clang/lib/Headers/__clang_hip_stdlib.h
clang/lib/Headers/openmp_wrappers/stdlib.h
clang/test/Headers/amdgcn_openmp_device_math_c.c

Modified: 
clang/lib/Headers/CMakeLists.txt
clang/lib/Headers/__clang_hip_runtime_wrapper.h
clang/test/Headers/Inputs/include/stdlib.h
llvm/utils/gn/secondary/clang/lib/Headers/BUILD.gn

Removed: 




diff  --git a/clang/lib/Headers/CMakeLists.txt 
b/clang/lib/Headers/CMakeLists.txt
index 4206ef27e4ec3..d24691fc50fff 100644
--- a/clang/lib/Headers/CMakeLists.txt
+++ b/clang/lib/Headers/CMakeLists.txt
@@ -60,6 +60,7 @@ set(hip_files
   __clang_hip_libdevice_declares.h
   __clang_hip_cmath.h
   __clang_hip_math.h
+  __clang_hip_stdlib.h
   __clang_hip_runtime_wrapper.h
   )
 

diff  --git a/clang/lib/Headers/__clang_hip_runtime_wrapper.h 
b/clang/lib/Headers/__clang_hip_runtime_wrapper.h
index 10cec58ed12f1..0508731de1062 100644
--- a/clang/lib/Headers/__clang_hip_runtime_wrapper.h
+++ b/clang/lib/Headers/__clang_hip_runtime_wrapper.h
@@ -113,6 +113,7 @@ __attribute__((weak)) inline __device__ void free(void 
*__ptr) {
 
 #include <__clang_hip_libdevice_declares.h>
 #include <__clang_hip_math.h>
+#include <__clang_hip_stdlib.h>
 
 #if defined(__HIPCC_RTC__)
 #include <__clang_hip_cmath.h>

diff  --git a/clang/lib/Headers/__clang_hip_stdlib.h 
b/clang/lib/Headers/__clang_hip_stdlib.h
new file mode 100644
index 0..bd770e2415f95
--- /dev/null
+++ b/clang/lib/Headers/__clang_hip_stdlib.h
@@ -0,0 +1,43 @@
+/*=== __clang_hip_stdlib.h - Device-side HIP math support --===
+ *
+ * Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+ * See https://llvm.org/LICENSE.txt for license information.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ *
+ *===---===
+ */
+#ifndef __CLANG_HIP_STDLIB_H__
+
+#if !defined(__HIP__) && !defined(__OPENMP_AMDGCN__)
+#error "This file is for HIP and OpenMP AMDGCN device compilation only."
+#endif
+
+#if !defined(__cplusplus)
+
+#include 
+
+#ifdef __OPENMP_AMDGCN__
+#define __DEVICE__ static inline __attribute__((always_inline, nothrow))
+#else
+#define __DEVICE__ static __device__ inline __attribute__((always_inline))
+#endif
+
+__DEVICE__
+int abs(int __x) {
+  int __sgn = __x >> (sizeof(int) * CHAR_BIT - 1);
+  return (__x ^ __sgn) - __sgn;
+}
+__DEVICE__
+long labs(long __x) {
+  long __sgn = __x >> (sizeof(long) * CHAR_BIT - 1);
+  return (__x ^ __sgn) - __sgn;
+}
+__DEVICE__
+long long llabs(long long __x) {
+  long long __sgn = __x >> (sizeof(long long) * CHAR_BIT - 1);
+  return (__x ^ __sgn) - __sgn;
+}
+
+#endif // !defined(__cplusplus)
+
+#endif // #define __CLANG_HIP_STDLIB_H__

diff  --git a/clang/lib/Headers/openmp_wrappers/stdlib.h 
b/clang/lib/Headers/openmp_wrappers/stdlib.h
new file mode 100644
index 0..d607469e04f79
--- /dev/null
+++ b/clang/lib/Headers/openmp_wrappers/stdlib.h
@@ -0,0 +1,29 @@
+/*=== openmp_wrapper/stdlib.h -- OpenMP math.h intercept - c++ -===
+ *
+ * Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+ * See https://llvm.org/LICENSE.txt for license information.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ *
+ *===---===
+ */
+
+#ifndef __CLANG_OPENMP_STDLIB_H__
+#define __CLANG_OPENMP_STDLIB_H__
+
+#ifndef _OPENMP
+#error "This file is for OpenMP compilation only."
+#endif
+
+#include_next 
+
+#ifdef __AMDGCN__
+#pragma omp begin declare variant match(device = {arch(amdgcn)})
+
+#define __OPENMP_AMDGCN__
+#include <__clang_hip_stdlib.h>
+#undef __OPENMP_AMDGCN__
+
+#pragma omp end declare variant
+#endif
+
+#endif // __CLANG_OPENMP_STDLIB_H__

diff  --git a/clang/test/Headers/Inputs/include/stdlib.h 
b/clang/test/Headers/Inputs/include/stdlib.h
index 47cd80ca84f01..dc1ff225e3af5 100644
--- a/clang/test/Headers/Inputs/include/stdlib.h
+++ b/clang/test/Headers/Inputs/include/stdlib.h
@@ -6,4 +6,6 @@ void free(void*);
 
 #ifndef __cplusplus
 extern int abs(int __x) __attribute__((__const__));
+extern long labs(long __x) __attribute__((__const__));
+extern long long llabs(long long __x) __attribute__((__const__));
 #endif

diff  --git a/clang/test/Headers/amdgcn_openmp_device_math_c.c 
b/clang/test/Headers/amdgcn_openmp_device_math_c.c
new file mode 100644
index 0..2a54e92ffc4fd
--- /dev/null
+++ b/clang/test/Headers/amdgcn_openmp_device_math_c.c
@@ -0,0 +1,131 @@
+// NOTE: Assertions 

[clang] 658ed95 - Fix host call to nohost function with host variant.

2022-12-19 Thread Doru Bercea via cfe-commits

Author: Doru Bercea
Date: 2022-12-19T06:13:26-06:00
New Revision: 658ed9547cdd6657895339a6c390c31aa77a5698

URL: 
https://github.com/llvm/llvm-project/commit/658ed9547cdd6657895339a6c390c31aa77a5698
DIFF: 
https://github.com/llvm/llvm-project/commit/658ed9547cdd6657895339a6c390c31aa77a5698.diff

LOG: Fix host call to nohost function with host variant.

Added: 
clang/test/OpenMP/declare_target_nohost_variant_messages.cpp

Modified: 
clang/lib/Sema/SemaOpenMP.cpp
clang/test/OpenMP/declare_target_messages.cpp

Removed: 




diff  --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 49f401dfff690..8d0754c9d7c94 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -2702,6 +2702,24 @@ void Sema::finalizeOpenMPDelayedAnalysis(const 
FunctionDecl *Caller,
   }
   if (!LangOpts.OpenMPIsDevice && !LangOpts.OpenMPOffloadMandatory && DevTy &&
   *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) {
+// In OpenMP 5.2 or later, if the function has a host variant then allow
+// that to be called instead
+auto & = [](const FunctionDecl *Callee) {
+  for (OMPDeclareVariantAttr *A :
+   Callee->specific_attrs()) {
+auto *DeclRefVariant = cast(A->getVariantFuncRef());
+auto *VariantFD = cast(DeclRefVariant->getDecl());
+Optional DevTy =
+OMPDeclareTargetDeclAttr::getDeviceType(
+VariantFD->getMostRecentDecl());
+if (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host)
+  return true;
+  }
+  return false;
+};
+if (getLangOpts().OpenMP >= 52 &&
+Callee->hasAttr() && HasHostAttr(Callee))
+  return;
 // Diagnose nohost function called during host codegen.
 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
 OMPC_device_type, OMPC_DEVICE_TYPE_nohost);

diff  --git a/clang/test/OpenMP/declare_target_messages.cpp 
b/clang/test/OpenMP/declare_target_messages.cpp
index 7e7cc60e75e35..bf23813999119 100644
--- a/clang/test/OpenMP/declare_target_messages.cpp
+++ b/clang/test/OpenMP/declare_target_messages.cpp
@@ -11,10 +11,12 @@
 // RUN: %clang_cc1 -triple x86_64-apple-macos10.7.0 -verify=expected,omp51 
-fopenmp-version=51 -fopenmp-simd -fnoopenmp-use-tls -ferror-limit 100 -o - %s
 // RUN: %clang_cc1 -triple x86_64-apple-macos10.7.0 -verify=expected,omp51 
-fopenmp-version=51 -fopenmp-simd -fnoopenmp-use-tls -ferror-limit 100 -o - %s
 
+// RUN: %clang_cc1 -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa 
-fopenmp-version=52 -DVERBOSE_MODE=1 -verify=expected,omp52 -fnoopenmp-use-tls 
-ferror-limit 100 -o - %s
+
 // RUN: %clang_cc1 -triple x86_64-apple-macos10.7.0 -verify=expected,omp5 
-fopenmp -fnoopenmp-use-tls -ferror-limit 100 -o - %s
 #pragma omp end declare target // expected-error {{unexpected OpenMP directive 
'#pragma omp end declare target'}}
 
-int a, b, z; // omp5-error {{variable captured in declare target region must 
appear in a to clause}} // omp51-error {{variable captured in declare target 
region must appear in a to clause}}
+int a, b, z; // omp5-error {{variable captured in declare target region must 
appear in a to clause}} // omp51-error {{variable captured in declare target 
region must appear in a to clause}} omp52-error {{variable captured in declare 
target region must appear in a to clause}}
 __thread int t; // expected-note {{defined as threadprivate or thread local}}
 
 #pragma omp declare target . // expected-error {{expected '(' after 'declare 
target'}}
@@ -23,16 +25,16 @@ __thread int t; // expected-note {{defined as threadprivate 
or thread local}}
 void f();
 #pragma omp end declare target shared(a) // expected-warning {{extra tokens at 
the end of '#pragma omp end declare target' are ignored}}
 
-#pragma omp declare target map(a) // omp45-error {{expected at least one 'to' 
or 'link' clause}} omp5-error {{expected at least one 'to' or 'link' clause}} 
omp51-error {{expected at least one 'to', 'link' or 'indirect' clause}} 
omp45-error {{unexpected 'map' clause, only 'to' or 'link' clauses expected}} 
omp5-error {{unexpected 'map' clause, only 'to', 'link' or 'device_type' 
clauses expected}} omp51-error {{unexpected 'map' clause, only 'to', 'link', 
'device_type' or 'indirect' clauses expected}}
+#pragma omp declare target map(a) // omp45-error {{expected at least one 'to' 
or 'link' clause}} omp5-error {{expected at least one 'to' or 'link' clause}} 
omp51-error {{expected at least one 'to', 'link' or 'indirect' clause}} 
omp45-error {{unexpected 'map' clause, only 'to' or 'link' clauses expected}} 
omp5-error {{unexpected 'map' clause, only 'to', 'link' or 'device_type' 
clauses expected}} omp51-error {{unexpected 'map' clause, only 'to', 'link', 
'device_type' or 'indirect' clauses expected}} omp52-error {{unexpected 'map' 
clause, only 'enter', 'link', 'device_type' or 'indirect' clauses expected}} 
omp52-error 

[clang] 9e595e9 - [Clang][OpenMP] Add support for default to/from map types on target enter/exit data

2022-11-18 Thread Doru Bercea via cfe-commits

Author: Doru Bercea
Date: 2022-11-18T16:12:35-06:00
New Revision: 9e595e911eb539caad99fd8642328007d47c6f4e

URL: 
https://github.com/llvm/llvm-project/commit/9e595e911eb539caad99fd8642328007d47c6f4e
DIFF: 
https://github.com/llvm/llvm-project/commit/9e595e911eb539caad99fd8642328007d47c6f4e.diff

LOG: [Clang][OpenMP] Add support for default to/from map types on target 
enter/exit data

Added: 
clang/test/OpenMP/target_enter_data_ast_print_openmp52.cpp
clang/test/OpenMP/target_exit_data_ast_print_openmp52.cpp

Modified: 
clang/lib/Parse/ParseOpenMP.cpp
clang/lib/Sema/SemaOpenMP.cpp
clang/test/OpenMP/target_enter_data_ast_print.cpp
clang/test/OpenMP/target_exit_data_ast_print.cpp

Removed: 




diff  --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index d6998548cf518..820dd179610b6 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -4392,6 +4392,12 @@ bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind 
DKind,
 }
 if (Data.ExtraModifier == OMPC_MAP_unknown) {
   Data.ExtraModifier = OMPC_MAP_tofrom;
+  if (getLangOpts().OpenMP >= 52) {
+if (DKind == OMPD_target_enter_data)
+  Data.ExtraModifier = OMPC_MAP_to;
+else if (DKind == OMPD_target_exit_data)
+  Data.ExtraModifier = OMPC_MAP_from;
+  }
   Data.IsMapTypeImplicit = true;
 }
 

diff  --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index edeca632ef6d5..a67983da2b66e 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -21724,10 +21724,12 @@ static void checkMappableExpressionList(
   // target enter data
   // OpenMP [2.10.2, Restrictions, p. 99]
   // A map-type must be specified in all map clauses and must be either
-  // to or alloc.
+  // to or alloc. Starting with OpenMP 5.2 the default map type is `to` if
+  // no map type is present.
   OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
   if (DKind == OMPD_target_enter_data &&
-  !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
+  !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc ||
+SemaRef.getLangOpts().OpenMP >= 52)) {
 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
 << (IsMapTypeImplicit ? 1 : 0)
 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
@@ -21738,10 +21740,11 @@ static void checkMappableExpressionList(
   // target exit_data
   // OpenMP [2.10.3, Restrictions, p. 102]
   // A map-type must be specified in all map clauses and must be either
-  // from, release, or delete.
+  // from, release, or delete. Starting with OpenMP 5.2 the default map
+  // type is `from` if no map type is present.
   if (DKind == OMPD_target_exit_data &&
   !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
-MapType == OMPC_MAP_delete)) {
+MapType == OMPC_MAP_delete || SemaRef.getLangOpts().OpenMP >= 52)) 
{
 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
 << (IsMapTypeImplicit ? 1 : 0)
 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)

diff  --git a/clang/test/OpenMP/target_enter_data_ast_print.cpp 
b/clang/test/OpenMP/target_enter_data_ast_print.cpp
index 0ccafaef5b59a..b11d5de13de67 100644
--- a/clang/test/OpenMP/target_enter_data_ast_print.cpp
+++ b/clang/test/OpenMP/target_enter_data_ast_print.cpp
@@ -6,6 +6,10 @@
 // RUN: %clang_cc1 -fopenmp-simd -fopenmp-version=45 -x c++ -std=c++11 
-emit-pch -o %t %s
 // RUN: %clang_cc1 -fopenmp-simd -fopenmp-version=45 -std=c++11 -include-pch 
%t -fsyntax-only -verify %s -ast-print | FileCheck %s
 
+// RUN: %clang_cc1 -verify -fopenmp-simd -fopenmp-version=52 -ast-print %s | 
FileCheck %s
+// RUN: %clang_cc1 -fopenmp-simd -fopenmp-version=52 -x c++ -std=c++11 
-emit-pch -o %t %s
+// RUN: %clang_cc1 -fopenmp-simd -fopenmp-version=52 -std=c++11 -include-pch 
%t -fsyntax-only -verify %s -ast-print | FileCheck %s
+
 // RUN: %clang_cc1 -verify -fopenmp -ast-print %s | FileCheck %s
 // RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -emit-pch -o %t %s
 // RUN: %clang_cc1 -fopenmp -std=c++11 -include-pch %t -fsyntax-only -verify 
%s -ast-print | FileCheck %s

diff  --git a/clang/test/OpenMP/target_enter_data_ast_print_openmp52.cpp 
b/clang/test/OpenMP/target_enter_data_ast_print_openmp52.cpp
new file mode 100644
index 0..578f9a2542744
--- /dev/null
+++ b/clang/test/OpenMP/target_enter_data_ast_print_openmp52.cpp
@@ -0,0 +1,65 @@
+// RUN: %clang_cc1 -verify -fopenmp-simd -fopenmp-version=52 -ast-print %s | 
FileCheck %s
+// RUN: %clang_cc1 -fopenmp-simd -fopenmp-version=52 -x c++ -std=c++11 
-emit-pch -o %t %s
+// RUN: %clang_cc1 -fopenmp-simd -fopenmp-version=52 -std=c++11 -include-pch 
%t -fsyntax-only -verify %s -ast-print | 

[clang] 98bfd7f - Fix declare target implementation to support enter.

2022-11-17 Thread Doru Bercea via cfe-commits

Author: Doru Bercea
Date: 2022-11-17T17:35:53-06:00
New Revision: 98bfd7f976f166e2eb7b444f3ee86843815ca73c

URL: 
https://github.com/llvm/llvm-project/commit/98bfd7f976f166e2eb7b444f3ee86843815ca73c
DIFF: 
https://github.com/llvm/llvm-project/commit/98bfd7f976f166e2eb7b444f3ee86843815ca73c.diff

LOG: Fix declare target implementation to support enter.

Added: 


Modified: 
clang/include/clang/Basic/Attr.td
clang/include/clang/Basic/DiagnosticParseKinds.td
clang/lib/AST/AttrImpl.cpp
clang/lib/CodeGen/CGExpr.cpp
clang/lib/CodeGen/CGOpenMPRuntime.cpp
clang/lib/CodeGen/CodeGenModule.cpp
clang/lib/Parse/ParseOpenMP.cpp
clang/lib/Sema/SemaOpenMP.cpp
clang/test/OpenMP/declare_target_ast_print.cpp

Removed: 




diff  --git a/clang/include/clang/Basic/Attr.td 
b/clang/include/clang/Basic/Attr.td
index 16cf932c3760b..eaf4a6db3600e 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -3749,8 +3749,8 @@ def OMPDeclareTargetDecl : InheritableAttr {
   let Documentation = [OMPDeclareTargetDocs];
   let Args = [
 EnumArgument<"MapType", "MapTypeTy",
- [ "to", "link" ],
- [ "MT_To", "MT_Link" ]>,
+ [ "to", "enter", "link" ],
+ [ "MT_To", "MT_Enter", "MT_Link" ]>,
 EnumArgument<"DevType", "DevTypeTy",
  [ "host", "nohost", "any" ],
  [ "DT_Host", "DT_NoHost", "DT_Any" ]>,

diff  --git a/clang/include/clang/Basic/DiagnosticParseKinds.td 
b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 674d6bd34fc54..27cd3da1f191c 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1383,12 +1383,22 @@ def note_omp_assumption_clause_continue_here
 : Note<"the ignored tokens spans until here">;
 def err_omp_declare_target_unexpected_clause: Error<
   "unexpected '%0' clause, only %select{'device_type'|'to' or 'link'|'to', 
'link' or 'device_type'|'device_type', 'indirect'|'to', 'link', 'device_type' 
or 'indirect'}1 clauses expected">;
+def err_omp_declare_target_unexpected_clause_52: Error<
+  "unexpected '%0' clause, only %select{'device_type'|'enter' or 
'link'|'enter', 'link' or 'device_type'|'device_type', 'indirect'|'enter', 
'link', 'device_type' or 'indirect'}1 clauses expected">;
 def err_omp_begin_declare_target_unexpected_implicit_to_clause: Error<
   "unexpected '(', only 'to', 'link' or 'device_type' clauses expected for 
'begin declare target' directive">;
-def err_omp_declare_target_unexpected_clause_after_implicit_to: Error<
+def err_omp_declare_target_wrong_clause_after_implicit_to: Error<
   "unexpected clause after an implicit 'to' clause">;
+def err_omp_declare_target_wrong_clause_after_implicit_enter: Error<
+  "unexpected clause after an implicit 'enter' clause">;
 def err_omp_declare_target_missing_to_or_link_clause: Error<
   "expected at least one %select{'to' or 'link'|'to', 'link' or 'indirect'}0 
clause">;
+def err_omp_declare_target_missing_enter_or_link_clause: Error<
+  "expected at least one %select{'enter' or 'link'|'enter', 'link' or 
'indirect'}0 clause">;
+def err_omp_declare_target_unexpected_to_clause: Error<
+  "unexpected 'to' clause, use 'enter' instead">;
+def err_omp_declare_target_unexpected_enter_clause: Error<
+  "unexpected 'enter' clause, use 'to' instead">;
 def err_omp_declare_target_multiple : Error<
   "%0 appears multiple times in clauses on the same declare target directive">;
 def err_omp_declare_target_indirect_device_type: Error<

diff  --git a/clang/lib/AST/AttrImpl.cpp b/clang/lib/AST/AttrImpl.cpp
index cecbd703ac61e..da842f6b190e7 100644
--- a/clang/lib/AST/AttrImpl.cpp
+++ b/clang/lib/AST/AttrImpl.cpp
@@ -137,7 +137,7 @@ void OMPDeclareTargetDeclAttr::printPrettyPragma(
   // Use fake syntax because it is for testing and debugging purpose only.
   if (getDevType() != DT_Any)
 OS << " device_type(" << ConvertDevTypeTyToStr(getDevType()) << ")";
-  if (getMapType() != MT_To)
+  if (getMapType() != MT_To && getMapType() != MT_Enter)
 OS << ' ' << ConvertMapTypeTyToStr(getMapType());
   if (Expr *E = getIndirectExpr()) {
 OS << " indirect(";

diff  --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index ee09a8566c371..77085ff34fca2 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -2495,14 +2495,16 @@ static Address 
emitDeclTargetVarDeclLValue(CodeGenFunction ,
const VarDecl *VD, QualType T) {
   llvm::Optional Res =
   OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
-  // Return an invalid address if variable is MT_To and unified
-  // memory is not enabled. For all other cases: MT_Link and
-  // MT_To with unified memory, return a valid address.
-  if (!Res || (*Res == OMPDeclareTargetDeclAttr::MT_To &&
+  // Return an