1#ifdef TI_WITH_LLVM
2#include "llvm/IR/Module.h"
3#include "llvm/Support/raw_ostream.h"
4#endif
5
6#include "taichi/ir/transforms.h"
7#include "taichi/util/file_sequence_writer.h"
8
9namespace taichi {
10
11FileSequenceWriter::FileSequenceWriter(std::string filename_template,
12 std::string file_type)
13 : counter_(0),
14 filename_template_(filename_template),
15 file_type_(file_type) {
16}
17
18#ifdef TI_WITH_LLVM
19std::string FileSequenceWriter::write(llvm::Module *module) {
20 std::string str;
21 llvm::raw_string_ostream ros(str);
22 module->print(ros, nullptr);
23 return write(str);
24}
25#endif
26
27std::string FileSequenceWriter::write(const std::string &str) {
28 auto [ofs, fn] = create_new_file();
29 ofs << str;
30 return fn;
31}
32
33std::string FileSequenceWriter::write(lang::IRNode *irnode) {
34 std::string content;
35 lang::irpass::print(irnode, &content);
36 return write(content);
37}
38
39std::pair<std::ofstream, std::string> FileSequenceWriter::create_new_file() {
40 auto fn = fmt::format(filename_template_, counter_);
41 TI_INFO("Saving {} to {}", file_type_, fn);
42 counter_++;
43 return {std::ofstream(fn), fn};
44}
45
46} // namespace taichi
47