• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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 #include "litecg.h"
17 #include "mir_builder.h"
18 #include "cg_option.h"
19 #include "mad.h"
20 #include "cg.h"
21 #include "maple_phase_support.h"
22 #include "maple_phase.h"
23 #include "cg_phasemanager.h"
24 #include "triple.h"
25 #include "driver_options.h"
26 
27 namespace maple {
28 
29 namespace litecg {
30 
31 using namespace maplebe;
32 
LiteCG(Module & mirModule,const std::vector<std::string> & litecgOptions)33 LiteCG::LiteCG(Module &mirModule, const std::vector<std::string> &litecgOptions) : module(mirModule)
34 {
35     // Create CGOption: set up default options
36     // should we make CGOptions local?
37     cgOptions = &CGOptions::GetInstance();
38     if (!litecgOptions.empty()) {
39         maplecl::CommandLine::GetCommandLine().Parse(litecgOptions, cgCategory);
40         cgOptions->SolveOptions(false);
41     }
42     cgOptions->EnableLiteCG();
43     cgOptions->SetOption(CGOptions::kDoCg);
44     Triple::GetTriple().Init(module.IsAArch64());
45     // module information prepare
46     std::string moduleName = module.GetFileName();
47     GStrIdx fileNameStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(moduleName);
48 
49     // is this strictly required?
50     GStrIdx nameStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName("INFO_filename");
51     module.PushFileInfoPair(MIRInfoPair(nameStrIdx, fileNameStrIdx.GetIdx()));
52     module.PushFileInfoIsString(true);
53 
54     module.SetFlavor(kFlavorUnknown);  // need a new flavor
55     module.GetImportFiles().clear();
56 
57     // Setup output file name
58     module.SetOutputFileName(moduleName + ".s");
59 }
60 
SetTargetType(TargetType config)61 LiteCG &LiteCG::SetTargetType(TargetType config)
62 {
63     // update target support
64     // cgOptions->SetTarget(X86_64)
65     return *this;
66 }
67 
SetDebugType(DebugType config)68 LiteCG &LiteCG::SetDebugType(DebugType config)
69 {
70     // fix the exposed debug options
71     // cgOptions->SetDebug(?)
72     return *this;
73 }
74 
SetVerbose(InfoType config)75 LiteCG &LiteCG::SetVerbose(InfoType config)
76 {
77     cgOptions->SetQuiet((config == kQuiet) ? true : false);
78     return *this;
79 }
80 
DumpIRToFile(const std::string & fileName)81 void LiteCG::DumpIRToFile(const std::string &fileName)
82 {
83     module.DumpToFile(fileName);
84 }
85 
DumpCGIR()86 void LiteCG::DumpCGIR()
87 {
88     cgOptions->GetDumpPhases().insert("*");
89     cgOptions->FuncFilter("*");
90 }
91 
SetupLiteCGEmitMemoryManager(void * codeSpace,MemoryManagerAllocateDataSectionCallback dataSectionAllocator,MemoryManagerSaveFunc2AddressInfoCallback funcAddressSaver,maplebe::MemoryManagerSaveFunc2FPtoPrevSPDeltaCallback funcFpSPDeltaSaver,maplebe::MemoryManagerSaveFunc2CalleeOffsetInfoCallback funcCallOffsetSaver,maplebe::MemoryManagerSavePC2DeoptInfoCallback pc2DeoptInfoSaver,maplebe::MemoryManagerSavePC2CallSiteInfoCallback pc2CallSiteInfoSaver)92 LiteCG &LiteCG::SetupLiteCGEmitMemoryManager(
93     void *codeSpace, MemoryManagerAllocateDataSectionCallback dataSectionAllocator,
94     MemoryManagerSaveFunc2AddressInfoCallback funcAddressSaver,
95     maplebe::MemoryManagerSaveFunc2FPtoPrevSPDeltaCallback funcFpSPDeltaSaver,
96     maplebe::MemoryManagerSaveFunc2CalleeOffsetInfoCallback funcCallOffsetSaver,
97     maplebe::MemoryManagerSavePC2DeoptInfoCallback pc2DeoptInfoSaver,
98     maplebe::MemoryManagerSavePC2CallSiteInfoCallback pc2CallSiteInfoSaver)
99 {
100     cgOptions->SetupEmitMemoryManager(codeSpace, dataSectionAllocator, funcAddressSaver, funcFpSPDeltaSaver,
101                                       funcCallOffsetSaver, pc2DeoptInfoSaver, pc2CallSiteInfoSaver);
102     return *this;
103 }
104 
DoCG(bool isJit)105 void LiteCG::DoCG(bool isJit)
106 {
107     bool timePhases = cgOptions->IsEnableTimePhases();
108     MPLTimer timer;
109     if (timePhases) {
110         timer.Start();
111     }
112 
113     cgOptions->SetUseJitCodeSign(isJit);
114 
115     Globals::GetInstance()->SetOptimLevel(cgOptions->GetOptimizeLevel());
116 
117     MAD *mad;
118     if (cgOptions->DoLocalSchedule()) {
119         mad = new MAD();
120         Globals::GetInstance()->SetMAD(*mad);
121     }
122 
123     // not sure how to do this.
124     auto cgPhaseManager = std::make_unique<ThreadLocalMemPool>(memPoolCtrler, "cg function phasemanager");
125     const MaplePhaseInfo *cgPMInfo = MaplePhaseRegister::GetMaplePhaseRegister()->GetPhaseByID(&CgFuncPM::id);
126     auto *cgfuncPhaseManager = static_cast<CgFuncPM *>(cgPMInfo->GetConstructor()(cgPhaseManager.get()));
127     cgfuncPhaseManager->SetQuiet(CGOptions::IsQuiet());
128 
129     if (timePhases) {
130         cgfuncPhaseManager->InitTimeHandler();
131     }
132 
133     /* It is a specifc work around  (need refactor) */
134     cgfuncPhaseManager->SetCGOptions(cgOptions);
135     (void)cgfuncPhaseManager->PhaseRun(module);
136 
137     if (timePhases) {
138         cgfuncPhaseManager->DumpPhaseTime();
139         timer.Stop();
140         LogInfo::MapleLogger() << "Mplcg consumed " << timer.ElapsedMilliseconds() << "ms" << '\n';
141     }
142 
143     if (cgOptions->DoLocalSchedule()) {
144         Globals::GetInstance()->ClearMAD();
145         delete mad;
146     }
147 
148     cgOptions->SetUseJitCodeSign(false);
149 }
150 
151 }  // namespace litecg
152 
153 }  // namespace maple
154