From 648350d74322d98c88543fbccc80da436daa7f8c Mon Sep 17 00:00:00 2001 From: ridethepig Date: Sun, 9 Jul 2023 23:54:55 +0800 Subject: [PATCH] fixup before submit --- .vscode/launch.json | 3 +- scripts/mytester.py | 64 +++++++++++++++++++++++++++++----------- src/visitor.cpp | 6 +++- src/visitor_llir_gen.cpp | 2 +- 4 files changed, 55 insertions(+), 20 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index b836c38..eab4d1e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,8 @@ "program": "${workspaceFolder}/build/sysy", // "args": ["../sysytests/functional_2021/069_greatest_common_divisor.sy", "-S", "-o", "build/my.s", "-O1", "-emit-llvm"], // "args" : ["-S", "../sysytests/functional_2022/21_if_test2.sy", "-o", "build/dbg.s", "-emit-llvm", "-O1",], - "args" : ["-S", "../sysytests/functional_h_2022/29_long_line.sy", "-o", "build/dbg.s", "-emit-llvm", "-O1",], + // "args" : ["-S", "../sysytests/functional_h_2022/29_long_line.sy", "-o", "build/dbg.s", "-emit-llvm", "-O1",], + "args" : ["-S", "../sysytests/performance_2021fin/conv0.sy", "-o", "build/dbg.s", "-emit-llvm", "-O1",], "cwd": "${workspaceFolder}" }, ] diff --git a/scripts/mytester.py b/scripts/mytester.py index e034098..f3b10d5 100644 --- a/scripts/mytester.py +++ b/scripts/mytester.py @@ -207,31 +207,46 @@ class BatTest: self.count_error = 0 self.label = f"{compiler.scheme['name']}_{compiler_rf.scheme['name']}" self.diffout_template = f"{compiler.target_dir}/{{testcase}}/{self.label}.diff" + self.perf_template = f"perf.log" - def bat_case(self, testcase, compile=False, emit_llvm=True): + def bat_case(self, testcase, compile=False, emit_llvm=True, perf=False): Print_C.print_subheader(f"Diff test {self.label} on {testcase}") if compile: self.compiler.clean_case(testcase) self.compiler_rf.clean_case(testcase) self.compiler.compile_case(testcase, emit_llvm) self.compiler_rf.compile_case(testcase, emit_llvm) - if not compiler.run_case(testcase, 0): return False - if not compiler_rf.run_case(testcase, 0): return False - myout = compiler.myout_template.format(testcase=testcase) - refout = compiler_rf.myout_template.format(testcase=testcase) - diffout = self.diffout_template.format(testcase=testcase) - diffout_file = open(diffout, "w") - p = subprocess.run(f"diff {myout} {refout}".split(), stdout=diffout_file, stderr=diffout_file, bufsize=1) - diffout_file.close() - if p.returncode != 0: - Print_C.print_error(f"Different output for {testcase}. See {diffout}") - return False + if not perf: + # should not run diff out when in perf mode, since running time may be different + if not compiler.run_case(testcase, 0): return False + if not compiler_rf.run_case(testcase, 0): return False + myout = compiler.myout_template.format(testcase=testcase) + refout = compiler_rf.myout_template.format(testcase=testcase) + null_file = open(os.devnull, "w") + subprocess.run(f"sed -i /TOTAL/d {myout} {refout}".split(), bufsize=1) + null_file.close() + diffout = self.diffout_template.format(testcase=testcase) + diffout_file = open(diffout, "w") + p = subprocess.run(f"diff -B {myout} {refout}".split(), stdout=diffout_file, stderr=diffout_file, bufsize=1) + diffout_file.close() + if p.returncode != 0: + Print_C.print_error(f"Different output for {testcase}. See {diffout}") + return False + else: + perflog_file = open(self.perf_template, "a") + t0 = time.time() + if not compiler.run_case(testcase, 0): return False + t1 = time.time() + perflog_file.write("{testcase}_my: {time:.3f}\n".format(testcase=testcase, time=(t1-t0))) + if not compiler_rf.run_case(testcase, 0): return False + t2 = time.time() + perflog_file.write("{testcase}_rf: {time:.3f}\n".format(testcase=testcase, time=(t2-t1))) + perflog_file.close() return True - def bat_all_tests(self, compile=False, emit_llvm=True): - + def bat_all_tests(self, compile=False, emit_llvm=True, perf=False): for testcase in self.testcases: - if not self.bat_case(testcase, compile, emit_llvm): break + if not self.bat_case(testcase, compile, emit_llvm, perf): break scheme_ref = { "name": "ref", @@ -257,7 +272,7 @@ scheme_my_rv64 = { "ir_asm": "llc --march=x86 --relocation-model=pic {ir} -o {asm}", "asm_obj": "riscv64-linux-gnu-as {asm} -o {obj}", "obj_bin": f"riscv64-linux-gnu-gcc -static {{obj}} {LIB_PATH_RV64} -o {{bin}}", - "sy_asm": f"build/sysy -S {{sy}} -o {{asm}} -emit-llvm -O1", + "sy_asm": f"build/sysy -S {{sy}} -o {{asm}} -O1", "run": f"spike /usr/local/riscv64-linux-gnu/bin/pk {{bin}}", } @@ -277,11 +292,22 @@ scheme_ref_rv64 = { "run": f"spike /usr/local/riscv64-linux-gnu/bin/pk {{bin}}", } +scheme_ref_rv64_o2 = { + "name" : "ref_rv64", + "sy_ir": f"clang -x c -c -fPIE -fno-stack-protector -S -emit-llvm --target=riscv64-linux-gnu -include {HDR_PATH} {{sy}} -o {{ir}}", + "ir_asm": "llc --march=riscv64 --relocation-model=pic --float-abi=hard -mcpu=sifive-7-rv64 -mattr=+f,+m,+d {ir} -o {asm}", + "asm_obj": "riscv64-linux-gnu-as {asm} -o {obj}", + "obj_bin": f"riscv64-linux-gnu-gcc -static {{obj}} {LIB_PATH_RV64} -o {{bin}}", + "sy_asm": f"clang -O2 -x c -S -fno-stack-protector -no-integrated-as --target=riscv64-linux-gnu -march=rv64gc -fPIE -mfloat-abi=hard -mcpu=sifive-7-rv64 -include {HDR_PATH} {{sy}} -o {{asm}}", + "run": f"spike /usr/local/riscv64-linux-gnu/bin/pk {{bin}}", +} + schemes = { "my": scheme_my, "ref": scheme_ref, "ref_rv64": scheme_ref_rv64, "my_rv64": scheme_my_rv64, + "ref_rv64_o2": scheme_ref_rv64_o2, } # subprocess.run(f"llc -O3 -march=arm -mcpu=cortex-a72 -float-abi=hard -filetype=asm {ir} -o {asm}".split(), stdout=log_file, stderr=log_file, bufsize=1) # subprocess.run(f"as -march=armv7-a -mfloat-abi=hard {asm} -o {obj}".split(), stdout=log_file, stderr=log_file, bufsize=1) @@ -299,6 +325,7 @@ if __name__ == "__main__": parser.add_argument('--run', action='store_true', default=False) parser.add_argument('--bat', action='store_true', default=False) parser.add_argument('--emit_llvm', action='store_true', default=False) + parser.add_argument('--perf', action='store_true', default=False) args = parser.parse_args() print(args) testcases = case_collector(args.case_selector, args.case_prefix) @@ -313,12 +340,15 @@ if __name__ == "__main__": if not os.path.exists(testcase_dir): os.makedirs(testcase_dir) os.system(f"cp {testcase_src}.* {testcase_dir}/") + if args.perf: + schemes[args.scheme]["run"] = f"qemu-riscv64 {{bin}}" + schemes[args.ref]["run"] = f"qemu-riscv64 {{bin}}" current_schem = schemes[args.scheme] if args.bat: compiler = Compiler(scheme=current_schem,target_dir=target_dir, testcases=testcases) compiler_rf = Compiler(scheme=schemes[args.ref],target_dir=target_dir, testcases=testcases) battester = BatTest(compiler, compiler_rf, testcases) - battester.bat_all_tests(args.compile, args.emit_llvm) + battester.bat_all_tests(args.compile, args.emit_llvm, args.perf) elif args.compile: compiler = Compiler(scheme=current_schem,target_dir=target_dir, testcases=testcases) compiler.prepare_dir() diff --git a/src/visitor.cpp b/src/visitor.cpp index 4eaccd8..4ffb941 100644 --- a/src/visitor.cpp +++ b/src/visitor.cpp @@ -232,7 +232,6 @@ std::any Visitor::visitVarDef(SysyParser::VarDefContext *ctx) { {base_ptr, CONST0, ConstantInt::New(array_value.size() * 4)}, _state.current_bb ); - // TODO: BAAU-2021 calls memset in `libc` (not sysylib) to optimize this process if (array_value[0]) InstStore::New(array_value[0], base_ptr, _state.current_bb); for (int i = 1; i < array_value.size(); ++i) { if (!array_value[i]) continue; @@ -625,6 +624,11 @@ std::any Visitor::visitUnaryExp(SysyParser::UnaryExpContext *ctx) { args.push_back(exp); } } + if (func->is_libfunc()) { + if (func_name == "starttime" || func_name == "stoptime") { + args.push_back(CONST0); + } + } return InstCall::New(func, args, _state.current_bb); } else if (ctx->primaryExp()) { diff --git a/src/visitor_llir_gen.cpp b/src/visitor_llir_gen.cpp index 0fd44f9..6536cf5 100644 --- a/src/visitor_llir_gen.cpp +++ b/src/visitor_llir_gen.cpp @@ -482,7 +482,7 @@ void Visitor::llir_gen(std::ostream &ostr) { auto lib_func_type = std::dynamic_pointer_cast(lib_func->type); ostr << "declare" << " " << lib_func_type->return_type->to_IR_string() << " " - << "@" << lib_func_name << "("; + << "@" << lib_func->name << "("; auto ¶m_list = lib_func->fparam_list; for (int i = 0; i < (int)param_list.size() - 1; ++i) { ostr << param_list[i]->type->to_IR_string() << ", ";