Skip to content

[OpenMP][OpenMPIRBuilder] Support SPIR-V device variant matches #126801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2025

Conversation

sarnex
Copy link
Member

@sarnex sarnex commented Feb 11, 2025

We should be able to use spirv64 as a device variant match and it should be considered a GPU.

Also add the triple to an RTTI check.

@sarnex sarnex marked this pull request as ready for review February 12, 2025 14:58
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. flang:openmp clang:openmp OpenMP related changes to Clang labels Feb 12, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 12, 2025

@llvm/pr-subscribers-flang-openmp
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: Nick Sarnie (sarnex)

Changes

We should be able to use spirv64 as a device variant match and it should be considered a GPU.

Also add the triple to an RTTI check.


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

4 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.h (+2-1)
  • (added) clang/test/OpenMP/spirv_variant_match.cpp (+46)
  • (modified) llvm/include/llvm/Frontend/OpenMP/OMPKinds.def (+2)
  • (modified) llvm/lib/Frontend/OpenMP/OMPContext.cpp (+2)
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index 0956296e2d5d8..c6f6fd5b9a7bd 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -1067,7 +1067,8 @@ class CodeGenModule : public CodeGenTypeCache {
   bool shouldEmitRTTI(bool ForEH = false) {
     return (ForEH || getLangOpts().RTTI) && !getLangOpts().CUDAIsDevice &&
            !(getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice &&
-             (getTriple().isNVPTX() || getTriple().isAMDGPU()));
+             (getTriple().isNVPTX() || getTriple().isAMDGPU() ||
+              getTriple().isSPIRV()));
   }
 
   /// Get the address of the RTTI descriptor for the given type.
diff --git a/clang/test/OpenMP/spirv_variant_match.cpp b/clang/test/OpenMP/spirv_variant_match.cpp
new file mode 100644
index 0000000000000..b37858bc3008b
--- /dev/null
+++ b/clang/test/OpenMP/spirv_variant_match.cpp
@@ -0,0 +1,46 @@
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-unknown -fopenmp-targets=spirv64-intel -emit-llvm-bc %s -o %t-host.bc -DDEVICE
+// RUN: %clang_cc1 -verify -triple spirv64-intel -aux-triple x86_64-unknown-unknown -fopenmp -fopenmp-is-target-device \
+// RUN:-fopenmp-host-ir-file-path %t-host.bc -nogpulib %s -emit-llvm -DDEVICE -o - | FileCheck %s
+
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-unknown -fopenmp-targets=spirv64-intel -emit-llvm-bc %s -o %t-host.bc -DTARGET
+// RUN: %clang_cc1 -verify -triple spirv64-intel -aux-triple x86_64-unknown-unknown -fopenmp -fopenmp-is-target-device \
+// RUN: -fopenmp-host-ir-file-path %t-host.bc -nogpulib %s -emit-llvm -DTARGET -o - | FileCheck %s
+
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-unknown -fopenmp-targets=spirv64-intel -emit-llvm-bc %s -o %t-host.bc -DTARGET_KIND
+// RUN: %clang_cc1 -verify -triple spirv64-intel -aux-triple x86_64-unknown-unknown -fopenmp -fopenmp-is-target-device \
+// RUN: -fopenmp-host-ir-file-path %t-host.bc -nogpulib %s -emit-llvm -DTARGET_KIND -o - | FileCheck %s
+
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-unknown -fopenmp-targets=spirv64-intel -emit-llvm-bc %s -o %t-host.bc
+// RUN: %clang_cc1 -verify -triple spirv64-intel -aux-triple x86_64-unknown-unknown -fopenmp -fopenmp-is-target-device \
+// RUN: -fopenmp-host-ir-file-path %t-host.bc -nogpulib %s -emit-llvm -o - | FileCheck %s
+
+// expected-no-diagnostics
+
+#pragma omp declare target
+int foo() { return 0; }
+
+#ifdef DEVICE
+#pragma omp begin declare variant match(device = {arch(spirv64)})
+#elif defined(TARGET)
+#pragma omp begin declare variant match(target_device = {arch(spirv64)})
+#elif defined(TARGET_KIND)
+#pragma omp begin declare variant match(target_device = {kind(gpu)})
+#else
+#pragma omp begin declare variant match(device = {kind(gpu)})
+#endif
+
+int foo() { return 1; }
+#pragma omp end declare variant
+#pragma omp end declare target
+
+// CHECK-DAG: define{{.*}} @{{"_Z[0-9]+foo\$ompvariant\$.*"}}()
+
+// CHECK-DAG: call spir_func noundef i32 @{{"_Z[0-9]+foo\$ompvariant\$.*"}}()
+
+int main() {
+  int res;
+#pragma omp target map(from \
+                       : res)
+  res = foo();
+  return res;
+}
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def b/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
index 44a9a37c70597..f974cfc78c8dd 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
@@ -1269,6 +1269,7 @@ __OMP_TRAIT_PROPERTY(device, arch, x86_64)
 __OMP_TRAIT_PROPERTY(device, arch, amdgcn)
 __OMP_TRAIT_PROPERTY(device, arch, nvptx)
 __OMP_TRAIT_PROPERTY(device, arch, nvptx64)
+__OMP_TRAIT_PROPERTY(device, arch, spirv64)
 
 __OMP_TRAIT_SET(target_device)
 
@@ -1301,6 +1302,7 @@ __OMP_TRAIT_PROPERTY(target_device, arch, x86_64)
 __OMP_TRAIT_PROPERTY(target_device, arch, amdgcn)
 __OMP_TRAIT_PROPERTY(target_device, arch, nvptx)
 __OMP_TRAIT_PROPERTY(target_device, arch, nvptx64)
+__OMP_TRAIT_PROPERTY(target_device, arch, spirv64)
 
 __OMP_TRAIT_SET(implementation)
 
diff --git a/llvm/lib/Frontend/OpenMP/OMPContext.cpp b/llvm/lib/Frontend/OpenMP/OMPContext.cpp
index 5e13da172d677..2edfd786c5c23 100644
--- a/llvm/lib/Frontend/OpenMP/OMPContext.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPContext.cpp
@@ -52,6 +52,7 @@ OMPContext::OMPContext(bool IsDeviceCompilation, Triple TargetTriple,
     case Triple::amdgcn:
     case Triple::nvptx:
     case Triple::nvptx64:
+    case Triple::spirv64:
       ActiveTraits.set(unsigned(TraitProperty::target_device_kind_gpu));
       break;
     default:
@@ -98,6 +99,7 @@ OMPContext::OMPContext(bool IsDeviceCompilation, Triple TargetTriple,
     case Triple::amdgcn:
     case Triple::nvptx:
     case Triple::nvptx64:
+    case Triple::spirv64:
       ActiveTraits.set(unsigned(TraitProperty::device_kind_gpu));
       ActiveTraits.set(unsigned(TraitProperty::target_device_kind_gpu));
       break;

@sarnex sarnex requested a review from jhuber6 February 12, 2025 14:59
Comment on lines +1070 to +1071
(getTriple().isNVPTX() || getTriple().isAMDGPU() ||
getTriple().isSPIRV()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really hate this pattern, would be nice to have a single helper that's like isGPUTarget but probably out of scope here. Also we could probably rework this from the frontend to just pass -fno-rtti for the device -cc1 job.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, after this pr ill try to find all spots that check for the gpu offload triples and replace them with a single helper, wasting hours tracking down bugs because i missed one of these spots is not exactly a great use of time :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a helper for whether or not it's an offloading target GPU device, so SYCL, OpenMP, CUDA, etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as in the same helper should be callable for all those languages? if so makes sense, hope my grep skills are state of the art.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just mean most of these nonstandard hacks are only for offloading languages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeah

@sarnex sarnex merged commit cb3498c into llvm:main Feb 12, 2025
15 checks passed
flovent pushed a commit to flovent/llvm-project that referenced this pull request Feb 13, 2025
…#126801)

We should be able to use `spirv64` as a device variant match and it
should be considered a GPU.

Also add the triple to an RTTI check.

Signed-off-by: Sarnie, Nick <[email protected]>
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
…#126801)

We should be able to use `spirv64` as a device variant match and it
should be considered a GPU.

Also add the triple to an RTTI check.

Signed-off-by: Sarnie, Nick <[email protected]>
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…#126801)

We should be able to use `spirv64` as a device variant match and it
should be considered a GPU.

Also add the triple to an RTTI check.

Signed-off-by: Sarnie, Nick <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang:openmp OpenMP related changes to Clang clang Clang issues not falling into any other category flang:openmp
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants