Skip to content

Commit

Permalink
vprintf WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanming-hu committed Oct 3, 2019
1 parent ce3f245 commit c664703
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lang/experimental/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sample: sample.cpp kernel.ll experiment.cu
llc -mcpu=sm_60 kernel.ll -o kernel.ptx
clang++-7 sample.cpp -o sample -O2 -g -I/usr/local/cuda/include -lcuda
clang++-7 experiment.cu --cuda-gpu-arch=sm_35 -std=c++14 -S -emit-llvm
clang++-7 experiment.cu --cuda-gpu-arch=sm_70 -std=c++14 -S -emit-llvm -nocudalib
4 changes: 4 additions & 0 deletions lang/runtime/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ using ContextArgType = long long;

extern "C" {

int vprintf(Ptr *format, Ptr *arg);
int printf(const char *, ...);

struct PhysicalCoordinates {
Expand Down Expand Up @@ -158,6 +159,7 @@ void *taichi_allocate(std::size_t size) {

void ___stubs___() {
printf("");
vprintf(nullptr, nullptr);
taichi_allocate(1);
taichi_allocate_aligned(1, 1);
}
Expand Down Expand Up @@ -279,4 +281,6 @@ void cuda_add(float *a, float *b, float *c) {
auto i = ti_cuda_tid_x();
c[i] = a[i] + b[i];
}


}
54 changes: 44 additions & 10 deletions lang/src/backends/llvm_gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ TLANG_NAMESPACE_BEGIN

using namespace llvm;

// NVVM IR Spec: https://docs.nvidia.com/cuda/archive/10.0/pdf/NVVM_IR_Specification.pdf
// NVVM IR Spec:
// https://docs.nvidia.com/cuda/archive/10.0/pdf/NVVM_IR_Specification.pdf

class CodeGenLLVMGPU : public CodeGenLLVM {
public:
Expand All @@ -34,16 +35,16 @@ class CodeGenLLVMGPU : public CodeGenLLVM {
llvm::Function *func = module->getFunction("test_kernel");

// Example annotation from llvm PTX doc:
/*
define void @kernel(float addrspace(1)* %A,
float addrspace(1)* %B,
float addrspace(1)* %C);
/*
define void @kernel(float addrspace(1)* %A,
float addrspace(1)* %B,
float addrspace(1)* %C);
!nvvm.annotations = !{!0}
!0 = !{void (float addrspace(1)*,
float addrspace(1)*,
float addrspace(1)*)* @kernel, !"kernel", i32 1}
*/
!nvvm.annotations = !{!0}
!0 = !{void (float addrspace(1)*,
float addrspace(1)*,
float addrspace(1)*)* @kernel, !"kernel", i32 1}
*/

// Add the nvvm annotation that it is a kernel function.
llvm::Metadata *md_args[] = {
Expand All @@ -62,6 +63,39 @@ define void @kernel(float addrspace(1)* %A,
TC_NOT_IMPLEMENTED
return nullptr;
}

void visit(PrintStmt *stmt) override {
TC_ASSERT(stmt->width() == 1);

auto value_type = tlctx->get_data_type(stmt->stmt->ret_type.data_type);
std::vector<llvm::Type *> types{value_type};
auto stype = llvm::StructType::get(*llvm_context, types, false);

std::string format;

auto values = builder->CreateAlloca(stype);
auto value_ptr = builder->CreateGEP(values, {0, 0});
auto value = stmt->stmt->value;

if (stmt->stmt->ret_type.data_type == DataType::i32) {
format = "%d";
} else if (stmt->stmt->ret_type.data_type == DataType::f32) {
format = "%f";
} else {
TC_NOT_IMPLEMENTED
}

builder->CreateStore(value, value_ptr);

auto format_str = "[debug] " + stmt->str + " = " + format + "\n";

stmt->value = builder->CreateCall(
get_runtime_function("vprintf"),
{builder->CreateGlobalStringPtr(format_str, "format string"),
builder->CreateBitCast(values,
llvm::Type::getInt8PtrTy(*llvm_context))},
"debug_printf");
}
};

FunctionType GPUCodeGen::codegen_llvm() {
Expand Down

0 comments on commit c664703

Please sign in to comment.