Skip to content

Commit dbd3834

Browse files
Lunderbergpfk-beta
authored andcommitted
[Hexagon][Codegen] Implement CodeGenHexagon::CreatePrintf (apache#10710)
* [Hexagon][Codegen] Implement CodeGenHexagon::CreatePrintf `CodeGenHexagon` inherits from `CodeGenLLVM`, but debug messages sent through `printf` calls do not make it back across the RPC server. Instead, the `FARF` preprocess macro provided from the Hexagon SDK should be used. This implementation of `CodeGenHexagon::CreatePrintf` generates the same `HAP_debug_v2` function call as would be generated by the `FARF` preprocessor macro, using the `ALWAYS` print level. * Updated following review comments * Updated from const llvm::ArrayRef& to llvm::ArrayRef.
1 parent 78051b3 commit dbd3834

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/target/llvm/codegen_hexagon.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class CodeGenHexagon final : public CodeGenLLVM {
6969
llvm::Module* GetModulePtr() const { return module_.get(); }
7070

7171
protected:
72+
void CreatePrintf(const std::string& format, llvm::ArrayRef<llvm::Value*> format_args) final;
73+
7274
// meta data
7375
llvm::MDNode* md_tbaa_ctx_ptr_{nullptr};
7476
llvm::FunctionType* ftype_tvm_func_call_{nullptr};
@@ -572,6 +574,35 @@ llvm::Value* CodeGenHexagon::CreateIntrinsic(const CallNode* op) {
572574
return CodeGenLLVM::CreateIntrinsic(op);
573575
}
574576

577+
void CodeGenHexagon::CreatePrintf(const std::string& format,
578+
llvm::ArrayRef<llvm::Value*> format_args) {
579+
// This function generates LLVM instructions to call HAP_debug_v2,
580+
// as if the FARF macro in `HAP_farf.h` were called as
581+
// FARF(ALWAYS, format, format_args[0], format_args[1], ...)
582+
std::string func_name = "HAP_debug_v2";
583+
584+
llvm::Function* func = module_->getFunction(func_name);
585+
if (func == nullptr) {
586+
llvm::FunctionType* ftype = llvm::FunctionType::get(
587+
t_void_, {t_int32_, t_char_->getPointerTo(), t_int32_, t_char_->getPointerTo()}, true);
588+
func = llvm::Function::Create(ftype, llvm::Function::ExternalLinkage, func_name, module_.get());
589+
}
590+
591+
llvm::Value* format_str = builder_->CreateGlobalStringPtr(format, "printf_format_str");
592+
593+
// The value of FARF_ALWAYS_LEVEL, defined as HAP_LEVEL_HIGH
594+
llvm::Value* level = ConstInt32(2);
595+
596+
// There is no such filename/line number for this print statement
597+
llvm::Value* filename = builder_->CreateGlobalStringPtr("generated-LLVM-code", "dummy_filename");
598+
llvm::Value* line_number = ConstInt32(1);
599+
600+
std::vector<llvm::Value*> func_args = {level, filename, line_number, format_str};
601+
func_args.insert(func_args.end(), format_args.begin(), format_args.end());
602+
603+
builder_->CreateCall(func, func_args);
604+
}
605+
575606
CodeGenLLVM::TypedPointer CodeGenHexagon::CreateBufferPtr(llvm::Value* buffer_ptr,
576607
DataType buffer_element_dtype,
577608
llvm::ArrayRef<llvm::Value*> indices,

src/target/llvm/codegen_llvm.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ llvm::Value* CodeGenLLVM::GetVarValue(const VarNode* v) const {
831831
}
832832

833833
void CodeGenLLVM::CreatePrintf(const std::string& format,
834-
const std::vector<llvm::Value*> format_args) {
834+
llvm::ArrayRef<llvm::Value*> format_args) {
835835
llvm::Function* func_printf = module_->getFunction("printf");
836836
if (func_printf == nullptr) {
837837
llvm::FunctionType* ftype = llvm::FunctionType::get(t_int32_, true);
@@ -850,9 +850,7 @@ void CodeGenLLVM::CreatePrintf(const std::string& format,
850850
str->setName("printf_format_str");
851851

852852
std::vector<llvm::Value*> printf_args = {str};
853-
for (auto arg : format_args) {
854-
printf_args.push_back(arg);
855-
}
853+
printf_args.insert(printf_args.end(), format_args.begin(), format_args.end());
856854
builder_->CreateCall(func_printf, printf_args);
857855

858856
// Call fflush() immediately, as this utility is intended for debug

src/target/llvm/codegen_llvm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class CodeGenLLVM : public ExprFunctor<llvm::Value*(const PrimExpr&)>,
238238
* printf(), immediately calls fflush() to flush the stdout buffer
239239
* in case of segfault.
240240
*/
241-
void CreatePrintf(const std::string& format, const std::vector<llvm::Value*> format_args);
241+
virtual void CreatePrintf(const std::string& format, llvm::ArrayRef<llvm::Value*> format_args);
242242

243243
/*! \brief Lookup return address, for debugging purposes
244244
*

0 commit comments

Comments
 (0)