1855ec517SAlex Zinenko //===- Support.cpp - Helpers for C interface to MLIR API ------------------===//
2855ec517SAlex Zinenko //
3855ec517SAlex Zinenko // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4855ec517SAlex Zinenko // See https://llvm.org/LICENSE.txt for license information.
5855ec517SAlex Zinenko // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6855ec517SAlex Zinenko //
7855ec517SAlex Zinenko //===----------------------------------------------------------------------===//
8855ec517SAlex Zinenko 
9*2387fadeSDaniel Resnick #include "mlir/CAPI/Support.h"
1030d61893SAlex Zinenko #include "llvm/ADT/StringRef.h"
11855ec517SAlex Zinenko 
12855ec517SAlex Zinenko #include <cstring>
13855ec517SAlex Zinenko 
mlirStringRefCreateFromCString(const char * str)14855ec517SAlex Zinenko MlirStringRef mlirStringRefCreateFromCString(const char *str) {
15855ec517SAlex Zinenko   return mlirStringRefCreate(str, strlen(str));
16855ec517SAlex Zinenko }
1730d61893SAlex Zinenko 
mlirStringRefEqual(MlirStringRef string,MlirStringRef other)1830d61893SAlex Zinenko bool mlirStringRefEqual(MlirStringRef string, MlirStringRef other) {
1930d61893SAlex Zinenko   return llvm::StringRef(string.data, string.length) ==
2030d61893SAlex Zinenko          llvm::StringRef(other.data, other.length);
2130d61893SAlex Zinenko }
22*2387fadeSDaniel Resnick 
23*2387fadeSDaniel Resnick //===----------------------------------------------------------------------===//
24*2387fadeSDaniel Resnick // TypeID API.
25*2387fadeSDaniel Resnick //===----------------------------------------------------------------------===//
26*2387fadeSDaniel Resnick 
mlirTypeIDCreate(const void * ptr)27*2387fadeSDaniel Resnick MlirTypeID mlirTypeIDCreate(const void *ptr) {
28*2387fadeSDaniel Resnick   assert(reinterpret_cast<uintptr_t>(ptr) % 8 == 0 &&
29*2387fadeSDaniel Resnick          "ptr must be 8 byte aligned");
30*2387fadeSDaniel Resnick   // This is essentially a no-op that returns back `ptr`, but by going through
31*2387fadeSDaniel Resnick   // the `TypeID` functions we can get compiler errors in case the `TypeID`
32*2387fadeSDaniel Resnick   // api/representation changes
33*2387fadeSDaniel Resnick   return wrap(mlir::TypeID::getFromOpaquePointer(ptr));
34*2387fadeSDaniel Resnick }
35*2387fadeSDaniel Resnick 
mlirTypeIDEqual(MlirTypeID typeID1,MlirTypeID typeID2)36*2387fadeSDaniel Resnick bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2) {
37*2387fadeSDaniel Resnick   return unwrap(typeID1) == unwrap(typeID2);
38*2387fadeSDaniel Resnick }
39*2387fadeSDaniel Resnick 
mlirTypeIDHashValue(MlirTypeID typeID)40*2387fadeSDaniel Resnick size_t mlirTypeIDHashValue(MlirTypeID typeID) {
41*2387fadeSDaniel Resnick   return hash_value(unwrap(typeID));
42*2387fadeSDaniel Resnick }
43*2387fadeSDaniel Resnick 
44*2387fadeSDaniel Resnick //===----------------------------------------------------------------------===//
45*2387fadeSDaniel Resnick // TypeIDAllocator API.
46*2387fadeSDaniel Resnick //===----------------------------------------------------------------------===//
47*2387fadeSDaniel Resnick 
mlirTypeIDAllocatorCreate()48*2387fadeSDaniel Resnick MlirTypeIDAllocator mlirTypeIDAllocatorCreate() {
49*2387fadeSDaniel Resnick   return wrap(new mlir::TypeIDAllocator());
50*2387fadeSDaniel Resnick }
51*2387fadeSDaniel Resnick 
mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator)52*2387fadeSDaniel Resnick void mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator) {
53*2387fadeSDaniel Resnick   delete unwrap(allocator);
54*2387fadeSDaniel Resnick }
55*2387fadeSDaniel Resnick 
mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator)56*2387fadeSDaniel Resnick MlirTypeID mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator) {
57*2387fadeSDaniel Resnick   return wrap(unwrap(allocator)->allocate());
58*2387fadeSDaniel Resnick }
59