[clang] [clang][NVPTX] Define macro indicating the PTX version (PR #94934)

Victor Lomuller via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 9 23:50:16 PDT 2024


https://github.com/Naghasan created https://github.com/llvm/llvm-project/pull/94934

Define __PTX_VERSION__ macro to indicate the used PTX version.

Usually each new PTX version brings a new sm version and the associated instructions. However, some of these instructions can also be made avialable to older sm. This allows applications to check more accuratly for available instructions.

>From 52623029bf504c10ee4e8df749c1e33eaa564cc2 Mon Sep 17 00:00:00 2001
From: Victor Lomuller <victor at codeplay.com>
Date: Thu, 6 Jun 2024 21:53:01 +0100
Subject: [PATCH] [clang][NVPTX] Define macro indicating the PTX version

Define __PTX_VERSION__ macro to indicate the used PTX version.

Usually each new PTX version brings a new sm version and the associated instructions.
However, some of these instructions can also be made avialable to older sm.
This allows applications to check more accuratly for available instructions.
---
 clang/lib/Basic/Targets/NVPTX.cpp              |  1 +
 clang/test/Preprocessor/cuda-ptx-versioning.cu | 11 +++++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 clang/test/Preprocessor/cuda-ptx-versioning.cu

diff --git a/clang/lib/Basic/Targets/NVPTX.cpp b/clang/lib/Basic/Targets/NVPTX.cpp
index ff7d2f1f92aa4..ebb1839d8cfd1 100644
--- a/clang/lib/Basic/Targets/NVPTX.cpp
+++ b/clang/lib/Basic/Targets/NVPTX.cpp
@@ -173,6 +173,7 @@ void NVPTXTargetInfo::getTargetDefines(const LangOptions &Opts,
                                        MacroBuilder &Builder) const {
   Builder.defineMacro("__PTX__");
   Builder.defineMacro("__NVPTX__");
+  Builder.defineMacro("__PTX_VERSION__", Twine(PTXVersion));
 
   // Skip setting architecture dependent macros if undefined.
   if (GPU == CudaArch::UNUSED && !HostTarget)
diff --git a/clang/test/Preprocessor/cuda-ptx-versioning.cu b/clang/test/Preprocessor/cuda-ptx-versioning.cu
new file mode 100644
index 0000000000000..2d7eb9b172b58
--- /dev/null
+++ b/clang/test/Preprocessor/cuda-ptx-versioning.cu
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 %s -E -dM -o - -x cuda -fcuda-is-device -triple nvptx64 \
+// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-CUDA32
+// CHECK-CUDA32: #define __PTX_VERSION__ 32
+
+// RUN: %clang_cc1 %s -E -dM -o - -x cuda -fcuda-is-device -triple nvptx64 -target-feature +ptx78 \
+// RUN:  -target-cpu sm_90 | FileCheck -match-full-lines %s --check-prefix=CHECK-CUDA78
+// CHECK-CUDA78: #define __PTX_VERSION__ 78
+
+// RUN: %clang_cc1 %s -E -dM -o - -x cuda -fcuda-is-device -triple nvptx64 -target-feature +ptx80 \
+// RUN:  -target-cpu sm_80 | FileCheck -match-full-lines %s --check-prefix=CHECK-CUDA80
+// CHECK-CUDA80: #define __PTX_VERSION__ 80



More information about the cfe-commits mailing list