Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rs_free_function #1494

Merged
merged 4 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ include (pythonutils)
include (externalpackages)
include (flexbison)
include (cuda_macros)
include (llvm_macros)

# Include all our testing apparatus and utils, but not if it's a subproject
if (${PROJECT_NAME}_BUILD_TESTS AND PROJECT_IS_TOP_LEVEL)
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions src/cmake/compiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,16 @@ if (PROJECT_IS_TOP_LEVEL)
"src/liboslcomp/*.cpp"
"src/liboslcomp/*.h"
"src/liboslexec/batched_rendservices.cpp"
"src/liboslexec/rs_fallback.cpp"
"src/liboslquery/*.cpp"
"src/liboslquery/*.h"
"src/oslinfo/*.cpp"
"src/oslc/*.cpp"
"src/osl.imageio/*.cpp"
"src/osltoy/*.cpp"
"src/osltoy/*.h"
"src/testshade/render_state.h"
"src/testshade/rs_simplerend.cpp"
"testsuite/*.cpp"
CACHE STRING "Glob patterns to include for clang-format")
set (CLANG_FORMAT_EXCLUDES
Expand Down
4 changes: 2 additions & 2 deletions src/cmake/cuda_macros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ function ( LLVM_COMPILE_CUDA llvm_src headers prefix llvm_bc_cpp_generated extra
MAKE_CUDA_BITCODE (${llvm_src} "" llvm_bc "${extra_clang_args}")

add_custom_command (OUTPUT ${llvm_bc_cpp}
COMMAND python "${CMAKE_SOURCE_DIR}/src/liboslexec/serialize-bc.py" ${llvm_bc} ${llvm_bc_cpp} ${prefix}
COMMAND python "${CMAKE_SOURCE_DIR}/src/build-scripts/serialize-bc.py" ${llvm_bc} ${llvm_bc_cpp} ${prefix}
MAIN_DEPENDENCY ${llvm_src}
DEPENDS "${CMAKE_SOURCE_DIR}/src/liboslexec/serialize-bc.py" ${llvm_src} ${headers} ${llvm_bc}
DEPENDS "${CMAKE_SOURCE_DIR}/src/build-scripts/serialize-bc.py" ${llvm_src} ${headers} ${llvm_bc}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" )
endfunction ()
128 changes: 128 additions & 0 deletions src/cmake/llvm_macros.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Copyright Contributors to the Open Shading Language project.
# SPDX-License-Identifier: BSD-3-Clause
# https://github.com/AcademySoftwareFoundation/OpenShadingLanguage


function ( EMBED_LLVM_BITCODE_IN_CPP src_list suffix output_name list_to_append_cpp extra_clang_args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to unify this with MAKE_CUDA_BITCODE at some point, since they're largely identical. And to generalize to other targets like SPIR-V. But that can wait until we're compiling for other non-host targets.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, they are very similar, but our ability to test the CUDA path is limited and we don't want to break it by accident, or create merge issues for any changes in the MAKE_CUDA_BITCODE that might happen. Maybe an interim solution would be to move both macros to a common file, so at least there right next to each other for reference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is just fine for now. It'll be something to keep in mind when the time comes to add MAKE_SPIRV_BITCODE, or whatever the next target might be.


if (VERBOSE)
message (STATUS "EMBED_LLVM_BITCODE_IN_CPP src_list=${src_list}")
endif ()

foreach ( src ${src_list} )
get_filename_component ( src_we ${src} NAME_WE )
set ( src_asm "${CMAKE_CURRENT_BINARY_DIR}/${src_we}${suffix}.s" )
set ( src_bc "${CMAKE_CURRENT_BINARY_DIR}/${src_we}${suffix}.bc" )
if (VERBOSE)
message (STATUS "EMBED_LLVM_BITCODE_IN_CPP in=${src}")
message (STATUS "EMBED_LLVM_BITCODE_IN_CPP asm=${src_asm}")
message (STATUS "EMBED_LLVM_BITCODE_IN_CPP bc=${src_bc}")
endif ()
list ( APPEND src_bc_list ${src_bc} )

get_property (CURRENT_DEFINITIONS DIRECTORY PROPERTY COMPILE_DEFINITIONS)
if (VERBOSE)
message (STATUS "Current #defines are ${CURRENT_DEFINITIONS}")
endif ()
foreach (def ${CURRENT_DEFINITIONS})
set (LLVM_COMPILE_FLAGS ${LLVM_COMPILE_FLAGS} "-D${def}")
endforeach()
set (LLVM_COMPILE_FLAGS ${LLVM_COMPILE_FLAGS} ${SIMD_COMPILE_FLAGS} ${CSTD_FLAGS} ${TOOLCHAIN_FLAGS})
# Avoid generating __dso_handle external global
set (LLVM_COMPILE_FLAGS ${LLVM_COMPILE_FLAGS} "-fno-use-cxa-atexit")

# Figure out what program we will use to make the bitcode.
if (NOT LLVM_BC_GENERATOR)
find_program (LLVM_BC_GENERATOR NAMES "clang++"
PATHS "${LLVM_DIRECTORY}/bin"
NO_CMAKE_PATH NO_DEFAULT_PATH NO_CMAKE_SYSTEM_PATH
NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_ENVIRONMENT_PATH)
endif ()
# If that didn't work, look anywhere
if (NOT LLVM_BC_GENERATOR)
# Wasn't in their build, look anywhere
find_program (LLVM_BC_GENERATOR NAMES clang++ llvm-g++)
endif ()

if (NOT LLVM_BC_GENERATOR)
message (FATAL_ERROR "You must have a valid llvm bitcode generator (clang++) somewhere.")
endif ()
if (VERBOSE)
message (STATUS "Using LLVM_BC_GENERATOR ${LLVM_BC_GENERATOR} to generate bitcode.")
endif()

# Fix specific problem I had on new Apple systems (e.g. Mavericks) with
# LLVM/libc++ installed -- for some reason, LLVM 3.4 wasn't finding it,
# so in that specific case, append another -I to point it in the right
# direction.
#if (APPLE AND ${LLVM_BC_GENERATOR} MATCHES ".*clang.*")
# exec_program ( "${LLVM_BC_GENERATOR}" ARGS --version OUTPUT_VARIABLE MY_CLANG_VERSION )
# string (REGEX REPLACE "clang version ([0-9][.][0-9]+).*" "\\1" MY_CLANG_VERSION "${MY_CLANG_VERSION}")
# if ((${MY_CLANG_VERSION} VERSION_GREATER "3.3")
# AND (EXISTS "/usr/lib/libc++.dylib")
# AND (EXISTS "/Library/Developer/CommandLineTools/usr/lib/c++/v1"))
# set (LLVM_COMPILE_FLAGS ${LLVM_COMPILE_FLAGS} "-I/Library/Developer/CommandLineTools/usr/lib/c++/v1")
# endif ()
#endif ()

list (TRANSFORM IMATH_INCLUDES PREPEND -I
OUTPUT_VARIABLE ALL_IMATH_INCLUDES)
list (TRANSFORM OPENEXR_INCLUDES PREPEND -I
OUTPUT_VARIABLE ALL_OPENEXR_INCLUDES)
list (TRANSFORM OpenImageIO_INCLUDES PREPEND -I
OUTPUT_VARIABLE ALL_OpenImageIO_INCLUDES)


# Command to turn the .cpp file into LLVM assembly language .s, into
# LLVM bitcode .bc, then back into a C++ file with the bc embedded!
add_custom_command ( OUTPUT ${src_bc}
COMMAND ${LLVM_BC_GENERATOR}
${LLVM_COMPILE_FLAGS}
"-I${CMAKE_CURRENT_SOURCE_DIR}"
"-I${CMAKE_SOURCE_DIR}/src/include"
"-I${CMAKE_BINARY_DIR}/include"
${ALL_OpenImageIO_INCLUDES}
${ALL_IMATH_INCLUDES}
#"-isystem ${Boost_INCLUDE_DIRS}" #Does not pick up usr installed boost/thread/tss.hpp for oslexec_pvt.h
"-I${Boost_INCLUDE_DIRS}"
-DOSL_COMPILING_TO_BITCODE=1
-Wno-deprecated-register
# the following 2 warnings can be restored when all 3rd parties have fixed their export macros
# (dllimport attribute is not supported when compiling for Cuda and triggers a ton of warnings)
-Wno-ignored-attributes -Wno-unknown-attributes
-O3 -fno-math-errno -S -emit-llvm ${extra_clang_args}
-o ${src_asm} ${src}
COMMAND "${LLVM_DIRECTORY}/bin/llvm-as" -f -o ${src_bc} ${src_asm}
# Do NOT setup a MAIN_DEPENDENCY because only 1 may exist
# and we may have the several outputs dependant on the same source
DEPENDS ${src} ${exec_headers} ${PROJECT_PUBLIC_HEADERS}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" )
endforeach ()


if (VERBOSE)
message ( STATUS "^^^^^^^^^^^^^^^^^^^^^^^^^^" )
message ( STATUS "src_bc_list: ${src_bc_list} ")
message ( STATUS "^^^^^^^^^^^^^^^^^^^^^^^^^^" )
endif()

# Link all of the individual LLVM bitcode files
set ( linked_src_bc "${CMAKE_CURRENT_BINARY_DIR}/${output_name}.bc" )
add_custom_command ( OUTPUT ${linked_src_bc}
COMMAND "${LLVM_DIRECTORY}/bin/llvm-link" -internalize ${src_bc_list} -o ${linked_src_bc}
DEPENDS ${src_bc_list} ${exec_headers} ${PROJECT_PUBLIC_HEADERS} ${src_list}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" )

# Serialize the linked bitcode into a CPP file
set ( src_bc_cpp "${CMAKE_CURRENT_BINARY_DIR}/${output_name}.bc.cpp" )
add_custom_command ( OUTPUT ${src_bc_cpp}
COMMAND python "${CMAKE_SOURCE_DIR}/src/build-scripts/serialize-bc.py"
${linked_src_bc} ${src_bc_cpp} ${output_name}
DEPENDS "${CMAKE_SOURCE_DIR}/src/build-scripts/serialize-bc.py" ${linked_src_bc}
${exec_headers} ${PROJECT_PUBLIC_HEADERS}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" )

# add generated .cpp with embedded bitcode to the list of soures
set ( ${list_to_append_cpp} ${${list_to_append_cpp}} ${src_bc_cpp} PARENT_SCOPE )

endfunction ( )
21 changes: 21 additions & 0 deletions src/cmake/testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ macro ( TESTSUITE )
endif ()
set (test_all_optix $ENV{TESTSUITE_OPTIX})
set (test_all_batched $ENV{TESTSUITE_BATCHED})
set (test_all_rs_bitcode $ENV{TESTSUITE_RS_BITCODE})
# Add the tests if all is well.
set (ALL_TEST_LIST "")
set (_testsuite "${CMAKE_SOURCE_DIR}/testsuite")
Expand Down Expand Up @@ -180,6 +181,25 @@ macro ( TESTSUITE )
ENV TESTSHADE_OPT=0 OSL_REGRESSION_TEST=BATCHED )
endif ()
endif ()

# if there is an RS_BITCODE marker file in the directory.
if ((EXISTS "${_testsrcdir}/RS_BITCODE" OR test_all_rs_bitcode)
AND NOT EXISTS "${_testsrcdir}/BATCHED_REGRESSION"
AND NOT EXISTS "${_testsrcdir}/RS_BITCODE_REGRESSION"
AND NOT EXISTS "${_testsrcdir}/NOOPTIMIZE")
add_one_testsuite ("${_testname}.rsbitcode.opt" "${_testsrcdir}"
ENV TESTSHADE_OPT=2 TESTSHADE_RS_BITCODE=1 )
endif ()

# if there is an RS_BITCODE_REGRESSION marker file in the directory.
if (EXISTS "${_testsrcdir}/RS_BITCODE_REGRESSION" OR (
test_all_rs_bitcode AND EXISTS "${_testsrcdir}/BATCHED_REGRESSION")
AND NOT EXISTS "${_testsrcdir}/NOOPTIMIZE")
# optimized for right now
add_one_testsuite ("${_testname}.regress.rsbitcode.opt" "${_testsrcdir}"
ENV TESTSHADE_OPT=2 OSL_REGRESSION_TEST=RS_BITCODE )
endif ()

endforeach ()
if (VERBOSE)
message (STATUS "Added tests: ${ALL_TEST_LIST}")
Expand Down Expand Up @@ -348,5 +368,6 @@ macro (osl_add_all_tests)
set_tests_properties (arithmetic-reg.regress.batched.opt PROPERTIES TIMEOUT 800)
set_tests_properties (transform-reg.regress.batched.opt PROPERTIES TIMEOUT 800)
endif ()
set_tests_properties (matrix-reg.regress.rsbitcode.opt PROPERTIES TIMEOUT 800)

endmacro()
8 changes: 7 additions & 1 deletion src/include/OSL/device_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ namespace DeviceStrings {
#undef STRDECL
}
#else
# define STRING_PARAMS(x) StringParams::x
# ifdef OSL_HOST_RS_BITCODE
# define STRING_PARAMS(x) RS_##x
# else
# define STRING_PARAMS(x) StringParams::x
# endif
#endif


Expand All @@ -146,3 +150,5 @@ namespace StringParams = OSL_NAMESPACE::DeviceStrings;
#else
namespace StringParams = OSL_NAMESPACE::Strings;
#endif


14 changes: 14 additions & 0 deletions src/include/OSL/llvm_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ class OSLEXECPUBLIC LLVM_Util {
// Name for this TargetISA enum.
static const char* target_isa_name(TargetISA isa);

/// Add a global mapping of a variable to its address
/// explicitly instead of relying on dlsym.
static void add_global_mapping (const char *global_var_name, void *global_var_addr);

/// Check all external global variables to ensure they have
/// been mapped to an address, a vector is populated with
/// the names that are unmapped.
void validate_global_mappings (std::vector<std::string> &names_of_unmapped_globals);

// For CPU compilation, inventory the host CPU capabilities. You can
// optionally request a specific ISA by name. If `no_fma` is true,
// specifically pretend there is no FMA capability, even if the hardware
Expand Down Expand Up @@ -908,6 +917,11 @@ class OSLEXECPUBLIC LLVM_Util {
/// file. If err is not NULL, errors will be deposited there.
void write_bitcode_file (const char *filename, std::string *err=NULL);

/// Insert bitcode from another module into the current Module
/// returns true if successful, false otherwise.
/// Ownership of the other_module is transferred.
bool absorb_module (std::unique_ptr<llvm::Module> other_module );

/// Generate PTX for the current Module and return it as a string
bool ptx_compile_group (llvm::Module* lib_module, const std::string& name,
std::string& out);
Expand Down
9 changes: 6 additions & 3 deletions src/include/OSL/oslexec.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ template<typename DataT, int WidthT> struct Wide;
#endif


/// RendererServices free function bitcode may reference global variables that
/// only exist in the renderer's program space and won't automatically be
/// found when referenced in JIT'd code inside OSL. A renderer may register
/// the addresses of these global variables to allow valid JIT to occur.
void register_JIT_Global(const char* global_var_name, void* global_var_addr);

/// Opaque pointer to whatever the renderer uses to represent a
/// (potentially motion-blurred) coordinate transformation.
Expand All @@ -45,9 +50,7 @@ class ShadingSystemImpl;
}

#if defined(__CUDA_ARCH__) && OPTIX_VERSION >= 70000
# define STRINGIFY(x) XSTR(x)
# define XSTR(x) #x
# define STRING_PARAMS(x) UStringHash::Hash(STRINGIFY(x))
# define STRING_PARAMS(x) UStringHash::Hash(__OSL_STRINGIFY(x))
#else
# define STRING_PARAMS(x) StringParams::x
#endif
Expand Down
3 changes: 3 additions & 0 deletions src/include/OSL/oslversion.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,7 @@ namespace @PROJ_NAME@ = @PROJ_NAMESPACE_V@;
#define __OSL_WIDE_PVT __OSL_CONCAT5(b,__OSL_WIDTH,_,__OSL_TARGET_ISA,_pvt)
#endif

#define __OSL_INDIRECT_STRINGIFY(x) #x
#define __OSL_STRINGIFY(x) __OSL_INDIRECT_STRINGIFY(x)

#endif /* OSLVERSION_H */
1 change: 1 addition & 0 deletions src/include/OSL/rendererservices.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ OSL_GCC_PRAGMA(GCC diagnostic ignored "-Wunused-parameter")
/// RendererServices defines an abstract interface through which a
/// renderer may provide callback to the ShadingSystem.
class OSLEXECPUBLIC RendererServices {
// Keep interface in sync with rs_free_function.h
public:
typedef TextureSystem::TextureHandle TextureHandle;
typedef TextureSystem::Perthread TexturePerthread;
Expand Down
Loading