Skip to content

Commit

Permalink
LLVM 16 and 17 support (#1730)
Browse files Browse the repository at this point in the history
* Bump minimum supported LLVM version to 17
* Rename `makeArrayRef` to `toArrayRef` to avoid `llvm::makeArrayRef`
  deprecation warnings.
* Minor required changes related to `llvm::Align`.
* Disable `initializeInstrumentation` and `createPruneEHPass` which
  were remove and appear to serve no purpose now.
* Add assert when using default optimization levels with legacy pass
  manager, which is no longer supported.

This depends on both opaque pointers and new pass manager support.

Signed-off-by: Brecht Van Lommel <brecht@blender.org>

* Keep CMAKE_CXX_STANDARD warning for LLVM 16

* Enable LLVM 17 too, adjust for changes in new pass manager PR

* Fix OptiX data layout mismatch with LLVM 16+

i128:128 was already in the NVPTX backend since LLVM 6, the docs
this was taken from seem outdated. Unclear it started failing now,
but the data layout seems clearly incomplete without this.

* Modify CI to run the bleeding edge test on LLVM 16

* Manually make global variable names valid for PTX

* Fix build with older LLVM

---------

Signed-off-by: Brecht Van Lommel <brecht@blender.org>
Co-authored-by: Larry Gritz <lg@larrygritz.com>
Co-authored-by: Tim Grant <tgrant@nvidia.com>
  • Loading branch information
3 people authored Oct 4, 2023
1 parent 3567aac commit cdfec4f
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 115 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ jobs:
setenvs: export LLVM_VERSION=10.0.0
OPENIMAGEIO_CMAKE_FLAGS="-DBUILD_FMT_VERSION=7.0.1"
PUGIXML_VERSION=v1.10
- desc: latest releases gcc11/C++17 llvm13 boost1.71 exr3.2 py3.9 avx2 batch-b16avx512
- desc: latest releases gcc11/C++17 llvm15 boost1.71 exr3.2 py3.9 avx2 batch-b16avx512
nametag: linux-latest-releases
os: ubuntu-22.04
cxx_compiler: g++-11
Expand All @@ -346,11 +346,11 @@ jobs:
python_ver: "3.10"
simd: avx2,f16c
batched: b8_AVX2,b8_AVX512,b16_AVX512
setenvs: export LLVM_VERSION=14.0.0
setenvs: export LLVM_VERSION=15.0.6
LLVM_DISTRO_NAME=ubuntu-18.04
OPENCOLORIO_VERSION=v2.2.0
PUGIXML_VERSION=v1.13
- desc: bleeding edge gcc12/C++17 llvm14 oiio/ocio/exr/pybind-master boost1.71 py3.10 avx2 batch-b16avx512
- desc: bleeding edge gcc12/C++17 llvm16 oiio/ocio/exr/pybind-master boost1.71 py3.10 avx2 batch-b16avx512
nametag: linux-bleeding-edge
os: ubuntu-22.04
cxx_compiler: g++-12
Expand All @@ -362,8 +362,8 @@ jobs:
python_ver: "3.10"
simd: avx2,f16c
batched: b8_AVX2,b8_AVX512,b16_AVX512
setenvs: export LLVM_VERSION=15.0.6
LLVM_DISTRO_NAME=ubuntu-18.04
setenvs: export LLVM_VERSION=16.0.4
LLVM_DISTRO_NAME=ubuntu-22.04
OPENCOLORIO_VERSION=main
PUGIXML_VERSION=master
- desc: clang14/C++17 llvm14 boost1.71 exr3.1 py3.8 avx2 batch-b16avx512
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 @@ -207,8 +207,8 @@ function ( CUDA_SHADEOPS_COMPILE prefix output_bc output_ptx input_srcs headers
endforeach ()

if (LLVM_NEW_PASS_MANAGER)
# --nvptx-assign-valid-global-names does not appaer to be supported
# by either opt or llc with the new pass manager.
# There is no --nvptx-assign-valid-global-names flag for the new
# pass manager, but it appears to run this pass by default.
string(REPLACE "-O" "O" opt_tool_flags ${CUDA_OPT_FLAG_CLANG})
set (opt_tool_flags -passes="default<${opt_tool_flags}>")
else()
Expand Down
3 changes: 1 addition & 2 deletions src/cmake/externalpackages.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ checked_find_package (pugixml REQUIRED
# LLVM library setup
checked_find_package (LLVM REQUIRED
VERSION_MIN 9.0
VERSION_MAX 15.9
VERSION_MAX 17.9
PRINT LLVM_SYSTEM_LIBRARIES CLANG_LIBRARIES)
# ensure include directory is added (in case of non-standard locations
include_directories (BEFORE SYSTEM "${LLVM_INCLUDES}")
Expand Down Expand Up @@ -148,7 +148,6 @@ if (LLVM_VERSION VERSION_GREATER_EQUAL 15.0
"15 or higher, or you may get build errors.${ColorReset}\n")
endif ()
if (LLVM_VERSION VERSION_GREATER_EQUAL 16.0)
message (ERROR "${ColorYellow}OSL is not yet compatible with LLVM 16.${ColorReset}\n")
if (CMAKE_CXX_STANDARD VERSION_LESS 17)
message (WARNING "${ColorYellow}LLVM 16+ requires C++17 or higher. "
"Please set CMAKE_CXX_STANDARD to 17 or higher.${ColorReset}\n")
Expand Down
5 changes: 3 additions & 2 deletions src/cmake/modules/FindLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ if (LLVM_VERSION VERSION_GREATER_EQUAL 9.0 AND (LLVM_SHARED_MODE STREQUAL "share
endif ()

foreach (COMPONENT clangFrontend clangDriver clangSerialization
clangParse clangSema clangAnalysis clangAST clangBasic
clangEdit clangLex clangSupport)
clangParse clangSema clangAnalysis clangAST
clangASTMatchers clangBasic clangEdit clangLex
clangSupport)
find_library ( _CLANG_${COMPONENT}_LIBRARY
NAMES ${COMPONENT}
PATHS ${LLVM_LIB_DIR})
Expand Down
3 changes: 2 additions & 1 deletion src/include/OSL/oslexec.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class ShadingSystemImpl;
}

#ifdef __CUDA_ARCH__
# define STRING_PARAMS(x) UStringHash::Hash(__OSL_STRINGIFY(x))
# define STRING_PARAMS(x) \
UStringHash::HashConstEval<UStringHash::Hash(__OSL_STRINGIFY(x))>
#else
# define STRING_PARAMS(x) StringParams::x
#endif
Expand Down
18 changes: 14 additions & 4 deletions src/liboslexec/llvm_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ BackendLLVM::run()
// The target triple and data layout used here are those specified
// for NVPTX (https://www.llvm.org/docs/NVPTXUsage.html#triples).
ll.module()->setDataLayout(
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64");
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64");
ll.module()->setTargetTriple("nvptx64-nvidia-cuda");
}
# endif
Expand Down Expand Up @@ -1816,7 +1816,7 @@ BackendLLVM::run()
err);

shadeops_module->setDataLayout(
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64");
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64");
shadeops_module->setTargetTriple("nvptx64-nvidia-cuda");

std::unique_ptr<llvm::Module> shadeops_ptr(shadeops_module);
Expand All @@ -1843,7 +1843,7 @@ BackendLLVM::run()
err);

rend_lib_module->setDataLayout(
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64");
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64");
rend_lib_module->setTargetTriple("nvptx64-nvidia-cuda");

for (llvm::Function& fn : *rend_lib_module) {
Expand All @@ -1863,7 +1863,7 @@ BackendLLVM::run()
// See: https://llvm.org/docs/NVPTXUsage.html
ll.module()->setTargetTriple("nvptx64-nvidia-cuda");
ll.module()->setDataLayout(
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64");
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64");

// Tag each function as an OSL library function to help with
// inlining and optimization after codegen.
Expand All @@ -1877,6 +1877,16 @@ BackendLLVM::run()
global.setLinkage(llvm::GlobalValue::ExternalLinkage);
global.setExternallyInitialized(true);
global.setInitializer(nullptr);
// Replace characters not supported in ptx, matching the LLVM
// NVPTXAssignValidGlobalNames pass.
string_view global_name(global.getName().data(),
global.getName().size());
if (Strutil::contains_any_char(global_name, ".@")) {
std::string valid_name = global_name;
valid_name = Strutil::replace(valid_name, ".", "_$_", true);
valid_name = Strutil::replace(valid_name, "@", "_$_", true);
global.setName(valid_name);
}
}
}
OSL_ASSERT(ll.module());
Expand Down
Loading

0 comments on commit cdfec4f

Please sign in to comment.