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

[triton-raise-block-ptr]: Avoid creating unnecessary index casts and divisions #3129

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

etiotto
Copy link
Contributor

@etiotto etiotto commented Jan 9, 2025

The triton-raise-block-pointer pass injects arithmetic index cast operations unnecessarily (when there is a prior index cast operation that is equivalent). These unnecessary operation "obfuscate" the IR and make it harder to understand the transformation while debugging it (although canonicalization can clean the up after the pass is done).

This PR improves readability of the generate IR by reusing constant (in the same basic block) when possible, and avoiding creating index_cast operations where they can be folded.

Copy link
Contributor

@whitneywhtsang whitneywhtsang left a comment

Choose a reason for hiding this comment

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

can you please check if createOrFold can be used to simplify the code?

@etiotto etiotto changed the title [triton-raise-block-ptr]: Avoid creating unnecessary index casts [triton-raise-block-ptr]: Avoid creating unnecessary index casts and divisions Jan 9, 2025
Copy link
Contributor

@mfrancepillois mfrancepillois left a comment

Choose a reason for hiding this comment

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

LGTM

@etiotto
Copy link
Contributor Author

etiotto commented Jan 10, 2025

can you please check if createOrFold can be used to simplify the code?

I have changed the code to use createOrFold rather than create. I did an experiment to see if createOrFold was able to avoid generating adds with zero. I changed this code:

    auto addIfNecessary = [&](Value lhs, Value rhs) {
#if 0
      return ttgi::isConstant(lhs, 0)
                 ? rhs
                 : (ttgi::isConstant(rhs, 0) ? lhs : abuilder.add(lhs, rhs));
#else
      if (isa<FloatType>(lhs.getType()))
        return builder.createOrFold<arith::AddFOp>(loc, lhs, rhs);
      return builder.createOrFold<arith::AddIOp>(loc, lhs, rhs);
#endif
    };

Unfortunately I do see the presence of adds by zero in the resulting IR, for example:

    %42 = arith.addi %40, %c1_i64 : i64
    %43 = arith.addi %34, %c0_i32 : i32 <<< 
    %44 = arith.addi %36, %c0_i64 : i64  <<<
    %45 = arith.addi %39, %c0_i32 : i32 <<<
    %46 = arith.addi %42, %c0_i64 : i64 <<<
    %47 = arith.trunci %44 : i64 to i32
    %48 = arith.divui %43, %47 : i32
    %49 = arith.trunci %46 : i64 to i32
    %50 = arith.divui %45, %49 : i32

@whitneywhtsang
Copy link
Contributor

Unfortunately I do see the presence of adds by zero in the resulting IR

Thanks for checking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[TRITON-BP]: Refactor and avoid generating unnecessary constants, adds and multiplies
3 participants