Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.
Closed
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
37 changes: 37 additions & 0 deletions cmake/findllvm.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
##
#######################################################################################################################
#
# Copyright (c) 2024 Advanced Micro Devices, Inc. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
#######################################################################################################################

if (NOT LLPC_LLVM_SRC_PATH)
# Find LLVM source. Allow client driver to override using its own name for overlay builds.
set(DEFAULT_LLPC_LLVM_SRC_PATH ${XGL_LLVM_SRC_PATH})
if (NOT DEFAULT_LLPC_LLVM_SRC_PATH)
if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/../../../imported/llvm-project/llvm)
set(DEFAULT_LLPC_LLVM_SRC_PATH ${CMAKE_CURRENT_LIST_DIR}/../../../imported/llvm-project/llvm)
elseif(EXISTS ${CMAKE_CURRENT_LIST_DIR}/../../llvm-project/llvm)
set(DEFAULT_LLPC_LLVM_SRC_PATH ${CMAKE_CURRENT_LIST_DIR}/../../llvm-project/llvm)
endif()
endif()
set(LLPC_LLVM_SRC_PATH ${DEFAULT_LLPC_LLVM_SRC_PATH} CACHE PATH "Specify the path to LLVM.")
endif()
129 changes: 129 additions & 0 deletions cmake/llvm.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
##
#######################################################################################################################
#
# Copyright (c) 2024 Advanced Micro Devices, Inc. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
#######################################################################################################################

# Build LLVM, using previously set up LLVM_EXTERNAL_PROJECTS and LLVM_EXTERNAL_*_SOURCE_DIR.
# Relies on findllvm.cmake being run first.

if (NOT LLPC_LLVM_SRC_PATH)
message(FATAL_ERROR "No LLPC_LLVM_SRC_PATH specified")
endif()

# Set cached options.
set(LLVMRAYTRACING_BUILD_TESTS ${LLPC_BUILD_TESTS})
set(LLVM_TARGETS_TO_BUILD AMDGPU CACHE STRING "LLVM targets to build")
set(LLVM_BUILD_TESTS OFF CACHE BOOL "LLVM build tests")
set(LLVM_BUILD_TOOLS ${LLPC_BUILD_LLVM_TOOLS} CACHE BOOL "LLVM build tools")
set(LLVM_BUILD_UTILS OFF CACHE BOOL "LLVM build utils")
set(LLVM_INCLUDE_DOCS OFF CACHE BOOL "LLVM include docs")
set(LLVM_INCLUDE_EXAMPLES OFF CACHE BOOL "LLVM include examples")
set(LLVM_INCLUDE_GO_TESTS OFF CACHE BOOL "LLVM include go tests")
set(LLVM_INCLUDE_TESTS ${LLPC_BUILD_TESTS} CACHE BOOL "LLVM include tests")
set(LLVM_INCLUDE_TOOLS ON CACHE BOOL "LLVM include tools")
set(LLVM_INCLUDE_UTILS ON CACHE BOOL "LLVM include utils")
set(LLVM_ENABLE_TERMINFO OFF CACHE BOOL "LLVM enable terminfo")
set(LLVM_RAM_PER_TABLEGEN_JOB 4000 CACHE STRING "LLVM RAM per tablegen job")
set(LLVM_RAM_PER_LINK_JOB 5000 CACHE STRING "LLVM RAM per link job")
if(CMAKE_BUILD_TYPE_DEBUG)
# Build optimized version of llvm-tblgen even in debug builds, for faster build times.
set(LLVM_OPTIMIZED_TABLEGEN ON CACHE BOOL "Build optimized llvm-tblgen")
#if _WIN32
if(LLVM_OPTIMIZED_TABLEGEN AND WIN32 AND (CMAKE_GENERATOR MATCHES "Ninja"))
# LLVM implements the Release build of llvm-tblgen as a cross-compile target, which fails to find
# our DK-based toolchain (created with amd_generate_msvc_toolchain). However, we can inject the toolchain
# argument into LLVM's add_custom_target that sets up this cross-compile build.
# See: llvm-project/llvm/cmake/modules/CrossCompile.cmake
set(CROSS_TOOLCHAIN_FLAGS_NATIVE "-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}" CACHE STRING
"Toolchain flags for native build" FORCE)
endif()
#endif
endif()

# This will greatly speed up debug builds because we won't be listing all the symbols with llvm-nm.
set(LLVM_BUILD_LLVM_C_DYLIB OFF CACHE BOOL "LLVM build LLVM-C dylib")

# Remove /nologo from CMAKE_RC_FLAGS to avoid getting an error from specifying it twice in LLVM.
if (CMAKE_RC_FLAGS)
string(REPLACE "/nologo" "" CMAKE_RC_FLAGS ${CMAKE_RC_FLAGS})
endif()

# Build LLVM.
if (NOT LLPC_LLVM_BUILD_PATH)
set(LLPC_LLVM_BUILD_PATH ${PROJECT_BINARY_DIR}/llvm)
endif()
if (ICD_BUILD_LLPC)
add_subdirectory(${LLPC_LLVM_SRC_PATH} ${LLPC_LLVM_BUILD_PATH})
else()
add_subdirectory(${LLPC_LLVM_SRC_PATH} ${LLPC_LLVM_BUILD_PATH} EXCLUDE_FROM_ALL)
endif()

# Get LLVMConfig onto cmake path.
list(APPEND CMAKE_MODULE_PATH
"${LLPC_LLVM_BUILD_PATH}/lib/cmake/llvm"
"${LLPC_LLVM_BUILD_PATH}/${CMAKE_CFG_INTDIR}/lib/cmake/llvm" # Workaround for VS generator with older LLVM.
)

# Export LLVM build path for client driver.
# TODO: Change uses to LLPC_LLVM_BUILD_PATH.
set(XGL_LLVM_BUILD_PATH ${LLPC_LLVM_BUILD_PATH} PARENT_SCOPE)

# Extract LLVM revision number for code outside the LLPC repository to use.
file(READ "${LLPC_LLVM_SRC_PATH}/include/llvm/Config/llvm-config.h.cmake" LLVM_CONFIG_HEADER)
string(REGEX MATCH "#define LLVM_MAIN_REVISION ([0-9]+)" "\\1" _ "${LLVM_CONFIG_HEADER}")
set(LLVM_MAIN_REVISION "${CMAKE_MATCH_1}")
set(LLVM_MAIN_REVISION ${LLVM_MAIN_REVISION} PARENT_SCOPE)

# Some of the games using old versions of the tcmalloc lib are crashing
# when allocating aligned memory. C++17 enables aligned new by default,
# so we need to disable it to prevent those crashes.
if (ICD_BUILD_LLPC AND NOT WIN32)
llvm_map_components_to_libnames(llvm_libs
AMDGPUAsmParser
AMDGPUCodeGen
AMDGPUDisassembler
AMDGPUInfo
Analysis
BinaryFormat
Core
Coroutines
BitReader
BitWriter
CodeGen
InstCombine
ipo
IRPrinter
IRReader
Linker
LTO
MC
Passes
ScalarOpts
Support
Target
TransformUtils
)
foreach (lib ${llvm_libs})
target_compile_options(${lib} PRIVATE "-fno-aligned-new")
endforeach()
endif()
113 changes: 113 additions & 0 deletions cmake/vkgc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
##
#######################################################################################################################
#
# Copyright (c) 2024 Advanced Micro Devices, Inc. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
#######################################################################################################################

### Top-level VKGC Interface ###
add_library(vkgc INTERFACE)

### VKGC header-only library ###
add_library(vkgc_headers INTERFACE)

target_link_libraries(vkgc_headers INTERFACE llpc_version)

### Options that affect the headers ####################################################################################
#if LLPC_BUILD_GFX11
if(LLPC_BUILD_GFX11)
target_compile_definitions(vkgc_headers INTERFACE LLPC_BUILD_GFX11)
endif()
#endif

#if LLPC_RAY_TRACING
if(LLPC_RAY_TRACING)
if(NOT LLPC_IS_STANDALONE)
target_compile_definitions(vkgc_headers INTERFACE HAVE_GPURT_SHIM)
endif()

target_compile_definitions(vkgc_headers INTERFACE LLPC_RAY_TRACING)
target_compile_definitions(vkgc_headers INTERFACE GPURT_CLIENT_INTERFACE_MAJOR_VERSION=${GPURT_CLIENT_INTERFACE_MAJOR_VERSION})
endif()
#endif

target_link_libraries(vkgc INTERFACE vkgc_headers)

### Expose header files ################################################################################################
target_include_directories(vkgc_headers
INTERFACE
${PROJECT_SOURCE_DIR}/include
)

### external SPIRV headers #########################################################
if (NOT SPIRV_HEADERS_PATH)
if(EXISTS ${PROJECT_SOURCE_DIR}/../SPIRV-Headers)
set(SPIRV_HEADERS_PATH ${PROJECT_SOURCE_DIR}/../SPIRV-Headers CACHE PATH "The path of SPIRV headers.")
elseif(EXISTS ${PROJECT_SOURCE_DIR}/../../../../SPIRV-Headers)
set(SPIRV_HEADERS_PATH ${PROJECT_SOURCE_DIR}/../../../../SPIRV-Headers CACHE PATH "The path of SPIRV headers.")
endif()
endif()

### Interface Target ###################################################################################################
### SPIRV Interface ###
add_library(khronos_spirv_interface INTERFACE)

if(EXISTS ${SPIRV_HEADERS_PATH})
target_include_directories(khronos_spirv_interface
INTERFACE
${SPIRV_HEADERS_PATH}/include
${PROJECT_SOURCE_DIR}/include/khronos
)
if (NOT SPIRV_HEADERS_PATH_INTERNAL)
target_compile_definitions(khronos_spirv_interface
INTERFACE
EXTERNAL_SPIRV_HEADERS=1
)
endif()
else()
target_include_directories(khronos_spirv_interface
INTERFACE
${PROJECT_SOURCE_DIR}/include/khronos
)
endif()

if(LLPC_BUILD_TOOLS)
# SPVGEN
if(EXISTS ${PROJECT_SOURCE_DIR}/../spvgen)
set(XGL_SPVGEN_PATH ${PROJECT_SOURCE_DIR}/../spvgen CACHE PATH "Specify the path to SPVGEN.")
elseif(EXISTS ${PROJECT_SOURCE_DIR}/../xgl/tools/spvgen)
set(XGL_SPVGEN_PATH ${PROJECT_SOURCE_DIR}/../xgl/tools/spvgen CACHE PATH "Specify the path to SPVGEN.")
else()
set(XGL_SPVGEN_PATH ${PROJECT_SOURCE_DIR}/../../../tools/spvgen CACHE PATH "Specify the path to SPVGEN.")
endif()

if(EXISTS ${XGL_SPVGEN_PATH})
set(XGL_SPVGEN_BUILD_PATH ${CMAKE_BINARY_DIR}/spvgen)
add_subdirectory(${XGL_SPVGEN_PATH} ${XGL_SPVGEN_BUILD_PATH} EXCLUDE_FROM_ALL)
endif()

endif(LLPC_BUILD_TOOLS)

if(ICD_BUILD_LLPC)
# Generate Strings for LLPC standalone tool and vkgc_gpurtshim
add_subdirectory(util ${PROJECT_BINARY_DIR}/util)
add_subdirectory(gfxruntime ${PROJECT_BINARY_DIR}/gfxruntime)
endif()
58 changes: 58 additions & 0 deletions compilerutils/include/compilerutils/IRSerializationUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
***********************************************************************************************************************
*
* Copyright (c) 2024 Advanced Micro Devices, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
**********************************************************************************************************************/

//===- IRSerializationUtils.h - Library for compiler frontends ------------===//
//
// Implements several shared helper functions for dumping IR in various forms
// including to DOT files and LL.
//
//===----------------------------------------------------------------------===//

#ifndef IRSERIALIZATIONUTILS_H
#define IRSERIALIZATIONUTILS_H

#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"

namespace irserializationutils {

// Returns an MD5 hash of the module LL. This is returned as a string so it can
// be used as part of a filename.
std::string getModuleHashStr(const llvm::Module &m);

// Writes a DOT file with the CFG of the function. The filename is:
// FilenamePrefix.FuncName.Hash.dot where FuncName is determined by demangling
// the DXIL function name, and Hash is given by getModuleHashStr.
// Set cfgOnly = false to include instructions within the BBs.
void writeCFGToDotFile(const llvm::Function &f, llvm::StringRef filenamePrefix = "cfg", bool cfgOnly = true);

// Writes an LL file with the module. The filename is:
// FilenamePrefix.Hash.ll where Hash is given by getModuleHashStr.
void writeModuleToLLFile(const llvm::Module &m, llvm::StringRef filenamePrefix = "module");

} // namespace irserializationutils

#endif
Loading
Loading