xref: /aosp_15_r20/art/compiler/common_compiler_test.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_COMPILER_COMMON_COMPILER_TEST_H_
18 #define ART_COMPILER_COMMON_COMPILER_TEST_H_
19 
20 #include <list>
21 #include <vector>
22 
23 #include <jni.h>
24 
25 #include "arch/instruction_set.h"
26 #include "arch/instruction_set_features.h"
27 #include "base/macros.h"
28 #include "common_runtime_test.h"
29 #include "compiler.h"
30 #include "oat/oat_file.h"
31 
32 namespace art HIDDEN {
33 namespace mirror {
34 class ClassLoader;
35 }  // namespace mirror
36 
37 class CompilerOptions;
38 class CumulativeLogger;
39 class DexFile;
40 class TimingLogger;
41 
42 template<class T> class Handle;
43 
44 // Export all symbols in `CommonCompilerTestImpl` for dex2oat tests.
45 class EXPORT CommonCompilerTestImpl {
46  public:
47   // Create compiler options from the given instruction set and variant. Optionally use a string of
48   // instruction set features in addition to the features from the variant.
49   static std::unique_ptr<CompilerOptions> CreateCompilerOptions(
50       InstructionSet instruction_set,
51       const std::string& variant,
52       const std::optional<std::string>& extra_features = std::nullopt);
53 
54   CommonCompilerTestImpl();
55   virtual ~CommonCompilerTestImpl();
56 
57   // Create an executable copy of the code with given metadata.
58   const void* MakeExecutable(ArrayRef<const uint8_t> code,
59                              ArrayRef<const uint8_t> vmap_table,
60                              InstructionSet instruction_set);
61 
62  protected:
63   void SetUp();
64 
65   void SetUpRuntimeOptionsImpl();
66 
GetCompilerFilter()67   virtual CompilerFilter::Filter GetCompilerFilter() const {
68     return CompilerFilter::kDefaultCompilerFilter;
69   }
70 
71   void TearDown();
72 
73   void CompileMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
74   std::vector<uint8_t> JniCompileCode(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
75 
76   void ApplyInstructionSet();
77   void OverrideInstructionSetFeatures(InstructionSet instruction_set, const std::string& variant);
78 
79   void ClearBootImageOption();
80 
81   InstructionSet instruction_set_ =
82       (kRuntimeISA == InstructionSet::kArm) ? InstructionSet::kThumb2 : kRuntimeISA;
83   // Take the default set of instruction features from the build.
84   std::unique_ptr<const InstructionSetFeatures> instruction_set_features_
85       = InstructionSetFeatures::FromCppDefines();
86 
87   std::unique_ptr<CompilerOptions> compiler_options_;
88 
89  protected:
90   virtual ClassLinker* GetClassLinker() = 0;
91   virtual Runtime* GetRuntime() = 0;
92   class OneCompiledMethodStorage;
93 
94  private:
95   class CodeAndMetadata;
96 
97   std::vector<CodeAndMetadata> code_and_metadata_;
98 };
99 
100 template <typename RuntimeBase>
101 class CommonCompilerTestBase : public CommonCompilerTestImpl, public RuntimeBase {
102  public:
SetUp()103   void SetUp() override {
104     RuntimeBase::SetUp();
105     CommonCompilerTestImpl::SetUp();
106   }
SetUpRuntimeOptions(RuntimeOptions * options)107   void SetUpRuntimeOptions(RuntimeOptions* options) override {
108     RuntimeBase::SetUpRuntimeOptions(options);
109     CommonCompilerTestImpl::SetUpRuntimeOptionsImpl();
110   }
TearDown()111   void TearDown() override {
112     CommonCompilerTestImpl::TearDown();
113     RuntimeBase::TearDown();
114   }
115 
116  protected:
GetClassLinker()117   ClassLinker* GetClassLinker() override {
118     return RuntimeBase::class_linker_;
119   }
GetRuntime()120   Runtime* GetRuntime() override {
121     return RuntimeBase::runtime_.get();
122   }
123 };
124 
125 class CommonCompilerTest : public CommonCompilerTestBase<CommonRuntimeTest> {};
126 
127 template <typename Param>
128 class CommonCompilerTestWithParam
129     : public CommonCompilerTestBase<CommonRuntimeTestWithParam<Param>> {};
130 
131 }  // namespace art
132 
133 #endif  // ART_COMPILER_COMMON_COMPILER_TEST_H_
134