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