Skip to content

Commit

Permalink
Merge branch 'lwawrzyniak/improve-errors-for-invalid-constants' into …
Browse files Browse the repository at this point in the history
…'main'

Improve errors for invalid constants

See merge request omniverse/warp!755
  • Loading branch information
mmacklin committed Sep 27, 2024
2 parents 296a6d4 + d603011 commit fbbcf58
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- Skip unused functions in module code generation, improving performance.
- Avoid reloading modules if their content does not change, improving performance.
- `wp.Mesh.points` is now a property instead of a raw data member, its reference can be changed after the mesh is initialized.
- Improve error message when invalid objects are referenced in a Warp kernel.

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion warp/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,7 @@ def emit_Name(adj, node):
if isinstance(obj, types.ModuleType):
return obj

raise RuntimeError("Cannot reference a global variable from a kernel unless `wp.constant()` is being used")
raise TypeError(f"Invalid external reference type: {type(obj)}")

@staticmethod
def resolve_type_attribute(var_type: type, attr: str):
Expand Down
12 changes: 6 additions & 6 deletions warp/tests/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,22 +405,22 @@ def kernel_3_fn(

kernel = wp.Kernel(func=kernel_1_fn)
with test.assertRaisesRegex(
RuntimeError,
r"Cannot reference a global variable from a kernel unless `wp.constant\(\)` is being used",
TypeError,
r"Invalid external reference type: <class 'warp.types.array'>",
):
wp.launch(kernel, dim=out.shape, inputs=(), outputs=(out,), device=device)

kernel = wp.Kernel(func=kernel_2_fn)
with test.assertRaisesRegex(
RuntimeError,
r"Cannot reference a global variable from a kernel unless `wp.constant\(\)` is being used",
TypeError,
r"Invalid external reference type: <class 'warp.types.array'>",
):
wp.launch(kernel, dim=out.shape, inputs=(), outputs=(out,), device=device)

kernel = wp.Kernel(func=kernel_3_fn)
with test.assertRaisesRegex(
RuntimeError,
r"Cannot reference a global variable from a kernel unless `wp.constant\(\)` is being used",
TypeError,
r"Invalid external reference type: <class 'warp.types.array'>",
):
wp.launch(kernel, dim=out.shape, inputs=(), outputs=(out,), device=device)

Expand Down
2 changes: 1 addition & 1 deletion warp/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def test_constant(self):
self.assertEqual(const, wp.vec3i(1, 2, 3))

def test_constant_error_invalid_type(self):
with self.assertRaisesRegex(RuntimeError, r"Invalid constant type: <class 'tuple'>$"):
with self.assertRaisesRegex(TypeError, r"Invalid constant type: <class 'tuple'>$"):
wp.constant((1, 2, 3))

def test_vector_assign(self):
Expand Down
6 changes: 3 additions & 3 deletions warp/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def constant(x):
x: Compile-time constant value, can be any of the built-in math types.
"""

if not isinstance(x, (builtins.bool, int, float, tuple(scalar_and_bool_types), ctypes.Array)):
raise RuntimeError(f"Invalid constant type: {type(x)}")
if not is_value(x):
raise TypeError(f"Invalid constant type: {type(x)}")

return x

Expand Down Expand Up @@ -1384,7 +1384,7 @@ def type_is_matrix(t):

# returns true for all value types (int, float, bool, scalars, vectors, matrices)
def type_is_value(x):
return x in value_types or issubclass(x, ctypes.Array)
return x in value_types or hasattr(x, "_wp_scalar_type_")


# equivalent of the above but for values
Expand Down

0 comments on commit fbbcf58

Please sign in to comment.