diff --git a/BUILD.gn b/BUILD.gn index bea20a7536327ea350d4a783d3a49699ed79edf5..080f62c82da64c5333019fc31132530572641972 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -182,6 +182,15 @@ config("ark_jsruntime_common_config") { "-O0", "-ggdb3", ] + if (is_linux && (current_cpu == "x86" || current_cpu == "x64") && + run_with_asan) { + cflags_cc += [ + "-fsanitize=address", + "-fno-omit-frame-pointer", + ] + ldflags += [ "-L" + rebase_path("${root_out_dir}/ark/ark_js_runtime") ] + libs = [ "clang_rt.asan-x86_64" ] + } } else { defines += [ "NDEBUG" ] } @@ -390,6 +399,11 @@ source_set("libark_jsruntime_static") { } else { deps += [ "$ark_root/runtime:libarkruntime" ] } + + if (is_debug && is_linux && (current_cpu == "x86" || current_cpu == "x64") && + run_with_asan) { + deps += [ ":copy_asan_runtime" ] + } } ohos_shared_library("libark_jsruntime") { @@ -423,6 +437,11 @@ source_set("libark_jsruntime_test_static") { sdk_libc_secshared_dep, ] + if (is_debug && is_linux && (current_cpu == "x86" || current_cpu == "x64") && + run_with_asan) { + deps += [ ":copy_asan_runtime" ] + } + public_configs = [ ":ark_jsruntime_public_config", # should add before # arkruntime_public_config @@ -472,3 +491,8 @@ action("copy_resource_xml") { inputs = [ src_path + src_xml ] outputs = [ "$target_out_dir" ] } + +ohos_copy("copy_asan_runtime") { + sources = [ "${asan_lib_path}/libclang_rt.asan-x86_64.a" ] + outputs = [ "${root_out_dir}/ark/ark_js_runtime/{{source_file_part}}" ] +} diff --git a/ecmascript/compiler/BUILD.gn b/ecmascript/compiler/BUILD.gn index 0cfb8f7727c12c625442f231d4514592698676f8..b211d1cdcee2968508eb1cc2760cedfb3a4d1c45 100644 --- a/ecmascript/compiler/BUILD.gn +++ b/ecmascript/compiler/BUILD.gn @@ -107,25 +107,28 @@ source_set("libark_jsoptimizer_static") { "LLVMAsmParser", "LLVMMCParser", "LLVMMIRParser", - "LLVMX86Info", - "LLVMAArch64Info", - "LLVMARMDesc", - "LLVMAArch64Desc", - "LLVMX86Desc", - "LLVMX86Disassembler", - "LLVMARMDisassembler", - "LLVMAArch64Disassembler", "LLVMMCDisassembler", - "LLVMAArch64CodeGen", - "LLVMARMCodeGen", "LLVMCodeGen", - "LLVMX86CodeGen", - "LLVMX86AsmParser", "LLVMTransformUtils", - "LLVMAArch64Utils", + "LLVMIRReader", "LLVMARMUtils", + "LLVMARMCodeGen", + "LLVMARMDisassembler", + "LLVMARMDesc", + "LLVMARMInfo", + "LLVMARMAsmParser", + "LLVMAArch64Utils", + "LLVMAArch64CodeGen", + "LLVMAArch64Info", + "LLVMAArch64Desc", + "LLVMAArch64Disassembler", + "LLVMAArch64AsmParser", "LLVMX86Utils", - "LLVMIRReader", + "LLVMX86AsmParser", + "LLVMX86CodeGen", + "LLVMX86Desc", + "LLVMX86Disassembler", + "LLVMX86Info", ] deps = [ @@ -174,8 +177,16 @@ ohos_shared_library("libark_jsoptimizer_test") { subsystem_name = "test" } +ark_gen_file("stub_aot_options_gen_h") { + template_file = "options.h.erb" + data_file = "stub_aot_options.yaml" + requires = [ "$ark_root/templates/common.rb" ] + output_file = "$target_gen_dir/generated/stub_aot_options_gen.h" +} + ohos_executable("ark_stub_opt") { sources = [ "stub_aot_compiler.cpp" ] + include_dirs = [ "$target_gen_dir" ] configs = [ ":include_llvm", @@ -185,6 +196,7 @@ ohos_executable("ark_stub_opt") { ] deps = [ + ":stub_aot_options_gen_h", "$ark_root/libpandabase:libarkbase", "//ark/js_runtime:libark_jsruntime", "//ark/js_runtime/ecmascript/compiler:libark_jsoptimizer", diff --git a/ecmascript/compiler/circuit_builder.cpp b/ecmascript/compiler/circuit_builder.cpp index 0fcc17683ab076fd3de0ade1fdad8dcc9f3c2fad..541311d2f5bc45ce4eec78e72bc84ddca284b799 100644 --- a/ecmascript/compiler/circuit_builder.cpp +++ b/ecmascript/compiler/circuit_builder.cpp @@ -365,7 +365,7 @@ AddrShift CircuitBuilder::NewCallGate(StubDescriptor *descriptor, AddrShift targ AddrShift CircuitBuilder::NewCallRuntimeGate(StubDescriptor *descriptor, AddrShift thread, AddrShift target, std::initializer_list args) { - ASSERT(descriptor->GetStubKind() == StubDescriptor::RUNTIME_STUB); + ASSERT(descriptor->GetStubKind() == StubDescriptor::CallStubKind::RUNTIME_STUB); std::vector inputs; auto dependEntry = Circuit::GetCircuitRoot(OpCode(OpCode::DEPEND_ENTRY)); inputs.push_back(dependEntry); @@ -382,7 +382,7 @@ AddrShift CircuitBuilder::NewCallRuntimeGate(StubDescriptor *descriptor, AddrShi AddrShift CircuitBuilder::NewCallRuntimeGate(StubDescriptor *descriptor, AddrShift thread, AddrShift target, AddrShift depend, std::initializer_list args) { - ASSERT(descriptor->GetStubKind() == StubDescriptor::RUNTIME_STUB); + ASSERT(descriptor->GetStubKind() == StubDescriptor::CallStubKind::RUNTIME_STUB); std::vector inputs; inputs.push_back(depend); inputs.push_back(target); diff --git a/ecmascript/compiler/fast_stub.cpp b/ecmascript/compiler/fast_stub.cpp index ac005e5b53c0a9d3b7c40941583eae2ff1687815..cea74727883b575c0adebad562f4845131261c8a 100644 --- a/ecmascript/compiler/fast_stub.cpp +++ b/ecmascript/compiler/fast_stub.cpp @@ -344,7 +344,7 @@ void FastDivStub::GenerateCircuit() } } -void FastFindOwnElementStub::GenerateCircuit() +void FindOwnElementStub::GenerateCircuit() { auto env = GetEnvironment(); AddrShift thread = PtrArgument(0); @@ -401,7 +401,7 @@ void FastFindOwnElementStub::GenerateCircuit() Return(GetHoleConstant()); } -void FastGetElementStub::GenerateCircuit() +void GetElementStub::GenerateCircuit() { auto env = GetEnvironment(); AddrShift thread = PtrArgument(0); @@ -432,7 +432,7 @@ void FastGetElementStub::GenerateCircuit() LoopEnd(&loopHead); } -void FastFindOwnElement2Stub::GenerateCircuit() +void FindOwnElement2Stub::GenerateCircuit() { auto env = GetEnvironment(); AddrShift thread = PtrArgument(0); @@ -493,7 +493,7 @@ void FastFindOwnElement2Stub::GenerateCircuit() Return(GetHoleConstant()); } -void FastSetElementStub::GenerateCircuit() +void SetElementStub::GenerateCircuit() { auto env = GetEnvironment(); AddrShift thread = PtrArgument(0); @@ -507,6 +507,7 @@ void FastSetElementStub::GenerateCircuit() AddrShift pattr = Alloca(static_cast(MachineRep::K_WORD32)); AddrShift pindexOrEntry = Alloca(static_cast(MachineRep::K_WORD32)); Label loopHead(env); + Label loopEnd(env); Jump(&loopHead); LoopBegin(&loopHead); { @@ -647,13 +648,15 @@ void FastSetElementStub::GenerateCircuit() } Bind(¬JsProxy); onPrototype = TrueConstant(); + Jump(&loopEnd); } } } + Bind(&loopEnd); LoopEnd(&loopHead); } -void FastGetPropertyByIndexStub::GenerateCircuit() +void GetPropertyByIndexStub::GenerateCircuit() { auto env = GetEnvironment(); AddrShift thread = PtrArgument(0); @@ -765,7 +768,7 @@ void FastGetPropertyByIndexStub::GenerateCircuit() } } -void FastSetPropertyByIndexStub::GenerateCircuit() +void SetPropertyByIndexStub::GenerateCircuit() { auto env = GetEnvironment(); AddrShift thread = PtrArgument(0); @@ -857,7 +860,7 @@ void FastSetPropertyByIndexStub::GenerateCircuit() } } -void FastGetPropertyByNameStub::GenerateCircuit() +void GetPropertyByNameStub::GenerateCircuit() { auto env = GetEnvironment(); AddrShift thread = PtrArgument(0); diff --git a/ecmascript/compiler/fast_stub.h b/ecmascript/compiler/fast_stub.h index 6814fa37c7eb71f9d6dd577b292a622817038769..109b7be5d62f9595c2e778ac4b298c0e1b3c3650 100644 --- a/ecmascript/compiler/fast_stub.h +++ b/ecmascript/compiler/fast_stub.h @@ -70,73 +70,73 @@ public: void GenerateCircuit() override; }; -class FastFindOwnElementStub : public Stub { +class FindOwnElementStub : public Stub { public: // 3 : 3 means argument counts - explicit FastFindOwnElementStub(Circuit *circuit) : Stub("FastFindOwnElement", 3, circuit) {} - ~FastFindOwnElementStub() = default; - NO_MOVE_SEMANTIC(FastFindOwnElementStub); - NO_COPY_SEMANTIC(FastFindOwnElementStub); + explicit FindOwnElementStub(Circuit *circuit) : Stub("FindOwnElement", 3, circuit) {} + ~FindOwnElementStub() = default; + NO_MOVE_SEMANTIC(FindOwnElementStub); + NO_COPY_SEMANTIC(FindOwnElementStub); void GenerateCircuit() override; }; -class FastGetElementStub : public Stub { +class GetElementStub : public Stub { public: // 3 : 3 means argument counts - explicit FastGetElementStub(Circuit *circuit) : Stub("FastGetElement", 3, circuit) {} - ~FastGetElementStub() = default; - NO_MOVE_SEMANTIC(FastGetElementStub); - NO_COPY_SEMANTIC(FastGetElementStub); + explicit GetElementStub(Circuit *circuit) : Stub("GetElement", 3, circuit) {} + ~GetElementStub() = default; + NO_MOVE_SEMANTIC(GetElementStub); + NO_COPY_SEMANTIC(GetElementStub); void GenerateCircuit() override; }; -class FastFindOwnElement2Stub : public Stub { +class FindOwnElement2Stub : public Stub { public: // 6 : 6 means argument counts - explicit FastFindOwnElement2Stub(Circuit *circuit) : Stub("FastFindOwnElement2", 6, circuit) {} - ~FastFindOwnElement2Stub() = default; - NO_MOVE_SEMANTIC(FastFindOwnElement2Stub); - NO_COPY_SEMANTIC(FastFindOwnElement2Stub); + explicit FindOwnElement2Stub(Circuit *circuit) : Stub("FindOwnElement2", 6, circuit) {} + ~FindOwnElement2Stub() = default; + NO_MOVE_SEMANTIC(FindOwnElement2Stub); + NO_COPY_SEMANTIC(FindOwnElement2Stub); void GenerateCircuit() override; }; -class FastSetElementStub : public Stub { +class SetElementStub : public Stub { public: // 5 : 5 means argument counts - explicit FastSetElementStub(Circuit *circuit) : Stub("FastSetElement", 5, circuit) {} - ~FastSetElementStub() = default; - NO_MOVE_SEMANTIC(FastSetElementStub); - NO_COPY_SEMANTIC(FastSetElementStub); + explicit SetElementStub(Circuit *circuit) : Stub("SetElement", 5, circuit) {} + ~SetElementStub() = default; + NO_MOVE_SEMANTIC(SetElementStub); + NO_COPY_SEMANTIC(SetElementStub); void GenerateCircuit() override; }; -class FastGetPropertyByIndexStub : public Stub { +class GetPropertyByIndexStub : public Stub { public: // 3 : 3 means argument counts - explicit FastGetPropertyByIndexStub(Circuit *circuit) : Stub("FastGetPropertyByIndex", 3, circuit) {} - ~FastGetPropertyByIndexStub() = default; - NO_MOVE_SEMANTIC(FastGetPropertyByIndexStub); - NO_COPY_SEMANTIC(FastGetPropertyByIndexStub); + explicit GetPropertyByIndexStub(Circuit *circuit) : Stub("GetPropertyByIndex", 3, circuit) {} + ~GetPropertyByIndexStub() = default; + NO_MOVE_SEMANTIC(GetPropertyByIndexStub); + NO_COPY_SEMANTIC(GetPropertyByIndexStub); void GenerateCircuit() override; }; -class FastSetPropertyByIndexStub : public Stub { +class SetPropertyByIndexStub : public Stub { public: // 4 : 4 means argument counts - explicit FastSetPropertyByIndexStub(Circuit *circuit) : Stub("FastSetPropertyByIndex", 4, circuit) {} - ~FastSetPropertyByIndexStub() = default; - NO_MOVE_SEMANTIC(FastSetPropertyByIndexStub); - NO_COPY_SEMANTIC(FastSetPropertyByIndexStub); + explicit SetPropertyByIndexStub(Circuit *circuit) : Stub("SetPropertyByIndex", 4, circuit) {} + ~SetPropertyByIndexStub() = default; + NO_MOVE_SEMANTIC(SetPropertyByIndexStub); + NO_COPY_SEMANTIC(SetPropertyByIndexStub); void GenerateCircuit() override; }; -class FastGetPropertyByNameStub : public Stub { +class GetPropertyByNameStub : public Stub { public: // 3 : 3 means argument counts - explicit FastGetPropertyByNameStub(Circuit *circuit) : Stub("FastGetPropertyByName", 3, circuit) {} - ~FastGetPropertyByNameStub() = default; - NO_MOVE_SEMANTIC(FastGetPropertyByNameStub); - NO_COPY_SEMANTIC(FastGetPropertyByNameStub); + explicit GetPropertyByNameStub(Circuit *circuit) : Stub("GetPropertyByName", 3, circuit) {} + ~GetPropertyByNameStub() = default; + NO_MOVE_SEMANTIC(GetPropertyByNameStub); + NO_COPY_SEMANTIC(GetPropertyByNameStub); void GenerateCircuit() override; }; diff --git a/ecmascript/compiler/llvm_codegen.cpp b/ecmascript/compiler/llvm_codegen.cpp index f58bef33abafa3372e9b0865dffeefa0f2ccee83..d2139f5288f233bd06adfb83b744d40648652c8a 100644 --- a/ecmascript/compiler/llvm_codegen.cpp +++ b/ecmascript/compiler/llvm_codegen.cpp @@ -43,10 +43,11 @@ void LLVMModuleAssembler::CopyAssembleCodeToModule(StubModule *module) module->SetStubEntry(i, stubEntry - codeBuff); } } + assembler_.Disassemble(); auto codeSize = assembler_.GetCodeSize(); - - MachineCode *code = reinterpret_cast(new char(sizeof(MachineCode) + codeSize)); + MachineCode *code = + reinterpret_cast(new uint64_t[(MachineCode::SIZE + codeSize) / sizeof(uint64_t) + 1]); code->SetInstructionSizeInBytes(nullptr, JSTaggedValue(codeSize), SKIP_BARRIER); code->SetData(reinterpret_cast(codeBuff), codeSize); module->SetCode(code); diff --git a/ecmascript/compiler/llvm_codegen.h b/ecmascript/compiler/llvm_codegen.h index f55463005b7d77852781413f352afa22d68b9784..63677de5089306fd13a342094f8210e043270ffb 100644 --- a/ecmascript/compiler/llvm_codegen.h +++ b/ecmascript/compiler/llvm_codegen.h @@ -38,7 +38,8 @@ private: class LLVMModuleAssembler { public: - explicit LLVMModuleAssembler(LLVMStubModule *module) : stubmodule_(module), assembler_(module->GetModule()) {} + explicit LLVMModuleAssembler(LLVMStubModule *module, const char* triple) + : stubmodule_(module), assembler_(module->GetModule(), triple) {} void AssembleModule(); void CopyAssembleCodeToModule(panda::ecmascript::StubModule *module); @@ -47,4 +48,4 @@ private: LLVMAssembler assembler_; }; } // namespace kungfu -#endif // ECMASCRIPT_COMPILER_LLVM_CODEGEN_H \ No newline at end of file +#endif // ECMASCRIPT_COMPILER_LLVM_CODEGEN_H diff --git a/ecmascript/compiler/llvm_ir_builder.cpp b/ecmascript/compiler/llvm_ir_builder.cpp index 388aed1e90bc7dc1f42f16bcf19e5c5b46ed1fb0..cba0feb9bb48248bce129ce8c2b92aeebb390943 100644 --- a/ecmascript/compiler/llvm_ir_builder.cpp +++ b/ecmascript/compiler/llvm_ir_builder.cpp @@ -15,8 +15,6 @@ #include "ecmascript/compiler/llvm_ir_builder.h" -#include - #include "ecmascript/compiler/circuit.h" #include "ecmascript/compiler/fast_stub.h" #include "ecmascript/compiler/gate.h" @@ -25,32 +23,16 @@ #include "ecmascript/js_thread.h" #include "llvm/IR/Instructions.h" #include "llvm/Support/Host.h" -#include "llvm_mcjit_engine.h" #include "securec.h" #include "utils/logger.h" namespace kungfu { std::unordered_map g_values = {}; -LLVMIRBuilder::LLVMIRBuilder(const std::vector> *schedule, const Circuit *circuit) - : schedule_(schedule), circuit_(circuit) -{ - module_ = LLVMModuleCreateWithName("simple_module"); - LLVMSetTarget(module_, "x86_64-unknown-linux-gnu"); - builder_ = LLVMCreateBuilder(); - context_ = LLVMGetGlobalContext(); - LLVMTypeRef paramTys[] = { - LLVMInt32Type(), - }; - function_ = LLVMAddFunction(module_, "foo", LLVMFunctionType(LLVMInt32Type(), paramTys, 1, 0)); - bbIdMapBb_.clear(); -} - LLVMIRBuilder::LLVMIRBuilder(const std::vector> *schedule, const Circuit *circuit, LLVMModuleRef module, LLVMValueRef function) : schedule_(schedule), circuit_(circuit), module_(module), function_(function) { - LLVMSetTarget(module_, "x86_64-unknown-linux-gnu"); builder_ = LLVMCreateBuilder(); context_ = LLVMGetGlobalContext(); bbIdMapBb_.clear(); @@ -61,7 +43,6 @@ LLVMIRBuilder::LLVMIRBuilder(const std::vector> *schedule : schedule_(schedule), circuit_(circuit), module_(module->GetModule()), function_(function), stubModule_(module) { - LLVMSetTarget(module_, "x86_64-unknown-linux-gnu"); builder_ = LLVMCreateBuilder(); context_ = LLVMGetGlobalContext(); bbIdMapBb_.clear(); @@ -740,8 +721,6 @@ void LLVMIRBuilder::VisitGoto(int block, int bbOut) void LLVMIRBuilder::VisitInt32Constant(AddrShift gate, int32_t value) const { LLVMValueRef llvmValue = LLVMConstInt(LLVMInt32Type(), value, 0); - LLVMTFBuilderBasicBlockImpl *impl = currentBb_->GetImpl(); - impl->values_[gate] = llvmValue; g_values[gate] = llvmValue; char *str = LLVMPrintValueToString(llvmValue); LOG_ECMA(INFO) << "VisitInt32Constant set gate:" << gate << " value:" << value; @@ -751,8 +730,6 @@ void LLVMIRBuilder::VisitInt32Constant(AddrShift gate, int32_t value) const void LLVMIRBuilder::VisitInt64Constant(AddrShift gate, int64_t value) const { LLVMValueRef llvmValue = LLVMConstInt(LLVMInt64Type(), value, 0); - LLVMTFBuilderBasicBlockImpl *impl = currentBb_->GetImpl(); - impl->values_[gate] = llvmValue; g_values[gate] = llvmValue; char *str = LLVMPrintValueToString(llvmValue); LOG_ECMA(INFO) << "VisitInt64Constant set gate:" << gate << " value:" << value; @@ -762,8 +739,6 @@ void LLVMIRBuilder::VisitInt64Constant(AddrShift gate, int64_t value) const void LLVMIRBuilder::VisitFloat64Constant(AddrShift gate, double value) const { LLVMValueRef llvmValue = LLVMConstReal(LLVMDoubleType(), value); - LLVMTFBuilderBasicBlockImpl *impl = currentBb_->GetImpl(); - impl->values_[gate] = llvmValue; g_values[gate] = llvmValue; char *str = LLVMPrintValueToString(llvmValue); LOG_ECMA(INFO) << "VisitFloat64Constant set gate:" << gate << " value:" << value; @@ -775,8 +750,6 @@ void LLVMIRBuilder::VisitParameter(AddrShift gate) const int argth = circuit_->LoadGatePtrConst(gate)->GetBitField(); LOG_ECMA(INFO) << " Parameter value" << argth; LLVMValueRef value = LLVMGetParam(function_, argth); - LLVMTFBuilderBasicBlockImpl *impl = currentBb_->GetImpl(); - impl->values_[gate] = value; g_values[gate] = value; LOG_ECMA(INFO) << "VisitParameter set gate:" << gate << " value:" << value; // NOTE: caller put args, otherwise crash @@ -791,8 +764,7 @@ void LLVMIRBuilder::VisitParameter(AddrShift gate) const void LLVMIRBuilder::VisitBranch(AddrShift gate, AddrShift cmp, int btrue, int bfalse) { LOG_ECMA(INFO) << "cmp gate:" << cmp; - LLVMTFBuilderBasicBlockImpl *impl = EnsureLLVMBBImpl(currentBb_); - if ((impl->values_.count(cmp) == 0) && (g_values.count(cmp) == 0)) { + if (g_values.count(cmp) == 0) { LOG_ECMA(ERROR) << "Branch condition gate is nullptr!"; return; } @@ -1130,12 +1102,10 @@ void LLVMIRBuilder::VisitCastDoubleToInt(AddrShift gate, AddrShift e1) const LOG_ECMA(INFO) << "result: " << LLVMPrintValueToString(result); } -LLVMStubModule::LLVMStubModule(const char *name) +LLVMStubModule::LLVMStubModule(const char *name, const char *triple) { - module_ = LLVMModuleCreateWithName("fast_stubs"); -#ifdef PANDA_TARGET_AMD64 - LLVMSetTarget(module_, "x86_64-unknown-linux-gnu"); -#endif + module_ = LLVMModuleCreateWithName(name); + LLVMSetTarget(module_, triple); } void LLVMStubModule::Initialize() diff --git a/ecmascript/compiler/llvm_ir_builder.h b/ecmascript/compiler/llvm_ir_builder.h index cd703fd7e3fac8cbf432c7787c183e803a7fddcb..ed35e2d0f34af2ec3ede32fbdd4a29cdfa8df380 100644 --- a/ecmascript/compiler/llvm_ir_builder.h +++ b/ecmascript/compiler/llvm_ir_builder.h @@ -96,7 +96,6 @@ struct NotMergedPhiDesc { struct LLVMTFBuilderBasicBlockImpl { LLVMBasicBlockRef llvm_bb_ = nullptr; LLVMBasicBlockRef continuation = nullptr; - std::unordered_map values_ = {}; bool started = false; bool ended = false; std::vector not_merged_phis; @@ -104,7 +103,7 @@ struct LLVMTFBuilderBasicBlockImpl { class LLVMStubModule { public: - explicit LLVMStubModule(const char *name); + explicit LLVMStubModule(const char *name, const char *triple); ~LLVMStubModule() = default; void Initialize(); @@ -140,7 +139,6 @@ private: class LLVMIRBuilder { public: - explicit LLVMIRBuilder(const std::vector> *schedule, const Circuit *circuit); explicit LLVMIRBuilder(const std::vector> *schedule, const Circuit *circuit, LLVMModuleRef module, LLVMValueRef function); explicit LLVMIRBuilder(const std::vector> *schedule, const Circuit *circuit, diff --git a/ecmascript/compiler/llvm_mcjit_engine.cpp b/ecmascript/compiler/llvm_mcjit_engine.cpp index 7d00ed89f9e68ec0bd859ed11517ce2cb32d88ea..bb3e4ec9a8d77940d802ef226d9b98930ff30163 100644 --- a/ecmascript/compiler/llvm_mcjit_engine.cpp +++ b/ecmascript/compiler/llvm_mcjit_engine.cpp @@ -77,7 +77,6 @@ static LLVMBool RoundTripFinalizeMemory(void *object, [[maybe_unused]] char **er static void RoundTripDestroy(void *object) { std::cout << "RoundTripDestroy object " << object << " - " << std::endl; - delete static_cast(object); } void LLVMAssembler::UseRoundTripSectionMemoryManager() @@ -114,8 +113,8 @@ void LLVMAssembler::BuildAndRunPasses() const std::cout << "BuildAndRunPasses + " << std::endl; } -LLVMAssembler::LLVMAssembler(LLVMModuleRef module): module_(module), engine_(nullptr), - hostTriple_(""), error_(nullptr) +LLVMAssembler::LLVMAssembler(LLVMModuleRef module, const char* triple): module_(module), engine_(nullptr), + hostTriple_(triple), error_(nullptr) { Initialize(); InitMember(); @@ -140,15 +139,31 @@ void LLVMAssembler::Run() void LLVMAssembler::Initialize() { -#if defined(PANDA_TARGET_AMD64) - LLVMInitializeX86TargetInfo(); - LLVMInitializeX86TargetMC(); - LLVMInitializeX86Disassembler(); - /* this method must be called, ohterwise "Target does not support MC emission" */ - LLVMInitializeX86AsmPrinter(); - LLVMInitializeX86AsmParser(); - LLVMInitializeX86Target(); -#endif + if (hostTriple_.compare(AMD64_TRIPLE) == 0) { + LLVMInitializeX86TargetInfo(); + LLVMInitializeX86TargetMC(); + LLVMInitializeX86Disassembler(); + /* this method must be called, ohterwise "Target does not support MC emission" */ + LLVMInitializeX86AsmPrinter(); + LLVMInitializeX86AsmParser(); + LLVMInitializeX86Target(); + } else if (hostTriple_.compare(ARM64_TRIPLE) == 0) { + LLVMInitializeAArch64TargetInfo(); + LLVMInitializeAArch64TargetMC(); + LLVMInitializeAArch64Disassembler(); + LLVMInitializeAArch64AsmPrinter(); + LLVMInitializeAArch64AsmParser(); + LLVMInitializeAArch64Target(); + } else if (hostTriple_.compare(ARM32_TRIPLE) == 0) { + LLVMInitializeARMTargetInfo(); + LLVMInitializeARMTargetMC(); + LLVMInitializeARMDisassembler(); + LLVMInitializeARMAsmPrinter(); + LLVMInitializeARMAsmParser(); + LLVMInitializeARMTarget(); + } else { + UNREACHABLE(); + } llvm::linkAllBuiltinGCs(); LLVMInitializeMCJITCompilerOptions(&options_, sizeof(options_)); options_.OptLevel = 2; // opt level 2 @@ -166,7 +181,7 @@ static const char *SymbolLookupCallback([[maybe_unused]] void *disInfo, [[maybe_ void LLVMAssembler::Disassemble(std::map addr2name) const { - LLVMDisasmContextRef dcr = LLVMCreateDisasm("x86_64-unknown-linux-gnu", nullptr, 0, nullptr, SymbolLookupCallback); + LLVMDisasmContextRef dcr = LLVMCreateDisasm(hostTriple_.c_str(), nullptr, 0, nullptr, SymbolLookupCallback); std::cout << "========================================================================" << std::endl; for (auto it : codeInfo_.GetCodeInfo()) { uint8_t *byteSp; diff --git a/ecmascript/compiler/llvm_mcjit_engine.h b/ecmascript/compiler/llvm_mcjit_engine.h index 3eaafb206b59a784256b55d05f8687304a4e758c..1e0cb6edd35d838ccd9f7ffc47de0cdd4830cc2c 100644 --- a/ecmascript/compiler/llvm_mcjit_engine.h +++ b/ecmascript/compiler/llvm_mcjit_engine.h @@ -38,35 +38,35 @@ struct CodeInfo { using ByteBuffer = std::vector; using BufferList = std::list; using StringList = std::list; - CodeInfo() : machineCode(nullptr), codeBufferPos(0), stackMapsSection_(nullptr) + CodeInfo() : machineCode_(nullptr), codeBufferPos_(0), stackMapsSection_(nullptr) { Reset(); static constexpr int prot = PROT_READ | PROT_WRITE | PROT_EXEC; // NOLINT(hicpp-signed-bitwise) static constexpr int flags = MAP_ANONYMOUS | MAP_SHARED; // NOLINT(hicpp-signed-bitwise) - machineCode = static_cast(mmap(nullptr, MAX_MACHINE_CODE_SIZE, prot, flags, -1, 0)); - std::cerr << std::hex << "machineCode : " << reinterpret_cast(machineCode) << std::endl; + machineCode_ = static_cast(mmap(nullptr, MAX_MACHINE_CODE_SIZE, prot, flags, -1, 0)); } ~CodeInfo() { Reset(); - munmap(machineCode, MAX_MACHINE_CODE_SIZE); + munmap(machineCode_, MAX_MACHINE_CODE_SIZE); + machineCode_ = nullptr; } uint8_t *AllocaCodeSection(uintptr_t size, const char *sectionName) { uint8_t *addr = nullptr; - if (codeBufferPos + size > MAX_MACHINE_CODE_SIZE) { - std::cerr << std::hex << "AllocaCodeSection failed alloc codeBufferPos:" << codeBufferPos + if (codeBufferPos_ + size > MAX_MACHINE_CODE_SIZE) { + std::cerr << std::hex << "AllocaCodeSection failed alloc codeBufferPos_:" << codeBufferPos_ << " size:" << size << " larger MAX_MACHINE_CODE_SIZE:" << MAX_MACHINE_CODE_SIZE << std::endl; return nullptr; } std::cout << "AllocaCodeSection size:" << size << std::endl; - std::vector codeBuffer(machineCode[codeBufferPos], size); + std::vector codeBuffer(machineCode_[codeBufferPos_], size); std::cout << " codeBuffer size: " << codeBuffer.size() << std::endl; codeSectionNames_.push_back(sectionName); - addr = machineCode + codeBufferPos; + addr = machineCode_ + codeBufferPos_; std::cout << "AllocaCodeSection addr:" << std::hex << reinterpret_cast(addr) << std::endl; codeInfo_.push_back({addr, size}); - codeBufferPos += size; + codeBufferPos_ += size; return addr; } @@ -91,7 +91,7 @@ struct CodeInfo { dataSectionList_.clear(); dataSectionNames_.clear(); codeSectionNames_.clear(); - codeBufferPos = 0; + codeBufferPos_ = 0; } uint8_t *GetStackMapsSection() const @@ -105,21 +105,21 @@ struct CodeInfo { int GetCodeSize() const { - return codeBufferPos; + return codeBufferPos_; } uint8_t *GetCodeBuff() const { - return machineCode; + return machineCode_; } private: BufferList dataSectionList_ {}; StringList dataSectionNames_ {}; StringList codeSectionNames_ {}; - uint8_t *machineCode; + uint8_t *machineCode_; const size_t MAX_MACHINE_CODE_SIZE = (1 << 20); // 1M - int codeBufferPos = 0; + int codeBufferPos_ = 0; /* for asssembler */ std::vector> codeInfo_ {}; /* stack map */ @@ -127,7 +127,7 @@ private: }; class LLVMAssembler { public: - explicit LLVMAssembler(LLVMModuleRef module); + explicit LLVMAssembler(LLVMModuleRef module, const char* triple); virtual ~LLVMAssembler(); void Run(); const LLVMExecutionEngineRef &GetEngine() @@ -149,6 +149,10 @@ public: return codeInfo_.GetCodeBuff(); } + const char *AMD64_TRIPLE = "x86_64-unknown-linux-gnu"; + const char *ARM64_TRIPLE = "aarch64-unknown-linux-gnu"; + const char *ARM32_TRIPLE = "arm-unknown-linux-gnu"; + private: void UseRoundTripSectionMemoryManager(); bool BuildMCJITEngine(); diff --git a/ecmascript/compiler/options.h.erb b/ecmascript/compiler/options.h.erb new file mode 100644 index 0000000000000000000000000000000000000000..55988ac120897155a28c9fc032aadc13b600b578 --- /dev/null +++ b/ecmascript/compiler/options.h.erb @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Autogenerated file -- DO NOT EDIT! + +#ifndef PANDA_<%= Common::module.name.upcase %>_OPTIONS_GEN_H_ +#define PANDA_<%= Common::module.name.upcase %>_OPTIONS_GEN_H_ + +#include "utils/pandargs.h" + +#include +#include +#include +#include + +namespace <%= Common::module.namespace %> { + +class <%= Common::module.name %>_Options { +public: + class Error { + public: + explicit Error(std::string msg) : msg_(std::move(msg)) {} + + std::string GetMessage() { + return msg_; + } + private: + std::string msg_; + }; + + explicit <%= Common::module.name %>_Options(const std::string &exe_path) : exe_dir_(GetExeDir(exe_path)) {} + + void AddOptions(PandArgParser *parser) { +% Common::options.each do |op| + parser->Add(&<%= op.field_name %>); +% end + } + +% Common::options.each do |op| + <%= op.type %> <%= op.getter_name %>() const { + return <%= op.field_name %>.GetValue(); + } + + void <%= op.setter_name %>(<%= op.type %> value) { + <%= op.field_name %>.SetValue(<%= op.type == 'std::string' || op.type == 'arg_list_t' ? 'std::move(value)' : 'value' %>); + } + + bool WasSet<%= op.name.split(Regexp.union(['-','.'])).map(&:capitalize).join %>() const { + return <%= op.field_name %>.WasSet(); + } + +% end +// NOLINTNEXTLINE(readability-function-size) + std::optional Validate() const { +% Common::options.each do |op| +% next unless defined? op.possible_values +% is_string = op.type == 'std::string' || op.type == 'arg_list_t' +% possible_values = op.possible_values.map { |e| is_string ? Common::to_raw(e) : e }.join(', ') + { + std::unordered_set<<%= is_string ? "std::string" : op.type %>> possible_values{<%= possible_values %>}; +% if op.type != 'arg_list_t' + std::vector<<%= op.type %>> values{<%= op.field_name %>.GetValue()}; +% else + const auto &values = <%= op.field_name %>.GetValue(); +% end + for (const auto &value : values) { + if (possible_values.find(value) == possible_values.cend()) { + return Error("argument --<%= op.name %>: invalid value: '" + <%= is_string ? "value" : "std::to_string(value)" %> + \ + R"('. Possible values: <%= op.possible_values %>)"); + } + } + } +% end + return {}; + } + +private: + static std::string GetExeDir(const std::string &exe_path) { + auto pos = exe_path.find_last_of('/'); + return exe_path.substr(0, pos); + } + +% Common::options.each do |op| +% next unless op.need_default_constant + static constexpr <%= op.type %> <%= op.default_constant_name %> = <%= op.default %>; + +% end + std::string exe_dir_; +% Common::options.each do |op| +% if defined? op.delimiter + PandArg<<%= op.type %>> <%= op.field_name %>{"<%= op.name %>", <%= op.default_value %>, <%= op.full_description %>, "<%= op.delimiter %>"}; +% elsif defined? op.range +% min, max = op.range.match('(\d+)\D*(\d+)').captures; +% if op.default.to_i > max.to_i || op.default.to_i < min.to_i +% abort "FAILED: Default value of argument " + op.name + " has out of range default parameter" +% end + PandArg<<%= op.type %>> <%= op.field_name %>{"<%= op.name %>", <%= op.default_value %>, <%= op.full_description %>, <%= min.to_i %>, <%= max.to_i %>}; +% else + PandArg<<%= op.type %>> <%= op.field_name %>{"<%= op.name %>", <%= op.default_value %>, <%= op.full_description %>}; +% end +% end +}; + +} // namespace <%= Common::module.namespace %> + +#endif // PANDA_<%= Common::module.name.upcase %>_OPTIONS_GEN_H_ diff --git a/ecmascript/compiler/stub.cpp b/ecmascript/compiler/stub.cpp index 4f9a0eeb822ae9ebd1864f0031869ce3e2a43b5b..d5e801fa9e7669ca32c928588dcb450b99234f57 100644 --- a/ecmascript/compiler/stub.cpp +++ b/ecmascript/compiler/stub.cpp @@ -378,7 +378,7 @@ AddrShift Stub::FindElementFromNumberDictionary(AddrShift thread, AddrShift elem { auto env = GetEnvironment(); Label subentry(env); - [[maybe_unused]] SubCircuitScope subCircuit(env, &subentry); + env->PushCurrentLabel(&subentry); DEFVARIABLE(result, INT32_TYPE, GetInteger32Constant(-1)); Label exit(env); AddrShift capcityoffset = @@ -427,14 +427,16 @@ AddrShift Stub::FindElementFromNumberDictionary(AddrShift thread, AddrShift elem count = Int32Add(*count, GetInteger32Constant(1)); LoopEnd(&loopHead); Bind(&exit); - return *result; + auto ret = *result; + env->PopCurrentLabel(); + return ret; } AddrShift Stub::IsMatchInNumberDictionary(AddrShift key, AddrShift other) { auto env = GetEnvironment(); Label entry(env); - [[maybe_unused]] SubCircuitScope subCircuit(env, &entry); + env->PushCurrentLabel(&entry); Label exit(env); DEFVARIABLE(result, BOOL_TYPE, FalseConstant()); Label isHole(env); @@ -464,14 +466,16 @@ AddrShift Stub::IsMatchInNumberDictionary(AddrShift key, AddrShift other) Bind(&keyNotInt); Jump(&exit); Bind(&exit); - return *result; + auto ret = *result; + env->PopCurrentLabel(); + return ret; } AddrShift Stub::GetKeyFromNumberDictionary(AddrShift elements, AddrShift entry) { auto env = GetEnvironment(); Label subentry(env); - [[maybe_unused]] SubCircuitScope subCircuit(env, &subentry); + env->PushCurrentLabel(&subentry); Label exit(env); DEFVARIABLE(result, TAGGED_TYPE, GetUndefinedConstant()); Label ltZero(env); @@ -493,7 +497,9 @@ AddrShift Stub::GetKeyFromNumberDictionary(AddrShift elements, AddrShift entry) result = GetValueFromTaggedArray(elements, arrayIndex); Jump(&exit); Bind(&exit); - return *result; + auto ret = *result; + env->PopCurrentLabel(); + return ret; } // int TaggedHashTable::FindEntry(const JSTaggedValue &key) in tagged_hash_table-inl.h @@ -501,7 +507,7 @@ AddrShift Stub::FindEntryFromNameDictionary(AddrShift thread, AddrShift elements { auto env = GetEnvironment(); Label funcEntry(env); - [[maybe_unused]] SubCircuitScope subCircuit(env, &funcEntry); + env->PushCurrentLabel(&funcEntry); Label exit(env); DEFVARIABLE(result, INT32_TYPE, GetInteger32Constant(-1)); AddrShift capcityoffset = @@ -594,14 +600,16 @@ AddrShift Stub::FindEntryFromNameDictionary(AddrShift thread, AddrShift elements } } Bind(&exit); - return *result; + auto ret = *result; + env->PopCurrentLabel(); + return ret; } AddrShift Stub::JSObjectGetProperty(AddrShift obj, AddrShift hClass, AddrShift attr) { auto env = GetEnvironment(); Label entry(env); - [[maybe_unused]] SubCircuitScope subCircuit(env, &entry); + env->PushCurrentLabel(&entry); Label exit(env); DEFVARIABLE(result, TAGGED_TYPE, GetUndefinedConstant()); Label inlinedProp(env); @@ -631,7 +639,9 @@ AddrShift Stub::JSObjectGetProperty(AddrShift obj, AddrShift hClass, AddrShift a } } Bind(&exit); - return *result; + auto ret = *result; + env->PopCurrentLabel(); + return ret; } void Stub::ThrowTypeAndReturn(AddrShift thread, int messageId, AddrShift val) @@ -646,7 +656,7 @@ AddrShift Stub::TaggedToRepresentation(AddrShift value) { auto env = GetEnvironment(); Label entry(env); - [[maybe_unused]] SubCircuitScope subCircuit(env, &entry); + env->PushCurrentLabel(&entry); Label exit(env); DEFVARIABLE(resultRep, INT64_TYPE, GetWord64Constant(static_cast(panda::ecmascript::Representation::OBJECT))); @@ -676,14 +686,16 @@ AddrShift Stub::TaggedToRepresentation(AddrShift value) } } Bind(&exit); - return *resultRep; + auto ret = *resultRep; + env->PopCurrentLabel(); + return ret; } AddrShift Stub::UpdateRepresention(AddrShift oldRep, AddrShift value) { auto env = GetEnvironment(); Label entry(env); - [[maybe_unused]] SubCircuitScope subCircuit(env, &entry); + env->PushCurrentLabel(&entry); Label exit(env); DEFVARIABLE(resultRep, INT64_TYPE, oldRep); Label isMixedRep(env); @@ -770,7 +782,9 @@ AddrShift Stub::UpdateRepresention(AddrShift oldRep, AddrShift value) } } Bind(&exit); - return *resultRep; + auto ret = *resultRep; + env->PopCurrentLabel(); + return ret; } void Stub::UpdateAndStoreRepresention(AddrShift hclass, AddrShift value) diff --git a/ecmascript/compiler/stub.h b/ecmascript/compiler/stub.h index 1260e34bb681ab99c7c9d6aabc655fec551dde4d..186759aba41aee54f8dcf93f57caf1040d4f965a 100644 --- a/ecmascript/compiler/stub.h +++ b/ecmascript/compiler/stub.h @@ -242,19 +242,19 @@ public: Label GetLabelFromSelector(AddrShift sel) { - LabelImpl *rawlabel = phi_to_labels[sel]; + LabelImpl *rawlabel = phi_to_labels_[sel]; return Label(rawlabel); } void AddSelectorToLabel(AddrShift sel, Label label) { - phi_to_labels[sel] = label.GetRawLabel(); + phi_to_labels_[sel] = label.GetRawLabel(); } LabelImpl *NewLabel(Environment *env, AddrShift control = -1) { auto impl = new LabelImpl(env, control); - rawlabels_.push_back(impl); + rawlabels_.emplace_back(impl); return impl; } @@ -286,7 +286,7 @@ public: Label *currentLabel_ {nullptr}; Circuit *circuit_; CircuitBuilder builder_; - std::unordered_map phi_to_labels; + std::unordered_map phi_to_labels_; std::vector arguments_; Label entry_; std::vector rawlabels_; @@ -1346,6 +1346,11 @@ public: return nextVariableId_++; } + std::string GetMethodName() const + { + return methodName_; + } + private: Environment env_; std::string methodName_; diff --git a/ecmascript/compiler/stub_aot_compiler.cpp b/ecmascript/compiler/stub_aot_compiler.cpp index 530c67699dd77f3d64112f581366211cf5368d53..d1b3fffac94b95dff64689e4b4658f106d999858 100644 --- a/ecmascript/compiler/stub_aot_compiler.cpp +++ b/ecmascript/compiler/stub_aot_compiler.cpp @@ -14,10 +14,14 @@ */ #include "stub_aot_compiler.h" + #include #include #include #include "fast_stub.h" +#include "generated/stub_aot_options_gen.h" +#include "libpandabase/utils/pandargs.h" +#include "libpandabase/utils/span.h" #include "llvm_codegen.h" #include "scheduler.h" #include "stub.h" @@ -99,15 +103,17 @@ public: } }; -void StubAotCompiler::BuildStubModule(panda::ecmascript::StubModule *module) +void StubAotCompiler::BuildStubModuleAndSave(const char *triple, panda::ecmascript::StubModule *module, + const std::string &filename) { - LLVMStubModule stubModule("fast_stubs"); + LLVMStubModule stubModule("fast_stubs", triple); stubModule.Initialize(); for (int i = 0; i < FAST_STUB_MAXCOUNT; i++) { - auto Stub = stubs_[i]; - if (Stub != nullptr) { - Stub->GenerateCircuit(); - auto circuit = Stub->GetEnvironment()->GetCircuit(); + auto stub = stubs_[i]; + if (stub != nullptr) { + std::cout << "Stub Name: " << stub->GetMethodName() << std::endl; + stub->GenerateCircuit(); + auto circuit = stub->GetEnvironment()->GetCircuit(); PassPayLoad data(circuit, &stubModule); PassRunner pipeline(&data); pipeline.RunPass(); @@ -116,68 +122,63 @@ void StubAotCompiler::BuildStubModule(panda::ecmascript::StubModule *module) } } - LLVMModuleAssembler assembler(&stubModule); + LLVMModuleAssembler assembler(&stubModule, triple); assembler.AssembleModule(); assembler.CopyAssembleCodeToModule(module); + + module->Save(filename); } } // namespace kungfu -int main(int argc, char *argv[]) +#define SET_STUB_TO_MODULE(module, name) \ + kungfu::Circuit name##Circuit; \ + kungfu::name##Stub name##Stub(& name##Circuit); \ + module.SetStub(FAST_STUB_ID(name), & name##Stub); + + +#define SET_ALL_STUB_TO_MODEULE(module) \ + SET_STUB_TO_MODULE(module, FastAdd) \ + SET_STUB_TO_MODULE(module, FastSub) \ + SET_STUB_TO_MODULE(module, FastMul) \ + SET_STUB_TO_MODULE(module, FastDiv) \ + SET_STUB_TO_MODULE(module, FindOwnElement) \ + SET_STUB_TO_MODULE(module, GetElement) \ + SET_STUB_TO_MODULE(module, FindOwnElement2) \ + SET_STUB_TO_MODULE(module, SetElement) \ + SET_STUB_TO_MODULE(module, GetPropertyByIndex) \ + SET_STUB_TO_MODULE(module, SetPropertyByIndex) \ + SET_STUB_TO_MODULE(module, GetPropertyByName) + +int main(const int argc, const char **argv) { - int opt; - // 3 means ark_stub_opt -f stub.m - if (argc != 3) { - std::cerr << "Usage: " << argv[0] << " -f stub.m" << std::endl; - return -1; - } - std::string moduleFilename; - while ((opt = getopt(argc, argv, "f:")) != -1) { - switch (opt) { - case 'f': - moduleFilename += optarg; - break; - default: /* '?' */ - std::cerr << "Usage: " << argv[0] << " -f stub.m" << std::endl; - return -1; - } + panda::Span sp(argv, argc); + panda::Stub_Aot_Options stubOptions(sp[0]); + panda::PandArg help("help", false, "Print this message and exit"); + panda::PandArg options("options", false, "Print compiler options"); + panda::PandArgParser paParser; + + stubOptions.AddOptions(&paParser); + paParser.Add(&help); + paParser.Add(&options); + + if (!paParser.Parse(argc, argv) || help.GetValue()) { + std::cerr << paParser.GetErrorString() << std::endl; + std::cerr << "Usage: " << "ark_stub_opt" << " [OPTIONS]" << std::endl; + std::cerr << std::endl; + std::cerr << "optional arguments:" << std::endl; + std::cerr << paParser.GetHelpString() << std::endl; + return 1; } + std::string moduleFilename = stubOptions.GetStubOutputFile(); + std::string tripes = stubOptions.GetTargetTriple(); + kungfu::StubAotCompiler mouldeBuilder; + SET_ALL_STUB_TO_MODEULE(mouldeBuilder); + panda::ecmascript::StubModule stubModule; - /* Set Stub into module */ - kungfu::Circuit fastaddCircuit; - kungfu::FastAddStub fastaddStub(&fastaddCircuit); - mouldeBuilder.SetStub(FAST_STUB_ID(FastAdd), &fastaddStub); - - kungfu::Circuit fastsubCircuit; - kungfu::FastSubStub fastsubStub(&fastsubCircuit); - mouldeBuilder.SetStub(FAST_STUB_ID(FastSub), &fastsubStub); - - kungfu::Circuit fastmulCircuit; - kungfu::FastMulStub fastmulStub(&fastmulCircuit); - mouldeBuilder.SetStub(FAST_STUB_ID(FastMul), &fastmulStub); - - kungfu::Circuit fastdivCircuit; - kungfu::FastDivStub fastdivStub(&fastdivCircuit); - mouldeBuilder.SetStub(FAST_STUB_ID(FastDiv), &fastdivStub); - - kungfu::Circuit fastFindOwnElementCircuit; - kungfu::FastFindOwnElementStub fastFindOwnElementStub(&fastFindOwnElementCircuit); - mouldeBuilder.SetStub(FAST_STUB_ID(FindOwnElement), &fastFindOwnElementStub); - - kungfu::Circuit fastGetElementCircuit; - kungfu::FastGetElementStub fastGetElementStub(&fastGetElementCircuit); - mouldeBuilder.SetStub(FAST_STUB_ID(GetElement), &fastGetElementStub); - - kungfu::Circuit fastFindOwnElement2Circuit; - kungfu::FastFindOwnElement2Stub fastFindOwnElement2Stub(&fastFindOwnElement2Circuit); - mouldeBuilder.SetStub(FAST_STUB_ID(FindOwnElement2), &fastFindOwnElement2Stub); - - kungfu::Circuit fastSetElementCircuit; - kungfu::FastSetElementStub fastSetElementStub(&fastSetElementCircuit); - mouldeBuilder.SetStub(FAST_STUB_ID(SetElement), &fastSetElementStub); - - mouldeBuilder.BuildStubModule(&stubModule); - stubModule.Save(moduleFilename); - exit(0); + mouldeBuilder.BuildStubModuleAndSave(tripes.c_str(), &stubModule, moduleFilename); + + std::cout << "BuildStubModuleAndSave success" << std::endl; + return 0; } \ No newline at end of file diff --git a/ecmascript/compiler/stub_aot_compiler.h b/ecmascript/compiler/stub_aot_compiler.h index 736ba00287164470360b19d5290d9418039bbd4f..e3e2aad05782bacc1725400664392f10067d4fc3 100644 --- a/ecmascript/compiler/stub_aot_compiler.h +++ b/ecmascript/compiler/stub_aot_compiler.h @@ -30,9 +30,15 @@ public: for (int i = 0; i < FAST_STUB_MAXCOUNT; i++) { stubs_[i] = nullptr; } - }; - ~StubAotCompiler() = default; - void BuildStubModule(panda::ecmascript::StubModule *module); + } + ~StubAotCompiler() + { + for (int i = 0; i < FAST_STUB_MAXCOUNT; i++) { + stubs_[i] = nullptr; + } + } + void BuildStubModuleAndSave(const char *triple, panda::ecmascript::StubModule *module, + const std::string &filename); void SetStub(int index, Stub *optimizer) { stubs_[index] = optimizer; diff --git a/ecmascript/compiler/stub_aot_options.yaml b/ecmascript/compiler/stub_aot_options.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4d58964a273964c1453f1162179d6362d59c1cc4 --- /dev/null +++ b/ecmascript/compiler/stub_aot_options.yaml @@ -0,0 +1,31 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +module: + name: Stub_Aot + namespace: panda + +options: +- name: stub-output-file + type: std::string + default: stub.m + description: stub aot compiler output file name + +- name: target-triple + type: std::string + default: x86_64-unknown-linux-gnu + possible_values: + - x86_64-unknown-linux-gnu + - arm-unknown-linux-gnu + - aarch64-unknown-linux-gnu + description: stub aot compiler target triple \ No newline at end of file diff --git a/ecmascript/compiler/tests/stub_tests.cpp b/ecmascript/compiler/tests/stub_tests.cpp index c684a94da8171bc183ae22d099ac1600159fd5d8..3101ab9631d4d07f5a1bbb33645ec2f3c306e61b 100644 --- a/ecmascript/compiler/tests/stub_tests.cpp +++ b/ecmascript/compiler/tests/stub_tests.cpp @@ -63,7 +63,7 @@ public: PandaVM *instance {nullptr}; EcmaHandleScope *scope {nullptr}; JSThread *thread {nullptr}; - LLVMStubModule stubModule{"fast_stub"}; + LLVMStubModule stubModule {"fast_stub", "x86_64-unknown-linux-gnu"}; }; HWTEST_F_L0(StubTest, FastLoadElement) @@ -94,7 +94,7 @@ HWTEST_F_L0(StubTest, FastLoadElement) } LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, module, function); llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); auto engine = assembler.GetEngine(); /* exec function */ @@ -166,7 +166,7 @@ HWTEST_F_L0(StubTest, PhiGateTest) } LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, module, function); llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); auto engine = assembler.GetEngine(); /* exec function */ @@ -262,7 +262,7 @@ HWTEST_F_L0(StubTest, LoopTest) } LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, module, function); llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); auto engine = assembler.GetEngine(); @@ -332,7 +332,7 @@ HWTEST_F_L0(StubTest, LoopTest1) } LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, module, function); llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); auto engine = assembler.GetEngine(); /* exec function */ @@ -368,7 +368,7 @@ HWTEST_F_L0(StubTest, FastAddTest) } LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, module, function); llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); auto engine = assembler.GetEngine(); /* exec function */ @@ -409,7 +409,7 @@ HWTEST_F_L0(StubTest, FastSubTest) } LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, module, function); llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); auto engine = assembler.GetEngine(); /* exec function */ @@ -445,7 +445,7 @@ HWTEST_F_L0(StubTest, FastMulTest) } LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, module, function); llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); auto engine = assembler.GetEngine(); @@ -502,7 +502,7 @@ HWTEST_F_L0(StubTest, FastDivTest) } LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, module, function); llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); auto engine = assembler.GetEngine(); auto fn = reinterpret_cast(LLVMGetPointerToGlobal(engine, function)); @@ -535,12 +535,12 @@ HWTEST_F_L0(StubTest, FastDivTest) EXPECT_EQ(res3, expectedG3); } -HWTEST_F_L0(StubTest, FastFindOwnElementStub) +HWTEST_F_L0(StubTest, FindOwnElementStub) { auto module = stubModule.GetModule(); LLVMValueRef findFunction = LLVMGetNamedFunction(module, "FindOwnElement"); Circuit netOfGates; - FastFindOwnElementStub optimizer(&netOfGates); + FindOwnElementStub optimizer(&netOfGates); optimizer.GenerateCircuit(); netOfGates.PrintAllGates(); auto cfg = Scheduler::Run(&netOfGates); @@ -553,16 +553,16 @@ HWTEST_F_L0(StubTest, FastFindOwnElementStub) } LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, &stubModule, findFunction); llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); } -HWTEST_F_L0(StubTest, FastGetElementStub) +HWTEST_F_L0(StubTest, GetElementStub) { auto module = stubModule.GetModule(); LLVMValueRef findFunction = LLVMGetNamedFunction(module, "FindOwnElement"); Circuit netOfGates; - FastFindOwnElementStub findOptimizer(&netOfGates); + FindOwnElementStub findOptimizer(&netOfGates); findOptimizer.GenerateCircuit(); auto cfg = Scheduler::Run(&netOfGates); for (size_t bbIdx = 0; bbIdx < cfg.size(); bbIdx++) { @@ -575,7 +575,7 @@ HWTEST_F_L0(StubTest, FastGetElementStub) LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, &stubModule, findFunction); llvmBuilder.Build(); Circuit getNetOfGates; - FastGetElementStub getOptimizer(&getNetOfGates); + GetElementStub getOptimizer(&getNetOfGates); getOptimizer.GenerateCircuit(); getNetOfGates.PrintAllGates(); auto getCfg = Scheduler::Run(&getNetOfGates); @@ -587,17 +587,17 @@ HWTEST_F_L0(StubTest, FastGetElementStub) } } llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); } -HWTEST_F_L0(StubTest, FastFindOwnElement2Stub) +HWTEST_F_L0(StubTest, FindOwnElement2Stub) { - std::cout << " ------------------------FastFindOwnElement2Stub ---------------------" << std::endl; + std::cout << " ------------------------FindOwnElement2Stub ---------------------" << std::endl; auto module = stubModule.GetModule(); LLVMValueRef function = LLVMGetNamedFunction(module, "FindOwnElement2"); Circuit netOfGates; - FastFindOwnElement2Stub optimizer(&netOfGates); + FindOwnElement2Stub optimizer(&netOfGates); optimizer.GenerateCircuit(); netOfGates.PrintAllGates(); auto cfg = Scheduler::Run(&netOfGates); @@ -610,7 +610,7 @@ HWTEST_F_L0(StubTest, FastFindOwnElement2Stub) } LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, &stubModule, function); llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); auto engine = assembler.GetEngine(); auto *findOwnElement2Ptr = reinterpret_cast( @@ -1166,12 +1166,12 @@ HWTEST_F_L0(StubTest, FastGetPropertyByIndexStub) EXPECT_EQ(resVal.GetNumber(), y); } -HWTEST_F_L0(StubTest, FastSetPropertyByIndexStub) +HWTEST_F_L0(StubTest, SetPropertyByIndexStub) { auto module = stubModule.GetModule(); LLVMValueRef function = LLVMGetNamedFunction(module, "SetPropertyByIndex"); Circuit netOfGates; - FastSetPropertyByIndexStub optimizer(&netOfGates); + SetPropertyByIndexStub optimizer(&netOfGates); optimizer.GenerateCircuit(); netOfGates.PrintAllGates(); bool result = Verifier::Run(&netOfGates); @@ -1186,7 +1186,7 @@ HWTEST_F_L0(StubTest, FastSetPropertyByIndexStub) } LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, &stubModule, function); llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); auto engine = assembler.GetEngine(); auto *setpropertyByIndex = reinterpret_cast( @@ -1206,12 +1206,12 @@ HWTEST_F_L0(StubTest, FastSetPropertyByIndexStub) } } -HWTEST_F_L0(StubTest, FastGetPropertyByNameStub) +HWTEST_F_L0(StubTest, GetPropertyByNameStub) { auto module = stubModule.GetModule(); LLVMValueRef function = LLVMGetNamedFunction(module, "GetPropertyByName"); Circuit netOfGates; - FastGetPropertyByNameStub optimizer(&netOfGates); + GetPropertyByNameStub optimizer(&netOfGates); optimizer.GenerateCircuit(); netOfGates.PrintAllGates(); bool result = Verifier::Run(&netOfGates); @@ -1226,7 +1226,7 @@ HWTEST_F_L0(StubTest, FastGetPropertyByNameStub) } LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, &stubModule, function); llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); auto engine = assembler.GetEngine(); auto *getPropertyByNamePtr = reinterpret_cast( @@ -1270,7 +1270,7 @@ HWTEST_F_L0(StubTest, FastModTest) } LLVMIRBuilder llvmBuilder(&cfg, &netOfGates, module, function); llvmBuilder.Build(); - LLVMAssembler assembler(module); + LLVMAssembler assembler(module, "x86_64-unknown-linux-gnu"); assembler.Run(); auto engine = assembler.GetEngine(); auto fn = reinterpret_cast(LLVMGetPointerToGlobal(engine, function)); diff --git a/ecmascript/mem/machine_code.h b/ecmascript/mem/machine_code.h index 6cfca754fe576599b6e7ec0854a91d59a26464ac..90221bf11b34bf9185947da4f605890da99ca5ee 100644 --- a/ecmascript/mem/machine_code.h +++ b/ecmascript/mem/machine_code.h @@ -36,10 +36,11 @@ public: static constexpr size_t INS_SIZE_OFFSET = TaggedObjectSize(); ACCESSORS(InstructionSizeInBytes, INS_SIZE_OFFSET, DATA_OFFSET); + static constexpr size_t SIZE = DATA_OFFSET; uintptr_t GetDataOffsetAddress(void) { - return reinterpret_cast(this + DATA_OFFSET); + return reinterpret_cast(this) + DATA_OFFSET; } void SetData(const uint8_t *codeData, size_t codeLength) diff --git a/js_runtime_config.gni b/js_runtime_config.gni index ea4700b46133793ad282302c26840b97d036cd91..bf6c0937819a3d9fcc36b4a42ef3b1676c257c39 100644 --- a/js_runtime_config.gni +++ b/js_runtime_config.gni @@ -14,7 +14,56 @@ ark_root = "//ark/runtime_core" js_root = "//ark/js_runtime" compile_llvm_online = false +run_with_asan = false +asan_lib_path = "/usr/lib/llvm-10/lib/clang/10.0.0/lib/linux" # For OpenHarmony build, always link with the static lib: sdk_libc_secshared_dep = "//utils/native/base:utilsecurec" sdk_libc_secshared_config = "//utils/native/base:utils_config" + +# Generate file for a template and YAML data provided. +# +# Mandatory arguments: +# data_file -- YAML data full name +# template_file -- template full name +# output_file -- output file full name +# requires -- a list of scripts that provide data-querying API for templates +# extra_dependencies -- a list of files that should be considered as dependencies, must be lable +template("ark_gen_file") { + assert(defined(invoker.data_file), "data_file is required!") + assert(defined(invoker.template_file), "template_file is required!") + assert(defined(invoker.output_file), "output_file is required!") + + requires = "" + if (defined(invoker.requires)) { + requires = string_join(",", rebase_path(invoker.requires, root_build_dir)) + } + + extra_dependencies = [] + if (defined(invoker.extra_dependencies)) { + extra_dependencies += invoker.extra_dependencies + } + + action("$target_name") { + script = "$ark_root/isa/gen.rb" + + # rerun action when data file or template file update + inputs = [ + invoker.template_file, + invoker.data_file, + ] + outputs = [ invoker.output_file ] + args = [ + "--template", + rebase_path(invoker.template_file, root_build_dir), + "--data", + rebase_path(invoker.data_file, root_build_dir), + "--require", + requires, + "--output", + rebase_path(outputs[0]), + ] + + deps = extra_dependencies + } +} diff --git a/test/test_helper.gni b/test/test_helper.gni index 4cc7e2300e5a007228604dd23dd13a7b43819bd7..6fda2157a78b41637a88aa23effa233cd63fa3db 100644 --- a/test/test_helper.gni +++ b/test/test_helper.gni @@ -38,6 +38,7 @@ template("host_unittest_action") { _host_test_target_ = ":${_target_name_}(${host_toolchain})" _root_out_dir_ = get_label_info(_host_test_target_, "root_out_dir") + deps = [ _host_test_target_ ] script = "//ark/js_runtime/test/run_ark_executable.py"