Skip to content

Commit 2b81e6f

Browse files
authored
irangeify some of runtime/core (#8609)
1 parent 9804ef9 commit 2b81e6f

11 files changed

+32
-16
lines changed

runtime/core/array_ref.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <array>
3030
#include <cstdint>
3131

32+
#include <c10/util/irange.h>
3233
#include <executorch/runtime/platform/assert.h>
3334

3435
namespace executorch {
@@ -149,7 +150,7 @@ class ArrayRef final {
149150
if (Length != RHS.Length) {
150151
return false;
151152
}
152-
for (size_t i = 0; i < this->Length; i++) {
153+
for (const auto i : c10::irange(this->Length)) {
153154
if (Data[i] != RHS.Data[i]) {
154155
return false;
155156
}

runtime/core/hierarchical_allocator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#pragma once
1010

11+
#include <c10/util/irange.h>
1112
#include <executorch/runtime/core/memory_allocator.h>
1213
#include <executorch/runtime/core/result.h>
1314
#include <executorch/runtime/core/span.h>
@@ -96,7 +97,7 @@ class HierarchicalAllocator final {
9697
"n_allocators %" PRIu32 " > %zu",
9798
n_allocators,
9899
kSpanArraySize);
99-
for (uint32_t i = 0; i < n_allocators; ++i) {
100+
for (const auto i : c10::irange(n_allocators)) {
100101
span_array_[i] =
101102
Span<uint8_t>(allocators[i].base_address(), allocators[i].size());
102103
}

runtime/core/portable_type/c10/c10/targets.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def define_common_targets():
4545
"-DC10_USING_CUSTOM_GENERATED_MACROS",
4646
],
4747
visibility = [
48-
"//executorch/runtime/core/portable_type/...",
48+
"//executorch/...",
4949
],
5050
deps = select({
5151
"DEFAULT": [],

runtime/core/portable_type/test/bfloat16_test.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9+
#include <c10/util/irange.h>
910
#include <executorch/runtime/core/portable_type/bfloat16.h>
1011

1112
#include <gtest/gtest.h>
@@ -41,7 +42,7 @@ uint16_t bits_from_f32(float src) {
4142
TEST(BFloat16Conversion, FloatToBFloat16AndBack) {
4243
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-magic-numbers,modernize-avoid-c-arrays)
4344
float in[100];
44-
for (int i = 0; i < 100; ++i) {
45+
for (const auto i : c10::irange(100)) {
4546
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers)
4647
in[i] = i + 1.25;
4748
}
@@ -51,7 +52,7 @@ TEST(BFloat16Conversion, FloatToBFloat16AndBack) {
5152
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-magic-numbers,modernize-avoid-c-arrays)
5253
float out[100];
5354

54-
for (int i = 0; i < 100; ++i) {
55+
for (const auto i : c10::irange(100)) {
5556
bfloats[i].x = bits_from_f32(in[i]);
5657
out[i] = f32_from_bits(bfloats[i].x);
5758

@@ -64,7 +65,7 @@ TEST(BFloat16Conversion, FloatToBFloat16AndBack) {
6465
TEST(BFloat16Conversion, FloatToBFloat16RNEAndBack) {
6566
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-magic-numbers,modernize-avoid-c-arrays)
6667
float in[100];
67-
for (int i = 0; i < 100; ++i) {
68+
for (const auto i : c10::irange(100)) {
6869
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers)
6970
in[i] = i + 1.25;
7071
}
@@ -74,7 +75,7 @@ TEST(BFloat16Conversion, FloatToBFloat16RNEAndBack) {
7475
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-magic-numbers,modernize-avoid-c-arrays)
7576
float out[100];
7677

77-
for (int i = 0; i < 100; ++i) {
78+
for (const auto i : c10::irange(100)) {
7879
bfloats[i].x = round_to_nearest_even(in[i]);
7980
out[i] = f32_from_bits(bfloats[i].x);
8081

runtime/core/portable_type/test/targets.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def define_common_targets():
1111
srcs = ["bfloat16_test.cpp"],
1212
deps = [
1313
"//executorch/runtime/core/portable_type:portable_type",
14+
"//executorch/runtime/core/portable_type/c10/c10:c10",
1415
],
1516
)
1617

@@ -52,5 +53,6 @@ def define_common_targets():
5253
deps = [
5354
"//executorch/runtime/core/exec_aten/util:tensor_util",
5455
"//executorch/runtime/core/portable_type:portable_type",
56+
"//executorch/runtime/core/portable_type/c10/c10:c10",
5557
],
5658
)

runtime/core/portable_type/test/tensor_impl_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9+
#include <c10/util/irange.h>
910
#include <executorch/runtime/core/portable_type/tensor_impl.h>
1011

1112
#include <gtest/gtest.h>
@@ -76,7 +77,7 @@ TEST_F(TensorImplTest, TestSetSizesContigContract) {
7677

7778
SizesType new_sizes[RANK] = {0, 0, 0, 0, 0};
7879
// assign random sizes between 1 and 100
79-
for (int i = 0; i < RANK; i++) {
80+
for (const auto i : c10::irange(RANK)) {
8081
new_sizes[i] = distribution(generator);
8182
}
8283
Error err = resize_tensor_impl(&t, {new_sizes, RANK});

runtime/core/targets.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def define_common_targets():
5050
],
5151
exported_preprocessor_flags = get_core_flags(),
5252
exported_deps = [
53+
"//executorch/runtime/core/portable_type/c10/c10:c10",
5354
"//executorch/runtime/platform:platform",
5455
],
5556
)
@@ -73,6 +74,7 @@ def define_common_targets():
7374
],
7475
exported_deps = [
7576
":core",
77+
"//executorch/runtime/core/portable_type/c10/c10:c10",
7678
],
7779
visibility = [
7880
"//executorch/...",
@@ -145,13 +147,16 @@ def define_common_targets():
145147
":tensor_layout",
146148
],
147149
)
148-
150+
149151
runtime.cxx_library(
150152
name = "tensor_layout",
151153
srcs = ["tensor_layout.cpp"],
152154
exported_headers = ["tensor_layout.h"],
155+
deps = [
156+
"//executorch/runtime/core/portable_type/c10/c10:c10",
157+
],
153158
exported_deps = [
154-
":core",
159+
":core",
155160
"//executorch/runtime/core/exec_aten:lib",
156161
],
157162
visibility = ["//executorch/..."],

runtime/core/tensor_layout.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9+
#include <c10/util/irange.h>
910
#include <executorch/runtime/core/exec_aten/exec_aten.h>
1011
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
1112
#include <executorch/runtime/core/span.h>
@@ -43,7 +44,7 @@ Result<const TensorLayout> TensorLayout::create(
4344
return Error::InvalidArgument;
4445
}
4546

46-
for (size_t i = 0; i < dim_order.size(); i++) {
47+
for (const auto i : c10::irange(dim_order.size())) {
4748
if (dim_order[i] >= sizes.size()) {
4849
return Error::InvalidArgument;
4950
}

runtime/core/test/event_tracer_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <gtest/gtest.h>
1010

11+
#include <c10/util/irange.h>
1112
#include <executorch/runtime/core/array_ref.h>
1213
#include <executorch/runtime/core/evalue.h>
1314
#include <executorch/runtime/core/event_tracer.h>
@@ -207,7 +208,7 @@ TEST(TestEventTracer, SimpleEventTracerTest) {
207208
// and also with a null pointer (to test that the null case works).
208209
DummyEventTracer dummy;
209210
std::vector<DummyEventTracer*> dummy_event_tracer_arr = {&dummy, nullptr};
210-
for (size_t i = 0; i < dummy_event_tracer_arr.size(); i++) {
211+
for (const auto i : c10::irange(dummy_event_tracer_arr.size())) {
211212
RunSimpleTracerTest(&dummy);
212213
RunSimpleTracerTest(nullptr);
213214
}
@@ -234,7 +235,7 @@ TEST(TestEventTracer, SimpleEventTracerTestDelegate) {
234235
// and also with a null pointer (to test that the null case works).
235236
DummyEventTracer dummy;
236237
std::vector<DummyEventTracer*> dummy_event_tracer_arr = {&dummy, nullptr};
237-
for (size_t i = 0; i < dummy_event_tracer_arr.size(); i++) {
238+
for (const auto i : c10::irange(dummy_event_tracer_arr.size())) {
238239
RunSimpleTracerTestDelegate(&dummy);
239240
RunSimpleTracerTestDelegate(nullptr);
240241
}

runtime/core/test/memory_allocator_test.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <array>
1010
#include <vector>
1111

12+
#include <c10/util/irange.h>
1213
#include <executorch/runtime/core/memory_allocator.h>
1314
#include <executorch/runtime/platform/runtime.h>
1415
#include <executorch/test/utils/alignment.h>
@@ -62,12 +63,12 @@ TEST_F(MemoryAllocatorTest, MemoryAllocatorAlignment) {
6263
128,
6364
2};
6465

65-
for (int i = 0; i < arr_size; i++) {
66+
for (const auto i : c10::irange(arr_size)) {
6667
auto align_size = alignment[i];
6768
constexpr size_t mem_size = 1000;
6869
uint8_t mem_pool[mem_size];
6970
MemoryAllocator allocator = MemoryAllocator(mem_size, mem_pool);
70-
for (int j = 0; j < arr_size; j++) {
71+
for (const auto j : c10::irange(arr_size)) {
7172
auto size = allocation[j];
7273
void* start = allocator.allocate(size, align_size);
7374
EXPECT_ALIGNED(start, align_size);
@@ -81,7 +82,7 @@ TEST_F(MemoryAllocatorTest, MemoryAllocatorNonPowerOfTwoAlignment) {
8182
MemoryAllocator allocator(mem_size, mem_pool);
8283

8384
size_t alignment[5] = {0, 5, 6, 12, 34};
84-
for (int i = 0; i < 5; i++) {
85+
for (const auto i : c10::irange(5)) {
8586
ASSERT_EQ(nullptr, allocator.allocate(8, alignment[i]));
8687
}
8788
}

runtime/core/test/targets.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def define_common_targets():
4040
],
4141
deps = [
4242
"//executorch/runtime/core:event_tracer",
43+
"//executorch/runtime/core/portable_type/c10/c10:c10",
4344
],
4445
)
4546

@@ -68,6 +69,7 @@ def define_common_targets():
6869
],
6970
deps = [
7071
"//executorch/runtime/core:memory_allocator",
72+
"//executorch/runtime/core/portable_type/c10/c10:c10",
7173
],
7274
)
7375

0 commit comments

Comments
 (0)