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/FileSystem.h"
12#include "llvm/Support/ThreadPool.h"
13#include "llvm/Support/raw_ostream.h"
14
15#include <cstring>
16#include <string>
17
18MlirStringRef mlirStringRefCreateFromCString(const char *str) {
19 return mlirStringRefCreate(str, length: strlen(s: str));
20}
21
22bool mlirStringRefEqual(MlirStringRef string, MlirStringRef other) {
23 return llvm::StringRef(string.data, string.length) ==
24 llvm::StringRef(other.data, other.length);
25}
26
27//===----------------------------------------------------------------------===//
28// LLVM ThreadPool API.
29//===----------------------------------------------------------------------===//
30MlirLlvmThreadPool mlirLlvmThreadPoolCreate() {
31 return wrap(cpp: new llvm::DefaultThreadPool());
32}
33
34void mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool threadPool) {
35 delete unwrap(c: threadPool);
36}
37
38int mlirLlvmThreadPoolGetMaxConcurrency(MlirLlvmThreadPool threadPool) {
39 return unwrap(c: threadPool)->getMaxConcurrency();
40}
41
42//===----------------------------------------------------------------------===//
43// LLVM raw_fd_ostream API.
44//===----------------------------------------------------------------------===//
45
46MlirLlvmRawFdOStream
47mlirLlvmRawFdOStreamCreate(const char *path, bool binary,
48 MlirStringCallback errorCallback, void *userData) {
49 std::error_code ec;
50 auto flags = binary ? llvm::sys::fs::OF_None : llvm::sys::fs::OF_Text;
51 auto *stream = new llvm::raw_fd_ostream(path, ec, flags);
52 if (ec) {
53 delete stream;
54 if (errorCallback) {
55 std::string message = ec.message();
56 errorCallback(mlirStringRefCreate(str: message.data(), length: message.size()),
57 userData);
58 }
59 return wrap(cpp: static_cast<llvm::raw_fd_ostream *>(nullptr));
60 }
61 return wrap(cpp: stream);
62}
63
64void mlirLlvmRawFdOStreamWrite(MlirLlvmRawFdOStream stream,
65 MlirStringRef string) {
66 unwrap(c: stream)->write(Ptr: string.data, Size: string.length);
67}
68
69bool mlirLlvmRawFdOStreamIsNull(MlirLlvmRawFdOStream stream) {
70 return !stream.ptr;
71}
72
73void mlirLlvmRawFdOStreamDestroy(MlirLlvmRawFdOStream stream) {
74 delete unwrap(c: stream);
75}
76
77//===----------------------------------------------------------------------===//
78// TypeID API.
79//===----------------------------------------------------------------------===//
80MlirTypeID mlirTypeIDCreate(const void *ptr) {
81 assert(reinterpret_cast<uintptr_t>(ptr) % 8 == 0 &&
82 "ptr must be 8 byte aligned");
83 // This is essentially a no-op that returns back `ptr`, but by going through
84 // the `TypeID` functions we can get compiler errors in case the `TypeID`
85 // api/representation changes
86 return wrap(cpp: mlir::TypeID::getFromOpaquePointer(pointer: ptr));
87}
88
89bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2) {
90 return unwrap(c: typeID1) == unwrap(c: typeID2);
91}
92
93size_t mlirTypeIDHashValue(MlirTypeID typeID) {
94 return hash_value(id: unwrap(c: typeID));
95}
96
97//===----------------------------------------------------------------------===//
98// TypeIDAllocator API.
99//===----------------------------------------------------------------------===//
100
101MlirTypeIDAllocator mlirTypeIDAllocatorCreate() {
102 return wrap(cpp: new mlir::TypeIDAllocator());
103}
104
105void mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator) {
106 delete unwrap(c: allocator);
107}
108
109MlirTypeID mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator) {
110 return wrap(cpp: unwrap(c: allocator)->allocate());
111}
112