[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2024-02-06 Thread Matt Arsenault via cfe-commits


@@ -104,3 +106,14 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+extern __global__ void external_func();
+extern void* const external_dep[] = {

arsenm wrote:

Sounds broken that the behavior would differ between array and non-array ?

https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-12-20 Thread Levon Ter-Grigoryan via cfe-commits

https://github.com/PatriosTheGreat updated 
https://github.com/llvm/llvm-project/pull/73549

>From 1c24a6774f08c9943cce1a2eee96a6a92b11fd02 Mon Sep 17 00:00:00 2001
From: Levon Ter-Grigoryan 
Date: Mon, 27 Nov 2023 18:09:22 +0100
Subject: [PATCH] [CUDA][HIP] Exclude external variables from constant
 promotion.

Promoting __constant__ to external variables includes them to PTX which then 
leads to nvlinker failure.
See changes at device-use-host-var test.
Befor this change those variables was included to PTX without definition.
---
 clang/lib/Sema/SemaCUDA.cpp   |  1 +
 clang/test/CodeGenCUDA/device-use-host-var.cu | 13 +
 2 files changed, 14 insertions(+)

diff --git a/clang/lib/Sema/SemaCUDA.cpp b/clang/lib/Sema/SemaCUDA.cpp
index 6a66ecf6f94c178..d744f9a8c5109f8 100644
--- a/clang/lib/Sema/SemaCUDA.cpp
+++ b/clang/lib/Sema/SemaCUDA.cpp
@@ -792,6 +792,7 @@ void Sema::MaybeAddCUDAConstantAttr(VarDecl *VD) {
   (VD->isFileVarDecl() || VD->isStaticDataMember()) &&
   !IsDependentVar(VD) &&
   ((VD->isConstexpr() || VD->getType().isConstQualified()) &&
+   VD->getStorageClass() != SC_Extern &&
HasAllowedCUDADeviceStaticInitializer(*this, VD,
  CICK_DeviceOrConstant))) {
 VD->addAttr(CUDAConstantAttr::CreateImplicit(getASTContext()));
diff --git a/clang/test/CodeGenCUDA/device-use-host-var.cu 
b/clang/test/CodeGenCUDA/device-use-host-var.cu
index 64de57e41b4b9f5..65aac2b7eca891c 100644
--- a/clang/test/CodeGenCUDA/device-use-host-var.cu
+++ b/clang/test/CodeGenCUDA/device-use-host-var.cu
@@ -2,6 +2,8 @@
 // RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck %s
 // RUN: %clang_cc1 -std=c++14 -triple amdgcn-amd-amdhsa \
 // RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck 
-check-prefix=NEG %s
+// RUN: %clang_cc1 -std=c++14 -triple nvptx64-nvidia-cuda  \
+// RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck 
-check-prefix=NEG -implicit-check-not=external_ %s
 
 #include "Inputs/cuda.h"
 
@@ -104,3 +106,14 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+extern __global__ void external_func();
+extern void* const external_dep[] = {
+  (void*)(external_func)
+};
+extern void* const external_arr[] = {};
+
+void* host_fun() {
+  (void) external_dep;
+  (void) external_arr;
+}

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


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-12-18 Thread Matt Arsenault via cfe-commits

https://github.com/arsenm commented:

Is #75799 related?

https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-12-18 Thread Levon Ter-Grigoryan via cfe-commits


@@ -104,3 +106,14 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+extern __global__ void external_func();
+extern void* const external_dep[] = {
+  (void*)(external_func)
+};
+extern void* const external_arr[] = {};
+
+void* host_fun() {
+  (void) external_dep;
+  (void) external_arr;
+}

PatriosTheGreat wrote:

This case is checked by flag "-implicit-check-not=external_" at the line 6, so 
we can check that external is not mentioned anywhere at the device code.

https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-12-18 Thread Levon Ter-Grigoryan via cfe-commits

https://github.com/PatriosTheGreat deleted 
https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-12-18 Thread Levon Ter-Grigoryan via cfe-commits


@@ -104,3 +106,14 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+extern __global__ void external_func();
+extern void* const external_dep[] = {
+  (void*)(external_func)
+};
+extern void* const external_arr[] = {};
+
+void* host_fun() {
+  (void) external_dep;
+  (void) external_arr;
+}

PatriosTheGreat wrote:

This case is checked by flag "-implicit-check-not=external_" at the line 6, so 
we can check that external is not mentioned anywhere at the device code.

https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-12-18 Thread Levon Ter-Grigoryan via cfe-commits

PatriosTheGreat wrote:

FYI

https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-12-07 Thread Yaxun Liu via cfe-commits


@@ -104,3 +106,14 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+extern __global__ void external_func();
+extern void* const external_dep[] = {

yxsamliu wrote:

It seems nvcc allows non-array type const var to be used in device code but not 
array type const var

https://godbolt.org/z/xjvbjPK77

I don't see why we cannot use array type const var in device code if we are 
able to emit them on device side. There may be CUDA/HIP code already using this 
feature. Disabling it may cause regressions.

On the other hand, I think disallow extern const var in device code is 
reasonable, since we do not know how it is initialized.

https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-12-05 Thread Artem Belevich via cfe-commits


@@ -104,3 +106,14 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+extern __global__ void external_func();
+extern void* const external_dep[] = {

Artem-B wrote:

This array is nomiannly host-only entity and should not be emitted on GPU at 
all, IMO. In fact, nvcc errors out if we attempt to access it on the GPU: 
https://godbolt.org/z/G15zn35Wd

Whether it's extern or not should not matter. I think.

@yxsamliu Sam, WDYT? I suspect there is/was a reason we may have allowed const 
access on both sides.

https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-12-05 Thread Artem Belevich via cfe-commits


@@ -104,3 +106,14 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+extern __global__ void external_func();
+extern void* const external_dep[] = {
+  (void*)(external_func)
+};
+extern void* const external_arr[] = {};
+
+void* host_fun() {
+  (void) external_dep;
+  (void) external_arr;
+}

Artem-B wrote:

There are no CHECK lines here. 

https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-11-30 Thread Levon Ter-Grigoryan via cfe-commits


@@ -104,3 +106,17 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+// NEG-NOT: external_func
+extern __global__ void external_func();
+// NEG-NOT: @external_dep
+extern void* const external_dep[] = {
+  (void*)(external_func)
+};
+// NEG-NOT: @external_arr

PatriosTheGreat wrote:

Ah, sorry misunderstood original comment.
Changed test code to use implicit-check, thanks for the reference

https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-11-30 Thread Levon Ter-Grigoryan via cfe-commits

https://github.com/PatriosTheGreat updated 
https://github.com/llvm/llvm-project/pull/73549

>From 0259038bcb4297a89c700ea2a21b80e7a22480db Mon Sep 17 00:00:00 2001
From: Levon Ter-Grigoryan 
Date: Mon, 27 Nov 2023 18:09:22 +0100
Subject: [PATCH] [CUDA][HIP] Exclude external variables from constant
 promotion.

Promoting __constant__ to external variables includes them to PTX which then 
leads to nvlinker failure.
See changes at device-use-host-var test.
Befor this change those variables was included to PTX without definition.
---
 clang/lib/Sema/SemaCUDA.cpp   |  1 +
 clang/test/CodeGenCUDA/device-use-host-var.cu | 13 +
 2 files changed, 14 insertions(+)

diff --git a/clang/lib/Sema/SemaCUDA.cpp b/clang/lib/Sema/SemaCUDA.cpp
index 318174f7be8fa95..f9d72e571e7b98b 100644
--- a/clang/lib/Sema/SemaCUDA.cpp
+++ b/clang/lib/Sema/SemaCUDA.cpp
@@ -783,6 +783,7 @@ void Sema::MaybeAddCUDAConstantAttr(VarDecl *VD) {
   (VD->isFileVarDecl() || VD->isStaticDataMember()) &&
   !IsDependentVar(VD) &&
   ((VD->isConstexpr() || VD->getType().isConstQualified()) &&
+   VD->getStorageClass() != SC_Extern &&
HasAllowedCUDADeviceStaticInitializer(*this, VD,
  CICK_DeviceOrConstant))) {
 VD->addAttr(CUDAConstantAttr::CreateImplicit(getASTContext()));
diff --git a/clang/test/CodeGenCUDA/device-use-host-var.cu 
b/clang/test/CodeGenCUDA/device-use-host-var.cu
index 64de57e41b4b9f5..65aac2b7eca891c 100644
--- a/clang/test/CodeGenCUDA/device-use-host-var.cu
+++ b/clang/test/CodeGenCUDA/device-use-host-var.cu
@@ -2,6 +2,8 @@
 // RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck %s
 // RUN: %clang_cc1 -std=c++14 -triple amdgcn-amd-amdhsa \
 // RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck 
-check-prefix=NEG %s
+// RUN: %clang_cc1 -std=c++14 -triple nvptx64-nvidia-cuda  \
+// RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck 
-check-prefix=NEG -implicit-check-not=external_ %s
 
 #include "Inputs/cuda.h"
 
@@ -104,3 +106,14 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+extern __global__ void external_func();
+extern void* const external_dep[] = {
+  (void*)(external_func)
+};
+extern void* const external_arr[] = {};
+
+void* host_fun() {
+  (void) external_dep;
+  (void) external_arr;
+}

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


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-11-30 Thread Matt Arsenault via cfe-commits


@@ -104,3 +106,17 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+// NEG-NOT: external_func
+extern __global__ void external_func();
+// NEG-NOT: @external_dep
+extern void* const external_dep[] = {
+  (void*)(external_func)
+};
+// NEG-NOT: @external_arr

arsenm wrote:

I mean negative checks are inherently fragile, and the place you are checking 
it does not exist isn't where globals are printed. An -implicit-check-not=xxx 
argument to FIleCheck would be more thorough 

https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-11-30 Thread Levon Ter-Grigoryan via cfe-commits


@@ -104,3 +106,17 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+// NEG-NOT: external_func
+extern __global__ void external_func();
+// NEG-NOT: @external_dep
+extern void* const external_dep[] = {
+  (void*)(external_func)
+};
+// NEG-NOT: @external_arr

PatriosTheGreat wrote:

Not sure I got it correctly, but AFAIU in this test we are checking only the 
device code.
So with the current patch an external global variable should not be included to 
the device code (only to the host code) if it's not used in any device 
functions at the current compilation unit. If it's actually used 
Though if this variable is used at the device function than it should be 
manually marked as __constant__ variable. I can add this test case as well if 
it's needed.


https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-11-28 Thread Matt Arsenault via cfe-commits


@@ -104,3 +106,17 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+// NEG-NOT: external_func
+extern __global__ void external_func();
+// NEG-NOT: @external_dep
+extern void* const external_dep[] = {
+  (void*)(external_func)
+};
+// NEG-NOT: @external_arr

arsenm wrote:

Not sure this negative check really works as expected; won't any global 
variable be printed at the top of the file before any functions?

https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-11-27 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Levon Ter-Grigoryan (PatriosTheGreat)


Changes

Promoting __constant__ to external variables includes them to PTX which then 
leads to nvlinker failure.
See changes at device-use-host-var test.
Befor this change those variables was included to PTX without definition.

---
Full diff: https://github.com/llvm/llvm-project/pull/73549.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaCUDA.cpp (+1) 
- (modified) clang/test/CodeGenCUDA/device-use-host-var.cu (+16) 


``diff
diff --git a/clang/lib/Sema/SemaCUDA.cpp b/clang/lib/Sema/SemaCUDA.cpp
index 318174f7be8fa95..f9d72e571e7b98b 100644
--- a/clang/lib/Sema/SemaCUDA.cpp
+++ b/clang/lib/Sema/SemaCUDA.cpp
@@ -783,6 +783,7 @@ void Sema::MaybeAddCUDAConstantAttr(VarDecl *VD) {
   (VD->isFileVarDecl() || VD->isStaticDataMember()) &&
   !IsDependentVar(VD) &&
   ((VD->isConstexpr() || VD->getType().isConstQualified()) &&
+   VD->getStorageClass() != SC_Extern &&
HasAllowedCUDADeviceStaticInitializer(*this, VD,
  CICK_DeviceOrConstant))) {
 VD->addAttr(CUDAConstantAttr::CreateImplicit(getASTContext()));
diff --git a/clang/test/CodeGenCUDA/device-use-host-var.cu 
b/clang/test/CodeGenCUDA/device-use-host-var.cu
index 64de57e41b4b9f5..807a485f4c14972 100644
--- a/clang/test/CodeGenCUDA/device-use-host-var.cu
+++ b/clang/test/CodeGenCUDA/device-use-host-var.cu
@@ -2,6 +2,8 @@
 // RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck %s
 // RUN: %clang_cc1 -std=c++14 -triple amdgcn-amd-amdhsa \
 // RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck 
-check-prefix=NEG %s
+// RUN: %clang_cc1 -std=c++14 -triple nvptx64-nvidia-cuda \
+// RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck 
-check-prefix=NEG %s
 
 #include "Inputs/cuda.h"
 
@@ -104,3 +106,17 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+// NEG-NOT: external_func
+extern __global__ void external_func();
+// NEG-NOT: @external_dep
+extern void* const external_dep[] = {
+  (void*)(external_func)
+};
+// NEG-NOT: @external_arr
+extern void* const external_arr[] = {};
+
+void* host_fun() {
+  (void) external_dep;
+  (void) external_arr;
+}

``




https://github.com/llvm/llvm-project/pull/73549
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CUDA][HIP] Exclude external variables from constant promotion. (PR #73549)

2023-11-27 Thread Levon Ter-Grigoryan via cfe-commits

https://github.com/PatriosTheGreat created 
https://github.com/llvm/llvm-project/pull/73549

Promoting __constant__ to external variables includes them to PTX which then 
leads to nvlinker failure.
See changes at device-use-host-var test.
Befor this change those variables was included to PTX without definition.

>From a1a50db9bd14467ca4463ec05affc381ab0ea1aa Mon Sep 17 00:00:00 2001
From: Levon Ter-Grigoryan 
Date: Mon, 27 Nov 2023 18:09:22 +0100
Subject: [PATCH] [CUDA][HIP] Exclude external variables from constant
 promotion.

Promoting __constant__ to external variables includes them to PTX which then 
leads to nvlinker failure.
See changes at device-use-host-var test.
Befor this change those variables was included to PTX without definition.
---
 clang/lib/Sema/SemaCUDA.cpp   |  1 +
 clang/test/CodeGenCUDA/device-use-host-var.cu | 16 
 2 files changed, 17 insertions(+)

diff --git a/clang/lib/Sema/SemaCUDA.cpp b/clang/lib/Sema/SemaCUDA.cpp
index 318174f7be8fa95..f9d72e571e7b98b 100644
--- a/clang/lib/Sema/SemaCUDA.cpp
+++ b/clang/lib/Sema/SemaCUDA.cpp
@@ -783,6 +783,7 @@ void Sema::MaybeAddCUDAConstantAttr(VarDecl *VD) {
   (VD->isFileVarDecl() || VD->isStaticDataMember()) &&
   !IsDependentVar(VD) &&
   ((VD->isConstexpr() || VD->getType().isConstQualified()) &&
+   VD->getStorageClass() != SC_Extern &&
HasAllowedCUDADeviceStaticInitializer(*this, VD,
  CICK_DeviceOrConstant))) {
 VD->addAttr(CUDAConstantAttr::CreateImplicit(getASTContext()));
diff --git a/clang/test/CodeGenCUDA/device-use-host-var.cu 
b/clang/test/CodeGenCUDA/device-use-host-var.cu
index 64de57e41b4b9f5..807a485f4c14972 100644
--- a/clang/test/CodeGenCUDA/device-use-host-var.cu
+++ b/clang/test/CodeGenCUDA/device-use-host-var.cu
@@ -2,6 +2,8 @@
 // RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck %s
 // RUN: %clang_cc1 -std=c++14 -triple amdgcn-amd-amdhsa \
 // RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck 
-check-prefix=NEG %s
+// RUN: %clang_cc1 -std=c++14 -triple nvptx64-nvidia-cuda \
+// RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck 
-check-prefix=NEG %s
 
 #include "Inputs/cuda.h"
 
@@ -104,3 +106,17 @@ void fun() {
   (void) b;
   (void) var_host_only;
 }
+
+// NEG-NOT: external_func
+extern __global__ void external_func();
+// NEG-NOT: @external_dep
+extern void* const external_dep[] = {
+  (void*)(external_func)
+};
+// NEG-NOT: @external_arr
+extern void* const external_arr[] = {};
+
+void* host_fun() {
+  (void) external_dep;
+  (void) external_arr;
+}

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