Skip to content

Commit 9b7a1a4

Browse files
tqchenalexwong
authored andcommitted
[REFACTOR][IR] Introduce include/tvm/target (apache#4721)
As part of Unified IR infra. Introduce target folder to store all the compilation target related information.
1 parent 9e24150 commit 9b7a1a4

File tree

12 files changed

+525
-440
lines changed

12 files changed

+525
-440
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ docs/_build/
6565
docs/gen_modules
6666

6767
# PyBuilder
68-
target/
68+
/target/
6969

7070
# IPython Notebook
7171
.ipynb_checkpoints

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ assign_source_group("Include" ${GROUP_INCLUDE})
128128
file(GLOB COMPILER_SRCS
129129
src/node/*.cc
130130
src/ir/*.cc
131+
src/target/*.cc
131132
src/api/*.cc
132133
src/arithmetic/*.cc
133134
src/autotvm/*.cc

include/tvm/build_module.h

Lines changed: 3 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -24,157 +24,20 @@
2424
#ifndef TVM_BUILD_MODULE_H_
2525
#define TVM_BUILD_MODULE_H_
2626

27+
#include <tvm/target/target.h>
28+
2729
#include <string>
2830
#include <vector>
2931
#include <utility>
3032
#include <unordered_map>
3133
#include <unordered_set>
34+
3235
#include "runtime/packed_func.h"
3336
#include "schedule_pass.h"
3437
#include "lowered_func.h"
3538

3639
namespace tvm {
3740

38-
/*!
39-
* \brief Container for target device information.
40-
* Use target::llvm, target::cuda etc functions instead of constructing directly.
41-
*/
42-
class TargetNode : public Object {
43-
public:
44-
/*! \brief The name of the target device */
45-
std::string target_name;
46-
/*! \brief The name of the target device */
47-
std::string device_name;
48-
/*! \brief The type of the target device */
49-
int device_type;
50-
/*! \brief The maximum threads that a schedule should use for this device */
51-
int max_num_threads = 1;
52-
/*! \brief The warp size that should be used by the LowerThreadAllreduce pass */
53-
int thread_warp_size = 1;
54-
/*! \brief Keys for this target */
55-
Array<PrimExpr> keys_array;
56-
/*! \brief Options for this target */
57-
Array<PrimExpr> options_array;
58-
/*! \brief Collection of imported libs */
59-
Array<PrimExpr> libs_array;
60-
61-
/*! \return the full device string to pass to codegen::Build */
62-
TVM_DLL const std::string& str() const;
63-
64-
void VisitAttrs(AttrVisitor* v) {
65-
v->Visit("target_name", &target_name);
66-
v->Visit("device_name", &device_name);
67-
v->Visit("device_type", &device_type);
68-
v->Visit("max_num_threads", &max_num_threads);
69-
v->Visit("thread_warp_size", &thread_warp_size);
70-
v->Visit("keys_array", &keys_array);
71-
v->Visit("options_array", &options_array);
72-
v->Visit("libs_array", &libs_array);
73-
}
74-
75-
/*! \brief Get the keys for this target as a vector of string */
76-
TVM_DLL std::vector<std::string> keys() const;
77-
78-
/*! \brief Get the options for this target as a vector of string */
79-
TVM_DLL std::vector<std::string> options() const;
80-
81-
/*! \brief Get the keys for this target as an unordered_set of string */
82-
TVM_DLL std::unordered_set<std::string> libs() const;
83-
84-
static constexpr const char* _type_key = "Target";
85-
TVM_DECLARE_FINAL_OBJECT_INFO(TargetNode, Object);
86-
87-
private:
88-
/*! \brief Internal string repr. */
89-
mutable std::string str_repr_;
90-
};
91-
92-
/*! \brief reference cpass to the target. */
93-
class Target : public ObjectRef {
94-
public:
95-
Target() {}
96-
explicit Target(ObjectPtr<Object> n) : ObjectRef(n) {}
97-
/*!
98-
* \brief Create a Target given a string
99-
* \param target_str the string to parse
100-
*/
101-
TVM_DLL static Target Create(const std::string& target_str);
102-
/*!
103-
* \brief Get the current target context from thread local storage.
104-
* \param allow_not_defined If the context stack is empty and this is set to true, an
105-
* undefined Target will be returned. Otherwise, an empty context stack will cause a
106-
* runtime error.
107-
* \return The target that is the current context. The target may not be defined if
108-
* allow_not_defined is true.
109-
*/
110-
TVM_DLL static tvm::Target Current(bool allow_not_defined = true);
111-
112-
const TargetNode* operator->() const {
113-
return static_cast<const TargetNode*>(get());
114-
}
115-
116-
using ContainerType = TargetNode;
117-
class Internal;
118-
private:
119-
// enable with syntax.
120-
friend class Internal;
121-
friend class With<Target>;
122-
/*!
123-
* \brief Push a new target context onto the thread local stack.
124-
* The Target on top of the stack is used to determine which
125-
* specialization to use when invoking a GenericFunc.
126-
*/
127-
TVM_DLL void EnterWithScope();
128-
/*!
129-
* \brief Pop a target off the thread local context stack,
130-
* restoring the previous target as the current context.
131-
*/
132-
TVM_DLL void ExitWithScope();
133-
};
134-
135-
/*! \brief This namespace provides functions to construct Target instances */
136-
namespace target {
137-
/*! \return A target for LLVM */
138-
TVM_DLL Target llvm(const std::vector<std::string>& options =
139-
std::vector<std::string>());
140-
141-
/*! \return A target for CUDA */
142-
TVM_DLL Target cuda(const std::vector<std::string>& options =
143-
std::vector<std::string>());
144-
145-
/*! \return A target for ROCm */
146-
TVM_DLL Target rocm(const std::vector<std::string>& options =
147-
std::vector<std::string>());
148-
149-
/*! \return A target for OpenCL */
150-
TVM_DLL Target opencl(const std::vector<std::string>& options =
151-
std::vector<std::string>());
152-
153-
/*! \return A target for Metal */
154-
TVM_DLL Target metal(const std::vector<std::string>& options =
155-
std::vector<std::string>());
156-
157-
/*! \return A target for rasp */
158-
TVM_DLL Target rasp(const std::vector<std::string>& options =
159-
std::vector<std::string>());
160-
161-
/*! \return A target for Mali */
162-
TVM_DLL Target mali(const std::vector<std::string>& options =
163-
std::vector<std::string>());
164-
165-
/*! \return A target for Intel Graphics */
166-
TVM_DLL Target intel_graphics(const std::vector<std::string>& options =
167-
std::vector<std::string>());
168-
169-
/*! \return A target for stackvm */
170-
TVM_DLL Target stackvm(const std::vector<std::string>& options =
171-
std::vector<std::string>());
172-
173-
/*! \return A target for external device */
174-
TVM_DLL Target ext_dev(const std::vector<std::string>& options =
175-
std::vector<std::string>());
176-
} // namespace target
177-
17841
/*!
17942
* \brief Container for build configuration options
18043
*/

include/tvm/target/target.h

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*!
21+
* \file tvm/target/target.h
22+
* \brief Compilation target object.
23+
*/
24+
#ifndef TVM_TARGET_TARGET_H_
25+
#define TVM_TARGET_TARGET_H_
26+
27+
#include <tvm/support/with.h>
28+
#include <tvm/node/container.h>
29+
#include <tvm/ir/expr.h>
30+
31+
#include <string>
32+
#include <vector>
33+
#include <unordered_set>
34+
35+
namespace tvm {
36+
/*!
37+
* \brief Compilation target.
38+
* \note Use target::llvm, target::cuda etc functions.
39+
* \sa Target
40+
*/
41+
class TargetNode : public Object {
42+
public:
43+
/*! \brief The name of the target device */
44+
std::string target_name;
45+
/*! \brief The name of the target device */
46+
std::string device_name;
47+
/*! \brief The type of the target device */
48+
int device_type;
49+
/*! \brief The maximum threads that a schedule should use for this device */
50+
int max_num_threads = 1;
51+
/*! \brief The warp size that should be used by the LowerThreadAllreduce pass */
52+
int thread_warp_size = 1;
53+
/*! \brief Keys for this target */
54+
Array<PrimExpr> keys_array;
55+
/*! \brief Options for this target */
56+
Array<PrimExpr> options_array;
57+
/*! \brief Collection of imported libs */
58+
Array<PrimExpr> libs_array;
59+
60+
/*! \return the full device string to pass to codegen::Build */
61+
TVM_DLL const std::string& str() const;
62+
63+
void VisitAttrs(AttrVisitor* v) {
64+
v->Visit("target_name", &target_name);
65+
v->Visit("device_name", &device_name);
66+
v->Visit("device_type", &device_type);
67+
v->Visit("max_num_threads", &max_num_threads);
68+
v->Visit("thread_warp_size", &thread_warp_size);
69+
v->Visit("keys_array", &keys_array);
70+
v->Visit("options_array", &options_array);
71+
v->Visit("libs_array", &libs_array);
72+
}
73+
74+
/*! \brief Get the keys for this target as a vector of string */
75+
TVM_DLL std::vector<std::string> keys() const;
76+
77+
/*! \brief Get the options for this target as a vector of string */
78+
TVM_DLL std::vector<std::string> options() const;
79+
80+
/*! \brief Get the keys for this target as an unordered_set of string */
81+
TVM_DLL std::unordered_set<std::string> libs() const;
82+
83+
static constexpr const char* _type_key = "Target";
84+
TVM_DECLARE_FINAL_OBJECT_INFO(TargetNode, Object);
85+
86+
private:
87+
/*! \brief Internal string repr. */
88+
mutable std::string str_repr_;
89+
};
90+
91+
/*!
92+
* \brief Managed reference class to TargetNode.
93+
* \sa TargetNode
94+
*/
95+
class Target : public ObjectRef {
96+
public:
97+
Target() {}
98+
explicit Target(ObjectPtr<Object> n) : ObjectRef(n) {}
99+
/*!
100+
* \brief Create a Target given a string
101+
* \param target_str the string to parse
102+
*/
103+
TVM_DLL static Target Create(const std::string& target_str);
104+
/*!
105+
* \brief Get the current target context from thread local storage.
106+
* \param allow_not_defined If the context stack is empty and this is set to true, an
107+
* undefined Target will be returned. Otherwise, an empty context stack will cause a
108+
* runtime error.
109+
* \return The target that is the current context. The target may not be defined if
110+
* allow_not_defined is true.
111+
*/
112+
TVM_DLL static tvm::Target Current(bool allow_not_defined = true);
113+
114+
const TargetNode* operator->() const {
115+
return static_cast<const TargetNode*>(get());
116+
}
117+
118+
using ContainerType = TargetNode;
119+
class Internal;
120+
private:
121+
// enable with syntax.
122+
friend class Internal;
123+
friend class With<Target>;
124+
/*!
125+
* \brief Push a new target context onto the thread local stack.
126+
* The Target on top of the stack is used to determine which
127+
* specialization to use when invoking a GenericFunc.
128+
*/
129+
TVM_DLL void EnterWithScope();
130+
/*!
131+
* \brief Pop a target off the thread local context stack,
132+
* restoring the previous target as the current context.
133+
*/
134+
TVM_DLL void ExitWithScope();
135+
};
136+
137+
/*! \brief This namespace provides functions to construct Target instances */
138+
namespace target {
139+
140+
/*! \return A target for LLVM */
141+
TVM_DLL Target llvm(const std::vector<std::string>& options =
142+
std::vector<std::string>());
143+
144+
/*! \return A target for CUDA */
145+
TVM_DLL Target cuda(const std::vector<std::string>& options =
146+
std::vector<std::string>());
147+
148+
/*! \return A target for ROCm */
149+
TVM_DLL Target rocm(const std::vector<std::string>& options =
150+
std::vector<std::string>());
151+
152+
/*! \return A target for OpenCL */
153+
TVM_DLL Target opencl(const std::vector<std::string>& options =
154+
std::vector<std::string>());
155+
156+
/*! \return A target for Metal */
157+
TVM_DLL Target metal(const std::vector<std::string>& options =
158+
std::vector<std::string>());
159+
160+
/*! \return A target for rasp */
161+
TVM_DLL Target rasp(const std::vector<std::string>& options =
162+
std::vector<std::string>());
163+
164+
/*! \return A target for Mali */
165+
TVM_DLL Target mali(const std::vector<std::string>& options =
166+
std::vector<std::string>());
167+
168+
/*! \return A target for Intel Graphics */
169+
TVM_DLL Target intel_graphics(const std::vector<std::string>& options =
170+
std::vector<std::string>());
171+
172+
/*! \return A target for stackvm */
173+
TVM_DLL Target stackvm(const std::vector<std::string>& options =
174+
std::vector<std::string>());
175+
176+
/*! \return A target for external device */
177+
TVM_DLL Target ext_dev(const std::vector<std::string>& options =
178+
std::vector<std::string>());
179+
} // namespace target
180+
} // namespace tvm
181+
#endif // TVM_TARGET_TARGET_H_

include/tvm/target_info.h renamed to include/tvm/target/target_info.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,23 @@
1818
*/
1919

2020
/*!
21-
* \file tvm/target_info.h
21+
* \file tvm/target/target_info.h
2222
* \brief Various information about target.
2323
*/
24-
#ifndef TVM_TARGET_INFO_H_
25-
#define TVM_TARGET_INFO_H_
24+
#ifndef TVM_TARGET_TARGET_INFO_H_
25+
#define TVM_TARGET_TARGET_INFO_H_
2626

27+
#include <tvm/ir/expr.h>
2728
#include <string>
28-
#include "expr.h"
2929

3030
namespace tvm {
3131

3232
/*!
3333
* \brief Memory information of special memory region.
3434
* Use MemoryInfo as its container type
3535
*/
36-
struct MemoryInfoNode : public Object {
36+
class MemoryInfoNode : public Object {
37+
public:
3738
/*! \brief The addressable unit */
3839
int unit_bits;
3940
/*! \brief Maximum number of bits supported in the memory */
@@ -71,4 +72,4 @@ class MemoryInfo : public ObjectRef {
7172
TVM_DLL MemoryInfo GetMemoryInfo(const std::string& scope);
7273

7374
} // namespace tvm
74-
#endif // TVM_TARGET_INFO_H_
75+
#endif // TVM_TARGET_TARGET_INFO_H_

0 commit comments

Comments
 (0)