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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Victor Lomuller (Naghasan)

<details>
<summary>Changes</summary>

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.

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


2 Files Affected:

- (modified) clang/lib/Basic/Targets/NVPTX.cpp (+1) 
- (added) clang/test/Preprocessor/cuda-ptx-versioning.cu (+11) 


``````````diff
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

``````````

</details>


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


More information about the cfe-commits mailing list