• 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 "cg_phasemanager.h"
18 #include "driver_options.h"
19 
20 namespace maple {
21 
22 namespace litecg {
23 
24 using namespace maplebe;
25 
LiteCG(Module & mirModule,const std::vector<std::string> & litecgOptions)26 LiteCG::LiteCG(Module &mirModule, const std::vector<std::string> &litecgOptions) : module(mirModule)
27 {
28     // Create CGOption: set up default options
29     // should we make CGOptions local?
30     cgOptions = &CGOptions::GetInstance();
31     if (!litecgOptions.empty()) {
32         maplecl::CommandLine::GetCommandLine().Parse(litecgOptions, cgCategory);
33         cgOptions->SolveOptions();
34     }
35     cgOptions->EnableLiteCG();
36     cgOptions->SetOption(CGOptions::kDoCg);
37     Triple::GetTriple().Init(module.IsAArch64());
38     // module information prepare
39     std::string moduleName = module.GetFileName();
40     GStrIdx fileNameStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(moduleName);
41 
42     // is this strictly required?
43     GStrIdx nameStrIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName("INFO_filename");
44     module.PushFileInfoPair(MIRInfoPair(nameStrIdx, fileNameStrIdx.GetIdx()));
45     module.PushFileInfoIsString(true);
46 
47     module.SetFlavor(kFlavorUnknown);  // need a new flavor
48     module.GetImportFiles().clear();
49 
50     // Setup output file name
51     module.SetOutputFileName(moduleName + ".s");
52 }
53 
54 #ifdef ARK_LITECG_DEBUG
DumpIRToFile(const std::string & fileName)55 void LiteCG::DumpIRToFile(const std::string &fileName)
56 {
57     module.DumpToFile(fileName);
58 }
59 #endif
60 
DumpCGIR()61 void LiteCG::DumpCGIR()
62 {
63     cgOptions->GetDumpPhases().insert("*");
64     cgOptions->FuncFilter("*");
65 }
66 
SetupLiteCGEmitMemoryManager(void * codeSpace,MemoryManagerAllocateDataSectionCallback dataSectionAllocator,MemoryManagerSaveFunc2AddressInfoCallback funcAddressSaver,maplebe::MemoryManagerSaveFunc2FPtoPrevSPDeltaCallback funcFpSPDeltaSaver,maplebe::MemoryManagerSaveFunc2CalleeOffsetInfoCallback funcCallOffsetSaver,maplebe::MemoryManagerSavePC2DeoptInfoCallback pc2DeoptInfoSaver,maplebe::MemoryManagerSavePC2CallSiteInfoCallback pc2CallSiteInfoSaver)67 LiteCG &LiteCG::SetupLiteCGEmitMemoryManager(
68     void *codeSpace, MemoryManagerAllocateDataSectionCallback dataSectionAllocator,
69     MemoryManagerSaveFunc2AddressInfoCallback funcAddressSaver,
70     maplebe::MemoryManagerSaveFunc2FPtoPrevSPDeltaCallback funcFpSPDeltaSaver,
71     maplebe::MemoryManagerSaveFunc2CalleeOffsetInfoCallback funcCallOffsetSaver,
72     maplebe::MemoryManagerSavePC2DeoptInfoCallback pc2DeoptInfoSaver,
73     maplebe::MemoryManagerSavePC2CallSiteInfoCallback pc2CallSiteInfoSaver)
74 {
75     cgOptions->SetupEmitMemoryManager(codeSpace, dataSectionAllocator, funcAddressSaver, funcFpSPDeltaSaver,
76                                       funcCallOffsetSaver, pc2DeoptInfoSaver, pc2CallSiteInfoSaver);
77     return *this;
78 }
79 
SetAotCodeCommentFile(const std::string & aotCodeCommentFile)80 void LiteCG::SetAotCodeCommentFile(const std::string &aotCodeCommentFile)
81 {
82     cgOptions->SetEmitAotCodeCommentFile(aotCodeCommentFile);
83 }
84 
DoCG(bool isJit)85 void LiteCG::DoCG(bool isJit)
86 {
87     bool timePhases = cgOptions->IsEnableTimePhases();
88     MPLTimer timer;
89     if (timePhases) {
90         timer.Start();
91     }
92 
93     cgOptions->SetUseJitCodeSign(isJit);
94 
95     Globals::GetInstance()->SetOptimLevel(cgOptions->GetOptimizeLevel());
96 
97     // not sure how to do this.
98     auto cgPhaseManager = std::make_unique<ThreadLocalMemPool>(memPoolCtrler, "cg function phasemanager");
99     const MaplePhaseInfo *cgPMInfo = MaplePhaseRegister::GetMaplePhaseRegister()->GetPhaseByID(&CgFuncPM::id);
100     auto *cgfuncPhaseManager = static_cast<CgFuncPM *>(cgPMInfo->GetConstructor()(cgPhaseManager.get()));
101 
102     if (timePhases) {
103         cgfuncPhaseManager->InitTimeHandler();
104     }
105 
106     /* It is a specifc work around  (need refactor) */
107     cgfuncPhaseManager->SetQuiet(true);
108     cgfuncPhaseManager->SetCGOptions(cgOptions);
109     (void)cgfuncPhaseManager->PhaseRun(module);
110 
111     if (timePhases) {
112         cgfuncPhaseManager->DumpPhaseTime();
113         timer.Stop();
114         LogInfo::MapleLogger() << "Mplcg consumed " << timer.ElapsedMilliseconds() << "ms" << '\n';
115     }
116     cgOptions->SetUseJitCodeSign(false);
117 }
118 
119 }  // namespace litecg
120 
121 }  // namespace maple
122