xref: /llvm-project/mlir/include/mlir/CAPI/Support.h (revision db791b278a414fb6df1acc1799adcf11d8fb9169)
1 //===- Support.h - C API Helpers Implementation -----------------*- C++ -*-===//
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 // This file contains definitions for converting MLIR C++ objects into helper
10 // C structures for the purpose of C API. This file should not be included from
11 // C++ code other than C API implementation nor from C code.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_CAPI_SUPPORT_H
16 #define MLIR_CAPI_SUPPORT_H
17 
18 #include "mlir-c/Support.h"
19 #include "mlir/CAPI/Wrap.h"
20 #include "mlir/Support/TypeID.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/LogicalResult.h"
23 
24 namespace llvm {
25 class ThreadPoolInterface;
26 } // namespace llvm
27 
28 /// Converts a StringRef into its MLIR C API equivalent.
wrap(llvm::StringRef ref)29 inline MlirStringRef wrap(llvm::StringRef ref) {
30   return mlirStringRefCreate(ref.data(), ref.size());
31 }
32 
33 /// Creates a StringRef out of its MLIR C API equivalent.
unwrap(MlirStringRef ref)34 inline llvm::StringRef unwrap(MlirStringRef ref) {
35   return llvm::StringRef(ref.data, ref.length);
36 }
37 
wrap(llvm::LogicalResult res)38 inline MlirLogicalResult wrap(llvm::LogicalResult res) {
39   if (mlir::succeeded(res))
40     return mlirLogicalResultSuccess();
41   return mlirLogicalResultFailure();
42 }
43 
unwrap(MlirLogicalResult res)44 inline llvm::LogicalResult unwrap(MlirLogicalResult res) {
45   return mlir::success(mlirLogicalResultIsSuccess(res));
46 }
47 
48 DEFINE_C_API_PTR_METHODS(MlirLlvmThreadPool, llvm::ThreadPoolInterface)
49 DEFINE_C_API_METHODS(MlirTypeID, mlir::TypeID)
50 DEFINE_C_API_PTR_METHODS(MlirTypeIDAllocator, mlir::TypeIDAllocator)
51 
52 #endif // MLIR_CAPI_SUPPORT_H
53