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
15MlirStringRef mlirStringRefCreateFromCString(const char *str) {
16 return mlirStringRefCreate(str, length: strlen(s: str));
17}
18
19bool 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//===----------------------------------------------------------------------===//
27MlirLlvmThreadPool mlirLlvmThreadPoolCreate() {
28 return wrap(cpp: new llvm::DefaultThreadPool());
29}
30
31void mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool threadPool) {
32 delete unwrap(c: threadPool);
33}
34
35//===----------------------------------------------------------------------===//
36// TypeID API.
37//===----------------------------------------------------------------------===//
38MlirTypeID 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(cpp: mlir::TypeID::getFromOpaquePointer(pointer: ptr));
45}
46
47bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2) {
48 return unwrap(c: typeID1) == unwrap(c: typeID2);
49}
50
51size_t mlirTypeIDHashValue(MlirTypeID typeID) {
52 return hash_value(id: unwrap(c: typeID));
53}
54
55//===----------------------------------------------------------------------===//
56// TypeIDAllocator API.
57//===----------------------------------------------------------------------===//
58
59MlirTypeIDAllocator mlirTypeIDAllocatorCreate() {
60 return wrap(cpp: new mlir::TypeIDAllocator());
61}
62
63void mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator) {
64 delete unwrap(c: allocator);
65}
66
67MlirTypeID mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator) {
68 return wrap(cpp: unwrap(c: allocator)->allocate());
69}
70

source code of mlir/lib/CAPI/IR/Support.cpp