Skip to content

Commit 4145231

Browse files
committed
[Clang][HIP] Warn when __AMDGCN_WAVEFRONT_SIZE is used in host code
The __AMDGCN_WAVEFRONT_SIZE and __AMDGCN_WAVEFRONT_SIZE__ macros in HIP can only provide meaningful values during device compilation. They are currently usable in host code, but only contain the default value of 64, independent of the target device(s). This patch redefines them during host compilation to issue a deprecation warning if the macros are used in host code. Their value during host compilation in actual HIP code as well as in preprocessing directives stays 64 as before. Macro uses in preprocessing directives are not diagnosed. Macro uses in device code are not affected. Implements SWDEV-449015.
1 parent 5cfc6bc commit 4145231

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,17 @@ void RocmInstallationDetector::AddHIPIncludeArgs(const ArgList &DriverArgs,
551551
CC1Args.push_back(DriverArgs.MakeArgString(P));
552552
}
553553

554+
{
555+
// This header implements diagnostics for problematic uses of
556+
// device-specific macros. Since these diagnostics should be issued even
557+
// when GPU headers are not included, this header is included separately.
558+
SmallString<128> P(D.ResourceDir);
559+
llvm::sys::path::append(P, "include");
560+
CC1Args.push_back("-internal-isystem");
561+
CC1Args.push_back(DriverArgs.MakeArgString(P));
562+
CC1Args.append({"-include", "__clang_hip_device_macro_guards.h"});
563+
}
564+
554565
const auto HandleHipStdPar = [=, &DriverArgs, &CC1Args]() {
555566
StringRef Inc = getIncludePath();
556567
auto &FS = D.getVFS();

clang/lib/Headers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ set(hip_files
7979
__clang_hip_math.h
8080
__clang_hip_stdlib.h
8181
__clang_hip_runtime_wrapper.h
82+
__clang_hip_device_macro_guards.h
8283
)
8384

8485
set(hlsl_h
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*===---- __clang_hip_device_macro_guards.h - guards for HIP device macros -===
2+
*
3+
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
* See https://llvm.org/LICENSE.txt for license information.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*
7+
*===-----------------------------------------------------------------------===
8+
*/
9+
10+
/*
11+
* WARNING: This header is intended to be directly -include'd by
12+
* the compiler and is not supposed to be included by users.
13+
*
14+
*/
15+
16+
#ifndef __CLANG_HIP_DEVICE_MACRO_GUARDS_H__
17+
#define __CLANG_HIP_DEVICE_MACRO_GUARDS_H__
18+
19+
#if __HIP__
20+
#if !defined(__HIP_DEVICE_COMPILE__)
21+
// The __AMDGCN_WAVEFRONT_SIZE macros cannot hold meaningful values during host
22+
// compilation as devices are not initialized when the macros are defined and
23+
// there may indeed be devices with differing wavefront sizes in the same
24+
// system. This code issues diagnostics when the macros are used in host code.
25+
26+
#undef __AMDGCN_WAVEFRONT_SIZE
27+
#undef __AMDGCN_WAVEFRONT_SIZE__
28+
29+
// Reference __hip_device_macro_guard in a way that is legal in preprocessor
30+
// directives and does not affect the value so that appropriate diagnostics are
31+
// issued. Function calls, casts, or the comma operator would make the macro
32+
// illegal for use in preprocessor directives.
33+
#define __AMDGCN_WAVEFRONT_SIZE (!__hip_device_macro_guard ? 64 : 64)
34+
#define __AMDGCN_WAVEFRONT_SIZE__ (!__hip_device_macro_guard ? 64 : 64)
35+
36+
// This function is referenced by the macro in device functions during host
37+
// compilation, it SHOULD NOT cause a diagnostic.
38+
__attribute__((device)) static constexpr int __hip_device_macro_guard(void) {
39+
return -1;
40+
}
41+
42+
// This function is referenced by the macro in host functions during host
43+
// compilation, it SHOULD cause a diagnostic.
44+
__attribute__((
45+
host, deprecated("The __AMDGCN_WAVEFRONT_SIZE macros do not correspond "
46+
"to the device(s) when used in host code and may only "
47+
"be used in device code."))) static constexpr int
48+
__hip_device_macro_guard(void) {
49+
return -1;
50+
}
51+
// TODO Change "deprecated" to "unavailable" to cause hard errors instead of
52+
// warnings.
53+
#endif
54+
#endif // __HIP__
55+
#endif // __CLANG_HIP_DEVICE_MACRO_GUARDS_H__
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// REQUIRES: amdgpu-registered-target
2+
// RUN: %clang -xhip --offload-arch=gfx1030 --offload-host-only -pedantic -nogpuinc -nogpulib -nobuiltininc -nostdinc -fsyntax-only -Xclang -verify=onhost %s
3+
// RUN: %clang -xhip --offload-arch=gfx1030 --offload-device-only -pedantic -nogpuinc -nogpulib -nobuiltininc -nostdinc -fsyntax-only -Xclang -verify=ondevice %s
4+
5+
// ondevice-no-diagnostics
6+
7+
#define WRAPPED __AMDGCN_WAVEFRONT_SIZE__
8+
9+
__attribute__((host, device)) void use(int, const char*);
10+
11+
template<int N> __attribute__((host, device)) int templatify(int x) {
12+
return x + N;
13+
}
14+
15+
// no warning expected
16+
#if defined(__HIP_DEVICE_COMPILE__) && (__AMDGCN_WAVEFRONT_SIZE__ == 64) && (__AMDGCN_WAVEFRONT_SIZE == 64)
17+
int foo(void);
18+
#endif
19+
20+
// no warning expected
21+
__attribute__((device)) int device_var = __AMDGCN_WAVEFRONT_SIZE__;
22+
23+
__attribute__((device))
24+
void device_fun() {
25+
// no warnings expected
26+
use(__AMDGCN_WAVEFRONT_SIZE, "device function");
27+
use(__AMDGCN_WAVEFRONT_SIZE__, "device function");
28+
use(WRAPPED, "device function");
29+
use(templatify<__AMDGCN_WAVEFRONT_SIZE__>(42), "device function");
30+
}
31+
32+
// warning expected
33+
int host_var = __AMDGCN_WAVEFRONT_SIZE__; // onhost-warning {{'__hip_device_macro_guard' is deprecated: The __AMDGCN_WAVEFRONT_SIZE macros do not correspond to the device(s) when used in host code and may only be used in device code.}}
34+
35+
__attribute__((host))
36+
void host_fun() {
37+
// warnings expected
38+
use(__AMDGCN_WAVEFRONT_SIZE, "host function"); // onhost-warning {{'__hip_device_macro_guard' is deprecated: The __AMDGCN_WAVEFRONT_SIZE macros do not correspond to the device(s) when used in host code and may only be used in device code.}}
39+
use(__AMDGCN_WAVEFRONT_SIZE__, "host function"); // onhost-warning {{'__hip_device_macro_guard' is deprecated: The __AMDGCN_WAVEFRONT_SIZE macros do not correspond to the device(s) when used in host code and may only be used in device code.}}
40+
use(WRAPPED, "host function"); // onhost-warning {{'__hip_device_macro_guard' is deprecated: The __AMDGCN_WAVEFRONT_SIZE macros do not correspond to the device(s) when used in host code and may only be used in device code.}}
41+
use(templatify<__AMDGCN_WAVEFRONT_SIZE__>(42), "host function"); // onhost-warning {{'__hip_device_macro_guard' is deprecated: The __AMDGCN_WAVEFRONT_SIZE macros do not correspond to the device(s) when used in host code and may only be used in device code.}}
42+
}
43+
44+
__attribute((host, device))
45+
void host_device_fun() {
46+
// warnings expected
47+
use(__AMDGCN_WAVEFRONT_SIZE__, "host device function"); // onhost-warning {{'__hip_device_macro_guard' is deprecated: The __AMDGCN_WAVEFRONT_SIZE macros do not correspond to the device(s) when used in host code and may only be used in device code.}}
48+
use(WRAPPED, "host device function"); // onhost-warning {{'__hip_device_macro_guard' is deprecated: The __AMDGCN_WAVEFRONT_SIZE macros do not correspond to the device(s) when used in host code and may only be used in device code.}}
49+
use(templatify<__AMDGCN_WAVEFRONT_SIZE__>(42), "host device function"); // onhost-warning {{'__hip_device_macro_guard' is deprecated: The __AMDGCN_WAVEFRONT_SIZE macros do not correspond to the device(s) when used in host code and may only be used in device code.}}
50+
}
51+
52+
// onhost-note@__clang_hip_device_macro_guards.h:45 0+ {{'__hip_device_macro_guard' has been explicitly marked deprecated here}}

clang/test/Preprocessor/predefined-arch-macros.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4399,7 +4399,6 @@
43994399
// RUN: %clang -x hip -E -dM %s -o - 2>&1 --offload-host-only -nogpulib \
44004400
// RUN: -nogpuinc --offload-arch=gfx803 -target x86_64-unknown-linux \
44014401
// RUN: | FileCheck -match-full-lines %s -check-prefixes=CHECK_HIP_HOST
4402-
// CHECK_HIP_HOST: #define __AMDGCN_WAVEFRONT_SIZE__ 64
44034402
// CHECK_HIP_HOST: #define __AMDGPU__ 1
44044403
// CHECK_HIP_HOST: #define __AMD__ 1
44054404

0 commit comments

Comments
 (0)