Skip to content

Commit beca645

Browse files
committed
Change batched to use ustringhash_pod instead of ustring_pod.
Signed-off-by: Tuomas Tonteri <[email protected]>
1 parent 0d122e7 commit beca645

23 files changed

+739
-624
lines changed

CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,14 @@ set (OSL_SHADER_INSTALL_DIR "${CMAKE_INSTALL_FULL_DATADIR}/${PROJECT_NAME}/shade
113113
set (OSL_PTX_INSTALL_DIR "${CMAKE_INSTALL_FULL_DATADIR}/${PROJECT_NAME}/ptx"
114114
CACHE STRING "Directory where OptiX PTX files will be installed")
115115
set (CMAKE_DEBUG_POSTFIX "" CACHE STRING "Library naming postfix for Debug builds (e.g., '_debug')")
116-
option (OSL_USTRINGREP_IS_HASH "Always use ustringhash for strings" OFF)
116+
option (OSL_USTRINGREP_IS_HASH "Always use ustringhash for strings" ON)
117117

118118

119119
set (OSL_NO_DEFAULT_TEXTURESYSTEM OFF CACHE BOOL "Do not use create a raw OIIO::TextureSystem")
120+
121+
if (OSL_USTRINGREP_IS_HASH)
122+
add_definitions ("-DOSL_USTRINGREP_IS_HASH=1")
123+
endif ()
120124
if (OSL_NO_DEFAULT_TEXTURESYSTEM)
121125
add_definitions ("-DOSL_NO_DEFAULT_TEXTURESYSTEM=1")
122126
endif ()

src/include/OSL/llvm_util.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ class OSLEXECPUBLIC LLVM_Util {
718718
/// Return an llvm::Value holding the given string constant (as
719719
/// determined by the ustring_rep).
720720
llvm::Value* constant(ustring s);
721+
llvm::Value* constant_real_ustring(ustring s);
721722
llvm::Value* constant(string_view s) { return constant(ustring(s)); }
722723

723724
llvm::Constant* constant_array(cspan<llvm::Constant*> constants);
@@ -750,6 +751,7 @@ class OSLEXECPUBLIC LLVM_Util {
750751
llvm::Constant* wide_constant(size_t i);
751752
llvm::Constant* wide_constant_bool(bool b);
752753
llvm::Value* wide_constant(ustring s);
754+
llvm::Value* wide_constant_real_ustring(ustring s);
753755
llvm::Value* wide_constant(string_view s)
754756
{
755757
return wide_constant(ustring(s));
@@ -1058,10 +1060,14 @@ class OSLEXECPUBLIC LLVM_Util {
10581060
IRBuilder& builder();
10591061

10601062
int m_debug;
1061-
bool m_dumpasm = false;
1062-
bool m_jit_fma = false;
1063-
bool m_jit_aggressive = false;
1063+
bool m_dumpasm = false;
1064+
bool m_jit_fma = false;
1065+
bool m_jit_aggressive = false;
1066+
#ifndef OSL_USTRINGREP_IS_HASH
10641067
UstringRep m_ustring_rep = UstringRep::charptr;
1068+
#else
1069+
UstringRep m_ustring_rep = UstringRep::hash;
1070+
#endif
10651071
PerThreadInfo::Impl* m_thread;
10661072
llvm::LLVMContext* m_llvm_context;
10671073
llvm::Module* m_llvm_module;

src/liboslexec/batched_backendllvm.cpp

+28-6
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ BatchedBackendLLVM::BatchedBackendLLVM(ShadingSystemImpl& shadingsys,
143143
case 8: m_true_mask_value = Mask<8>(true).value(); break;
144144
default: OSL_ASSERT(0 && "unsupported vector width");
145145
}
146+
147+
// Select the appropriate ustring representation
148+
ll.ustring_rep(LLVM_Util::UstringRep::hash);
149+
146150
ll.dumpasm(shadingsys.m_llvm_dumpasm);
147151
ll.jit_fma(shadingsys.m_llvm_jit_fma);
148152
ll.jit_aggressive(shadingsys.m_llvm_jit_aggressive);
@@ -191,7 +195,7 @@ BatchedBackendLLVM::llvm_pass_type(const TypeSpec& typespec)
191195
else if (t == TypeDesc::INT)
192196
lt = ll.type_int();
193197
else if (t == TypeDesc::STRING)
194-
lt = (llvm::Type*)ll.type_ustring();
198+
lt = (llvm::Type*)ll.type_real_ustring();
195199
else if (t.aggregate == TypeDesc::VEC3)
196200
lt = (llvm::Type*)ll.type_void_ptr(); //llvm_type_triple_ptr();
197201
else if (t.aggregate == TypeDesc::MATRIX44)
@@ -271,9 +275,9 @@ BatchedBackendLLVM::llvm_assign_zero(const Symbol& sym)
271275
zero = ll.wide_constant(0);
272276
} else if (elemtype.is_string_based()) {
273277
if (sym.is_uniform())
274-
zero = ll.constant(ustring());
278+
zero = ll.constant(uint64_t(0));
275279
else
276-
zero = ll.wide_constant(ustring());
280+
zero = ll.wide_constant(uint64_t(0));
277281
} else if (elemtype.is_closure_based()) {
278282
if (sym.is_uniform())
279283
zero = ll.void_ptr_null();
@@ -715,7 +719,8 @@ llvm::Value*
715719
BatchedBackendLLVM::llvm_load_value(const Symbol& sym, int deriv,
716720
llvm::Value* arrayindex, int component,
717721
TypeDesc cast, bool op_is_uniform,
718-
bool index_is_uniform)
722+
bool index_is_uniform,
723+
bool always_real_ustring)
719724
{
720725
// A uniform symbol can be broadcast into a varying value.
721726
// But a varying symbol can NOT be loaded into a uniform value.
@@ -780,9 +785,15 @@ BatchedBackendLLVM::llvm_load_value(const Symbol& sym, int deriv,
780785
if (sym.typespec().is_string()) {
781786
ustring string_val = sym.get_string();
782787
if (op_is_uniform) {
783-
return ll.constant(string_val);
788+
if (!always_real_ustring)
789+
return ll.constant(string_val);
790+
else
791+
return ll.constant_real_ustring(string_val);
784792
} else {
785-
return ll.wide_constant(string_val);
793+
if (!always_real_ustring)
794+
return ll.wide_constant(string_val);
795+
else
796+
return ll.wide_constant_real_ustring(string_val);
786797
}
787798
}
788799
OSL_ASSERT(0 && "unhandled constant type");
@@ -796,7 +807,17 @@ BatchedBackendLLVM::llvm_load_value(const Symbol& sym, int deriv,
796807
sym.forced_llvm_bool());
797808
}
798809

810+
llvm::Value*
811+
BatchedBackendLLVM::llvm_const_hash(string_view str)
812+
{
813+
return llvm_const_hash(ustring(str));
814+
}
799815

816+
llvm::Value*
817+
BatchedBackendLLVM::llvm_const_hash(ustring str)
818+
{
819+
return ll.constant64((uint64_t)str.hash());
820+
}
800821

801822
llvm::Value*
802823
BatchedBackendLLVM::llvm_load_mask(const Symbol& cond)
@@ -1717,6 +1738,7 @@ BatchedBackendLLVM::llvm_call_function(const FuncSpec& name,
17171738
= llvm_load_value(s, /*deriv=*/d,
17181739
/*component*/ c, TypeUnknown,
17191740
function_is_uniform);
1741+
17201742
// Store our wide pointer on the stack
17211743
llvm_store_value(wide_value, tmpptr, t, d, NULL, c,
17221744
/*dst_is_uniform*/ false);

src/liboslexec/batched_backendllvm.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,14 @@ class BatchedBackendLLVM : public OSOProcessorBase {
9898
/// performed if cast is the default of UNKNOWN).
9999
llvm::Value* llvm_load_value(const Symbol& sym, int deriv,
100100
llvm::Value* arrayindex, int component,
101-
TypeDesc cast = TypeDesc::UNKNOWN,
102-
bool op_is_uniform = true,
103-
bool index_is_uniform = true);
101+
TypeDesc cast = TypeDesc::UNKNOWN,
102+
bool op_is_uniform = true,
103+
bool index_is_uniform = true,
104+
bool always_real_ustring = false);
104105

106+
llvm::Value* llvm_const_hash(string_view str);
107+
108+
llvm::Value* llvm_const_hash(ustring str);
105109

106110
/// Given an llvm::Value* of a pointer (and the type of the data
107111
/// that it points to), Return the llvm::Value* corresponding to the

src/liboslexec/batched_llvm_gen.cpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#endif
1616

1717
#include "batched_backendllvm.h"
18-
18+
#include <llvm/Support/raw_ostream.h>
1919

2020

2121
using namespace OSL;
@@ -375,6 +375,7 @@ LLVMGEN(llvm_gen_printf)
375375
std::string ourformat(oldfmt, format); // straddle the format
376376
// Doctor it to fix mismatches between format and data
377377
Symbol& sym(*rop.opargsym(op, arg));
378+
378379
OSL_ASSERT(!sym.typespec().is_structure_based());
379380

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

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

488+
485489
// The format op returns a string value, put in in the right spot
486490
if (op.opname() == op_format)
487491
rop.llvm_store_value(ret, *rop.opargsym(op, 0));
@@ -861,6 +865,7 @@ LLVMGEN(llvm_gen_generic)
861865
&& (uniformFormOfFunction || functionIsLlvmInlined)) {
862866
OSL_DEV_ONLY(std::cout << ">>stores return value "
863867
<< rop.build_name(func_spec) << std::endl);
868+
864869
llvm::Value* r = rop.llvm_call_function(
865870
func_spec, &(args[1]), op.nargs() - 1,
866871
/*deriv_ptrs*/ false, uniformFormOfFunction,
@@ -3300,7 +3305,7 @@ LLVMGEN(llvm_gen_construct_triple)
33003305
= { rop.sg_void_ptr(), rop.ll.void_ptr(transform),
33013306
space_is_uniform ? rop.llvm_load_value(Space)
33023307
: rop.llvm_void_ptr(Space),
3303-
rop.ll.constant(Strings::common),
3308+
rop.llvm_const_hash(Strings::common),
33043309
rop.ll.mask_as_int(rop.ll.current_mask()) };
33053310

33063311
// Dynamically build function name

src/liboslexec/batched_llvm_instance.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1038,9 +1038,8 @@ BatchedBackendLLVM::llvm_assign_initial_value(
10381038
: ll.wide_constant(std::numeric_limits<int>::min());
10391039
}
10401040
} else if (sym.typespec().is_string_based()) {
1041-
u = sym.is_uniform()
1042-
? ll.constant(Strings::uninitialized_string)
1043-
: ll.wide_constant(Strings::uninitialized_string);
1041+
u = sym.is_uniform() ? ll.constant(uint64_t(0))
1042+
: ll.wide_constant(uint64_t(0));
10441043
}
10451044
if (u) {
10461045
//std::cout << "Assigning uninit value to symbol=" << sym.name().c_str() << std::endl;

src/liboslexec/builtindecl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ DECL(osl_closure_to_string, "sXX")
133133
DECL(osl_closure_to_ustringhash, "hXX")
134134
#endif
135135
DECL(osl_format, "hh*")
136-
DECL(osl_gen_ustringhash_pod, "hs")
136+
DECL(osl_gen_ustringhash_pod, "hh")
137137
DECL(osl_gen_ustring, "sh")
138138
DECL(osl_gen_printfmt, "xXhiXiX")
139139
DECL(osl_gen_filefmt, "xXhhiXiX")

0 commit comments

Comments
 (0)