Skip to content

Commit

Permalink
prevent casting handle to other types (apache#9114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sen Yang authored and ylc committed Jan 13, 2022
1 parent 7f26803 commit 4e4c06d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
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__])

0 comments on commit 4e4c06d

Please sign in to comment.