Skip to content

Commit 9ead7b1

Browse files
[refactor] Finalize root FieldsBuilder only when it is not finalized (#3288)
* [refactor] Finalize root FieldsBuilder only when it is not finalized * fix * fix * Auto Format Co-authored-by: Taichi Gardener <[email protected]>
1 parent 6d4cb61 commit 9ead7b1

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

python/taichi/lang/impl.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,18 @@ def create_program(self):
310310
self.prog = _ti_core.Program()
311311

312312
def materialize_root_fb(self, is_first_call):
313-
if not root.finalized and not root.empty:
314-
root.finalize()
315-
elif is_first_call:
316-
root.finalize(raise_warning=False)
317-
318313
if root.finalized:
319-
global _root_fb
320-
_root_fb = FieldsBuilder()
314+
return
315+
if not is_first_call and root.empty:
316+
# We have to forcefully finalize when `is_first_call` is True (even
317+
# if the root itself is empty), so that there is a valid struct
318+
# llvm::Module, if no field has been declared before the first kernel
319+
# invocation. Example case:
320+
# https://github.com/taichi-dev/taichi/blob/27bb1dc3227d9273a79fcb318fdb06fd053068f5/tests/python/test_ad_basics.py#L260-L266
321+
return
322+
root.finalize(raise_warning=not is_first_call)
323+
global _root_fb
324+
_root_fb = FieldsBuilder()
321325

322326
def _finalize_root_fb_for_aot(self):
323327
if _root_fb.finalized:

0 commit comments

Comments
 (0)