[Fix][Codegen] Avoid extraneous parentheses in if_then_else generated code - #20285
Open
fengz72 wants to merge 1 commit into
Open
[Fix][Codegen] Avoid extraneous parentheses in if_then_else generated code#20285fengz72 wants to merge 1 commit into
fengz72 wants to merge 1 commit into
Conversation
… code The if_then_else builtin call is expanded into an if/else statement, and the condition string is wrapped in another pair of parentheses even when it is already parenthesized, e.g. `if ((i == 0))`. This triggers clang's -Wparentheses-equality warning on the generated code. Reuse the same leading/trailing parenthesis check as the IfThenElseNode handling so the redundant parentheses are not emitted. Add a regression test asserting the generated C source contains no `if ((` and the runtime result stays correct.
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.
Problem
Since #16242, the
if_then_elsebuiltin 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, producingif ((i == 0)).Impact
The doubled parentheses trigger clang's
-Wparentheses-equalitywarning 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
IfThenElseNodehandling inCodeGenC, so a condition that already starts and ends with parentheses is printed as-is. The generated code now readsif (i == 0).Since
CodeGenCis the shared base of all C-style backends (CUDA, Metal, ROCm, Vulkan, WebGPU, Hexagon), this fix covers them all at once.Testing
test_if_then_else_avoids_extraneous_parenthesesintests/python/codegen/test_target_codegen_c_host.py, which asserts the generated C source contains noif ((and validates the runtime result.test_target_codegen_c_host.pysuite on aarch64 Linux with a local build: 10/10 passed.pre-commit(clang-format, ruff-check, ruff-format) passed on the changed files.