1/**
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef GLOW_BACKENDS_CPU_CPUBACKEND_H
17#define GLOW_BACKENDS_CPU_CPUBACKEND_H
18
19#include "CPUDeviceManager.h"
20
21#include "glow/Backend/Backend.h"
22#include "glow/Base/Tensor.h"
23#include "glow/LLVMIRCodeGen/LLVMBackend.h"
24
25#include "llvm/ADT/ArrayRef.h"
26#include "llvm/IR/IRBuilder.h"
27
28#include <vector>
29
30namespace glow {
31
32class NodeInfo;
33
34class CPUBackend : public LLVMBackend {
35public:
36 CPUBackend();
37
38 /// @name Backend methods.
39 /// This is the implementation of the Backend interface.
40 ///@{
41 virtual ~CPUBackend() override = default;
42
43 std::string getBackendName() const override {
44 return Named::getName().empty() ? getName() : Named::getName().str();
45 }
46 static std::string getName() { return "CPU"; }
47 static unsigned numDevices();
48 static std::vector<unsigned> scanDeviceIDs();
49
50 Expected<bool> transformPostLowering(
51 Function *F, CompilationContext &cctx,
52 const glow::runtime::DeviceInfo *devInfo = nullptr) const override;
53
54 /// \returns whether the provided \p NI is supported by the backend.
55 bool isOpSupported(const NodeInfo &NI) const override;
56
57 bool shouldLower(const Node *N) const override;
58
59 /// \returns whether the backend supports fusing \p activation into \p parent.
60 bool supportsFusedActivation(Node *parent, Node *activation) const override;
61
62 runtime::DeviceManager *
63 createDeviceManager(const runtime::DeviceConfig &deviceConfig) override {
64 return createCPUDeviceManager(deviceConfig);
65 }
66
67 /// \returns true if network supports Type Lowering from \p T1 to \p T2.
68 /// Populates PrecisionConfiguration with black list of operations that can't
69 /// be converted.
70 virtual bool
71 canDoIndexTypeDemotion(ElemKind fromTy, ElemKind toTy,
72 PrecisionConfiguration &precConfig) const override;
73
74 llvm::ArrayRef<llvm::MemoryBufferRef> getObjectRegistry() const override;
75 /// @}
76
77public:
78 /// @name LLVMBackend methods.
79 /// This is the implementation of the LLVMBackend interface.
80 ///@{
81 virtual std::unique_ptr<LLVMIRGen>
82 createIRGen(const IRFunction *IR,
83 AllocationsInfo &allocationsInfo) const override;
84
85protected:
86 virtual std::unique_ptr<CompiledFunction>
87 createCompiledFunction(std::unique_ptr<GlowJIT> JIT,
88 runtime::RuntimeBundle &&runtimeBundle) const override;
89
90 virtual llvm::StringRef getLibjitBitcode() const override;
91 /// @}
92};
93
94} // namespace glow
95
96#endif // GLOW_BACKENDS_CPU_CPUBACKEND_H
97