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

[Bugfix] Prevent casting handle to other types #9114

Merged
merged 1 commit into from
Sep 27, 2021
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
1 change: 1 addition & 0 deletions src/tir/op/op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ PrimExpr cast(const DataType& t, PrimExpr value, Span span) {
} else if (const FloatImmNode* op = value.as<FloatImmNode>()) {
return make_const(t, op->value, op->span);
}
ICHECK(!value.dtype().is_handle()) << "Can't cast a handle to other types.";
return tir::Cast(t, value, span);
} else {
if (value.dtype().lanes() == 1) {
Expand Down
32 changes: 7 additions & 25 deletions tests/python/unittest/test_tir_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import pytest
import tvm
from tvm import te
import numpy as np
Expand Down Expand Up @@ -104,6 +105,11 @@ def test_cast():
assert isinstance(z, tvm.tir.Broadcast)
assert z.lanes == 4

s = tvm.tir.StringImm("s")
with pytest.raises(tvm.error.TVMError) as cm:
s.astype("int")
assert "Can't cast a handle to other types" in str(cm.execption)


def test_attr():
x = te.var("x")
Expand Down Expand Up @@ -468,28 +474,4 @@ def test_block_blockrealize():


if __name__ == "__main__":
test_intimm_cond()
test_buffer_load_store()
test_vars()
test_prim_func()
test_cast()
test_attr()
test_const()
test_scalar_dtype_inference()
test_make()
test_ir()
test_basic()
test_stmt()
test_let()
test_dir()
test_dtype()
test_any()
test_all()
test_bitwise()
test_float_bitwise()
test_shift_bounds()
test_divide_by_zero()
test_isnan()
test_equality()
test_equality_string_imm()
test_block_blockrealize()
pytest.main([__file__])