Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix out of bound enum conversion #13967

Merged
merged 1 commit into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions include/tvm/runtime/ndarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ namespace tvm {
// alias DLDevice
using Device = DLDevice;

// A 'null' device type, does not correspond to any DLDeviceType enum.
// TODO(mbs): This is to help us as we transition away from representing the 'homogenous' case
// as a singleton target map indexed by the invalid DLDeviceType '0'.
constexpr DLDeviceType kNullDeviceType = static_cast<DLDeviceType>(0);

// An 'invalid' device type, does not correspond to any DLDeviceType enum.
constexpr DLDeviceType kInvalidDeviceType = static_cast<DLDeviceType>(-1);

namespace runtime {

/*!
Expand Down
18 changes: 14 additions & 4 deletions include/tvm/target/virtual_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ namespace tvm {
*/
using MemoryScope = String;

// NOTE: cannot use enum as they are out of bound of the original enum
// and results in an undefined behavior
// A 'null' device type, does not correspond to any DLDeviceType enum.
// TODO(mbs): This is to help us as we transition away from representing the 'homogenous' case
// as a singleton target map indexed by the invalid DLDeviceType '0'.
constexpr int kNullDeviceType = 0;

// An 'invalid' device type, does not correspond to any DLDeviceType enum.
constexpr int kInvalidDeviceType = -1;

/*!
* \brief Describes at compile time the constraints on where data is to be stored at runtime
* down to the (virtual) device and memory scope level, and how to compile code to compute that
Expand Down Expand Up @@ -229,7 +239,7 @@ class VirtualDeviceNode : public AttrsNode<VirtualDeviceNode> {
* Physical Devices" above.
*/
Device ToDevice() const {
ICHECK(device_type() != kInvalidDeviceType);
ICHECK(device_type_int != kInvalidDeviceType);
ICHECK(virtual_device_id != -1);
Device device;
device.device_type = device_type();
Expand Down Expand Up @@ -262,7 +272,7 @@ class VirtualDevice : public ObjectRef {
public:
/*!
* \brief Construct a virtual device.
* \param device_type The device type for the virtual device, or \p kInvalidDeviceType if
* \param device_type_int The device type for the virtual device, or \p kInvalidDeviceType if
* unconstrained. If \p target is defined then must match its \p target->GetTargetDeviceType().
* \param virtual_device_id The device id for the virtual device, or -1 if unconstrained.
* \param target The target describing how to compile for the virtual device, or null if
Expand All @@ -271,7 +281,7 @@ class VirtualDevice : public ObjectRef {
* unconstrained.
* \return The virtual device.
*/
explicit VirtualDevice(DLDeviceType device_type = kInvalidDeviceType, int virtual_device_id = -1,
explicit VirtualDevice(int device_type_int = kInvalidDeviceType, int virtual_device_id = -1,
Target target = {}, MemoryScope memory_scope = {});

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

/*!
Expand Down
12 changes: 6 additions & 6 deletions src/target/virtual_device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
p->stream << ")";
});

VirtualDevice::VirtualDevice(DLDeviceType device_type, int virtual_device_id, Target target,
VirtualDevice::VirtualDevice(int device_type_int, int virtual_device_id, Target target,
MemoryScope memory_scope) {
ICHECK(!target.defined() || device_type == target->GetTargetDeviceType())
ICHECK(!target.defined() || device_type_int == target->GetTargetDeviceType())
<< "target " << target->ToDebugString() << " has device type "
<< target->GetTargetDeviceType() << " but virtual device has device type " << device_type;
<< target->GetTargetDeviceType() << " but virtual device has device type " << device_type_int;
auto node = make_object<VirtualDeviceNode>();
node->device_type_int = device_type;
node->device_type_int = device_type_int;
node->virtual_device_id = virtual_device_id;
node->target = std::move(target);
node->memory_scope = std::move(memory_scope);
Expand Down Expand Up @@ -166,8 +166,8 @@ VirtualDevice VirtualDevice::Default(const VirtualDevice& lhs, const VirtualDevi
defaulted_memory_scope);
}

VirtualDevice VirtualDeviceCache::Make(DLDeviceType device_type, int virtual_device_id,
Target target, MemoryScope memory_scope) {
VirtualDevice VirtualDeviceCache::Make(int device_type, int virtual_device_id, Target target,
MemoryScope memory_scope) {
VirtualDevice prototype(device_type, virtual_device_id, std::move(target),
std::move(memory_scope));
if (prototype->IsFullyUnconstrained()) {
Expand Down