xref: /llvm-project/mlir/lib/CAPI/IR/Support.cpp (revision 716042a63f26cd020eb72960f72fa97b9a197382)
1 //===- Support.cpp - Helpers for C interface to MLIR API ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/CAPI/Support.h"
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/Support/ThreadPool.h"
12 
13 #include <cstring>
14 
mlirStringRefCreateFromCString(const char * str)15 MlirStringRef mlirStringRefCreateFromCString(const char *str) {
16   return mlirStringRefCreate(str, strlen(str));
17 }
18 
mlirStringRefEqual(MlirStringRef string,MlirStringRef other)19 bool mlirStringRefEqual(MlirStringRef string, MlirStringRef other) {
20   return llvm::StringRef(string.data, string.length) ==
21          llvm::StringRef(other.data, other.length);
22 }
23 
24 //===----------------------------------------------------------------------===//
25 // LLVM ThreadPool API.
26 //===----------------------------------------------------------------------===//
mlirLlvmThreadPoolCreate()27 MlirLlvmThreadPool mlirLlvmThreadPoolCreate() {
28   return wrap(new llvm::DefaultThreadPool());
29 }
30 
mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool threadPool)31 void mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool threadPool) {
32   delete unwrap(threadPool);
33 }
34 
35 //===----------------------------------------------------------------------===//
36 // TypeID API.
37 //===----------------------------------------------------------------------===//
mlirTypeIDCreate(const void * ptr)38 MlirTypeID mlirTypeIDCreate(const void *ptr) {
39   assert(reinterpret_cast<uintptr_t>(ptr) % 8 == 0 &&
40          "ptr must be 8 byte aligned");
41   // This is essentially a no-op that returns back `ptr`, but by going through
42   // the `TypeID` functions we can get compiler errors in case the `TypeID`
43   // api/representation changes
44   return wrap(mlir::TypeID::getFromOpaquePointer(ptr));
45 }
46 
mlirTypeIDEqual(MlirTypeID typeID1,MlirTypeID typeID2)47 bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2) {
48   return unwrap(typeID1) == unwrap(typeID2);
49 }
50 
mlirTypeIDHashValue(MlirTypeID typeID)51 size_t mlirTypeIDHashValue(MlirTypeID typeID) {
52   return hash_value(unwrap(typeID));
53 }
54 
55 //===----------------------------------------------------------------------===//
56 // TypeIDAllocator API.
57 //===----------------------------------------------------------------------===//
58 
mlirTypeIDAllocatorCreate()59 MlirTypeIDAllocator mlirTypeIDAllocatorCreate() {
60   return wrap(new mlir::TypeIDAllocator());
61 }
62 
mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator)63 void mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator) {
64   delete unwrap(allocator);
65 }
66 
mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator)67 MlirTypeID mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator) {
68   return wrap(unwrap(allocator)->allocate());
69 }
70