xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/cpu/target_machine_features_fake.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_CPU_TARGET_MACHINE_FEATURES_FAKE_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_TARGET_MACHINE_FEATURES_FAKE_H_
18 
19 #include "tensorflow/compiler/xla/service/cpu/target_machine_features.h"
20 
21 namespace xla {
22 namespace cpu {
23 // Delegates calls to minimum_alignment_for_allocation to a user provided
24 // std::function, crashes on all other methods.
25 //
26 // Primarily useful for testing.
27 class TargetMachineFeaturesWithFakeAlignmentLogic
28     : public TargetMachineFeatures {
29  public:
TargetMachineFeaturesWithFakeAlignmentLogic(std::function<int64_t (int64_t)> fake_alignment_logic)30   explicit TargetMachineFeaturesWithFakeAlignmentLogic(
31       std::function<int64_t(int64_t)> fake_alignment_logic)
32       : fake_alignment_logic_(std::move(fake_alignment_logic)) {}
33 
vectorization_factor_in_bytes()34   int vectorization_factor_in_bytes() const override {
35     LOG(FATAL) << "Unexpected call to " << __func__;
36   }
37 
vector_register_byte_size(const llvm::Function & function)38   int vector_register_byte_size(const llvm::Function& function) const override {
39     LOG(FATAL) << "Unexpected call to " << __func__;
40   }
41 
vector_register_num_elements(const llvm::Function & function,PrimitiveType type)42   int vector_register_num_elements(const llvm::Function& function,
43                                    PrimitiveType type) const override {
44     LOG(FATAL) << "Unexpected call to " << __func__;
45   }
46 
vector_register_count(const llvm::Function & function)47   int vector_register_count(const llvm::Function& function) const override {
48     LOG(FATAL) << "Unexpected call to " << __func__;
49   }
50 
minimum_alignment_for_allocation(int64_t size_bytes)51   int64_t minimum_alignment_for_allocation(int64_t size_bytes) const override {
52     return fake_alignment_logic_(size_bytes);
53   }
54 
55  private:
56   std::function<int64_t(int64_t)> fake_alignment_logic_;
57 };
58 }  // namespace cpu
59 }  // namespace xla
60 
61 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_CPU_TARGET_MACHINE_FEATURES_FAKE_H_
62