MLIR 22.0.0git
Support.cpp
Go to the documentation of this file.
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
16 return mlirStringRefCreate(str, strlen(str));
17}
18
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(new llvm::DefaultThreadPool());
29}
30
31void mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool threadPool) {
32 delete unwrap(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
45}
46
47bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2) {
48 return unwrap(typeID1) == unwrap(typeID2);
49}
50
51size_t mlirTypeIDHashValue(MlirTypeID typeID) {
52 return hash_value(unwrap(typeID));
53}
54
55//===----------------------------------------------------------------------===//
56// TypeIDAllocator API.
57//===----------------------------------------------------------------------===//
58
59MlirTypeIDAllocator mlirTypeIDAllocatorCreate() {
60 return wrap(new mlir::TypeIDAllocator());
61}
62
63void mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator) {
64 delete unwrap(allocator);
65}
66
67MlirTypeID mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator) {
68 return wrap(unwrap(allocator)->allocate());
69}
bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2)
Checks if two type ids are equal.
Definition Support.cpp:47
MlirTypeID mlirTypeIDCreate(const void *ptr)
ptr must be 8 byte aligned and unique to a type valid for the duration of the returned type id's usag...
Definition Support.cpp:38
size_t mlirTypeIDHashValue(MlirTypeID typeID)
Returns the hash value of the type id.
Definition Support.cpp:51
void mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator)
Deallocates the allocator and all allocated type ids.
Definition Support.cpp:63
void mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool threadPool)
Destroy an LLVM thread pool.
Definition Support.cpp:31
MlirStringRef mlirStringRefCreateFromCString(const char *str)
Constructs a string reference from a null-terminated C string.
Definition Support.cpp:15
bool mlirStringRefEqual(MlirStringRef string, MlirStringRef other)
Returns true if two string references are equal, false otherwise.
Definition Support.cpp:19
MlirTypeID mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator)
Allocates a type id that is valid for the lifetime of the allocator.
Definition Support.cpp:67
MlirLlvmThreadPool mlirLlvmThreadPoolCreate()
Create an LLVM thread pool.
Definition Support.cpp:27
MlirTypeIDAllocator mlirTypeIDAllocatorCreate()
Creates a type id allocator for dynamic type id creation.
Definition Support.cpp:59
This class provides a way to define new TypeIDs at runtime.
Definition TypeID.h:349
static TypeID getFromOpaquePointer(const void *pointer)
Definition TypeID.h:135
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition Diagnostics.h:24
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition Diagnostics.h:19
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
Definition Support.h:82
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:73
const char * data
Pointer to the first symbol.
Definition Support.h:74
size_t length
Length of the fragment.
Definition Support.h:75