Module: Mesa
Branch: main
Commit: 981bc603b46ce9668fbfb5b766b4b4b9b47dd5a1
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=981bc603b46ce9668fbfb5b766b4b4b9b47dd5a1

Author: Thomas Debesse <[email protected]>
Date:   Mon Oct  3 04:12:21 2022 +0200

clover: implement CLOVER_DEVICE_TYPE like RUSTICL_DEVICE_TYPE

Allows to make Clover devices appearing as cpu, gpu or accelerator
by setting the CLOVER_DEVICE_TYPE environment variable like
the RUSTICL_DEVICE_TYPE environment variable does.

For example it can make the CPU llvmpipe device appear as GPU or GPU devices
appear as CPU. This is useful for testing OpenCL with applications that may
use different code path given the OpenCL device is a CPU or a GPU.

The initial motivation for RUSTICL_DEVICE_TYPE implementation was to test
rusticl with llvmipe on applications ignoring CPU devices.

This brings Clover on par with rusticl on that topic.

CL_DEVICE_TYPE_CUSTOM isn't implemented or applications may crash when
iterating devices because CL_DEVICE_TYPE_CUSTOM is OpenCL 1.2 and Clover
is OpenCL 1.1.

Signed-off-by: Thomas Debesse <[email protected]>
Reviewed-by: Mihai Preda <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18931>

---

 src/gallium/frontends/clover/core/device.cpp | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/src/gallium/frontends/clover/core/device.cpp 
b/src/gallium/frontends/clover/core/device.cpp
index 3b41caf06ae..66159e9f297 100644
--- a/src/gallium/frontends/clover/core/device.cpp
+++ b/src/gallium/frontends/clover/core/device.cpp
@@ -134,6 +134,27 @@ namespace {
 
       return version;
    }
+
+   static cl_device_type
+   parse_env_device_type() {
+      const char* val = getenv("CLOVER_DEVICE_TYPE");
+      if (!val) {
+         return 0;
+      }
+      if (strcmp(val, "cpu") == 0) {
+         return CL_DEVICE_TYPE_CPU;
+      }
+      if (strcmp(val, "gpu") == 0) {
+         return CL_DEVICE_TYPE_GPU;
+      }
+      if (strcmp(val, "accelerator") == 0) {
+         return CL_DEVICE_TYPE_ACCELERATOR;
+      }
+      /* CL_DEVICE_TYPE_CUSTOM isn't implemented
+      because CL_DEVICE_TYPE_CUSTOM is OpenCL 1.2
+      and Clover is OpenCL 1.1. */
+      return 0;
+   }
 }
 
 device::device(clover::platform &platform, pipe_loader_device *ldev) :
@@ -189,6 +210,11 @@ device::operator==(const device &dev) const {
 
 cl_device_type
 device::type() const {
+   cl_device_type type = parse_env_device_type();
+   if (type != 0) {
+      return type;
+   }
+
    switch (ldev->type) {
    case PIPE_LOADER_DEVICE_SOFTWARE:
       return CL_DEVICE_TYPE_CPU;

Reply via email to