diff --git a/compiler_service/BUILD.gn b/compiler_service/BUILD.gn index e1e850c9e0..ede1a95506 100644 --- a/compiler_service/BUILD.gn +++ b/compiler_service/BUILD.gn @@ -74,6 +74,12 @@ ohos_shared_library("libcompiler_service") { defines += [ "CODE_SIGN_ENABLE" ] } + if (!ark_standalone_build && !(defined(is_arkui_x) && is_arkui_x) && + is_ohos && is_standard_system) { + external_deps += [ "init:libbegetutil" ] + defines += [ "ENABLE_COMPILER_SERVICE_GET_PARAMETER" ] + } + install_enable = true install_images = [ "system" ] diff --git a/compiler_service/include/aot_compiler_constants.h b/compiler_service/include/aot_compiler_constants.h index a8fe7c5be3..daacebce8d 100644 --- a/compiler_service/include/aot_compiler_constants.h +++ b/compiler_service/include/aot_compiler_constants.h @@ -35,6 +35,7 @@ const std::string APP_SIGNATURE = "appIdentifier"; const std::string ABC_PATH = "ABC-Path"; const std::string TARGET_COMPILER_MODE = "target-compiler-mode"; const std::string COMPILER_PKG_INFO = "compiler-pkg-info"; +const std::string COMPILER_ENABLE_AOT_CODE_COMMENT = "compiler-enable-aot-code-comment"; } // namespace ArgsIdx diff --git a/compiler_service/src/aot_compiler_impl.cpp b/compiler_service/src/aot_compiler_impl.cpp index fb17c681a1..705d83c066 100644 --- a/compiler_service/src/aot_compiler_impl.cpp +++ b/compiler_service/src/aot_compiler_impl.cpp @@ -35,6 +35,9 @@ #ifdef CODE_SIGN_ENABLE #include "local_code_sign_kit.h" #endif +#ifdef ENABLE_COMPILER_SERVICE_GET_PARAMETER +#include "base/startup/init/interfaces/innerkits/include/syspara/parameters.h" +#endif #include "system_ability_definition.h" namespace OHOS::ArkCompiler { @@ -96,6 +99,13 @@ int32_t AotCompilerImpl::PrepareArgs(const std::unordered_map> optionSelectMethods_; std::map> optionSkipMethods_; }; diff --git a/ecmascript/compiler/code_generator.h b/ecmascript/compiler/code_generator.h index 252a718bff..cfc8921566 100644 --- a/ecmascript/compiler/code_generator.h +++ b/ecmascript/compiler/code_generator.h @@ -166,8 +166,21 @@ public: { return codeInfo_; } + + void SetAotCodeCommentFile(const std::string &aotCodeCommentFile) + { + litecgCodeCommentFile_ = aotCodeCommentFile; + } + + const std::string &GetAotCodeCommentFile() const + { + return litecgCodeCommentFile_; + } + protected: CodeInfo codeInfo_; +private: + std::string litecgCodeCommentFile_ = ""; }; class CodeGeneratorImpl { diff --git a/ecmascript/compiler/codegen/maple/litecg_codegen.cpp b/ecmascript/compiler/codegen/maple/litecg_codegen.cpp index bf5466b53b..dcca1fe7b0 100644 --- a/ecmascript/compiler/codegen/maple/litecg_codegen.cpp +++ b/ecmascript/compiler/codegen/maple/litecg_codegen.cpp @@ -113,6 +113,7 @@ void LiteCGAssembler::Run(const CompilerLog &log, [[maybe_unused]] bool fastComp liteCG.SetupLiteCGEmitMemoryManager(&codeInfo_, isJit ? AllocateCodeSectionOnDemand : AllocateCodeSection, SaveFunc2Addr, SaveFunc2FPtoPrevSPDelta, SaveFunc2CalleeOffsetInfo, SavePC2DeoptInfo, SavePC2CallSiteInfo); + liteCG.SetAotCodeCommentFile(GetAotCodeCommentFile()); #ifdef JIT_ENABLE_CODE_SIGN isJit &= JitFort::IsResourceAvailable(); if (isJit) { diff --git a/ecmascript/compiler/codegen/maple/maple_be/include/cg/cg_option.h b/ecmascript/compiler/codegen/maple/maple_be/include/cg/cg_option.h index 1ef9bab77f..ae93c8ba4d 100644 --- a/ecmascript/compiler/codegen/maple/maple_be/include/cg/cg_option.h +++ b/ecmascript/compiler/codegen/maple/maple_be/include/cg/cg_option.h @@ -332,6 +332,16 @@ public: ehExclusiveFile = ehExclusive; } + void SetEmitAotCodeCommentFile(const std::string &aotCodeCommentFile) + { + aotCodeCommentFilePath = aotCodeCommentFile; + } + + const std::string &GetEmitAotCodeCommentFile() const + { + return aotCodeCommentFilePath; + } + static std::unordered_set &GetDumpPhases() { return dumpPhases; @@ -887,6 +897,7 @@ private: std::string ehExclusiveFile; /* we don't do exception handling in this list */ std::vector ehExclusiveFunctionName; + std::string aotCodeCommentFilePath = ""; static std::string targetArch; static std::unordered_set dumpPhases; diff --git a/ecmascript/compiler/codegen/maple/maple_be/include/cg/emit.h b/ecmascript/compiler/codegen/maple/maple_be/include/cg/emit.h index 41610e33d6..550cce77bb 100644 --- a/ecmascript/compiler/codegen/maple/maple_be/include/cg/emit.h +++ b/ecmascript/compiler/codegen/maple/maple_be/include/cg/emit.h @@ -22,6 +22,7 @@ #include #include #include +#include #include "isa.h" #include "asm_info.h" #include "cg.h" diff --git a/ecmascript/compiler/codegen/maple/maple_be/include/litecg/litecg.h b/ecmascript/compiler/codegen/maple/maple_be/include/litecg/litecg.h index 8eec0163bb..c4160f68e5 100644 --- a/ecmascript/compiler/codegen/maple/maple_be/include/litecg/litecg.h +++ b/ecmascript/compiler/codegen/maple/maple_be/include/litecg/litecg.h @@ -63,6 +63,7 @@ public: #endif void DumpCGIR(); void DoCG(bool isJit = false); + void SetAotCodeCommentFile(const std::string &aotCodeCommentFile); private: Module &module; diff --git a/ecmascript/compiler/codegen/maple/maple_be/src/cg/emit.cpp b/ecmascript/compiler/codegen/maple/maple_be/src/cg/emit.cpp index 9b936d5e61..94e0f915ac 100644 --- a/ecmascript/compiler/codegen/maple/maple_be/src/cg/emit.cpp +++ b/ecmascript/compiler/codegen/maple/maple_be/src/cg/emit.cpp @@ -2796,9 +2796,12 @@ void Emitter::EmitHexUnsigned(uint64 num) void Emitter::WriteDebugCommentToFile() { #ifdef ARK_LITECG_DEBUG - std::ofstream file(DEFAULT_PATH.c_str(), std::ios::app); + struct stat buffer; + std::string filePath = cg->GetCGOptions().GetEmitAotCodeCommentFile(); + std::string outputFile = stat(filePath.c_str(), &buffer) == 0 ? filePath : DEFAULT_PATH; + std::ofstream file(outputFile.c_str(), std::ios::app); if (!file.is_open()) { - std::cerr << DEFAULT_PATH << " Unable to open file for writing." << std::endl; + std::cerr << outputFile << " Unable to open file for writing." << std::endl; return; } diff --git a/ecmascript/compiler/codegen/maple/maple_be/src/litecg/litecg.cpp b/ecmascript/compiler/codegen/maple/maple_be/src/litecg/litecg.cpp index 5d8ea5635c..b069830d3b 100644 --- a/ecmascript/compiler/codegen/maple/maple_be/src/litecg/litecg.cpp +++ b/ecmascript/compiler/codegen/maple/maple_be/src/litecg/litecg.cpp @@ -83,6 +83,11 @@ LiteCG &LiteCG::SetupLiteCGEmitMemoryManager( return *this; } +void LiteCG::SetAotCodeCommentFile(const std::string &aotCodeCommentFile) +{ + cgOptions->SetEmitAotCodeCommentFile(aotCodeCommentFile); +} + void LiteCG::DoCG(bool isJit) { bool timePhases = cgOptions->IsEnableTimePhases(); diff --git a/ecmascript/compiler/file_generators.cpp b/ecmascript/compiler/file_generators.cpp index 52a8c4a849..1a2086a978 100644 --- a/ecmascript/compiler/file_generators.cpp +++ b/ecmascript/compiler/file_generators.cpp @@ -320,8 +320,13 @@ uintptr_t Module::GetSectionAddr(ElfSecName sec) const return assembler_->GetSectionAddr(sec); } -void Module::RunAssembler(const CompilerLog &log, bool fastCompileMode, bool isJit) +void Module::RunAssembler(const CompilerLog &log, bool fastCompileMode, bool isJit, const std::string &filename) { + if (!IsLLVM()) { + assembler_->SetAotCodeCommentFile(filename); + } else { + assembler_->SetAotCodeCommentFile(""); + } assembler_->Run(log, fastCompileMode, isJit); } @@ -592,7 +597,7 @@ void AOTFileGenerator::CompileLatestModuleThenDestroy(bool isJit) { TimeScope timescope("LLVMIROpt", const_cast(log_)); bool fastCompileMode = compilationEnv_->GetJSOptions().GetFastAOTCompileMode(); - latestModule->RunAssembler(*(log_), fastCompileMode, isJit); + latestModule->RunAssembler(*(log_), fastCompileMode, isJit, GetAotCodeCommentFile()); } { TimeScope timescope("LLVMCodeGen", const_cast(log_)); @@ -804,4 +809,50 @@ bool AOTFileGenerator::SaveSnapshotFile() SetSecurityLabel(aiPath.c_str()); return true; } + +bool AOTFileGenerator::CreateAOTCodeCommentFile(const std::string &filename) +{ + if (!CreateDirIfNotExist(filename)) { + LOG_COMPILER(ERROR) << "Fail to access dir: " << filename; + return false; + } + + std::string realPath; + if (!panda::ecmascript::RealPath(filename, realPath, false)) { + LOG_COMPILER(ERROR) << "Fail to get realPath: " << filename; + return false; + } + + auto index = realPath.find_last_of('/'); + if (index == std::string::npos) { + LOG_COMPILER(ERROR) << "Path: " << realPath << " is illegal"; + return false; + } + + std::string aotCodeCommentFile = realPath.substr(0, index) + "/aot_code_comment.txt"; + SetAotCodeCommentFile(aotCodeCommentFile); + if (FileExist(aotCodeCommentFile.c_str())) { + if (Unlink(aotCodeCommentFile.c_str()) == -1) { + SetAotCodeCommentFile(""); + LOG_COMPILER(ERROR) << "remove " << aotCodeCommentFile << " failed and errno is " << errno; + return false; + } + } + + std::ofstream file(aotCodeCommentFile.c_str(), std::ofstream::app); + if (!file.is_open()) { + SetAotCodeCommentFile(""); + LOG_COMPILER(ERROR) << "Failed to create " << aotCodeCommentFile; + return false; + } + file.close(); + + if (!panda::ecmascript::SetFileModeAsDefault(aotCodeCommentFile)) { + SetAotCodeCommentFile(""); + Unlink(aotCodeCommentFile.c_str()); + LOG_COMPILER(ERROR) << "Fail to set file mode: " << aotCodeCommentFile; + return false; + } + return true; +} } // namespace panda::ecmascript::kungfu diff --git a/ecmascript/compiler/file_generators.h b/ecmascript/compiler/file_generators.h index 9b2058db81..f5c53de7ec 100644 --- a/ecmascript/compiler/file_generators.h +++ b/ecmascript/compiler/file_generators.h @@ -83,7 +83,8 @@ public: return std::make_tuple(addrBeforeText, sizeBeforeText, addrAfterText, sizeAfterText); } - void RunAssembler(const CompilerLog &log, bool fastCompileMode, bool isJit = false); + void RunAssembler(const CompilerLog &log, bool fastCompileMode, bool isJit = false, + const std::string &filename = ""); void DisassemblerFunc(std::map &addr2name, uint64_t textOffset, const CompilerLog &log, const MethodLogList &logList, std::ostringstream &codeStream); @@ -221,6 +222,19 @@ public: bool GetMemoryCodeInfos(MachineCodeDesc &machineCodeDesc); void JitCreateLitecgModule(); bool isAArch64() const; + + bool CreateAOTCodeCommentFile(const std::string &filename); + + const std::string &GetAotCodeCommentFile() const + { + return aotCodeCommentFile_; + } + + void SetAotCodeCommentFile(const std::string &filename) + { + aotCodeCommentFile_ = filename; + } + private: // collect aot component info void CollectCodeInfo(Module *module, uint32_t moduleIdx); @@ -236,6 +250,7 @@ private: std::map methodToEntryIndexMap_ {}; const bool useLiteCG_; CodeInfo::CodeSpaceOnDemand jitCodeSpace_ {}; + std::string aotCodeCommentFile_ = ""; }; enum class StubFileKind { diff --git a/ecmascript/js_runtime_options.cpp b/ecmascript/js_runtime_options.cpp index 750ac3e7bf..5b51de70b9 100644 --- a/ecmascript/js_runtime_options.cpp +++ b/ecmascript/js_runtime_options.cpp @@ -196,7 +196,9 @@ const std::string PUBLIC_API HELP_OPTION_MSG = "--async-load-abc-test: Enable asynchronous load abc test. Default: 'false'\n" "--compiler-enable-concurrent: Enable concurrent compile(only support in ark_stub_compiler).\n" " Default: 'true'\n" - "--compiler-opt-frame-state-elimination: Enable frame state elimination. Default: 'true'\n\n"; + "--compiler-opt-frame-state-elimination: Enable frame state elimination. Default: 'true'\n" + "--compiler-enable-aot-code-comment Enable generate aot_code_comment.txt file during compilation.\n" + " Default : 'false'\n\n"; bool JSRuntimeOptions::ParseCommand(const int argc, const char **argv) { @@ -336,6 +338,7 @@ bool JSRuntimeOptions::ParseCommand(const int argc, const char **argv) {"compiler-enable-concurrent", required_argument, nullptr, OPTION_COMPILER_ENABLE_CONCURRENT}, {"compiler-opt-frame-state-elimination", required_argument, nullptr, OPTION_COMPILER_OPT_FRAME_STATE_ELIMINATION}, + {"compiler-enable-aot-code-comment", required_argument, nullptr, OPTION_COMPILER_ENABLE_AOT_CODE_COMMENT}, {nullptr, 0, nullptr, 0}, }; @@ -1301,13 +1304,21 @@ bool JSRuntimeOptions::ParseCommand(const int argc, const char **argv) } break; case OPTION_COMPILER_ENABLE_PGO_SPACE: - ret = ParseBoolParam(&argBool); + ret = ParseBoolParam(&argBool); if (ret) { SetCompilerEnablePgoSpace(argBool); } else { return false; } break; + case OPTION_COMPILER_ENABLE_AOT_CODE_COMMENT: + ret = ParseBoolParam(&argBool); + if (ret) { + SetEnableAotCodeComment(argBool); + } else { + return false; + } + break; default: LOG_ECMA(ERROR) << "Invalid option\n"; return false; diff --git a/ecmascript/js_runtime_options.h b/ecmascript/js_runtime_options.h index 77dc98d0b5..ee44bf2b2f 100644 --- a/ecmascript/js_runtime_options.h +++ b/ecmascript/js_runtime_options.h @@ -216,6 +216,7 @@ enum CommandValues { OPTION_OPEN_ARK_TOOLS, OPTION_COMPILER_OPT_FRAME_STATE_ELIMINATION, OPTION_COMPILER_ENABLE_PGO_SPACE, + OPTION_COMPILER_ENABLE_AOT_CODE_COMMENT, }; static_assert(OPTION_SPLIT_ONE == 64); // add new option at the bottom, DO NOT modify this value static_assert(OPTION_SPLIT_TWO == 128); // add new option at the bottom, DO NOT modify this value @@ -1965,6 +1966,16 @@ public: return enablePgoSpace_; } + void SetEnableAotCodeComment(bool value) + { + enableAotCodeComment_ = value; + } + + bool IsEnableAotCodeComment() const + { + return enableAotCodeComment_; + } + public: static constexpr int32_t MAX_APP_COMPILE_METHOD_SIZE = 4_KB; @@ -2126,6 +2137,7 @@ private: bool enableTypedOpProfiler_ {false}; bool enableBranchProfiling_ {true}; bool enablePgoSpace_ {false}; + bool enableAotCodeComment_ {false}; bool testAssert_ {false}; std::pair compileMethodsRange_ {0, UINT32_MAX}; arg_list_t compileCodegenOption_ {{""}};