areusch commented on a change in pull request #5921:
URL: https://github.com/apache/incubator-tvm/pull/5921#discussion_r452524711



##########
File path: src/runtime/crt/common/crt_runtime_api.c
##########
@@ -0,0 +1,335 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+// LINT_C_FILE
+
+#include <assert.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <tvm/runtime/c_runtime_api.h>
+#include <tvm/runtime/crt/func_registry.h>
+#include <tvm/runtime/crt/internal/common/ndarray.h>
+#include <tvm/runtime/crt/internal/common/packed_func.h>
+#include <tvm/runtime/crt/internal/graph_runtime/graph_runtime.h>
+#include <tvm/runtime/crt/memory.h>
+#include <tvm/runtime/crt/platform.h>
+
+// Handle internal errors
+
+static char g_last_error[1024];
+
+void TVMAPISetLastError(const char* msg) { strncpy(g_last_error, msg, 
sizeof(g_last_error)); }
+
+__attribute__((format(printf, 1, 2))) int TVMAPIErrorf(const char* msg, ...) {
+  va_list args;
+  int to_return;
+
+  va_start(args, msg);
+  to_return = vsnprintf(g_last_error, sizeof(g_last_error), msg, args);
+  va_end(args);
+
+  return to_return;
+}
+
+const char* TVMGetLastError(void) { return g_last_error; }
+
+// Manipulate NDArray on target device
+
+int TVMArrayAlloc(const tvm_index_t* shape, int ndim, int dtype_code, int 
dtype_bits,
+                  int dtype_lanes, int device_type, int device_id, 
TVMArrayHandle* out) {
+  DLDataType dtype;
+  dtype.code = dtype_code;
+  dtype.bits = dtype_bits;
+  dtype.lanes = dtype_lanes;
+  DLContext ctx;
+  ctx.device_type = (DLDeviceType)device_type;
+  ctx.device_id = device_id;
+  TVMNDArray arr = TVMNDArray_Empty(ndim, shape, dtype, ctx);
+  **out = arr.dl_tensor;
+  return 0;
+}
+
+int TVMArrayFree(TVMArrayHandle handle) {
+  TVMNDArray arr;
+  arr.dl_tensor = *handle;
+  return TVMNDArray_Release(&arr);
+}
+
+int TVMDeviceAllocDataSpace(DLContext ctx, size_t nbytes, size_t alignment, 
DLDataType type_hint,
+                            void** out_data) {
+  if (alignment != 1) {
+    nbytes = (nbytes + alignment - 1) / alignment * alignment;
+  }
+
+  *out_data = vmalloc(nbytes);
+  return 0;
+}
+
+int TVMDeviceFreeDataSpace(TVMContext ctx, void* ptr) {
+  vfree(ptr);
+  return 0;
+}
+
+int TVMDeviceCopyDataFromTo(const void* from, size_t from_offset, void* to, 
size_t to_offset,
+                            size_t num_bytes, TVMContext ctx_from, TVMContext 
ctx_to,
+                            DLDataType type_hint, TVMStreamHandle stream) {
+  memcpy(((uint8_t*)to) + to_offset, ((uint8_t*)from) + from_offset, 
num_bytes);
+  return 0;
+}
+
+int TVMSynchronize(int device_type, int device_id, TVMStreamHandle stream) { 
return 0; }
+
+static TVMMutableFuncRegistry global_func_registry;
+
+int TVMFuncRegisterGlobal(const char* name, TVMFunctionHandle f, int override) 
{
+  return TVMMutableFuncRegistry_Set(&global_func_registry, name, f, override 
!= 0);
+}
+
+static const TVMModule* registered_modules[TVM_CRT_MAX_REGISTERED_MODULES];
+
+/*! \brief Passed as `module_index` to EncodeFunctionHandle. */
+static const tvm_module_index_t kGlobalFuncModuleIndex = 
TVM_CRT_MAX_REGISTERED_MODULES;
+
+static int DecodeModuleHandle(TVMModuleHandle handle, tvm_module_index_t* 
out_module_index) {
+  tvm_module_index_t module_index;
+
+  module_index = ((tvm_module_index_t)((uintptr_t)handle)) & ~0x8000;
+  if (module_index > TVM_CRT_MAX_REGISTERED_MODULES || 
registered_modules[module_index] == NULL) {
+    TVMAPIErrorf("invalid module handle: %08x", module_index);
+    return -1;
+  }
+
+  *out_module_index = module_index;
+  return 0;
+}
+
+static TVMModuleHandle EncodeModuleHandle(tvm_module_index_t module_index) {
+  return (TVMModuleHandle)((uintptr_t)(module_index | 0x8000));
+}
+
+static int _TVMModCreateFromCModule(const TVMModule* mod, TVMModuleHandle* 
out_handle) {
+  tvm_module_index_t idx;
+
+  for (idx = 0; idx < TVM_CRT_MAX_REGISTERED_MODULES; idx++) {
+    if (registered_modules[idx] == NULL) {
+      registered_modules[idx] = mod;
+      *out_handle = EncodeModuleHandle(idx);
+      return 0;
+    }
+  }
+
+  return -1;
+}
+
+int TVMModFree(TVMModuleHandle mod) {
+  tvm_module_index_t module_index;
+  if (DecodeModuleHandle(mod, &module_index) != 0) {
+    return -1;
+  }
+
+  registered_modules[module_index] = NULL;
+  return 0;
+}
+
+static const TVMModuleHandle kTVMModuleHandleUninitialized = 
(TVMModuleHandle)(~0UL);
+
+static TVMModuleHandle system_lib_handle;
+
+int SystemLibraryCreate(TVMValue* args, int* type_codes, int num_args, 
TVMValue* ret_val,
+                        int* ret_type_codes) {
+  const TVMModule* system_lib;
+
+  if (system_lib_handle == kTVMModuleHandleUninitialized) {
+    system_lib = TVMSystemLibEntryPoint();
+    if (_TVMModCreateFromCModule(system_lib, &system_lib_handle) != 0) {
+      TVMAPIErrorf("error registering system lib");
+      return -1;
+    }
+  }
+
+  ret_val[0].v_handle = system_lib_handle;
+  ret_type_codes[0] = kTVMModuleHandle;
+  return 0;
+}
+
+static TVMFunctionHandle EncodeFunctionHandle(tvm_module_index_t module_index,
+                                              tvm_function_index_t 
function_index) {
+  return (TVMFunctionHandle)((uintptr_t)(
+      ((module_index | 0x8000) << (sizeof(tvm_function_index_t) * 8)) | 
(function_index | 0x8000)));

Review comment:
       I left this in so that all valid TVMFunctionHandle are non-zero




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to