| 1 |
// RUN: %clang_cc1 -verify -fopenmp -fopenmp-targets=powerpc64le-ibm-linux-gnu -x c++ -triple powerpc64le-unknown-unknown -emit-llvm %s -o - | FileCheck %s |
| 2 |
// RUN: %clang_cc1 -fopenmp -fopenmp-targets=powerpc64le-ibm-linux-gnu -x c++ -std=c++11 -triple powerpc64le-unknown-unknown -emit-pch -o %t %s |
| 3 |
// RUN: %clang_cc1 -fopenmp -fopenmp-targets=powerpc64le-ibm-linux-gnu -x c++ -triple powerpc64le-unknown-unknown -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s |
| 4 |
// expected-no-diagnostics |
| 5 |
|
| 6 |
#ifndef HEADER |
| 7 |
#define HEADER |
| 8 |
|
| 9 |
/// ======================================================================== |
| 10 |
/// Test: Firstprivate pointer handling in OpenMP target regions |
| 11 |
/// ======================================================================== |
| 12 |
/// |
| 13 |
/// This test verifies that pointers with firstprivate semantics get the |
| 14 |
/// OMP_MAP_LITERAL flag, enabling the runtime to pass pointer values directly |
| 15 |
/// without performing present table lookups. |
| 16 |
/// |
| 17 |
/// Map type values: |
| 18 |
/// 288 = OMP_MAP_TARGET_PARAM (32) + OMP_MAP_LITERAL (256) |
| 19 |
/// Used for explicit firstprivate(ptr) |
| 20 |
/// |
| 21 |
/// 800 = OMP_MAP_TARGET_PARAM (32) + OMP_MAP_LITERAL (256) + OMP_MAP_IS_PTR (512) |
| 22 |
/// Used for implicit firstprivate pointers (e.g., from defaultmap clauses) |
| 23 |
/// Note: 512 is OMP_MAP_IS_PTR, not IMPLICIT. Implicitness is tracked separately. |
| 24 |
/// |
| 25 |
/// 544 = OMP_MAP_TARGET_PARAM (32) + OMP_MAP_IS_PTR (512) |
| 26 |
/// Incorrect behavior - missing LITERAL flag, causes runtime present table lookup |
| 27 |
/// |
| 28 |
|
| 29 |
///========================================================================== |
| 30 |
/// Test 1: Explicit firstprivate(pointer) → map type 288 |
| 31 |
///========================================================================== |
| 32 |
|
| 33 |
// CHECK-DAG: @.offload_maptypes{{[^.]*}} = private unnamed_addr constant [1 x i64] [i64 288] |
| 34 |
// CHECK-DAG: @.offload_sizes{{[^.]*}} = private unnamed_addr constant [1 x i64] zeroinitializer |
| 35 |
|
| 36 |
void test1_explicit_firstprivate() { |
| 37 |
double *ptr = nullptr; |
| 38 |
|
| 39 |
// Explicit firstprivate should generate map type 288 |
| 40 |
// (TARGET_PARAM | LITERAL, no IS_PTR flag for explicit clauses) |
| 41 |
#pragma omp target firstprivate(ptr) |
| 42 |
{ |
| 43 |
if (ptr) ptr[0] = 1.0; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
///========================================================================== |
| 48 |
/// Test 2: defaultmap(firstprivate:pointer) → map type 800 |
| 49 |
///========================================================================== |
| 50 |
|
| 51 |
// CHECK-DAG: @.offload_maptypes{{.*}} = private unnamed_addr constant [1 x i64] [i64 800] |
| 52 |
// CHECK-DAG: @.offload_sizes{{.*}} = private unnamed_addr constant [1 x i64] zeroinitializer |
| 53 |
|
| 54 |
void test2_defaultmap_firstprivate_pointer() { |
| 55 |
double *ptr = nullptr; |
| 56 |
|
| 57 |
// defaultmap(firstprivate:pointer) creates implicit firstprivate |
| 58 |
// Should generate map type 800 (TARGET_PARAM | LITERAL | IS_PTR) |
| 59 |
#pragma omp target defaultmap(firstprivate:pointer) |
| 60 |
{ |
| 61 |
if (ptr) ptr[0] = 2.0; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
///========================================================================== |
| 66 |
/// Test 3: defaultmap(firstprivate:scalar) with double → map type 800 |
| 67 |
///========================================================================== |
| 68 |
|
| 69 |
// CHECK-DAG: @.offload_maptypes{{.*}} = private unnamed_addr constant [1 x i64] [i64 800] |
| 70 |
|
| 71 |
void test3_defaultmap_scalar_double() { |
| 72 |
double d = 3.0; |
| 73 |
|
| 74 |
// OpenMP's "scalar" category excludes pointers but includes arithmetic types |
| 75 |
// Double gets implicit firstprivate → map type 800 |
| 76 |
#pragma omp target defaultmap(firstprivate:scalar) |
| 77 |
{ |
| 78 |
d += 1.0; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
///========================================================================== |
| 83 |
/// Test 4: Pointer with defaultmap(firstprivate:scalar) → map type 800 |
| 84 |
///========================================================================== |
| 85 |
|
| 86 |
// CHECK-DAG: @.offload_maptypes{{.*}} = private unnamed_addr constant [1 x i64] [i64 800] |
| 87 |
// CHECK-DAG: @.offload_sizes{{.*}} = private unnamed_addr constant [1 x i64] zeroinitializer |
| 88 |
|
| 89 |
void test4_pointer_with_scalar_defaultmap() { |
| 90 |
double *ptr = nullptr; |
| 91 |
|
| 92 |
// Note: defaultmap(firstprivate:scalar) does NOT apply to pointers (scalar excludes pointers). |
| 93 |
// However, the pointer still gets 800 because in OpenMP 5.0+, pointers without explicit |
| 94 |
// data-sharing attributes are implicitly firstprivate and lowered as IS_PTR|LITERAL|TARGET_PARAM. |
| 95 |
// This is the default pointer behavior, NOT due to the scalar defaultmap. |
| 96 |
#pragma omp target defaultmap(firstprivate:scalar) |
| 97 |
{ |
| 98 |
if (ptr) ptr[0] = 4.0; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
///========================================================================== |
| 103 |
/// Test 5: Multiple pointers with explicit firstprivate → all get 288 |
| 104 |
///========================================================================== |
| 105 |
|
| 106 |
// CHECK-DAG: @.offload_maptypes{{.*}} = private unnamed_addr constant [3 x i64] [i64 288, i64 288, i64 288] |
| 107 |
// CHECK-DAG: @.offload_sizes{{.*}} = private unnamed_addr constant [3 x i64] zeroinitializer |
| 108 |
|
| 109 |
void test5_multiple_firstprivate() { |
| 110 |
int *a = nullptr; |
| 111 |
float *b = nullptr; |
| 112 |
double *c = nullptr; |
| 113 |
|
| 114 |
// All explicit firstprivate pointers get map type 288 |
| 115 |
#pragma omp target firstprivate(a, b, c) |
| 116 |
{ |
| 117 |
if (a) a[0] = 6; |
| 118 |
if (b) b[0] = 7.0f; |
| 119 |
if (c) c[0] = 8.0; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
///========================================================================== |
| 124 |
/// Test 6: Pointer to const with firstprivate → map type 288 |
| 125 |
///========================================================================== |
| 126 |
|
| 127 |
// CHECK-DAG: @.offload_maptypes{{.*}} = private unnamed_addr constant [1 x i64] [i64 288] |
| 128 |
// CHECK-DAG: @.offload_sizes{{.*}} = private unnamed_addr constant [1 x i64] zeroinitializer |
| 129 |
|
| 130 |
void test6_const_pointer() { |
| 131 |
const double *const_ptr = nullptr; |
| 132 |
|
| 133 |
// Const pointer with explicit firstprivate → 288 |
| 134 |
#pragma omp target firstprivate(const_ptr) |
| 135 |
{ |
| 136 |
if (const_ptr) { |
| 137 |
double val = const_ptr[0]; |
| 138 |
(void)val; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
///========================================================================== |
| 144 |
/// Test 7: Pointer-to-pointer with firstprivate → map type 288 |
| 145 |
///========================================================================== |
| 146 |
|
| 147 |
// CHECK-DAG: @.offload_maptypes{{.*}} = private unnamed_addr constant [1 x i64] [i64 288] |
| 148 |
// CHECK-DAG: @.offload_sizes{{.*}} = private unnamed_addr constant [1 x i64] zeroinitializer |
| 149 |
|
| 150 |
void test7_pointer_to_pointer() { |
| 151 |
int **pp = nullptr; |
| 152 |
|
| 153 |
// Pointer-to-pointer with explicit firstprivate → 288 |
| 154 |
#pragma omp target firstprivate(pp) |
| 155 |
{ |
| 156 |
if (pp && *pp) (*pp)[0] = 9; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
///========================================================================== |
| 161 |
/// Verification: The key fix is that firstprivate pointers now include |
| 162 |
/// the LITERAL flag (256), which tells the runtime to pass the pointer |
| 163 |
/// value directly instead of performing a present table lookup. |
| 164 |
/// |
| 165 |
/// Before fix: Pointers got 544 (TARGET_PARAM | IS_PTR) → runtime lookup |
| 166 |
/// After fix: Pointers get 288 or 800 (includes LITERAL) → direct pass |
| 167 |
///========================================================================== |
| 168 |
|
| 169 |
#endif // HEADER |