1 //===- InterfaceSupport.cpp - MLIR Interface Support Classes --------------===//
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 defines several support classes for defining interfaces.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Support/InterfaceSupport.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/raw_ostream.h"
16 
17 #define DEBUG_TYPE "interfaces"
18 
19 using namespace mlir;
20 
InterfaceMap(MutableArrayRef<std::pair<TypeID,void * >> elements)21 detail::InterfaceMap::InterfaceMap(
22     MutableArrayRef<std::pair<TypeID, void *>> elements)
23     : interfaces(elements.begin(), elements.end()) {
24   llvm::sort(interfaces, [](const auto &lhs, const auto &rhs) {
25     return compare(lhs.first, rhs.first);
26   });
27 }
28 
insert(ArrayRef<std::pair<TypeID,void * >> elements)29 void detail::InterfaceMap::insert(
30     ArrayRef<std::pair<TypeID, void *>> elements) {
31   // Insert directly into the right position to keep the interfaces sorted.
32   for (auto &element : elements) {
33     TypeID id = element.first;
34     auto *it = llvm::lower_bound(interfaces, id, [](const auto &it, TypeID id) {
35       return compare(it.first, id);
36     });
37     if (it != interfaces.end() && it->first == id) {
38       LLVM_DEBUG(llvm::dbgs() << "Ignoring repeated interface registration");
39       free(element.second);
40       continue;
41     }
42     interfaces.insert(it, element);
43   }
44 }
45