Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DAGCombiner][RISCV] Prefer to sext i32 non-negative values #65984

Merged
merged 1 commit into from
Sep 12, 2023

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Sep 11, 2023

By default, DAGCombiner folds sext x to zext x when x is non-negative. It will generate redundant zext inst seq on riscv64 (typically slli (srli x, 32), 32).
godbolt: https://godbolt.org/z/osf6adP1o
This patch applies the transform iff zext is cheaper than sext.

@dtcxzyw dtcxzyw requested review from a team as code owners September 11, 2023 17:18
@llvmbot llvmbot added the llvm:SelectionDAG SelectionDAGISel as well label Sep 11, 2023
Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dtcxzyw dtcxzyw merged commit 4793c2c into llvm:main Sep 12, 2023
3 of 4 checks passed
@dtcxzyw dtcxzyw deleted the sext-non-negative branch September 12, 2023 11:02
@topperc
Copy link
Collaborator

topperc commented Sep 12, 2023

InstCombine does the same thing. Do we need a reverse transform to turn zext into sext explicitly? Or perhaps an isel pattern?

@dtcxzyw
Copy link
Member Author

dtcxzyw commented Sep 13, 2023

InstCombine does the same thing.

I think you mean ISel.

// Some targets like RISCV prefer to sign extend some types.

Do we need a reverse transform to turn zext into sext explicitly? Or perhaps an isel pattern?

It is unsafe to turn zext into sext. Why not just prevent tuning zext into sext?

@topperc
Copy link
Collaborator

topperc commented Sep 13, 2023

InstCombine does the same thing.

I think you mean ISel.

// Some targets like RISCV prefer to sign extend some types.

Do we need a reverse transform to turn zext into sext explicitly? Or perhaps an isel pattern?

It is unsafe to turn zext into sext. Why not just prevent tuning zext into sext?

visitSExt in InstCombine will turn sext into zext. That code will happen before DAGCombine. The DAGCombine change will only effect sext that is created during SelectionDAG. sext that already existed in IR may get changed by InstCombine.

So my question was do we need to change zext to sext if the sign bit of the input is known to be 0 by computeKnownBits.

@dtcxzyw
Copy link
Member Author

dtcxzyw commented Sep 15, 2023

// If the value being extended is zero or positive, use a zext instead.
if (isKnownNonNegative(Src, DL, 0, &AC, &Sext, &DT))
return CastInst::Create(Instruction::ZExt, Src, DestTy);

@dtcxzyw
Copy link
Member Author

dtcxzyw commented Sep 16, 2023

Diff: main...dtcxzyw:llvm-project:zext-to-sext
Evaluation result: dtcxzyw/llvm-ci#627

It seems better to sext a non-negative value on riscv64. But this change broke some regression tests.

ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
By default, `DAGCombiner` folds `sext x` to `zext x` when `x` is
non-negative. It will generate redundant `zext` inst seq on riscv64
(typically `slli (srli x, 32), 32`).
godbolt: https://godbolt.org/z/osf6adP1o
This patch applies the transform iff `zext` is **cheaper** than `sext`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:RISC-V llvm:SelectionDAG SelectionDAGISel as well
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants