Skip to content

Commit fd93394

Browse files
apivovarovtensorflower-gardener
authored andcommitted
Replace std::reverse with Abseil container algorithms.
This change replaces calls to `std::reverse(container.begin(), container.end())` with `absl::c_reverse`. The Abseil versions are more concise and often safer as they operate directly on the container. PiperOrigin-RevId: 937649754
1 parent 910ef0c commit fd93394

16 files changed

Lines changed: 45 additions & 38 deletions

File tree

third_party/xla/xla/hlo/analysis/shape_tracker.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ ShapeTracker::BufferView ShapeTracker::BufferView::FromShape(
271271
view.extents_.push_back(shape.dimensions(i));
272272
stride *= shape.dimensions(i);
273273
}
274-
std::reverse(view.strides_.begin(), view.strides_.end());
275-
std::reverse(view.extents_.begin(), view.extents_.end());
274+
absl::c_reverse(view.strides_);
275+
absl::c_reverse(view.extents_);
276276
if (view.strides_.empty()) {
277277
view.strides_.push_back(1);
278278
view.extents_.push_back(1);
@@ -929,7 +929,7 @@ std::vector<ShapeTracker::Step> ShapeTracker::OptimizeSteps(
929929
input_shape.dimensions().end());
930930

931931
std::vector<int64_t> mapping(current_shape.size());
932-
std::iota(mapping.begin(), mapping.end(), 0);
932+
absl::c_iota(mapping, 0);
933933

934934
// If the final step is a reshape, skip it for two reasons:
935935
// - Unlike other steps, it may contain degenerate dimensions which

third_party/xla/xla/hlo/builder/lib/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,15 @@ cc_library(
584584
name = "quantize",
585585
hdrs = ["quantize.h"],
586586
deps = [
587-
":constants",
587+
"//xla:shape_util",
588588
"//xla:types",
589589
"//xla:util",
590590
"//xla:xla_data_proto_cc",
591591
"//xla/hlo/builder:xla_builder",
592592
"//xla/tsl/platform:status_macros",
593+
"@com_google_absl//absl/algorithm:container",
594+
"@com_google_absl//absl/status:statusor",
595+
"@com_google_absl//absl/strings:string_view",
593596
"@tsl//tsl/platform:bfloat16",
594597
],
595598
)

third_party/xla/xla/hlo/builder/lib/quantize.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ limitations under the License.
1616
#ifndef XLA_HLO_BUILDER_LIB_QUANTIZE_H_
1717
#define XLA_HLO_BUILDER_LIB_QUANTIZE_H_
1818

19-
#include <algorithm>
19+
#include <climits>
20+
#include <cstdint>
2021
#include <limits>
21-
#include <numeric>
22+
#include <type_traits>
2223
#include <vector>
2324

25+
#include "absl/algorithm/container.h"
26+
#include "absl/status/statusor.h"
27+
#include "absl/strings/string_view.h"
2428
#include "xla/tsl/platform/status_macros.h"
25-
#include "xla/hlo/builder/lib/constants.h"
2629
#include "xla/hlo/builder/xla_builder.h"
30+
#include "xla/shape.h"
2731
#include "xla/types.h"
2832
#include "xla/util.h"
2933
#include "xla/xla_data.pb.h"
@@ -122,8 +126,7 @@ inline XlaOp Dequantize(XlaOp input, const QuantizedRange& range,
122126
}
123127

124128
std::vector<int64_t> shift_transpose_dimensions(shape.dimensions().size());
125-
std::iota(shift_transpose_dimensions.begin(),
126-
shift_transpose_dimensions.end(), 0);
129+
absl::c_iota(shift_transpose_dimensions, 0);
127130
shift_transpose_dimensions.insert(shift_transpose_dimensions.begin(), 1,
128131
shape.dimensions().size());
129132

@@ -156,8 +159,8 @@ inline XlaOp Dequantize(XlaOp input, const QuantizedRange& range,
156159
}
157160

158161
std::vector<int64_t> transpose_dimensions(shape.dimensions().size());
159-
std::iota(transpose_dimensions.begin(), transpose_dimensions.end(), 1);
160-
std::reverse(transpose_dimensions.begin(), transpose_dimensions.end());
162+
absl::c_iota(transpose_dimensions, 1);
163+
absl::c_reverse(transpose_dimensions);
161164
transpose_dimensions.insert(transpose_dimensions.begin() + 1, 1, 0);
162165

163166
// Transpose the result to be [dn, unpack_size, dn-1, ..., d1, d0].
@@ -173,8 +176,8 @@ inline XlaOp Dequantize(XlaOp input, const QuantizedRange& range,
173176

174177
// Transpose the result to be [d0, d1, ..., dn-1, dn * unpack_size].
175178
std::vector<int64_t> result_dimensions(shape.dimensions().size());
176-
std::iota(result_dimensions.begin(), result_dimensions.end(), 0);
177-
std::reverse(result_dimensions.begin(), result_dimensions.end());
179+
absl::c_iota(result_dimensions, 0);
180+
absl::c_reverse(result_dimensions);
178181

179182
return Transpose(reshaped_result, result_dimensions);
180183
});

third_party/xla/xla/hlo/ir/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ cc_library(
101101
"//xla/service:mapped_ptr_container_sorter",
102102
"//xla/service:metrics_proto_cc",
103103
"//xla/service:name_uniquer",
104-
"//xla/tsl/concurrency:ref_count",
105104
"//xla/tsl/lib/gtl:iterator_range",
106105
"//xla/tsl/lib/gtl:map_util",
107106
"//xla/tsl/platform:env",

third_party/xla/xla/hlo/ir/hlo_computation.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ limitations under the License.
5555
#include "xla/hlo/ir/hlo_instructions.h"
5656
#include "xla/hlo/ir/hlo_module.h"
5757
#include "xla/hlo/ir/hlo_opcode.h"
58-
#include "xla/hlo/ir/hlo_payload_deduplicator.h"
5958
#include "xla/hlo/ir/hlo_print_options.h"
6059
#include "xla/hlo/ir/ptrvec.h"
6160
#include "xla/literal.h"
@@ -71,9 +70,7 @@ limitations under the License.
7170
#include "xla/shape_util.h"
7271
#include "xla/status_macros.h"
7372
#include "xla/tsl/lib/gtl/iterator_range.h"
74-
#include "xla/tsl/platform/errors.h"
7573
#include "xla/tsl/platform/logging.h"
76-
#include "xla/tsl/platform/statusor.h"
7774
#include "xla/util.h"
7875
#include "xla/xla_data.pb.h"
7976

@@ -1082,7 +1079,7 @@ HloComputation::MakeInstructionPostOrderWithReshapeFirst() const {
10821079
}
10831080
}
10841081

1085-
std::reverse(sorted.begin(), sorted.end());
1082+
absl::c_reverse(sorted);
10861083
CHECK_EQ(sorted.size(), instruction_count());
10871084
return sorted;
10881085
}

third_party/xla/xla/hlo/ir/hlo_instruction.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,9 @@ limitations under the License.
8585
#include "xla/side_effect_util.h"
8686
#include "xla/sort_json.h"
8787
#include "xla/status_macros.h"
88-
#include "xla/tsl/concurrency/ref_count.h"
8988
#include "xla/tsl/lib/gtl/iterator_range.h"
9089
#include "xla/tsl/lib/gtl/map_util.h"
9190
#include "xla/tsl/platform/logging.h" // IWYU pragma: keep
92-
#include "xla/tsl/platform/statusor.h"
9391
#include "xla/util.h"
9492
#include "xla/xla_data.pb.h"
9593

@@ -3017,7 +3015,7 @@ HloInstruction::LatestNonGteAncestorAndIndex() const {
30173015
}
30183016

30193017
// We built up index in the reverse order from what we want.
3020-
std::reverse(index.begin(), index.end());
3018+
absl::c_reverse(index);
30213019

30223020
return {hlo, index};
30233021
}

third_party/xla/xla/hlo/pass/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ xla_cc_test(
127127
"//xla/tsl/lib/core:status_test_util",
128128
"//xla/tsl/platform:statusor",
129129
"//xla/tsl/platform:test_main",
130+
"@com_google_absl//absl/algorithm:container",
130131
"@com_google_absl//absl/container:flat_hash_set",
131132
"@com_google_absl//absl/status",
132133
"@com_google_absl//absl/status:statusor",

third_party/xla/xla/hlo/pass/hlo_pass_pipeline_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ limitations under the License.
1515

1616
#include "xla/hlo/pass/hlo_pass_pipeline.h"
1717

18-
#include <algorithm>
1918
#include <memory>
2019
#include <string>
2120
#include <vector>
2221

2322
#include <gmock/gmock.h>
2423
#include <gtest/gtest.h>
24+
#include "absl/algorithm/container.h"
2525
#include "absl/container/flat_hash_set.h"
2626
#include "absl/status/status.h"
2727
#include "absl/status/statusor.h"
@@ -84,7 +84,7 @@ class ReverseStringModulePass : public HloModulePass {
8484
module->computations(execution_threads)) {
8585
HloInstruction* root = computation->root_instruction();
8686
std::string name(root->name());
87-
std::reverse(name.begin(), name.end());
87+
absl::c_reverse(name);
8888
root->SetAndSanitizeName(name);
8989
changed = true;
9090
}

third_party/xla/xla/hlo/tools/hlo_diff/utils/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ cc_library(
7777
srcs = ["text_diff.cc"],
7878
hdrs = ["text_diff.h"],
7979
deps = [
80+
"@com_google_absl//absl/algorithm:container",
8081
"@com_google_absl//absl/base:no_destructor",
8182
"@com_google_absl//absl/container:flat_hash_map",
8283
"@com_google_absl//absl/strings:string_view",

third_party/xla/xla/hlo/tools/hlo_diff/utils/text_diff.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License.
2020
#include <utility>
2121
#include <vector>
2222

23+
#include "absl/algorithm/container.h"
2324
#include "absl/base/no_destructor.h"
2425
#include "absl/container/flat_hash_map.h"
2526
#include "absl/strings/string_view.h"
@@ -122,7 +123,7 @@ void ComputeDiffRecursive(absl::string_view left, absl::string_view right,
122123
j--;
123124
}
124125
}
125-
std::reverse(reversed_chunks.begin(), reversed_chunks.end());
126+
absl::c_reverse(reversed_chunks);
126127
for (const auto& chunk : reversed_chunks) {
127128
MergeOrPushChunk(chunk.type, chunk.text, chunks);
128129
}

0 commit comments

Comments
 (0)