Skip to content

Commit

Permalink
Add new testminimal and closure-string test.
Browse files Browse the repository at this point in the history
Signed-off-by: Tuomas Tonteri <[email protected]>
  • Loading branch information
johnfea committed Jun 20, 2024
1 parent 0d122e7 commit a3e70cb
Show file tree
Hide file tree
Showing 32 changed files with 1,346 additions and 612 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ else ()
endif ()
set (OSL_LIBNAME_SUFFIX "" CACHE STRING
"Optional name appended to ${PROJECT_NAME} libraries that are built")
option (OSL_BUILD_TESTS "Build the unit tests, testshade, testrender" ON)
option (OSL_BUILD_TESTS "Build the unit tests, testminimal, testshade, testrender" ON)
if (WIN32)
option (USE_LLVM_BITCODE "Generate embedded LLVM bitcode" OFF)
else ()
Expand All @@ -113,10 +113,14 @@ set (OSL_SHADER_INSTALL_DIR "${CMAKE_INSTALL_FULL_DATADIR}/${PROJECT_NAME}/shade
set (OSL_PTX_INSTALL_DIR "${CMAKE_INSTALL_FULL_DATADIR}/${PROJECT_NAME}/ptx"
CACHE STRING "Directory where OptiX PTX files will be installed")
set (CMAKE_DEBUG_POSTFIX "" CACHE STRING "Library naming postfix for Debug builds (e.g., '_debug')")
option (OSL_USTRINGREP_IS_HASH "Always use ustringhash for strings" OFF)
option (OSL_USTRINGREP_IS_HASH "Always use ustringhash for strings" ON)


set (OSL_NO_DEFAULT_TEXTURESYSTEM OFF CACHE BOOL "Do not use create a raw OIIO::TextureSystem")

if (OSL_USTRINGREP_IS_HASH)
add_definitions ("-DOSL_USTRINGREP_IS_HASH=1")
endif ()
if (OSL_NO_DEFAULT_TEXTURESYSTEM)
add_definitions ("-DOSL_NO_DEFAULT_TEXTURESYSTEM=1")
endif ()
Expand Down Expand Up @@ -220,6 +224,7 @@ add_subdirectory (src/oslc)
add_subdirectory (src/oslinfo)

if (OSL_BUILD_TESTS AND BUILD_TESTING)
add_subdirectory (src/testminimal)
add_subdirectory (src/testshade)
add_subdirectory (src/testrender)
endif ()
Expand Down
2 changes: 1 addition & 1 deletion src/cmake/testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ macro (osl_add_all_tests)
bug-array-heapoffsets bug-locallifetime bug-outputinit
bug-param-duplicate bug-peep bug-return
calculatenormal-reg
cellnoise closure closure-array closure-layered closure-parameters closure-zero closure-conditional
cellnoise closure closure-array closure-layered closure-parameters closure-string closure-zero closure-conditional
color color-reg colorspace comparison
complement-reg compile-buffer compassign-bool compassign-reg
component-range
Expand Down
12 changes: 9 additions & 3 deletions src/include/OSL/llvm_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ class OSLEXECPUBLIC LLVM_Util {
/// Return an llvm::Value holding the given string constant (as
/// determined by the ustring_rep).
llvm::Value* constant(ustring s);
llvm::Value* constant_real_ustring(ustring s);
llvm::Value* constant(string_view s) { return constant(ustring(s)); }

llvm::Constant* constant_array(cspan<llvm::Constant*> constants);
Expand Down Expand Up @@ -750,6 +751,7 @@ class OSLEXECPUBLIC LLVM_Util {
llvm::Constant* wide_constant(size_t i);
llvm::Constant* wide_constant_bool(bool b);
llvm::Value* wide_constant(ustring s);
llvm::Value* wide_constant_real_ustring(ustring s);
llvm::Value* wide_constant(string_view s)
{
return wide_constant(ustring(s));
Expand Down Expand Up @@ -1058,10 +1060,14 @@ class OSLEXECPUBLIC LLVM_Util {
IRBuilder& builder();

int m_debug;
bool m_dumpasm = false;
bool m_jit_fma = false;
bool m_jit_aggressive = false;
bool m_dumpasm = false;
bool m_jit_fma = false;
bool m_jit_aggressive = false;
#ifndef OSL_USTRINGREP_IS_HASH
UstringRep m_ustring_rep = UstringRep::charptr;
#else
UstringRep m_ustring_rep = UstringRep::hash;
#endif
PerThreadInfo::Impl* m_thread;
llvm::LLVMContext* m_llvm_context;
llvm::Module* m_llvm_module;
Expand Down
35 changes: 29 additions & 6 deletions src/liboslexec/batched_backendllvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ BatchedBackendLLVM::BatchedBackendLLVM(ShadingSystemImpl& shadingsys,
case 8: m_true_mask_value = Mask<8>(true).value(); break;
default: OSL_ASSERT(0 && "unsupported vector width");
}

// Select the appropriate ustring representation
ll.ustring_rep(LLVM_Util::UstringRep::hash);

ll.dumpasm(shadingsys.m_llvm_dumpasm);
ll.jit_fma(shadingsys.m_llvm_jit_fma);
ll.jit_aggressive(shadingsys.m_llvm_jit_aggressive);
Expand Down Expand Up @@ -191,7 +195,7 @@ BatchedBackendLLVM::llvm_pass_type(const TypeSpec& typespec)
else if (t == TypeDesc::INT)
lt = ll.type_int();
else if (t == TypeDesc::STRING)
lt = (llvm::Type*)ll.type_ustring();
lt = (llvm::Type*)ll.type_real_ustring();
else if (t.aggregate == TypeDesc::VEC3)
lt = (llvm::Type*)ll.type_void_ptr(); //llvm_type_triple_ptr();
else if (t.aggregate == TypeDesc::MATRIX44)
Expand Down Expand Up @@ -667,10 +671,11 @@ BatchedBackendLLVM::llvm_get_pointer(const Symbol& sym, int deriv,
#endif

int d = deriv * std::max(1, t.arraylen);
if (arrayindex)
if (arrayindex) {
arrayindex = ll.op_add(arrayindex, ll.constant(d));
else
} else {
arrayindex = ll.constant(d);
}

llvm::Type* result_type = llvm_type(t.elementtype());
if (!sym.is_uniform()) {
Expand Down Expand Up @@ -715,7 +720,8 @@ llvm::Value*
BatchedBackendLLVM::llvm_load_value(const Symbol& sym, int deriv,
llvm::Value* arrayindex, int component,
TypeDesc cast, bool op_is_uniform,
bool index_is_uniform)
bool index_is_uniform,
bool always_real_ustring)
{
// A uniform symbol can be broadcast into a varying value.
// But a varying symbol can NOT be loaded into a uniform value.
Expand Down Expand Up @@ -780,9 +786,15 @@ BatchedBackendLLVM::llvm_load_value(const Symbol& sym, int deriv,
if (sym.typespec().is_string()) {
ustring string_val = sym.get_string();
if (op_is_uniform) {
return ll.constant(string_val);
if (!always_real_ustring)
return ll.constant(string_val);
else
return ll.constant_real_ustring(string_val);
} else {
return ll.wide_constant(string_val);
if (!always_real_ustring)
return ll.wide_constant(string_val);
else
return ll.wide_constant_real_ustring(string_val);
}
}
OSL_ASSERT(0 && "unhandled constant type");
Expand All @@ -796,7 +808,17 @@ BatchedBackendLLVM::llvm_load_value(const Symbol& sym, int deriv,
sym.forced_llvm_bool());
}

llvm::Value*
BatchedBackendLLVM::llvm_const_hash(string_view str)
{
return llvm_const_hash(ustring(str));
}

llvm::Value*
BatchedBackendLLVM::llvm_const_hash(ustring str)
{
return ll.constant64((uint64_t)str.hash());
}

llvm::Value*
BatchedBackendLLVM::llvm_load_mask(const Symbol& cond)
Expand Down Expand Up @@ -1717,6 +1739,7 @@ BatchedBackendLLVM::llvm_call_function(const FuncSpec& name,
= llvm_load_value(s, /*deriv=*/d,
/*component*/ c, TypeUnknown,
function_is_uniform);

// Store our wide pointer on the stack
llvm_store_value(wide_value, tmpptr, t, d, NULL, c,
/*dst_is_uniform*/ false);
Expand Down
10 changes: 7 additions & 3 deletions src/liboslexec/batched_backendllvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ class BatchedBackendLLVM : public OSOProcessorBase {
/// performed if cast is the default of UNKNOWN).
llvm::Value* llvm_load_value(const Symbol& sym, int deriv,
llvm::Value* arrayindex, int component,
TypeDesc cast = TypeDesc::UNKNOWN,
bool op_is_uniform = true,
bool index_is_uniform = true);
TypeDesc cast = TypeDesc::UNKNOWN,
bool op_is_uniform = true,
bool index_is_uniform = true,
bool always_real_ustring = false);

llvm::Value* llvm_const_hash(string_view str);

llvm::Value* llvm_const_hash(ustring str);

/// Given an llvm::Value* of a pointer (and the type of the data
/// that it points to), Return the llvm::Value* corresponding to the
Expand Down
19 changes: 12 additions & 7 deletions src/liboslexec/batched_llvm_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#endif

#include "batched_backendllvm.h"

#include <llvm/Support/raw_ostream.h>


using namespace OSL;
Expand Down Expand Up @@ -375,6 +375,7 @@ LLVMGEN(llvm_gen_printf)
std::string ourformat(oldfmt, format); // straddle the format
// Doctor it to fix mismatches between format and data
Symbol& sym(*rop.opargsym(op, arg));

OSL_ASSERT(!sym.typespec().is_structure_based());

bool arg_is_uniform = sym.is_uniform();
Expand Down Expand Up @@ -416,11 +417,13 @@ LLVMGEN(llvm_gen_printf)
// widened, so our typical op_is_uniform doesn't do what we
// want for this when loading. So just pass arg_is_uniform
// which will avoid widening any uniform arguments.
llvm::Value* loaded
= rop.llvm_load_value(sym, 0, arrind, c,
TypeDesc::UNKNOWN,
/*op_is_uniform*/ arg_is_uniform,
/*index_is_uniform*/ true);
// Always use real ustring here because wide/opstring printf va_args expects
// strings and not hashes
bool always_real_ustring = true;
llvm::Value* loaded = rop.llvm_load_value(
sym, 0, arrind, c, TypeDesc::UNKNOWN,
/*op_is_uniform*/ arg_is_uniform,
/*index_is_uniform*/ true, always_real_ustring);

// Always expand llvm booleans to integers
if (sym.forced_llvm_bool()) {
Expand Down Expand Up @@ -482,6 +485,7 @@ LLVMGEN(llvm_gen_printf)
llvm::Value* ret = rop.ll.call_function(rop.build_name(func_spec),
call_args);


// The format op returns a string value, put in in the right spot
if (op.opname() == op_format)
rop.llvm_store_value(ret, *rop.opargsym(op, 0));
Expand Down Expand Up @@ -861,6 +865,7 @@ LLVMGEN(llvm_gen_generic)
&& (uniformFormOfFunction || functionIsLlvmInlined)) {
OSL_DEV_ONLY(std::cout << ">>stores return value "
<< rop.build_name(func_spec) << std::endl);

llvm::Value* r = rop.llvm_call_function(
func_spec, &(args[1]), op.nargs() - 1,
/*deriv_ptrs*/ false, uniformFormOfFunction,
Expand Down Expand Up @@ -3300,7 +3305,7 @@ LLVMGEN(llvm_gen_construct_triple)
= { rop.sg_void_ptr(), rop.ll.void_ptr(transform),
space_is_uniform ? rop.llvm_load_value(Space)
: rop.llvm_void_ptr(Space),
rop.ll.constant(Strings::common),
rop.llvm_const_hash(Strings::common),
rop.ll.mask_as_int(rop.ll.current_mask()) };

// Dynamically build function name
Expand Down
2 changes: 1 addition & 1 deletion src/liboslexec/builtindecl.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ DECL(osl_closure_to_string, "sXX")
DECL(osl_closure_to_ustringhash, "hXX")
#endif
DECL(osl_format, "hh*")
DECL(osl_gen_ustringhash_pod, "hs")
DECL(osl_gen_ustringhash_pod, "hh")
DECL(osl_gen_ustring, "sh")
DECL(osl_gen_printfmt, "xXhiXiX")
DECL(osl_gen_filefmt, "xXhhiXiX")
Expand Down
Loading

0 comments on commit a3e70cb

Please sign in to comment.