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

[lang] Add popcnt to llvm intrinsic support #7772

Merged
merged 25 commits into from
Apr 18, 2023

Conversation

lgyStoic
Copy link
Contributor

@lgyStoic lgyStoic commented Apr 10, 2023

[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

fix: add test api for popcnt

Issue: #7541

Brief Summary

🤖 Generated by Copilot at 9893837

Added a new popcnt function to count the number of bits set to 1 in an integer, both for Python and Taichi expressions. Implemented the function in the taichi.lang.ops and taichi.math modules, and the CUDA and LLVM backends. Added tests and documentation for the function.

Walkthrough

🤖 Generated by Copilot at 9893837

  • Add a new function popcnt to the taichi.lang.ops module that returns the number of bits set to 1 in an integer (link, link)
  • Import and wrap the popcnt function in the taichi.math.mathimpl module to support both Taichi expressions and Python scalars (link, link, link)
  • Add the popcnt operation to the UnaryOpType enum class and the expression macros in the taichi.inc.unary_op.inc.h and taichi.ir.expression_ops.h files (link, link)
  • Add the popcnt operation to the UNARY_STD functions in the taichi.codegen.cuda.codegen_cuda.cpp and taichi.codegen.llvm.codegen_llvm.cpp files, which handle the code generation for unary operations on the CUDA and LLVM backends (link, link)
  • Add a temporary workaround for creating a UnaryOpStmt with the popcnt operation using a BinaryOpStmt with the min operation in the IRBuilder class in the taichi.ir.ir_builder.cpp and taichi.ir.ir_builder.h files (link, link)
  • Export the popcnt function to the Python module taichi.lang.expr using the DEFINE_EXPRESSION_OP macro in the taichi.python.export_lang.cpp file (link)
  • Add the popcnt function to the list of math functions that are tested for consistency between Taichi expressions and Python scalars in the tests.python.test_api.py file (link)

@netlify
Copy link

netlify bot commented Apr 10, 2023

Deploy Preview for docsite-preview ready!

Name Link
🔨 Latest commit 2a24e82
🔍 Latest deploy log https://app.netlify.com/sites/docsite-preview/deploys/643d5e38fc57910008eef930
😎 Deploy Preview https://deploy-preview-7772--docsite-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@lgyStoic lgyStoic changed the title [lang][cpu][cuda]: add popcnt to llvm intrinsic support [lang] Add popcnt to llvm intrinsic support Apr 10, 2023
@lgyStoic lgyStoic marked this pull request as draft April 11, 2023 00:58
@lgyStoic
Copy link
Contributor Author

@ailzhang would you please have a look what is going on, if I use

import taichi as ti
ti.init(arch=ti.cuda, print_kernel_llvm_ir_optimized=True)
@ti.kernel
def test():
    n = ti.uint64(10000)
    ti.math.max(10, n)
    pt = ti.math.popcnt(n)
    print(pt)

test()

using uint64 as popcnt type, then I pass to cuda backend for compute, then assert like the picture mentioned.
image

@ailzhang
Copy link
Contributor

@lgyStoic the error message indicates that you're passing a pointer to u64 to a function requires a pointer to i64. you'll need to use builder->CreateBitCast to cast it to i64 before sending to the function.

@lgyStoic lgyStoic marked this pull request as ready for review April 15, 2023 13:52
@lgyStoic
Copy link
Contributor Author

@lgyStoic the error message indicates that you're passing a pointer to u64 to a function requires a pointer to i64. you'll need to use builder->CreateBitCast to cast it to i64 before sending to the function.

At last, found beause return type of popcll is i32, but in statement return type pointer is to u64 point, so the print function was crash.... at first, I was thinking there is some problem on call 'popcll' function..

@lgyStoic
Copy link
Contributor Author

/rebase

Copy link
Member

@turbo0628 turbo0628 left a comment

Choose a reason for hiding this comment

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

LGTM!

@taichi-gardener taichi-gardener merged commit 98ffbc2 into taichi-dev:master Apr 18, 2023
quadpixels pushed a commit to quadpixels/taichi that referenced this pull request May 13, 2023
[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

fix: add test api for popcnt

Issue: taichi-dev#7541

### Brief Summary

<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at 9893837</samp>

Added a new `popcnt` function to count the number of bits set to 1 in an
integer, both for Python and Taichi expressions. Implemented the
function in the `taichi.lang.ops` and `taichi.math` modules, and the
CUDA and LLVM backends. Added tests and documentation for the function.

### Walkthrough

<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at 9893837</samp>

* Add a new function `popcnt` to the `taichi.lang.ops` module that
returns the number of bits set to 1 in an integer
([link](https://github.com/taichi-dev/taichi/pull/7772/files?diff=unified&w=0#diff-059028cb0798284bed05638becbc32d256736846de19746e196fe5f5ee7fd061R532-R538),
[link](https://github.com/taichi-dev/taichi/pull/7772/files?diff=unified&w=0#diff-059028cb0798284bed05638becbc32d256736846de19746e196fe5f5ee7fd061L1400-R1409))
* Import and wrap the `popcnt` function in the `taichi.math.mathimpl`
module to support both Taichi expressions and Python scalars
([link](https://github.com/taichi-dev/taichi/pull/7772/files?diff=unified&w=0#diff-5b3923516b48467202850afb384ef9901ecefae0173f03bcc9055adffe96d738L12-R12),
[link](https://github.com/taichi-dev/taichi/pull/7772/files?diff=unified&w=0#diff-5b3923516b48467202850afb384ef9901ecefae0173f03bcc9055adffe96d738R749-R753),
[link](https://github.com/taichi-dev/taichi/pull/7772/files?diff=unified&w=0#diff-5b3923516b48467202850afb384ef9901ecefae0173f03bcc9055adffe96d738L754-R759))
* Add the `popcnt` operation to the `UnaryOpType` enum class and the
expression macros in the `taichi.inc.unary_op.inc.h` and
`taichi.ir.expression_ops.h` files
([link](https://github.com/taichi-dev/taichi/pull/7772/files?diff=unified&w=0#diff-f95015864ea3251da5d376f2a11e8f5a0045d7aaf4602370471686f56561dafdR21),
[link](https://github.com/taichi-dev/taichi/pull/7772/files?diff=unified&w=0#diff-b0b26408cd63f0a7edc6e9a6936ec09df7dc5f37c2ab65d72b3f9125f1385ba1R89))
* Add the `popcnt` operation to the `UNARY_STD` functions in the
`taichi.codegen.cuda.codegen_cuda.cpp` and
`taichi.codegen.llvm.codegen_llvm.cpp` files, which handle the code
generation for unary operations on the CUDA and LLVM backends
([link](https://github.com/taichi-dev/taichi/pull/7772/files?diff=unified&w=0#diff-50537ad5ea3b900c0d55a088f3cc285986340ad68c9b96fea481187c4dce49eaR270-R277),
[link](https://github.com/taichi-dev/taichi/pull/7772/files?diff=unified&w=0#diff-3c663c78745adcd3f6a7ac81fe99e628decc3040f292ea1e20ecd4b85a7f4313R209-R212))
* Add a temporary workaround for creating a `UnaryOpStmt` with the
`popcnt` operation using a `BinaryOpStmt` with the `min` operation in
the `IRBuilder` class in the `taichi.ir.ir_builder.cpp` and
`taichi.ir.ir_builder.h` files
([link](https://github.com/taichi-dev/taichi/pull/7772/files?diff=unified&w=0#diff-bdb4f85a29d6478a4482d81ca072237534fb641b52f3c529aca93e872ade6fecR341-R344),
[link](https://github.com/taichi-dev/taichi/pull/7772/files?diff=unified&w=0#diff-1894085b261e833e3e66924fc5b1cf63b9dd8b8aa0b3e78ec64366396131470dR196))
* Export the `popcnt` function to the Python module `taichi.lang.expr`
using the `DEFINE_EXPRESSION_OP` macro in the
`taichi.python.export_lang.cpp` file
([link](https://github.com/taichi-dev/taichi/pull/7772/files?diff=unified&w=0#diff-af631a0c71978fe591e17005f01f7c06bc30ae36c65df306bbb3b08ade770167R887))
* Add the `popcnt` function to the list of math functions that are
tested for consistency between Taichi expressions and Python scalars in
the `tests.python.test_api.py` file
([link](https://github.com/taichi-dev/taichi/pull/7772/files?diff=unified&w=0#diff-3509125c0fe3c95de660001069a4006253de3ea41bba2989b839177d825e8879L107-R107))

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Haidong Lan <[email protected]>
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.

5 participants