Skip to content

Commit a5a6e7f

Browse files
authored
Fix out of bound enum conversion (#13967)
This PR fixes Wenum-constexpr-conversion which is introduced in recent version of clang. Explciitly use integer instead of enum to store the device that may contain invalid options.
1 parent 8b2b165 commit a5a6e7f

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

include/tvm/runtime/ndarray.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ namespace tvm {
4242
// alias DLDevice
4343
using Device = DLDevice;
4444

45-
// A 'null' device type, does not correspond to any DLDeviceType enum.
46-
// TODO(mbs): This is to help us as we transition away from representing the 'homogenous' case
47-
// as a singleton target map indexed by the invalid DLDeviceType '0'.
48-
constexpr DLDeviceType kNullDeviceType = static_cast<DLDeviceType>(0);
49-
50-
// An 'invalid' device type, does not correspond to any DLDeviceType enum.
51-
constexpr DLDeviceType kInvalidDeviceType = static_cast<DLDeviceType>(-1);
52-
5345
namespace runtime {
5446

5547
/*!

include/tvm/target/virtual_device.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ namespace tvm {
4444
*/
4545
using MemoryScope = String;
4646

47+
// NOTE: cannot use enum as they are out of bound of the original enum
48+
// and results in an undefined behavior
49+
// A 'null' device type, does not correspond to any DLDeviceType enum.
50+
// TODO(mbs): This is to help us as we transition away from representing the 'homogenous' case
51+
// as a singleton target map indexed by the invalid DLDeviceType '0'.
52+
constexpr int kNullDeviceType = 0;
53+
54+
// An 'invalid' device type, does not correspond to any DLDeviceType enum.
55+
constexpr int kInvalidDeviceType = -1;
56+
4757
/*!
4858
* \brief Describes at compile time the constraints on where data is to be stored at runtime
4959
* down to the (virtual) device and memory scope level, and how to compile code to compute that
@@ -229,7 +239,7 @@ class VirtualDeviceNode : public AttrsNode<VirtualDeviceNode> {
229239
* Physical Devices" above.
230240
*/
231241
Device ToDevice() const {
232-
ICHECK(device_type() != kInvalidDeviceType);
242+
ICHECK(device_type_int != kInvalidDeviceType);
233243
ICHECK(virtual_device_id != -1);
234244
Device device;
235245
device.device_type = device_type();
@@ -262,7 +272,7 @@ class VirtualDevice : public ObjectRef {
262272
public:
263273
/*!
264274
* \brief Construct a virtual device.
265-
* \param device_type The device type for the virtual device, or \p kInvalidDeviceType if
275+
* \param device_type_int The device type for the virtual device, or \p kInvalidDeviceType if
266276
* unconstrained. If \p target is defined then must match its \p target->GetTargetDeviceType().
267277
* \param virtual_device_id The device id for the virtual device, or -1 if unconstrained.
268278
* \param target The target describing how to compile for the virtual device, or null if
@@ -271,7 +281,7 @@ class VirtualDevice : public ObjectRef {
271281
* unconstrained.
272282
* \return The virtual device.
273283
*/
274-
explicit VirtualDevice(DLDeviceType device_type = kInvalidDeviceType, int virtual_device_id = -1,
284+
explicit VirtualDevice(int device_type_int = kInvalidDeviceType, int virtual_device_id = -1,
275285
Target target = {}, MemoryScope memory_scope = {});
276286

277287
/*! \brief Returns the unique fully unconstrained \p VirtualDevice. */
@@ -349,7 +359,7 @@ class VirtualDevice : public ObjectRef {
349359
class VirtualDeviceCache {
350360
public:
351361
/*! \brief Returns the unique \p VirtualDevice representing given fields. */
352-
VirtualDevice Make(DLDeviceType device_type = kInvalidDeviceType, int virtual_device_id = -1,
362+
VirtualDevice Make(int device_type = kInvalidDeviceType, int virtual_device_id = -1,
353363
Target target = {}, MemoryScope memory_scope = {});
354364

355365
/*!

src/target/virtual_device.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
6666
p->stream << ")";
6767
});
6868

69-
VirtualDevice::VirtualDevice(DLDeviceType device_type, int virtual_device_id, Target target,
69+
VirtualDevice::VirtualDevice(int device_type_int, int virtual_device_id, Target target,
7070
MemoryScope memory_scope) {
71-
ICHECK(!target.defined() || device_type == target->GetTargetDeviceType())
71+
ICHECK(!target.defined() || device_type_int == target->GetTargetDeviceType())
7272
<< "target " << target->ToDebugString() << " has device type "
73-
<< target->GetTargetDeviceType() << " but virtual device has device type " << device_type;
73+
<< target->GetTargetDeviceType() << " but virtual device has device type " << device_type_int;
7474
auto node = make_object<VirtualDeviceNode>();
75-
node->device_type_int = device_type;
75+
node->device_type_int = device_type_int;
7676
node->virtual_device_id = virtual_device_id;
7777
node->target = std::move(target);
7878
node->memory_scope = std::move(memory_scope);
@@ -166,8 +166,8 @@ VirtualDevice VirtualDevice::Default(const VirtualDevice& lhs, const VirtualDevi
166166
defaulted_memory_scope);
167167
}
168168

169-
VirtualDevice VirtualDeviceCache::Make(DLDeviceType device_type, int virtual_device_id,
170-
Target target, MemoryScope memory_scope) {
169+
VirtualDevice VirtualDeviceCache::Make(int device_type, int virtual_device_id, Target target,
170+
MemoryScope memory_scope) {
171171
VirtualDevice prototype(device_type, virtual_device_id, std::move(target),
172172
std::move(memory_scope));
173173
if (prototype->IsFullyUnconstrained()) {

0 commit comments

Comments
 (0)