jplehr created this revision.
Herald added subscribers: guansong, yaxunl.
Herald added a project: All.
jplehr requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: cfe-commits, sstefan1.
Herald added a project: clang.

Resolves a potential segmentation fault in target offload when a constant
variable is mapped as tofrom to the device but placed into constant
memory on the host.

Sema checks if targets of map from: or tofrom: are const-qualified and
warns.
CodeGen changes the map to to: for such variables to remove the write
back to host.
OpenMP Clarification: https://github.com/OpenMP/spec/issues/2158


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137649

Files:
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/Sema/SemaOpenMP.cpp
  clang/test/OpenMP/distribute_firstprivate_messages.cpp
  clang/test/OpenMP/distribute_parallel_for_firstprivate_messages.cpp
  clang/test/OpenMP/distribute_parallel_for_lastprivate_messages.cpp
  clang/test/OpenMP/distribute_parallel_for_reduction_messages.cpp
  clang/test/OpenMP/distribute_parallel_for_simd_aligned_messages.cpp
  clang/test/OpenMP/distribute_parallel_for_simd_shared_messages.cpp
  clang/test/OpenMP/distribute_simd_aligned_messages.cpp
  clang/test/OpenMP/distribute_simd_firstprivate_messages.cpp
  clang/test/OpenMP/distribute_simd_lastprivate_messages.cpp
  clang/test/OpenMP/distribute_simd_linear_messages.cpp
  clang/test/OpenMP/distribute_simd_reduction_messages.cpp
  clang/test/OpenMP/target_teams_distribute_firstprivate_messages.cpp
  clang/test/OpenMP/teams_distribute_parallel_for_simd_aligned_messages.cpp
  clang/test/OpenMP/teams_distribute_parallel_for_simd_linear_messages.cpp
  clang/test/OpenMP/teams_distribute_simd_aligned_messages.cpp
  clang/test/OpenMP/teams_distribute_simd_linear_messages.cpp

Index: clang/test/OpenMP/teams_distribute_simd_linear_messages.cpp
===================================================================
--- clang/test/OpenMP/teams_distribute_simd_linear_messages.cpp
+++ clang/test/OpenMP/teams_distribute_simd_linear_messages.cpp
@@ -259,7 +259,7 @@
 
 
 #pragma omp target
-#pragma omp teams distribute simd linear (a, b) // expected-error {{incomplete type 'S1' where a complete type is required}} expected-error {{linear variable with incomplete type 'S1'}} expected-error {{argument of a linear clause should be of integral or pointer type, not 'S2'}}
+#pragma omp teams distribute simd linear (a, b) // expected-error {{incomplete type 'S1' where a complete type is required}} expected-error {{linear variable with incomplete type 'S1'}} expected-error {{argument of a linear clause should be of integral or pointer type, not 'S2'}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}}
   for (int k = 0; k < argc; ++k) ++k;
 
 #pragma omp target
Index: clang/test/OpenMP/teams_distribute_simd_aligned_messages.cpp
===================================================================
--- clang/test/OpenMP/teams_distribute_simd_aligned_messages.cpp
+++ clang/test/OpenMP/teams_distribute_simd_aligned_messages.cpp
@@ -254,7 +254,7 @@
   for (int k = 0; k < argc; ++k) ++k;
 
 #pragma omp target
-#pragma omp teams distribute simd aligned (a, b) // expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S1'}} expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S2'}} expected-error {{incomplete type 'S1' where a complete type is required}}
+#pragma omp teams distribute simd aligned (a, b) // expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S1'}} expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S2'}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}}
   for (int k = 0; k < argc; ++k) ++k;
 
 #pragma omp target
Index: clang/test/OpenMP/teams_distribute_parallel_for_simd_linear_messages.cpp
===================================================================
--- clang/test/OpenMP/teams_distribute_parallel_for_simd_linear_messages.cpp
+++ clang/test/OpenMP/teams_distribute_parallel_for_simd_linear_messages.cpp
@@ -259,7 +259,7 @@
 
 
 #pragma omp target
-#pragma omp teams distribute parallel for simd linear (a, b) // expected-error {{linear variable with incomplete type 'S1'}} expected-error {{argument of a linear clause should be of integral or pointer type, not 'S2'}} expected-error {{incomplete type 'S1' where a complete type is required}}
+#pragma omp teams distribute parallel for simd linear (a, b) // expected-error {{linear variable with incomplete type 'S1'}} expected-error {{argument of a linear clause should be of integral or pointer type, not 'S2'}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}}
   for (int k = 0; k < argc; ++k) ++k;
 
 #pragma omp target
Index: clang/test/OpenMP/teams_distribute_parallel_for_simd_aligned_messages.cpp
===================================================================
--- clang/test/OpenMP/teams_distribute_parallel_for_simd_aligned_messages.cpp
+++ clang/test/OpenMP/teams_distribute_parallel_for_simd_aligned_messages.cpp
@@ -253,7 +253,7 @@
   for (int k = 0; k < argc; ++k) ++k;
 
 #pragma omp target
-#pragma omp teams distribute parallel for simd aligned (a, b) // expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S1'}} expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S2'}} expected-error {{incomplete type 'S1' where a complete type is required}}
+#pragma omp teams distribute parallel for simd aligned (a, b) // expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S1'}} expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S2'}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}}
   for (int k = 0; k < argc; ++k) ++k;
 
 #pragma omp target
Index: clang/test/OpenMP/target_teams_distribute_firstprivate_messages.cpp
===================================================================
--- clang/test/OpenMP/target_teams_distribute_firstprivate_messages.cpp
+++ clang/test/OpenMP/target_teams_distribute_firstprivate_messages.cpp
@@ -119,7 +119,7 @@
   for (i = 0; i < argc; ++i) foo();
 
 #pragma omp target
-#pragma omp teams distribute firstprivate(ca) // expected-error {{no matching constructor for initialization of 'S3'}} expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp teams distribute firstprivate(ca) // expected-error {{no matching constructor for initialization of 'S3'}} expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ca' from target map clause}}
   for (i = 0; i < argc; ++i) foo();
 
 #pragma omp target teams distribute firstprivate(da, z)
Index: clang/test/OpenMP/distribute_simd_reduction_messages.cpp
===================================================================
--- clang/test/OpenMP/distribute_simd_reduction_messages.cpp
+++ clang/test/OpenMP/distribute_simd_reduction_messages.cpp
@@ -379,17 +379,17 @@
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd reduction(+ : ba) // expected-error {{const-qualified variable cannot be reduction}} expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute simd reduction(+ : ba) // expected-error {{const-qualified variable cannot be reduction}} expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ba' from target map clause}}
   for (int i = 0; i < 10; ++i)
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd reduction(* : ca) // expected-error {{const-qualified variable cannot be reduction}} expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute simd reduction(* : ca) // expected-error {{const-qualified variable cannot be reduction}} expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ca' from target map clause}}
   for (int i = 0; i < 10; ++i)
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd reduction(- : da) // expected-error {{const-qualified variable cannot be reduction}}
+#pragma omp distribute simd reduction(- : da) // expected-error {{const-qualified variable cannot be reduction}} expected-warning {{discarding implicit write to const-qualified variable 'da' from target map clause}}
   for (int i = 0; i < 10; ++i)
     foo();
 #pragma omp target
Index: clang/test/OpenMP/distribute_simd_linear_messages.cpp
===================================================================
--- clang/test/OpenMP/distribute_simd_linear_messages.cpp
+++ clang/test/OpenMP/distribute_simd_linear_messages.cpp
@@ -292,7 +292,7 @@
 
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd linear (a, b) // expected-error {{linear variable with incomplete type 'S1'}} expected-error {{argument of a linear clause should be of integral or pointer type, not 'S2'}} expected-error {{incomplete type 'S1' where a complete type is required}}
+#pragma omp distribute simd linear (a, b) // expected-error {{linear variable with incomplete type 'S1'}} expected-error {{argument of a linear clause should be of integral or pointer type, not 'S2'}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}}
   for (int k = 0; k < argc; ++k) ++k;
 
 #pragma omp target
Index: clang/test/OpenMP/distribute_simd_lastprivate_messages.cpp
===================================================================
--- clang/test/OpenMP/distribute_simd_lastprivate_messages.cpp
+++ clang/test/OpenMP/distribute_simd_lastprivate_messages.cpp
@@ -120,7 +120,7 @@
     ++k;
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd lastprivate(a, b) // expected-error {{lastprivate variable with incomplete type 'S1'}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute simd lastprivate(a, b) // expected-error {{lastprivate variable with incomplete type 'S1'}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}}
   for (int k = 0; k < argc; ++k)
     ++k;
 #pragma omp target
@@ -229,7 +229,7 @@
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd lastprivate(a, b, c, d, f) // expected-error {{lastprivate variable with incomplete type 'S1'}} expected-error 1 {{const-qualified variable without mutable fields cannot be lastprivate}} expected-error 2 {{const-qualified variable cannot be lastprivate}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute simd lastprivate(a, b, c, d, f) // expected-error {{lastprivate variable with incomplete type 'S1'}} expected-error 1 {{const-qualified variable without mutable fields cannot be lastprivate}} expected-error 2 {{const-qualified variable cannot be lastprivate}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'c' from target map clause}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
 #pragma omp target
@@ -244,17 +244,17 @@
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd lastprivate(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute simd lastprivate(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ba' from target map claus}}
   for (i = 0; i < argc; ++i)
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd lastprivate(ca) // expected-error {{const-qualified variable without mutable fields cannot be lastprivate}} expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute simd lastprivate(ca) // expected-error {{const-qualified variable without mutable fields cannot be lastprivate}} expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ca' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd lastprivate(da) // expected-error {{const-qualified variable cannot be lastprivate}}
+#pragma omp distribute simd lastprivate(da) // expected-error {{const-qualified variable cannot be lastprivate}} expected-warning {{discarding implicit write to const-qualified variable 'da' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
   int xa;
Index: clang/test/OpenMP/distribute_simd_firstprivate_messages.cpp
===================================================================
--- clang/test/OpenMP/distribute_simd_firstprivate_messages.cpp
+++ clang/test/OpenMP/distribute_simd_firstprivate_messages.cpp
@@ -111,7 +111,7 @@
     ++k;
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd firstprivate(z, a, b) // expected-error {{firstprivate variable with incomplete type 'S1'}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute simd firstprivate(z, a, b) // expected-error {{firstprivate variable with incomplete type 'S1'}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}}
   for (int k = 0; k < argc; ++k)
     ++k;
 #pragma omp target
@@ -233,7 +233,7 @@
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd firstprivate(a, b, c, d, f) // expected-error {{firstprivate variable with incomplete type 'S1'}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute simd firstprivate(a, b, c, d, f) // expected-error {{firstprivate variable with incomplete type 'S1'}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}} expected-warning {{discarding implicit write to const-qualified variable 'c' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
 #pragma omp target
@@ -248,17 +248,17 @@
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd firstprivate(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute simd firstprivate(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ba' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd firstprivate(ca) // expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute simd firstprivate(ca) // expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ca' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd firstprivate(da) // OK
+#pragma omp distribute simd firstprivate(da) // expected-warning {{discarding implicit write to const-qualified variable 'da' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
   int xa;
Index: clang/test/OpenMP/distribute_simd_aligned_messages.cpp
===================================================================
--- clang/test/OpenMP/distribute_simd_aligned_messages.cpp
+++ clang/test/OpenMP/distribute_simd_aligned_messages.cpp
@@ -287,7 +287,7 @@
 
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute simd aligned (a, b) // expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S1'}} expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S2'}} expected-error {{incomplete type 'S1' where a complete type is required}}
+#pragma omp distribute simd aligned (a, b) // expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S1'}} expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S2'}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}}
   for (int k = 0; k < argc; ++k) ++k;
 
 #pragma omp target
Index: clang/test/OpenMP/distribute_parallel_for_simd_shared_messages.cpp
===================================================================
--- clang/test/OpenMP/distribute_parallel_for_simd_shared_messages.cpp
+++ clang/test/OpenMP/distribute_parallel_for_simd_shared_messages.cpp
@@ -117,7 +117,7 @@
 
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for simd shared (a, b, c, d, f) // expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for simd shared (a, b, c, d, f) // expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}} expected-warning {{discarding implicit write to const-qualified variable 'c' from target map clause}}
   for(int k = 0 ; k < n ; k++) {
     acc++;
   }
@@ -131,21 +131,21 @@
 
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for simd shared(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for simd shared(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ba' from target map clause}}
   for(int k = 0 ; k < n ; k++) {
     acc++;
   }
 
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for simd shared(ca) // expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for simd shared(ca) // expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ca' from target map clause}}
   for(int k = 0 ; k < n ; k++) {
     acc++;
   }
 
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for simd shared(da)
+#pragma omp distribute parallel for simd shared(da) // expected-warning {{discarding implicit write to const-qualified variable 'da' from target map clause}}
   for(int k = 0 ; k < n ; k++) {
     acc++;
   }
@@ -291,7 +291,7 @@
 
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for simd shared (a, b, c, d, f) // expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for simd shared (a, b, c, d, f) // expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}} expected-warning {{discarding implicit write to const-qualified variable 'c' from target map clause}}
   for(int k = 0 ; k < n ; k++) {
     acc++;
   }
@@ -305,21 +305,21 @@
 
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for simd shared(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for simd shared(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ba' from target map clause}}
   for(int k = 0 ; k < n ; k++) {
     acc++;
   }
 
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for simd shared(ca) // expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for simd shared(ca) // expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ca' from target map clause}}
   for(int k = 0 ; k < n ; k++) {
     acc++;
   }
 
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for simd shared(da)
+#pragma omp distribute parallel for simd shared(da) // expected-warning {{discarding implicit write to const-qualified variable 'da' from target map claus}}
   for(int k = 0 ; k < n ; k++) {
     acc++;
   }
Index: clang/test/OpenMP/distribute_parallel_for_simd_aligned_messages.cpp
===================================================================
--- clang/test/OpenMP/distribute_parallel_for_simd_aligned_messages.cpp
+++ clang/test/OpenMP/distribute_parallel_for_simd_aligned_messages.cpp
@@ -287,7 +287,7 @@
 
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for simd aligned (a, b) // expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S1'}} expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S2'}} expected-error {{incomplete type 'S1' where a complete type is required}}
+#pragma omp distribute parallel for simd aligned (a, b) // expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S1'}} expected-error {{argument of aligned clause should be array, pointer, reference to array or reference to pointer, not 'S2'}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}}
   for (int k = 0; k < argc; ++k) ++k;
 
 #pragma omp target
Index: clang/test/OpenMP/distribute_parallel_for_reduction_messages.cpp
===================================================================
--- clang/test/OpenMP/distribute_parallel_for_reduction_messages.cpp
+++ clang/test/OpenMP/distribute_parallel_for_reduction_messages.cpp
@@ -374,17 +374,17 @@
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for reduction(+ : ba) // expected-error {{const-qualified variable cannot be reduction}} expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for reduction(+ : ba) // expected-error {{const-qualified variable cannot be reduction}} expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ba' from target map clause}}
   for (int i = 0; i < 10; ++i)
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for reduction(* : ca) // expected-error {{const-qualified variable cannot be reduction}} expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for reduction(* : ca) // expected-error {{const-qualified variable cannot be reduction}} expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ca' from target map clause}}
   for (int i = 0; i < 10; ++i)
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for reduction(- : da) // expected-error {{const-qualified variable cannot be reduction}}
+#pragma omp distribute parallel for reduction(- : da) // expected-error {{const-qualified variable cannot be reduction}} expected-warning {{discarding implicit write to const-qualified variable 'da' from target map clause}}
   for (int i = 0; i < 10; ++i)
     foo();
 #pragma omp target
Index: clang/test/OpenMP/distribute_parallel_for_lastprivate_messages.cpp
===================================================================
--- clang/test/OpenMP/distribute_parallel_for_lastprivate_messages.cpp
+++ clang/test/OpenMP/distribute_parallel_for_lastprivate_messages.cpp
@@ -119,7 +119,7 @@
     ++k;
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for lastprivate(a, b) // expected-error {{lastprivate variable with incomplete type 'S1'}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for lastprivate(a, b) // expected-error {{lastprivate variable with incomplete type 'S1'}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}}
   for (int k = 0; k < argc; ++k)
     ++k;
 #pragma omp target
@@ -228,7 +228,7 @@
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for lastprivate(a, b, c, d, f) // expected-error {{lastprivate variable with incomplete type 'S1'}} expected-error 1 {{const-qualified variable without mutable fields cannot be lastprivate}} expected-error 2 {{const-qualified variable cannot be lastprivate}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for lastprivate(a, b, c, d, f) // expected-error {{lastprivate variable with incomplete type 'S1'}} expected-error 1 {{const-qualified variable without mutable fields cannot be lastprivate}} expected-error 2 {{const-qualified variable cannot be lastprivate}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'c' from target map clause}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
 #pragma omp target
@@ -243,17 +243,17 @@
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for lastprivate(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for lastprivate(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ba' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for lastprivate(ca) // expected-error {{const-qualified variable without mutable fields cannot be lastprivate}} expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for lastprivate(ca) // expected-error {{const-qualified variable without mutable fields cannot be lastprivate}} expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ca' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for lastprivate(da) // expected-error {{const-qualified variable cannot be lastprivate}}
+#pragma omp distribute parallel for lastprivate(da) // expected-error {{const-qualified variable cannot be lastprivate}} expected-warning {{discarding implicit write to const-qualified variable 'da' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
   int xa;
Index: clang/test/OpenMP/distribute_parallel_for_firstprivate_messages.cpp
===================================================================
--- clang/test/OpenMP/distribute_parallel_for_firstprivate_messages.cpp
+++ clang/test/OpenMP/distribute_parallel_for_firstprivate_messages.cpp
@@ -119,7 +119,7 @@
     ++k;
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for firstprivate(a, b) // expected-error {{firstprivate variable with incomplete type 'S1'}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for firstprivate(a, b) // expected-error {{firstprivate variable with incomplete type 'S1'}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}}
   for (int k = 0; k < argc; ++k)
     ++k;
 #pragma omp target
@@ -241,7 +241,7 @@
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for firstprivate(a, b, c, d, f) // expected-error {{firstprivate variable with incomplete type 'S1'}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for firstprivate(a, b, c, d, f) // expected-error {{firstprivate variable with incomplete type 'S1'}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}} expected-warning {{discarding implicit write to const-qualified variable 'c' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
 #pragma omp target
@@ -256,17 +256,17 @@
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for firstprivate(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for firstprivate(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ba' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for firstprivate(ca) // expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+#pragma omp distribute parallel for firstprivate(ca) // expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ca' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
 #pragma omp target
 #pragma omp teams
-#pragma omp distribute parallel for firstprivate(da) // OK
+#pragma omp distribute parallel for firstprivate(da) // expected-warning {{discarding implicit write to const-qualified variable 'da' from target map clause}}
   for (i = 0; i < argc; ++i)
     foo();
   int xa;
Index: clang/test/OpenMP/distribute_firstprivate_messages.cpp
===================================================================
--- clang/test/OpenMP/distribute_firstprivate_messages.cpp
+++ clang/test/OpenMP/distribute_firstprivate_messages.cpp
@@ -95,7 +95,7 @@
   for (i = 0; i < argc; ++i) foo();
   #pragma omp target
   #pragma omp teams
-  #pragma omp distribute firstprivate (a, b, c, d, f) // expected-error {{firstprivate variable with incomplete type 'S1'}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-error {{no matching constructor for initialization of 'S3'}}
+  #pragma omp distribute firstprivate (a, b, c, d, f) // expected-error {{firstprivate variable with incomplete type 'S1'}} expected-warning {{Type 'const S2' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'const S3' is not trivially copyable and not guaranteed to be mapped correctly}} expected-error {{incomplete type 'S1' where a complete type is required}} expected-error {{no matching constructor for initialization of 'S3'}} expected-warning {{discarding implicit write to const-qualified variable 'b' from target map clause}} expected-warning {{discarding implicit write to const-qualified variable 'c' from target map clause}}
   for (i = 0; i < argc; ++i) foo();
   #pragma omp target
   #pragma omp teams
@@ -103,15 +103,15 @@
   for (i = 0; i < argc; ++i) foo();
   #pragma omp target
   #pragma omp teams
-  #pragma omp distribute firstprivate(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+  #pragma omp distribute firstprivate(ba) // expected-warning {{Type 'const S2[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ba' from target map clause}}
   for (i = 0; i < argc; ++i) foo();
   #pragma omp target
   #pragma omp teams
-  #pragma omp distribute firstprivate(ca) // expected-error {{no matching constructor for initialization of 'S3'}} expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}}
+  #pragma omp distribute firstprivate(ca) // expected-error {{no matching constructor for initialization of 'S3'}} expected-warning {{Type 'const S3[5]' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{discarding implicit write to const-qualified variable 'ca' from target map clause}}
   for (i = 0; i < argc; ++i) foo();
   #pragma omp target
   #pragma omp teams
-  #pragma omp distribute firstprivate(da, z)
+  #pragma omp distribute firstprivate(da, z) // expected-warning {{discarding implicit write to const-qualified variable 'da' from target map clause}}
   for (i = 0; i < argc; ++i) foo();
   #pragma omp target
   #pragma omp teams
Index: clang/lib/Sema/SemaOpenMP.cpp
===================================================================
--- clang/lib/Sema/SemaOpenMP.cpp
+++ clang/lib/Sema/SemaOpenMP.cpp
@@ -21699,6 +21699,26 @@
         continue;
       }
 
+      // target, target data
+      // OpenMP 6.0 [clarification]
+      // Warn if map(from:) or map(toFrom:) is applied to const-qualified entity
+      // see https://github.com/OpenMP/spec/issues/2158
+      if ((DKind == OMPD_target_data ||
+           isOpenMPTargetExecutionDirective(DKind)) &&
+          (MapType == OMPC_MAP_from || MapType == OMPC_MAP_tofrom)) {
+        // TODO: const object w/o mutable member, but member is mapped
+        if (const auto DRE = dyn_cast<DeclRefExpr>(VE)) {
+          if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
+            if (VD->isConstexpr() ||
+                VD->getType().isConstant(SemaRef.getASTContext())) {
+              SemaRef.Diag(ELoc, diag::warn_omp_map_to_constant_variable)
+                  << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
+                  << getOpenMPDirectiveName(DKind) << VD;
+            }
+          }
+        }
+      }
+
       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
       // A list item cannot appear in both a map clause and a data-sharing
       // attribute clause on the same construct
Index: clang/lib/CodeGen/CGOpenMPRuntime.cpp
===================================================================
--- clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -8951,6 +8951,14 @@
           /*ForDeviceAddr=*/false, VD, VarRef, OverlappedComponents);
       IsFirstComponentList = false;
     }
+
+    const auto IsConstQualified = [&](const ValueDecl *V) {
+      const auto VD = dyn_cast_or_null<VarDecl>(V);
+      if (!VD) {
+        return false;
+      }
+      return VD->isConstexpr() || VD->getType().isConstant(VD->getASTContext());
+    };
     // Go through other elements without overlapped elements.
     for (const MapData &L : DeclComponentLists) {
       OMPClauseMappableExprCommon::MappableExprComponentListRef Components;
@@ -8962,11 +8970,19 @@
       std::tie(Components, MapType, MapModifiers, IsImplicit, Mapper, VarRef) =
           L;
       auto It = OverlappedData.find(&L);
-      if (It == OverlappedData.end())
+      if (It == OverlappedData.end()) {
+        if (MapType == OpenMPMapClauseKind::OMPC_MAP_tofrom ||
+            MapType == OpenMPMapClauseKind::OMPC_MAP_from) {
+          const auto FC = Components.front();
+          if (IsConstQualified(FC.getAssociatedDeclaration())) {
+            MapType = OpenMPMapClauseKind::OMPC_MAP_to;
+          }
+        }
         generateInfoForComponentList(MapType, MapModifiers, llvm::None,
                                      Components, CombinedInfo, PartialStruct,
                                      IsFirstComponentList, IsImplicit, Mapper,
                                      /*ForDeviceAddr=*/false, VD, VarRef);
+      }
       IsFirstComponentList = false;
     }
   }
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10794,6 +10794,9 @@
   "threadprivate variables are not allowed in '%0' clause">;
 def err_omp_wrong_ordered_loop_count : Error<
   "the parameter of the 'ordered' clause must be greater than or equal to the parameter of the 'collapse' clause">;
+def warn_omp_map_to_constant_variable : Warning<
+  "discarding implicit write to const-qualified variable %2 from %1 map clause">,
+  InGroup<OpenMPMapping>;
 def note_collapse_loop_count : Note<
   "parameter of the 'collapse' clause">;
 def err_omp_clauses_mutually_exclusive : Error<
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D137649: [Clang][... Jan-Patrick Lehr via Phabricator via cfe-commits

Reply via email to