Skip to content

Commit a15ee3b

Browse files
committed
Merge branch 'hp/tiles-4' into hp/tiles-5
# Conflicts: # docs/source/user_guide/tile16.md # tests/python/test_tile16.py
2 parents af5571d + 96f29ab commit a15ee3b

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

python/quadrants/lang/ast/ast_transformer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
from quadrants.lang.matrix import Matrix, MatrixType
4141
from quadrants.lang.snode import append, deactivate, length
4242
from quadrants.lang.struct import Struct, StructType
43+
from quadrants.lang.util import (
44+
is_from_quadrants_module as _is_from_quadrants_module,
45+
)
4346
from quadrants.lang.util import (
4447
is_quadrants_internal_file as _is_quadrants_internal_file,
4548
)
@@ -678,7 +681,7 @@ def build_Attribute(ctx: ASTTransformerFuncContext, node: ast.Attribute):
678681
violation = False
679682
if violation and node.value.ptr in [qd_math, math, np]:
680683
violation = False
681-
if violation and _is_quadrants_internal_file(ctx.file):
684+
if violation and _is_from_quadrants_module(node.value.ptr):
682685
violation = False
683686
if violation:
684687
message = f"[PURE.VIOLATION] WARNING: Accessing global var {node.attr} from outside function scope within pure kernel {node.value.violates_pure_reason}"

python/quadrants/lang/util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,14 @@ def is_quadrants_internal_file(filepath: str) -> bool:
366366
return os.path.realpath(filepath).startswith(_quadrants_package_dir() + os.sep)
367367

368368

369+
def is_from_quadrants_module(obj: object) -> bool:
370+
"""Return True if obj belongs to the quadrants package (module, class, or instance)."""
371+
import types # pylint: disable=C0415
372+
373+
if isinstance(obj, types.ModuleType):
374+
return getattr(obj, "__name__", "").startswith("quadrants")
375+
mod = getattr(obj, "__module__", None) or getattr(type(obj), "__module__", None)
376+
return mod is not None and mod.startswith("quadrants")
377+
378+
369379
__all__ = []

tests/python/quadrants/lang/fast_caching/test_pure_validation.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,34 @@ def k2() -> qd.f32:
251251

252252
with pytest.warns(UserWarning, match=r"\[PURE\.VIOLATION\]"):
253253
assert k2() == 23
254+
255+
256+
@test_utils.test()
257+
def test_pure_validation_quadrants_module_attribute_allowed():
258+
"""Accessing an int constant from the quadrants package should not trigger a purity violation."""
259+
260+
@qd.kernel(pure=True)
261+
def k1() -> qd.i32:
262+
return qd.simt.Tile16x16.SIZE
263+
264+
assert k1() == 16
265+
266+
267+
@test_utils.test()
268+
def test_pure_validation_non_quadrants_attribute_warns():
269+
"""Accessing an int attribute on a non-quadrants object should still warn/error."""
270+
assert qd.lang is not None
271+
arch = qd.lang.impl.current_cfg().arch
272+
qd.init(arch=arch, offline_cache=False)
273+
274+
class Config:
275+
SIZE = 32
276+
277+
cfg = Config()
278+
279+
@qd.kernel(pure=True)
280+
def k1() -> qd.i32:
281+
return cfg.SIZE
282+
283+
with pytest.warns(UserWarning, match=r"\[PURE\.VIOLATION\]"):
284+
assert k1() == 32

0 commit comments

Comments
 (0)