1 /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #include "tensorflow/compiler/xla/runtime/custom_call_registry.h" 17 18 #include <memory> 19 #include <string> 20 #include <utility> 21 #include <vector> 22 23 #include "llvm/ADT/SmallVector.h" 24 #include "llvm/ADT/StringMap.h" 25 26 namespace xla { 27 namespace runtime { 28 29 struct CustomCallRegistry::Impl { 30 llvm::StringMap<std::unique_ptr<CustomCall>> custom_calls; 31 }; 32 CustomCallRegistry()33CustomCallRegistry::CustomCallRegistry() : impl_(std::make_unique<Impl>()) {} 34 Register(std::unique_ptr<CustomCall> custom_call)35void CustomCallRegistry::Register(std::unique_ptr<CustomCall> custom_call) { 36 llvm::StringRef key = custom_call->name(); 37 auto inserted = impl_->custom_calls.insert({key, std::move(custom_call)}); 38 assert(inserted.second && "duplicate custom call registration"); 39 (void)inserted; 40 } 41 Find(llvm::StringRef callee) const42CustomCall* CustomCallRegistry::Find(llvm::StringRef callee) const { 43 auto it = impl_->custom_calls.find(callee); 44 if (it == impl_->custom_calls.end()) return nullptr; 45 return it->second.get(); 46 } 47 48 static std::vector<CustomCallRegistry::RegistrationFunction>* GetCustomCallRegistrations()49GetCustomCallRegistrations() { 50 static auto* ret = new std::vector<CustomCallRegistry::RegistrationFunction>; 51 return ret; 52 } 53 RegisterStaticCustomCalls(CustomCallRegistry * custom_call_registry)54void RegisterStaticCustomCalls(CustomCallRegistry* custom_call_registry) { 55 for (auto func : *GetCustomCallRegistrations()) func(custom_call_registry); 56 } 57 AddStaticCustomCallRegistration(CustomCallRegistry::RegistrationFunction registration)58void AddStaticCustomCallRegistration( 59 CustomCallRegistry::RegistrationFunction registration) { 60 GetCustomCallRegistrations()->push_back(registration); 61 } 62 63 } // namespace runtime 64 } // namespace xla 65