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 
12 #include <cstring>
13 
14 MlirStringRef mlirStringRefCreateFromCString(const char *str) {
15   return mlirStringRefCreate(str, strlen(str));
16 }
17 
18 bool mlirStringRefEqual(MlirStringRef string, MlirStringRef other) {
19   return llvm::StringRef(string.data, string.length) ==
20          llvm::StringRef(other.data, other.length);
21 }
22 
23 //===----------------------------------------------------------------------===//
24 // TypeID API.
25 //===----------------------------------------------------------------------===//
26 
27 MlirTypeID mlirTypeIDCreate(const void *ptr) {
28   assert(reinterpret_cast<uintptr_t>(ptr) % 8 == 0 &&
29          "ptr must be 8 byte aligned");
30   // This is essentially a no-op that returns back `ptr`, but by going through
31   // the `TypeID` functions we can get compiler errors in case the `TypeID`
32   // api/representation changes
33   return wrap(mlir::TypeID::getFromOpaquePointer(ptr));
34 }
35 
36 bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2) {
37   return unwrap(typeID1) == unwrap(typeID2);
38 }
39 
40 size_t mlirTypeIDHashValue(MlirTypeID typeID) {
41   return hash_value(unwrap(typeID));
42 }
43 
44 //===----------------------------------------------------------------------===//
45 // TypeIDAllocator API.
46 //===----------------------------------------------------------------------===//
47 
48 MlirTypeIDAllocator mlirTypeIDAllocatorCreate() {
49   return wrap(new mlir::TypeIDAllocator());
50 }
51 
52 void mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator) {
53   delete unwrap(allocator);
54 }
55 
56 MlirTypeID mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator) {
57   return wrap(unwrap(allocator)->allocate());
58 }
59