MLIR  16.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 
12 #include <cstring>
13 
15  return mlirStringRefCreate(str, strlen(str));
16 }
17 
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
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 }
const char * data
Pointer to the first symbol.
Definition: Support.h:72
MlirTypeIDAllocator mlirTypeIDAllocatorCreate()
Creates a type id allocator for dynamic type id creation.
Definition: Support.cpp:48
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&#39;s usag...
Definition: Support.cpp:27
MlirStringRef mlirStringRefCreateFromCString(const char *str)
Constructs a string reference from a null-terminated C string.
Definition: Support.cpp:14
void mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator)
Deallocates the allocator and all allocated type ids.
Definition: Support.cpp:52
llvm::hash_code hash_value(const MPInt &x)
Redeclarations of friend declaration above to make it discoverable by lookups.
Definition: MPInt.cpp:15
size_t mlirTypeIDHashValue(MlirTypeID typeID)
Returns the hash value of the type id.
Definition: Support.cpp:40
MlirTypeID mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator)
Allocates a type id that is valid for the lifetime of the allocator.
Definition: Support.cpp:56
size_t length
Length of the fragment.
Definition: Support.h:73
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
Definition: Support.h:80
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition: Support.h:71
static TypeID getFromOpaquePointer(const void *pointer)
Definition: TypeID.h:132
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition: Diagnostics.h:24
This class provides a way to define new TypeIDs at runtime.
Definition: TypeID.h:292
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition: Diagnostics.h:19
bool mlirStringRefEqual(MlirStringRef string, MlirStringRef other)
Returns true if two string references are equal, false otherwise.
Definition: Support.cpp:18
bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2)
Checks if two type ids are equal.
Definition: Support.cpp:36