Commit Graph

9 Commits

Author SHA1 Message Date
Richard Barnes
72cdbf6a3f Fix spurious "missing return" error in irange.h (#102785)
Summary:
Fixes:
```
warning: missing return statement at end of non-void function
```
This warning is cluttering a lot of compilation logs!

Test Plan: Sandcastle

Differential Revision: D46374554

Pull Request resolved: https://github.com/pytorch/pytorch/pull/102785
Approved by: https://github.com/Skylion007
2023-06-02 09:23:29 +00:00
Richard Barnes
be94ff976d Have irange use if constexpr (#94050)
Test Plan: Sandcastle

Differential Revision: D42997779

Pull Request resolved: https://github.com/pytorch/pytorch/pull/94050
Approved by: https://github.com/Skylion007, https://github.com/soumith, https://github.com/malfet
2023-05-22 20:12:37 +00:00
mikey dagitses
e402259b8a avoid warning in irange for unsigned types (#97973)
Unsigned types should not be compared to be less than zero.

Differential Revision: [D44538384](https://our.internmc.facebook.com/intern/diff/D44538384/)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/97973
Approved by: https://github.com/Skylion007
2023-04-01 23:52:37 +00:00
mikey dagitses
368a1cbd02 fix c10::detail::integer_iterator for C++17 (#90174)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/pytorch/pytorch/pull/90174).
* __->__ #90174

fix c10::detail::integer_iterator for C++17

Summary: std::iterator is deprecated.

Test Plan: Rely on CI.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/90174
Approved by: https://github.com/clee2000, https://github.com/malfet
2022-12-05 18:39:47 +00:00
Scott Wolchok
1c0d47cb17 [PyTorch] Make c10::irange(x) generate the same assembly as for loop (#86841)
`c10::irange(n)` generated an extra `sar` and `andn` instruction compared to a traditional `for` loop. now it doesn't.

Differential Revision: [D40321009](https://our.internmc.facebook.com/intern/diff/D40321009/)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/86841
Approved by: https://github.com/r-barnes, https://github.com/malfet
2022-11-02 21:34:22 +00:00
Scott Wolchok
44cc873fba [PyTorch] Autoformat c10 (#56830)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56830

Opt into formatting on GitHub and format everything. This is a trial run before turning on formatting for more and eventually all of the codebase.

Test Plan: CI

Reviewed By: zertosh

Differential Revision: D27979080

fbshipit-source-id: a80f0c48691c08ae8ca0af06377b87e6a2351151
2021-04-30 21:23:28 -07:00
frdong
92770d25cd fix comparison of narrow type with wide type in loop condition (#53951)
Summary:
fix Semmle warning: Comparison of narrow type with wide type in loop condition

For example there is below piece of code:
for (int i=0; i<array.size(); ++i) {}

The problem is that array.size() return type is size_t can be larger type than int depending on the implementation so there is chance that i overflows (for very large array that array size is beyond the range of integer) and this loop will never be terminated.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/53951

Reviewed By: zou3519

Differential Revision: D27181495

Pulled By: malfet

fbshipit-source-id: 0612c5cedcdc656c193085e7fbb87dd163f20688
2021-03-22 16:40:35 -07:00
Richard Barnes
29c4290a8d Use c10::irange for great good (#52153)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/52153

Test Plan: Sandcastle

Reviewed By: ngimel

Differential Revision: D26407087

fbshipit-source-id: ea8ce1c17299cb9d89621e4a39f31edc2faa9fd6
2021-02-24 18:43:50 -08:00
Richard Barnes
8e7402441d Move irange to c10 (#46414)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/46414

For loops are often written with mismatched data types which causes silent type and sign coercion in the absence of integer conversion warnings. Getting around this in templated code requires convoluted patterns such as
```
for(auto i=decltype(var){0};i<var;i++)
```
with this diff we can instead write
```
for(const auto i = c10::irange(var))
```
Note that this loop is type-safe and const-safe.

The function introduced here (`c10::irange`) allows for type-safety and const-ness within for loops, which prevents the accidental truncation or modification of integers and other types, improving code safety.

Test Plan:
```
buck test //caffe2/c10:c10_test_0
```

Reviewed By: ngimel

Differential Revision: D24334732

fbshipit-source-id: fec5ebda3643ec5589f7ea3a8e7bbea4432ed771
2021-01-15 11:44:55 -08:00