P4C
The P4 Compiler
Loading...
Searching...
No Matches
p4tools/common/core/target.h
1/*
2 * SPDX-FileCopyrightText: 2022 The P4 Language Consortium
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef BACKENDS_P4TOOLS_COMMON_CORE_TARGET_H_
8#define BACKENDS_P4TOOLS_COMMON_CORE_TARGET_H_
9
10#include <cstdlib>
11#include <map>
12#include <optional>
13#include <string>
14
15#include "ir/ir.h"
16#include "lib/compile_context.h"
17#include "lib/exceptions.h"
18
19namespace P4::P4Tools {
20
25class Target {
26 public:
28 struct Spec {
29 // TODO: Prefix the variables here and in target with "_" to avoid ambiguities.
30 std::string deviceName;
31 std::string archName;
32
34 Spec(std::string_view deviceName, std::string_view archName);
35
37 bool operator<(const Spec &) const;
38 };
39
44 static bool init(std::string_view deviceName, std::string_view archName);
45
52 static bool setDevice(std::string_view deviceName);
53
60 static bool setArch(std::string_view archName);
61
63 std::string toolName;
64
67
68 // A virtual destructor makes this class polymorphic, so that the dynamic cast in get() can
69 // work.
70 virtual ~Target() = default;
71
76 virtual const IR::Expression *createTargetUninitialized(const IR::Type *type,
77 bool forceTaint) const;
78
81 static std::optional<ICompileContext *> initializeTarget(std::string_view toolName,
82 const std::vector<const char *> &args);
83 static std::optional<ICompileContext *> initializeTarget(std::string_view toolName,
84 std::string_view target,
85 std::string_view arch);
86
87 protected:
90 Target(std::string_view toolName, const std::string &deviceName, const std::string &archName);
91
93 [[nodiscard]] virtual ICompileContext *makeContext() const = 0;
94
96 //
97 // Implemented here because of limitations of templates.
98 template <class TargetImpl>
99 static const TargetImpl &get(std::string_view toolName) {
100 if (curTarget == std::nullopt) {
101 FATAL_ERROR(
102 "Target not initialized. Please provide a target using the --target option.");
103 }
104
105 const auto &instances = registry.at(*curTarget);
106 auto instance = instances.find(toolName);
107 BUG_CHECK(instance != instances.end(),
108 "Architecture %1% on device %2% not supported for %3%", curTarget->archName,
109 curTarget->deviceName, toolName);
110
111 const auto *casted = dynamic_cast<const TargetImpl *>(instance->second);
112 BUG_CHECK(casted, "%1%/%2% implementation for %3% has wrong type", curTarget->deviceName,
113 curTarget->archName, toolName);
114 return *casted;
115 }
116
117 private:
119 static std::optional<Spec> curTarget;
120
122 static std::map<Spec, std::map<std::string, const Target *, std::less<>>> registry;
123
125 static std::map<std::string, std::string, std::less<>> defaultArchByDevice;
126
128 static std::map<std::string, std::string, std::less<>> defaultDeviceByArch;
129};
130
131} // namespace P4::P4Tools
132
133#endif /* BACKENDS_P4TOOLS_COMMON_CORE_TARGET_H_ */
Definition compile_context.h:23
static const TargetImpl & get(std::string_view toolName)
Definition p4tools/common/core/target.h:99
std::string toolName
The name of the tool supported by this instance.
Definition p4tools/common/core/target.h:63
virtual ICompileContext * makeContext() const =0
Target(std::string_view toolName, const std::string &deviceName, const std::string &archName)
Definition p4tools/common/core/target.cpp:146
static bool setDevice(std::string_view deviceName)
Definition p4tools/common/core/target.cpp:117
static std::optional< ICompileContext * > initializeTarget(std::string_view toolName, const std::vector< const char * > &args)
Definition p4tools/common/core/target.cpp:73
static bool init(std::string_view deviceName, std::string_view archName)
Definition p4tools/common/core/target.cpp:38
Spec spec
The device and architecture supported by this instance.
Definition p4tools/common/core/target.h:66
virtual const IR::Expression * createTargetUninitialized(const IR::Type *type, bool forceTaint) const
Definition p4tools/common/core/target.cpp:138
static bool setArch(std::string_view archName)
Definition p4tools/common/core/target.cpp:127
Definition common/compiler/compiler_result.cpp:7
Specifies a target device and architecture by their names in lower case.
Definition p4tools/common/core/target.h:28
Spec(std::string_view deviceName, std::string_view archName)
Names provided to this constructor are converted to lower case.
Definition p4tools/common/core/target.cpp:19
bool operator<(const Spec &) const
Lexicographic ordering on (deviceName, archName).
Definition p4tools/common/core/target.cpp:22