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

Promotion from AMD internal branch for 2023.Q4.3 #2891

Merged
merged 3 commits into from
Jan 3, 2024
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 .typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ USCALED = "USCALED"
Datas = "Datas"
HSA = "HSA"
VALU = "VALU"
Derivate = "Derivate"
13 changes: 6 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,21 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(LLPC_IS_STANDALONE ON)
endif()

### Version info ###
include(cmake/llpc_version.cmake)
add_llpc_version_projects()

### 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)

### Cached Project Options #############################################################################################
option(LLPC_BUILD_TOOLS "LLPC build all tools" OFF)

### Options that affect the headers ####################################################################################
if (LLPC_CLIENT_INTERFACE_MAJOR_VERSION)
target_compile_definitions(vkgc_headers INTERFACE LLPC_CLIENT_INTERFACE_MAJOR_VERSION=${LLPC_CLIENT_INTERFACE_MAJOR_VERSION})
else()
message(FATAL_ERROR "Client of LLPC must set LLPC_CLIENT_INTERFACE_MAJOR_VERSION")
endif()

#if VKI_BUILD_GFX11
if(LLPC_BUILD_GFX11)
target_compile_definitions(vkgc_headers INTERFACE VKI_BUILD_GFX11)
Expand Down
2 changes: 2 additions & 0 deletions cmake/continuations.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@

set(LLPC_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/..")

include("${LLPC_SOURCE_DIR}/cmake/llpc_version.cmake")
include("${LLPC_SOURCE_DIR}/cmake/compilerutils.cmake")

# Macro to add continuations and its dependencies as LLVM external projects.
# This appends the project names to LLVM_EXTERNAL_PROJECTS and sets each LLVM_EXTERNAL_*_SOURCE_DIR,
# all in the caller's scope.
macro(add_continuations_projects)
add_llpc_version_projects()
add_compilerutils_projects()
if (NOT continuations IN_LIST LLVM_EXTERNAL_PROJECTS)
if (NOT llvm_dialects IN_LIST LLVM_EXTERNAL_PROJECTS)
Expand Down
2 changes: 2 additions & 0 deletions cmake/lgc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@

set(LLPC_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/..")

include("${LLPC_SOURCE_DIR}/cmake/llpc_version.cmake")
include("${LLPC_SOURCE_DIR}/cmake/continuations.cmake")

# Macro to add LGC and its dependencies as LLVM external projects.
# This appends the project names to LLVM_EXTERNAL_PROJECTS and sets each LLVM_EXTERNAL_*_SOURCE_DIR,
# all in the caller's scope.
macro(add_lgc_projects)
add_llpc_version_projects()
add_continuations_projects()
if (NOT lgc IN_LIST LLVM_EXTERNAL_PROJECTS)
if (NOT llvm_dialects IN_LIST LLVM_EXTERNAL_PROJECTS)
Expand Down
34 changes: 34 additions & 0 deletions cmake/llpc_version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
##
#######################################################################################################################
#
# Copyright (c) 2023 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.
#
#######################################################################################################################

set(LLPC_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/..")

macro(add_llpc_version_projects)
if (NOT TARGET llpc_version)
# Force the binary directory to account for the possibility that LLPC is
# taken from an external source directory.
add_subdirectory(${LLPC_SOURCE_DIR}/version ${CMAKE_CURRENT_BINARY_DIR}/llpc_version)
endif()
endmacro()
4 changes: 4 additions & 0 deletions compilerutils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_llvm_library(LLVMCompilerUtils
Analysis
Core
Support
TransformUtils
)

target_include_directories(LLVMCompilerUtils PUBLIC
Expand All @@ -35,3 +36,6 @@ set_compiler_options(LLVMCompilerUtils)

target_compile_features(LLVMCompilerUtils PUBLIC cxx_std_17)
set_target_properties(LLVMCompilerUtils PROPERTIES CXX_EXTENSIONS OFF)

add_subdirectory(tool/cross-module-inline)
add_subdirectory(test)
59 changes: 50 additions & 9 deletions compilerutils/include/compilerutils/CompilerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@
#include "llvm/IR/Attributes.h"
#include "llvm/IR/IRBuilder.h"

namespace llvm {

class CallInst;
class Function;
class Type;
class Value;

} // namespace llvm

namespace CompilerUtils {

// Create an LLVM function call to the named function. The callee is built
Expand All @@ -66,6 +57,56 @@ llvm::CallInst *createNamedCall(llvm::IRBuilder<> &, llvm::StringRef, llvm::Type
llvm::Function *mutateFunctionArguments(llvm::Function &, llvm::Type *, const llvm::ArrayRef<llvm::Type *>,
llvm::AttributeList);

// Create a new function based on another function, copying attributes and
// other properties.
// Specify targetModule to create the function in a different module than f.
llvm::Function *cloneFunctionHeader(llvm::Function &f, llvm::FunctionType *newType, llvm::AttributeList attributes,
llvm::Module *targetModule = nullptr);

// Overload of cloneFunctionHeader that takes the new attributes for arguments and preserves the rest.
llvm::Function *cloneFunctionHeader(llvm::Function &f, llvm::FunctionType *newType,
llvm::ArrayRef<llvm::AttributeSet> argAttrs, llvm::Module *targetModule = nullptr);

struct CrossModuleInlinerResult {
llvm::Value *returnValue;
llvm::iterator_range<llvm::Function::iterator> newBBs;
};

// The class caches already mapped constants. Reusing an instance of this class is more efficient than creating a new
// instance every time but it does not have an impact on the generated code.
// One CrossModuleInliner instance must only be used for a single target module, otherwise things can go wrong.
class CrossModuleInliner {
public:
CrossModuleInliner() = default;

// Inline a call to a function even if the called function is in a different module.
// If the result of that function call should be used, a use must exist before calling this function.
// Returns the new created basic blocks. These blocks may also contain instructions that were already
// there before, if the function got inlined into an existing block.
//
// The insertion point of existing IRBuilders may have their insertion point invalidated because this
// function splits basic blocks.
// They can be made functional again with b.SetInsertPoint(&*b.GetInsertPoint()).
llvm::iterator_range<llvm::Function::iterator> inlineCall(llvm::CallBase &cb);

// Inline a call to a function even if the called function is in a different module.
// Returns the result of the call and new created basic blocks. These blocks may also contain instructions that were
// already there before, if the function got inlined into an existing block.
//
// This is a convenience wrapper around inlineCall(CallBase&). As users of the callee's return value are not known
// while inlining, using this function can result in slightly less folding in the IR.
CrossModuleInlinerResult inlineCall(llvm::IRBuilder<> &b, llvm::Function *callee,
llvm::ArrayRef<llvm::Value *> args = std::nullopt);

// Find a global value (function or variable) that was copied by the cross-module inliner.
// Arguments are the global from the source module and the target module. Returns the corresponding global from the
// target module.
llvm::GlobalValue *findCopiedGlobal(llvm::GlobalValue &sourceGv, llvm::Module &targetModule);

private:
llvm::SmallDenseMap<llvm::GlobalValue *, llvm::GlobalValue *> mappedGlobals;
};

} // namespace CompilerUtils

#endif
Loading
Loading