Skip to content

Commit

Permalink
Revert "[Malloc] Pass tsl global in malloc"
Browse files Browse the repository at this point in the history
This reverts commit a6135c9.

Somehow these commits cause crash, I have figured other ways to solve the problem and it is also a cheaper way
  • Loading branch information
JiayinCao committed Feb 10, 2021
1 parent d203899 commit 669b38c
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/include/tsl_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class TSL_INTERFACE ShadingSystemInterface {
//! There are things to be noticed in this interface.
//! - Shaders are not responsible to release the memory allocator allocates, it is up to the renderer to do so.
//! - This implementation has to be thread safe.
virtual void* allocate(unsigned int size, void* tsl_global) const = 0;
virtual void* allocate(unsigned int size) const = 0;

//! @brief This will be automatically called when there is error during shader compilation.
//!
Expand Down
13 changes: 0 additions & 13 deletions src/tsl_lib/compiler/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,6 @@ llvm::Value* AstNode_Binary_Add::codegen(TSL_Compile_Context& context) const {

// allocate the tree data structure
std::vector<llvm::Value*> args = { ConstantInt::get(*context.context, APInt(32, sizeof(ClosureTreeNodeAdd))) };
if (context.tsl_global_value)
args.push_back(context.tsl_global_value);
else
args.push_back(ConstantPointerNull::get(PointerType::get(get_int_32_ptr_ty(context), 0)));
auto closure_tree_node_ptr = builder.CreateCall(malloc_function, args);
auto converted_closure_tree_node_ptr = builder.CreatePointerCast(closure_tree_node_ptr, closure_tree_node_ptr_type);

Expand Down Expand Up @@ -503,11 +499,6 @@ llvm::Value* AstNode_Binary_Multi::codegen(TSL_Compile_Context& context) const {

// allocate the tree data structure
std::vector<llvm::Value*> args = { ConstantInt::get(*context.context, APInt(32, sizeof(ClosureTreeNodeMul))) };
if (context.tsl_global_value)
args.push_back(context.tsl_global_value);
else
args.push_back(ConstantPointerNull::get(PointerType::get(get_int_32_ptr_ty(context), 0)));

auto closure_tree_node_ptr = builder.CreateCall(malloc_function, args);
auto converted_closure_tree_node_ptr = builder.CreatePointerCast(closure_tree_node_ptr, closure_tree_node_ptr_type);

Expand Down Expand Up @@ -1268,10 +1259,6 @@ llvm::Value* AstNode_Expression_MakeClosure::codegen(TSL_Compile_Context& contex
for (const auto& arg : m_args->get_arg_list())
args_llvm.push_back(arg->codegen(context));
}
if (context.tsl_global_value)
args_llvm.push_back(context.tsl_global_value);
else
args_llvm.push_back(ConstantPointerNull::get(PointerType::get(get_int_32_ptr_ty(context), 0)));

return context.builder->CreateCall(function, args_llvm);
}
Expand Down
5 changes: 1 addition & 4 deletions src/tsl_lib/compiler/global_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ ClosureID GlobalModule::register_closure_type(const std::string& name, ClosureAr
arg_types.push_back(type);
}

// always append a tsl global pointer here in case it is needed
arg_types.push_back(get_int_32_ptr_ty(m_llvm_compiling_context));

// return type is always int* to avoid debugging error
auto ret_type = get_closure_ty(m_llvm_compiling_context);

Expand Down Expand Up @@ -234,7 +231,7 @@ void GlobalModule::declare_global_module(TSL_Compile_Context& context){
context.m_structure_type_maps["float3"] = float3_meta_data;

// malloc function
Function* malloc_function = Function::Create(FunctionType::get(get_int_32_ptr_ty(context), { get_int_32_ty(context), get_int_32_ptr_ty(context) }, false), Function::ExternalLinkage, "TSL_MALLOC", context.module);
Function* malloc_function = Function::Create(FunctionType::get(get_int_32_ptr_ty(context), { get_int_32_ty(context) }, false), Function::ExternalLinkage, "TSL_MALLOC", context.module);
context.m_func_symbols["TSL_MALLOC"] = std::make_pair(malloc_function, nullptr);

// texture 2d sampling
Expand Down
4 changes: 2 additions & 2 deletions src/tsl_lib/system/callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ using generic_ptr = int*;

extern "C" {
// allocate memory inside shaders
DLLEXPORT int* TSL_MALLOC(int size, void* ptr) {
return (int*)allocate_memory(size, ptr);
DLLEXPORT int* TSL_MALLOC(int size) {
return (int*)allocate_memory(size);
}

// 2D texture sample
Expand Down
2 changes: 1 addition & 1 deletion src/tsl_lib/system/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct ShadingContext_Impl {
};

//! @brief Allocate memory in shader.
void* allocate_memory(const unsigned size, void* ptr);
void* allocate_memory(const unsigned size);

//! @brief Output error during shader compilation.
void emit_error(const char* error, ...);
Expand Down
4 changes: 2 additions & 2 deletions src/tsl_lib/system/shading_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ void ShadingSystem::register_shadingsystem_interface(std::unique_ptr<ShadingSyst
g_shading_system_impl->m_callback = std::move(ssi);
}

void* allocate_memory(const unsigned size, void* ptr) {
void* allocate_memory(const unsigned size) {
const auto callback = g_shading_system_impl->m_callback.get();
return callback ? callback->allocate(size, ptr) : nullptr;
return callback ? callback->allocate(size) : nullptr;
}

void emit_error(const char* format, ...) {
Expand Down
2 changes: 1 addition & 1 deletion src/tsl_sample/rt_tsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static thread_local char buf[BUF_MEM_SIZE];
class ShadingSystemInterfaceSimple : public Tsl_Namespace::ShadingSystemInterface {
public:
// Simply fetch some memory from the memory pool
void* allocate(unsigned int size, void* tsl_global) const override {
void* allocate(unsigned int size) const override {
assert(buf_index + size < BUF_MEM_SIZE);
void* ret = buf + buf_index;
buf_index += size;
Expand Down
5 changes: 1 addition & 4 deletions src/tsl_test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,11 @@
#include "tsl_system.h"
#include "test/test_common.h"

// this is fairly ugly, but this is just unit test, I can live with it.
extern void* ptr;

class ShadingSystemInterfaceSimple : public Tsl_Namespace::ShadingSystemInterface {
public:
// This is by no mean a good example of allocating memory of bxdf in real renderer.
// The purpose of this code is simply for testing, not for performance.
void* allocate(unsigned int size, void* tsl_global) const override {
void* allocate(unsigned int size) const override {
m_memory_holder.push_back(std::move(std::make_unique<char[]>(size)));
return m_memory_holder.back().get();
}
Expand Down

0 comments on commit 669b38c

Please sign in to comment.