MLIR 23.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/FileSystem.h"
12#include "llvm/Support/ThreadPool.h"
13#include "llvm/Support/raw_ostream.h"
14
15#include <cstring>
16#include <string>
17
19 return mlirStringRefCreate(str, strlen(str));
20}
21
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(new llvm::DefaultThreadPool());
32}
33
34void mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool threadPool) {
35 delete unwrap(threadPool);
36}
37
38int mlirLlvmThreadPoolGetMaxConcurrency(MlirLlvmThreadPool threadPool) {
39 return unwrap(threadPool)->getMaxConcurrency();
40}
41
42//===----------------------------------------------------------------------===//
43// LLVM raw_fd_ostream API.
44//===----------------------------------------------------------------------===//
45
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(message.data(), message.size()),
57 userData);
58 }
59 return wrap(static_cast<llvm::raw_fd_ostream *>(nullptr));
60 }
61 return wrap(stream);
62}
63
65 MlirStringRef string) {
66 unwrap(stream)->write(string.data, string.length);
67}
68
70 return !stream.ptr;
71}
72
74 delete unwrap(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
87}
88
89bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2) {
90 return unwrap(typeID1) == unwrap(typeID2);
91}
92
93size_t mlirTypeIDHashValue(MlirTypeID typeID) {
94 return hash_value(unwrap(typeID));
95}
96
97//===----------------------------------------------------------------------===//
98// TypeIDAllocator API.
99//===----------------------------------------------------------------------===//
100
101MlirTypeIDAllocator mlirTypeIDAllocatorCreate() {
102 return wrap(new mlir::TypeIDAllocator());
103}
104
105void mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator) {
106 delete unwrap(allocator);
107}
108
109MlirTypeID mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator) {
110 return wrap(unwrap(allocator)->allocate());
111}
void mlirLlvmRawFdOStreamDestroy(MlirLlvmRawFdOStream stream)
Destroy a raw_fd_ostream created with mlirLlvmRawFdOStreamCreate.
Definition Support.cpp:73
bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2)
Checks if two type ids are equal.
Definition Support.cpp:89
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:80
bool mlirLlvmRawFdOStreamIsNull(MlirLlvmRawFdOStream stream)
Checks if a raw_fd_ostream is null.
Definition Support.cpp:69
void mlirLlvmRawFdOStreamWrite(MlirLlvmRawFdOStream stream, MlirStringRef string)
Write a string to a raw_fd_ostream created with mlirLlvmRawFdOStreamCreate.
Definition Support.cpp:64
size_t mlirTypeIDHashValue(MlirTypeID typeID)
Returns the hash value of the type id.
Definition Support.cpp:93
void mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator)
Deallocates the allocator and all allocated type ids.
Definition Support.cpp:105
void mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool threadPool)
Destroy an LLVM thread pool.
Definition Support.cpp:34
MlirStringRef mlirStringRefCreateFromCString(const char *str)
Constructs a string reference from a null-terminated C string.
Definition Support.cpp:18
int mlirLlvmThreadPoolGetMaxConcurrency(MlirLlvmThreadPool threadPool)
Returns the maximum number of threads in the thread pool.
Definition Support.cpp:38
bool mlirStringRefEqual(MlirStringRef string, MlirStringRef other)
Returns true if two string references are equal, false otherwise.
Definition Support.cpp:22
MlirTypeID mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator)
Allocates a type id that is valid for the lifetime of the allocator.
Definition Support.cpp:109
MlirLlvmThreadPool mlirLlvmThreadPoolCreate()
Create an LLVM thread pool.
Definition Support.cpp:30
MlirTypeIDAllocator mlirTypeIDAllocatorCreate()
Creates a type id allocator for dynamic type id creation.
Definition Support.cpp:101
MlirLlvmRawFdOStream mlirLlvmRawFdOStreamCreate(const char *path, bool binary, MlirStringCallback errorCallback, void *userData)
Create a raw_fd_ostream for the given path.
Definition Support.cpp:47
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:87
void(* MlirStringCallback)(MlirStringRef, void *)
A callback for returning string references.
Definition Support.h:110
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:78
const char * data
Pointer to the first symbol.
Definition Support.h:79
size_t length
Length of the fragment.
Definition Support.h:80