Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit 886b563

Browse files
Add support for printing emitted assembly
This commit introduces a --llvm_dump_asm flag and the corresponding --llvm_dump_asm_options to emit assembly to LOG(INFO)
1 parent df64aee commit 886b563

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

tc/core/flags.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ DEFINE_string(
5555
// CPU codegen options
5656
DEFINE_bool(llvm_dump_before_opt, false, "Print IR before optimization");
5757
DEFINE_bool(llvm_dump_after_opt, false, "Print IR after optimization");
58+
DEFINE_bool(llvm_dump_asm, false, "Print asm");
59+
DEFINE_string(
60+
llvm_dump_asm_options,
61+
"-march=x86-64 -mcpu=broadwell -filetype=asm",
62+
"Options used when dumping asm");
5863

5964
DEFINE_uint32(
6065
benchmark_warmup,

tc/core/flags.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ DECLARE_string(nvcc_flags);
3939
// llvm codegen
4040
DECLARE_bool(llvm_dump_before_opt);
4141
DECLARE_bool(llvm_dump_after_opt);
42+
DECLARE_bool(llvm_dump_asm);
43+
DECLARE_string(llvm_dump_asm_options);
4244

4345
// Used in benchmarking and autotuning
4446
DECLARE_uint32(benchmark_warmup);

tc/core/polyhedral/codegen_llvm.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#include "tc/core/polyhedral/codegen_llvm.h"
1717

18+
#include <fstream>
1819
#include <sstream>
1920
#include <vector>
2021

@@ -41,7 +42,9 @@
4142
#include "tc/core/polyhedral/schedule_isl_conversion.h"
4243
#include "tc/core/polyhedral/scop.h"
4344
#include "tc/core/scope_guard.h"
45+
#include "tc/core/utils/system.h"
4446
#include "tc/external/isl.h"
47+
#include "tc/tc_config.h"
4548

4649
#ifndef LLVM_VERSION_MAJOR
4750
#error LLVM_VERSION_MAJOR not set
@@ -640,6 +643,34 @@ std::unique_ptr<llvm::Module> emitLLVMKernel(
640643
<< "LLVM generated module is invalid." << cg.str().c_str();
641644

642645
cg.halide_cg.optimize_module();
646+
if (FLAGS_llvm_dump_asm) {
647+
std::string pat("/tmp/tcXXXXXX");
648+
std::vector<char> ifn(pat.begin(), pat.end());
649+
TC_CHECK_GE(mkstemp(ifn.data()), 0); // string.c_str is const char*
650+
std::string fileName(ifn.begin(), ifn.end());
651+
std::string optFile = fileName + "-opt.ll";
652+
std::string asmFile = fileName + ".s";
653+
// cstdio's std::remove to delete files
654+
tc::ScopeGuard sgi([&]() {
655+
std::remove(optFile.c_str());
656+
std::remove(asmFile.c_str());
657+
});
658+
{
659+
std::ofstream ostream(optFile, std::ios::binary);
660+
ostream << cg.str();
661+
}
662+
utils::checkedSystemCall(
663+
std::string(TC_STRINGIFY(TC_LLVM_BIN_DIR)) + "/llc",
664+
{FLAGS_llvm_dump_asm_options, optFile, std::string("-o ") + asmFile});
665+
{
666+
std::ifstream is(asmFile);
667+
std::string str(
668+
(std::istreambuf_iterator<char>(is)),
669+
std::istreambuf_iterator<char>());
670+
LOG(INFO) << str;
671+
}
672+
}
673+
643674
return cg.halide_cg.move_module();
644675
}
645676

0 commit comments

Comments
 (0)