Skip to content

Commit fbda4ef

Browse files
authored
[FRONTEND] Relax calling function inside a loop (#7002)
We should allow calling functions inside a loop even if they return a value as this doesn't cause early exit. If the function has unstructured control flow it may not be inlined, we should have a separate check about that.
1 parent 33faa6e commit fbda4ef

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

python/test/unit/language/test_frontend.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,20 @@ def test_list_of_functions():
109109
# CHECK-NEXT: call @anchor
110110
# CHECK-NEXT: call @forward
111111
list_of_functions_constexpr(tl.arange(0, 4), [anchor, forward])
112+
113+
114+
@triton.jit
115+
def accumulate(a, b):
116+
return a + b
117+
118+
119+
# Check that we can call a function returning a value from a loop.
120+
@filecheck_test
121+
@triton.jit
122+
def test_call_in_loop():
123+
# CHECK-LABEL: test_call_in_loop
124+
acc = 0
125+
# CHECK: scf.for
126+
# CHECK: call @accumulate
127+
for i in range(10):
128+
acc = accumulate(acc, i)

python/triton/compiler/code_generator.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,9 @@ def _visit_stmts(self, body) -> bool:
135135
return any(self.visit(s) for s in body)
136136

137137
def _visit_function(self, fn) -> bool:
138-
# Currently we only support JITFunctions defined in the global scope
139-
if isinstance(fn, JITFunction) and not fn.noinline:
140-
fn_node = fn.parse()
141-
return ContainsReturnChecker(self.gscope).visit(fn_node)
138+
# no need to check within the function as it won't cause an early return.
139+
# If the function itself has unstructured control flow we may not be able to inline it causing poor performance.
140+
# We should check for this and fail or emit a warning.
142141
return False
143142

144143
def generic_visit(self, node) -> bool:

0 commit comments

Comments
 (0)