1 | #include "taichi/runtime/wasm/aot_module_builder_impl.h" |
2 | #include "taichi/runtime/program_impls/llvm/llvm_program.h" |
3 | |
4 | #include <fstream> |
5 | |
6 | #include "taichi/util/file_sequence_writer.h" |
7 | #include "llvm/Linker/Linker.h" |
8 | |
9 | namespace taichi::lang { |
10 | namespace wasm { |
11 | |
12 | AotModuleBuilderImpl::AotModuleBuilderImpl(const CompileConfig &compile_config, |
13 | TaichiLLVMContext &tlctx) |
14 | : compile_config_(compile_config), module_(nullptr), tlctx_(tlctx) { |
15 | TI_AUTO_PROF |
16 | } |
17 | |
18 | void AotModuleBuilderImpl::eliminate_unused_functions() const { |
19 | TaichiLLVMContext::eliminate_unused_functions( |
20 | module_.get(), [&](std::string func_name) { |
21 | for (auto &name : name_list_) { |
22 | if (name == func_name) |
23 | return true; |
24 | } |
25 | return false; |
26 | }); |
27 | } |
28 | |
29 | void AotModuleBuilderImpl::dump(const std::string &output_dir, |
30 | const std::string &filename) const { |
31 | std::string bin_path = output_dir + "/" + fmt::format("{}.ll" , filename); |
32 | |
33 | eliminate_unused_functions(); |
34 | FileSequenceWriter writer(bin_path, "optimized LLVM IR (WASM)" ); |
35 | writer.write(module_.get()); |
36 | } |
37 | |
38 | void AotModuleBuilderImpl::add_per_backend(const std::string &identifier, |
39 | Kernel *kernel) { |
40 | auto module_info = KernelCodeGenWASM(compile_config_, kernel, tlctx_) |
41 | .compile_kernel_to_module(); |
42 | if (module_) { |
43 | llvm::Linker::linkModules(*module_, std::move(module_info.module), |
44 | llvm::Linker::OverrideFromSrc); |
45 | } else { |
46 | module_ = std::move(module_info.module); |
47 | } |
48 | } |
49 | |
50 | void AotModuleBuilderImpl::add_field_per_backend(const std::string &identifier, |
51 | const SNode *rep_snode, |
52 | bool is_scalar, |
53 | DataType dt, |
54 | std::vector<int> shape, |
55 | int row_num, |
56 | int column_num) { |
57 | } |
58 | |
59 | void AotModuleBuilderImpl::add_per_backend_tmpl(const std::string &identifier, |
60 | const std::string &key, |
61 | Kernel *kernel) { |
62 | } |
63 | |
64 | } // namespace wasm |
65 | } // namespace taichi::lang |
66 | |