Skip to content

Commit 0eabf59

Browse files
committed
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
1 parent 3ab7912 commit 0eabf59

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10387,7 +10387,9 @@ void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD,
1038710387
}
1038810388
Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false);
1038910389
// Temp solution to prevent optimizations of the internal variables.
10390-
if (CGM.getLangOpts().OpenMPIsDevice && !VD->isExternallyVisible()) {
10390+
if (CGM.getLangOpts().OpenMPIsDevice &&
10391+
(!VD->isExternallyVisible() ||
10392+
Linkage == llvm::GlobalValue::LinkOnceODRLinkage)) {
1039110393
// Do not create a "ref-variable" if the original is not also available
1039210394
// on the host.
1039310395
if (!OffloadEntriesInfoManager.hasDeviceGlobalVarEntryInfo(VarName))
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// 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
2+
// REQUIRES: amdgpu-registered-target
3+
4+
5+
// Test target codegen - host bc file has to be created first.
6+
// 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
7+
// 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
8+
9+
// expected-no-diagnostics
10+
11+
#ifndef HEADER
12+
#define HEADER
13+
14+
#pragma omp declare target
15+
class A {
16+
public:
17+
static constexpr double pi = 3.141592653589793116;
18+
//.
19+
// CHECK: @_ZN1A2piE = linkonce_odr constant double 0x400921FB54442D18, comdat, align 8
20+
// CHECK: @_ZL9anotherPi = internal constant double 3.140000e+00, align 8
21+
// CHECK: @llvm.compiler.used = appending global [2 x ptr] [ptr @"__ZN1A2piE$ref", ptr @"__ZL9anotherPi$ref"], section "llvm.metadata"
22+
//.
23+
A() { ; }
24+
~A() { ; }
25+
};
26+
#pragma omp end declare target
27+
28+
void F(const double &);
29+
void Test() { F(A::pi); }
30+
31+
#pragma omp declare target
32+
constexpr static double anotherPi = 3.14;
33+
#pragma omp end declare target
34+
35+
#endif
36+
37+
38+
//
39+
//// NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
40+
// CHECK: {{.*}}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %libomptarget-compileoptxx-run-and-check-generic
2+
3+
#include <omp.h>
4+
#include <stdio.h>
5+
6+
#pragma omp declare target
7+
class A {
8+
public:
9+
constexpr static double pi = 3.141592653589793116;
10+
A() { ; }
11+
~A() { ; }
12+
};
13+
#pragma omp end declare target
14+
15+
#pragma omp declare target
16+
constexpr static double anotherPi = 3.14;
17+
#pragma omp end declare target
18+
19+
int main() {
20+
double a[2];
21+
#pragma omp target map(tofrom : a[:2])
22+
{
23+
a[0] = A::pi;
24+
a[1] = anotherPi;
25+
}
26+
27+
// CHECK: pi = 3.141592653589793116
28+
printf("pi = %.18f\n", a[0]);
29+
30+
// CHECK: anotherPi = 3.14
31+
printf("anotherPi = %.2f\n", a[1]);
32+
33+
return 0;
34+
}

0 commit comments

Comments
 (0)