[Codegen] Fix if_then_else codegen - #16242
Merged
Merged
Conversation
junrushao
reviewed
Dec 17, 2023
Member
|
isn't short circuiting the defined behavior for ternary expression? |
Contributor
Author
When generating codes for |
jinhongyii
approved these changes
Jan 4, 2024
junrushao
added a commit
to junrushao/tvm
that referenced
this pull request
Jan 7, 2024
* fix * lint * fix while loop * clean * clean --------- Co-authored-by: Junru Shao <junrushao1994@gmail.com>
Contributor
Hi, bohan, I recently am supporting Ternary Operator(like cond?true_branch:false_branch) in our device. I don't understand this modification. Can u explain this with a case? Thanks a lot! |
tlopex
pushed a commit
that referenced
this pull request
Sep 10, 2026
… code (#20285) ## Problem Since #16242, the `if_then_else` builtin call is expanded into an if/else statement. When printing the condition, the codegen wraps it in another pair of parentheses even though it is already parenthesized, producing `if ((i == 0))`. ## Impact The doubled parentheses trigger clang's `-Wparentheses-equality` warning under `-Wall`/`-Wparentheses`, which is noisy for downstream users who compile TVM-generated C sources with strict warning settings. Correctness is not affected — the issue is cosmetic. ## Solution Reuse the same leading/trailing parenthesis check as the existing `IfThenElseNode` handling in `CodeGenC`, so a condition that already starts and ends with parentheses is printed as-is. The generated code now reads `if (i == 0)`. Since `CodeGenC` is the shared base of all C-style backends (CUDA, Metal, ROCm, Vulkan, WebGPU, Hexagon), this fix covers them all at once. ## Testing - Added a regression test `test_if_then_else_avoids_extraneous_parentheses` in `tests/python/codegen/test_target_codegen_c_host.py`, which asserts the generated C source contains no `if ((` and validates the runtime result. - Ran the full `test_target_codegen_c_host.py` suite on aarch64 Linux with a local build: 10/10 passed. - `pre-commit` (clang-format, ruff-check, ruff-format) passed on the changed files. Co-authored-by: hejun <hejun72@outlook.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Evaluating
op.if_then_elsewitha?b:cis potentially dangerous as it might evaluate expressions in branches without a guard of condition.While loop condition expression might require several var bindings generated, so we correct the while loop codegen from
while (cond)towhile (True)