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

Boolean (u1) type support #577

Closed
yuanming-hu opened this issue Mar 10, 2020 · 7 comments
Closed

Boolean (u1) type support #577

yuanming-hu opened this issue Mar 10, 2020 · 7 comments
Labels
feature request Suggest an idea on this project welcome contribution

Comments

@yuanming-hu
Copy link
Member

yuanming-hu commented Mar 10, 2020

Concisely describe the proposed feature
Currently, binary comparisons return type ti.i32. This is not only counter-intuitive but also inefficient in some cases. Also having native boolean types will allow us to do more compiler optimizations on Taichi IRs.

Boolean types should be implemented as ti.u1, i.e., a single-bit integer.

Note: implementing this feature is harder than it seems to me. You'll have to modify quite a few compiler components. For example, update the type-checker to let comparisons return u1 instead of i32, and the codegens to emit correct types. Fortunately, as we are not running into any urgent need for this, we have plenty of time to implement this.

Related issue: #504

@yuanming-hu yuanming-hu added feature request Suggest an idea on this project welcome contribution labels Mar 10, 2020
@archibate
Copy link
Collaborator

GLSL have bool and bvec2.
Also GLSL isn't happy with:

int tmp1 = 233;
int tmp2 = tmp1 == 0;
int tmp3 = tmp2 ? xxx : yyy;

I had to use int(tmp1 == 0) and tmp2 == 0 ? instead.
Believe this could be saved by making compare result i1, which is bool in GLSL.

@archibate
Copy link
Collaborator

Btw, do you think the name u1 is better? Since i1 seems like a signed integer, which means it's highest bit is used as a sign mark. However we only have one bit, and no place for value..

@yuanming-hu
Copy link
Member Author

Agreed. u1 sounds better here. i1 only has values -1 and 0.

@yuanming-hu yuanming-hu changed the title Boolean (i1) type support Boolean (u1) type support Mar 11, 2020
@xumingkuan
Copy link
Contributor

bool is interpreted (probably by llvm) as i1 now. Following is a mismatch I just wrote:

[I 03/10/20 23:21:59.532] [llvm_codegen_utils.cpp:taichi::lang::check_func_call_signature@38]   parameter 1 mismatch: required=i1, provided=i32

@yuanming-hu
Copy link
Member Author

yuanming-hu commented Mar 11, 2020

Yeah, that was the reason why I initially picked i1. However, we should note that LLVM does not really distinguish unsigned integers from signed ones. I.e., it doesn't have u1. This makes LLVM closer to the hardware. As an IR that works at a higher level, I guess using u1 in Taichi is better.

@github-actions
Copy link

Warning: The issue has been out-of-update for 50 days, marking stale.

@github-actions github-actions bot added the stale stale issues and PRs label May 21, 2020
@bobcao3 bobcao3 reopened this Feb 8, 2022
@bobcao3 bobcao3 removed the stale stale issues and PRs label Feb 8, 2022
strongoier pushed a commit that referenced this issue Dec 3, 2022
Issue: #577 #6036

### Brief Summary

This PR adds `bool` as an alias to `ti.i32`. Specifically,

- `x: bool` is equivalent to `x: i32`
- `-> bool` is equivalent to `-> i32`
- `bool(x)` is equivalent to `ti.cast(x, i32)`.

This is a temporary solution while we work towards a standalone bool
type.

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
quadpixels pushed a commit to quadpixels/taichi that referenced this issue May 13, 2023
…6742)

Issue: taichi-dev#577 taichi-dev#6036

### Brief Summary

This PR adds `bool` as an alias to `ti.i32`. Specifically,

- `x: bool` is equivalent to `x: i32`
- `-> bool` is equivalent to `-> i32`
- `bool(x)` is equivalent to `ti.cast(x, i32)`.

This is a temporary solution while we work towards a standalone bool
type.

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Suggest an idea on this project welcome contribution
Projects
None yet
Development

No branches or pull requests

6 participants