xref: /llvm-project/mlir/lib/Dialect/SparseTensor/IR/Detail/TemplateExtras.h (revision 7c2ef38c36eda2907cd6a3efff88bb86a1b381a3)
16b88c852SAart Bik //===- TemplateExtras.h -----------------------------------------*- C++ -*-===//
26b88c852SAart Bik //
36b88c852SAart Bik // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
46b88c852SAart Bik // See https://llvm.org/LICENSE.txt for license information.
56b88c852SAart Bik // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66b88c852SAart Bik //
76b88c852SAart Bik //===----------------------------------------------------------------------===//
86b88c852SAart Bik 
96b88c852SAart Bik #ifndef MLIR_DIALECT_SPARSETENSOR_IR_DETAIL_TEMPLATEEXTRAS_H
106b88c852SAart Bik #define MLIR_DIALECT_SPARSETENSOR_IR_DETAIL_TEMPLATEEXTRAS_H
116b88c852SAart Bik 
126b88c852SAart Bik #include <utility>
136b88c852SAart Bik 
146b88c852SAart Bik #include "llvm/ADT/STLExtras.h"
156b88c852SAart Bik #include "llvm/Support/raw_ostream.h"
166b88c852SAart Bik 
176b88c852SAart Bik namespace mlir {
186b88c852SAart Bik namespace sparse_tensor {
196b88c852SAart Bik namespace ir_detail {
206b88c852SAart Bik 
216b88c852SAart Bik //===----------------------------------------------------------------------===//
226b88c852SAart Bik template <typename T>
236b88c852SAart Bik using has_print_method =
246b88c852SAart Bik     decltype(std::declval<T>().print(std::declval<llvm::raw_ostream &>()));
256b88c852SAart Bik template <typename T>
266b88c852SAart Bik using detect_has_print_method = llvm::is_detected<has_print_method, T>;
276b88c852SAart Bik template <typename T, typename R = void>
286b88c852SAart Bik using enable_if_has_print_method =
296b88c852SAart Bik     std::enable_if_t<detect_has_print_method<T>::value, R>;
306b88c852SAart Bik 
316b88c852SAart Bik /// Generic template for defining `operator<<` overloads which delegate
32*34ed07e6SYinying Li /// to `T::print(raw_ostream&) const`.
336b88c852SAart Bik template <typename T>
346b88c852SAart Bik inline enable_if_has_print_method<T, llvm::raw_ostream &>
356b88c852SAart Bik operator<<(llvm::raw_ostream &os, T const &t) {
366b88c852SAart Bik   t.print(os);
376b88c852SAart Bik   return os;
386b88c852SAart Bik }
396b88c852SAart Bik 
406b88c852SAart Bik //===----------------------------------------------------------------------===//
416b88c852SAart Bik template <typename T>
426b88c852SAart Bik static constexpr bool IsZeroCostAbstraction =
43*34ed07e6SYinying Li     // These two predicates license the compiler to make optimizations.
446b88c852SAart Bik     std::is_trivially_copyable_v<T> && std::is_trivially_destructible_v<T> &&
45*34ed07e6SYinying Li     // This helps ensure ABI compatibility (e.g., padding and alignment).
466b88c852SAart Bik     std::is_standard_layout_v<T> &&
476b88c852SAart Bik     // These two are what SmallVector uses to determine whether it can
48*34ed07e6SYinying Li     // use memcpy.
496a684dbcSFangrui Song     std::is_trivially_copy_constructible<T>::value &&
506a684dbcSFangrui Song     std::is_trivially_move_constructible<T>::value;
516b88c852SAart Bik 
526b88c852SAart Bik //===----------------------------------------------------------------------===//
536b88c852SAart Bik 
546b88c852SAart Bik } // namespace ir_detail
556b88c852SAart Bik } // namespace sparse_tensor
566b88c852SAart Bik } // namespace mlir
576b88c852SAart Bik 
586b88c852SAart Bik #endif // MLIR_DIALECT_SPARSETENSOR_IR_DETAIL_TEMPLATEEXTRAS_H
59