================
@@ -0,0 +1,175 @@
+//===-- LanguageRuntime.cpp - Kernel language runtime API implementation 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LANGUAGE
+#error This file should be included, or used, with a LANGUAGE macro set.
+#endif
+
+// Rename the generic runtime API before declaring or defining language 
symbols.
+// clang-format off
+#include "DefineLanguageNames.inc"
+#include "LanguageRuntime.h"
+// clang-format on
+
+#include "LanguageErrors.h"
+#include "LanguageUtils.h"
+#include "State.h"
+#include "Types.h"
+
+#include "OffloadAPI.h"
+
+#include <cassert>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+
+#define STR(X) #X
+#define LANGUAGE_STR STR(LANGUAGE)
+
+using RuntimeState = llvm::offload::StateTy;
+using ThreadState = llvm::offload::ThreadStateTy;
+
+Error_t Malloc(void **DevPtr, size_t Size) {
+  ol_device_handle_t Device = ThreadState::getDefaultDevice();
+  ol_result_t Result = olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, Size, DevPtr);
+  return SetLastError(convertResult(Result));
+}
+
+Error_t Free(void *DevPtr) {
+  ol_result_t Result = olMemFree(DevPtr);
+  return SetLastError(convertResult(Result));
+}
+
+Error_t Memcpy(void *Dst, const void *Src, size_t Size, MemcpyKind Kind) {
+  ol_queue_handle_t Queue = ThreadState::getDefaultQueue();
+
+  ol_result_t Result;
+  switch (Kind) {
+  case MemcpyHostToHost: {
+    ol_device_handle_t Host = RuntimeState::getHostDevice();
+    Result = olMemcpy(nullptr, Dst, Host, const_cast<void *>(Src), Host, Size);
+    break;
+  }
+  case MemcpyHostToDevice: {
+    ol_device_handle_t Device = ThreadState::getDefaultDevice();
+    ol_device_handle_t Host = RuntimeState::getHostDevice();
+    Result = olMemcpy(Queue, Dst, Device, const_cast<void *>(Src), Host, Size);
+    break;
+  }
+  case MemcpyDeviceToHost: {
+    ol_device_handle_t Device = ThreadState::getDefaultDevice();
+    ol_device_handle_t Host = RuntimeState::getHostDevice();
+
+    Result = olMemcpy(Queue, Dst, Host, const_cast<void *>(Src), Device, Size);
+    break;
+  }
+  case MemcpyDeviceToDevice: {
+    ol_device_handle_t Device = ThreadState::getDefaultDevice();
+
+    Result =
+        olMemcpy(Queue, Dst, Device, const_cast<void *>(Src), Device, Size);
+    break;
+  }
+  case MemcpyDefault:
+    fprintf(stderr, LANGUAGE_STR "MemcpyDefault is not implemented yet");
+    abort();
+  };
+  if (Result != OL_SUCCESS) {
+    return SetLastError(convertResult(Result));
+  }
----------------
kevinsala wrote:

nit


```suggestion
  if (Result != OL_SUCCESS)
    return SetLastError(convertResult(Result));
```

https://github.com/llvm/llvm-project/pull/213389
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to