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 2024.Q4.3 #2912

Merged
merged 1 commit into from
Dec 23, 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
25 changes: 25 additions & 0 deletions cmake/CompilerFlags.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
##
#######################################################################################################################
#
# 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.
#
#######################################################################################################################

function(set_compiler_options PROJECT_NAME ENABLE_WERROR)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
set_target_properties(${PROJECT_NAME} PROPERTIES
Expand Down
6 changes: 3 additions & 3 deletions cmake/findllvm.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ 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)
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.")
set(LLPC_LLVM_SRC_PATH ${DEFAULT_LLPC_LLVM_SRC_PATH} CACHE PATH "Specify the path to LLVM." FORCE)
endif()
6 changes: 6 additions & 0 deletions cmake/llvm.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ if(CMAKE_BUILD_TYPE_DEBUG)
# 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)

# Fail early to avoid the dreaded -ologo error.
if(CMAKE_VERSION VERSION_LESS "3.27")
message(FATAL_ERROR "Using LLVM_OPTIMIZED_TABLEGEN in a Debug build requires CMake 3.27 or higher."
" The current CMake version is ${CMAKE_VERSION}.")
endif()
endif()
#endif
endif()
Expand Down
25 changes: 25 additions & 0 deletions compilerutils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
##
#######################################################################################################################
#
# 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.
#
#######################################################################################################################

cmake_minimum_required(VERSION 3.13.4)

project(CompilerUtils LANGUAGES CXX)
Expand Down
9 changes: 1 addition & 8 deletions compilerutils/include/compilerutils/CompilerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class CrossModuleInliner {
// Writes instructions which are redundant after the replacement into
// the given ToBeRemoved vector.
// The caller has to handle the erasure afterwards.
void replaceAllPointerUses(llvm::IRBuilder<> *builder, llvm::Value *oldPointerValue, llvm::Value *newPointerValue,
void replaceAllPointerUses(llvm::Value *oldPointerValue, llvm::Value *newPointerValue,
llvm::SmallVectorImpl<llvm::Instruction *> &toBeRemoved);

// Create a GEP if idx is non-null, otherwise return the pointer.
Expand All @@ -169,13 +169,6 @@ llvm::Value *simplifyingCreateConstInBoundsGEP1_32(llvm::IRBuilder<> &builder, l

namespace llvm {

// Replacement for PointerType::getWithSamePointeeType that works with new LLVM.
// Returns a typed pointer type if the pointer type is typed.
//
// TODO: Remove this as soon as all internal users of opaque pointers have been
// fixed.
PointerType *getWithSamePointeeType(PointerType *ptrTy, unsigned addressSpace);

/// Free-standing helpers.

// Helper to visit all calls of a function.
Expand Down
4 changes: 2 additions & 2 deletions compilerutils/include/compilerutils/ModuleBunch.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class ModuleBunchToModulePassAdaptor : public PassInfoMixin<ModuleBunchToModuleP

/// Construct with a function that returns a pass. It can then parallelize compilation by calling
/// the function once for each parallel thread.
explicit ModuleBunchToModulePassAdaptor(function_ref<std::unique_ptr<PassConceptT>()> PassMaker,
explicit ModuleBunchToModulePassAdaptor(std::function<std::unique_ptr<PassConceptT>()> PassMaker,
bool EagerlyInvalidate = false)
: PassMaker(PassMaker), EagerlyInvalidate(EagerlyInvalidate) {}

Expand All @@ -160,7 +160,7 @@ class ModuleBunchToModulePassAdaptor : public PassInfoMixin<ModuleBunchToModuleP

private:
std::unique_ptr<PassConceptT> Pass;
function_ref<std::unique_ptr<PassConceptT>()> PassMaker;
std::function<std::unique_ptr<PassConceptT>()> PassMaker;
bool EagerlyInvalidate;
};

Expand Down
37 changes: 15 additions & 22 deletions compilerutils/lib/CompilerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ Value *CrossModuleInliner::CrossModuleValueMaterializer::materialize(Value *v) {
auto InsertToMappedTypes = [&mappedTypes](Type *sourceType, Type *copiedType) {
assert((sourceType != nullptr) && (copiedType != nullptr));
if (sourceType != copiedType) {
auto found = mappedTypes.insert(std::make_pair(sourceType, copiedType));
[[maybe_unused]] auto found = mappedTypes.insert(std::make_pair(sourceType, copiedType));
assert((found.second || copiedType == found.first->second) && "Inconsistent type mapping");
}
};
Expand Down Expand Up @@ -527,18 +527,14 @@ std::string CrossModuleInliner::getCrossModuleName(GlobalValue &gv) {
return (Twine(gv.getName()) + ".cloned." + gv.getParent()->getName()).str();
}

PointerType *llvm::getWithSamePointeeType(PointerType *ptrTy, unsigned addressSpace) {
return PointerType::get(ptrTy->getContext(), addressSpace);
}

void CrossModuleInliner::checkTargetModule(llvm::Module &targetModule) {
if (impl->targetMod == nullptr)
impl->targetMod = &targetModule;
else
assert(impl->targetMod == &targetModule);
}

void CompilerUtils::replaceAllPointerUses(IRBuilder<> *builder, Value *oldPointerValue, Value *newPointerValue,
void CompilerUtils::replaceAllPointerUses(Value *oldPointerValue, Value *newPointerValue,
SmallVectorImpl<Instruction *> &toBeRemoved) {
// Note: The implementation explicitly supports typed pointers, which
// complicates some of the code below.
Expand All @@ -548,23 +544,20 @@ void CompilerUtils::replaceAllPointerUses(IRBuilder<> *builder, Value *oldPointe
(void)oldPtrTy;
PointerType *newPtrTy = cast<PointerType>(newPointerValue->getType());
unsigned newAS = newPtrTy->getAddressSpace();
assert(newAS != oldPtrTy->getAddressSpace());
assert(getWithSamePointeeType(oldPtrTy, newAS) == newPtrTy);

// If a change of address space is not necessary then simply replace uses.
if (newAS == oldPtrTy->getAddressSpace()) {
oldPointerValue->replaceAllUsesWith(newPointerValue);
return;
}

// Propagate a change of address space by traversing through the users and setup the addrspace.

oldPointerValue->mutateType(newPtrTy);

// Traverse through the users and setup the addrspace
SmallVector<Use *> worklist(make_pointer_range(oldPointerValue->uses()));
oldPointerValue->replaceAllUsesWith(newPointerValue);

// Given a pointer type, get a pointer with the same pointee type (possibly
// opaque) as the given type that uses the newAS address space.
auto getMutatedPtrTy = [newAS](Type *ty) {
PointerType *ptrTy = cast<PointerType>(ty);
// Support typed pointers:
return getWithSamePointeeType(ptrTy, newAS);
};

#ifndef NDEBUG
DenseSet<Value *> PhiElems;
#endif
Expand Down Expand Up @@ -620,29 +613,29 @@ void CompilerUtils::replaceAllPointerUses(IRBuilder<> *builder, Value *oldPointe
// This can happen with typed pointers
assert(cast<BitCastOperator>(inst)->getSrcTy()->isPointerTy() &&
cast<BitCastOperator>(inst)->getDestTy()->isPointerTy());
inst->mutateType(getMutatedPtrTy(inst->getType()));
inst->mutateType(newPtrTy);
break;
}
case Instruction::AddrSpaceCast:
// Check that the pointer operand has already been fixed
assert(inst->getOperand(0)->getType()->getPointerAddressSpace() == newAS);
// Push the correct users before RAUW.
worklist.append(usesRange.begin(), usesRange.end());
inst->mutateType(getMutatedPtrTy(inst->getType()));
inst->mutateType(newPtrTy);
// Since we are mutating the address spaces of users as well,
// we can just use the (already mutated) cast operand.
inst->replaceAllUsesWith(inst->getOperand(0));
toBeRemoved.push_back(inst);
continue;
case Instruction::IntToPtr:
case Instruction::GetElementPtr: {
inst->mutateType(getMutatedPtrTy(inst->getType()));
inst->mutateType(newPtrTy);
break;
}
case Instruction::Select: {
auto *oldType = inst->getType();
if (oldType->isPointerTy()) {
Type *newType = getMutatedPtrTy(oldType);
Type *newType = newPtrTy;
// No further processing if the type has the correct pointer type
if (newType == oldType)
continue;
Expand All @@ -666,7 +659,7 @@ void CompilerUtils::replaceAllPointerUses(IRBuilder<> *builder, Value *oldPointe
}
#endif

Type *newType = getMutatedPtrTy(oldType);
Type *newType = newPtrTy;
// No further processing if the type has the correct pointer type
if (newType == oldType)
continue;
Expand Down
25 changes: 25 additions & 0 deletions compilerutils/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
##
#######################################################################################################################
#
# 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.
#
#######################################################################################################################

set(LLVM_COMPILERUTILSPLUGIN_LINK_INTO_TOOLS ON CACHE BOOL "Link plugin into tools" FORCE)

add_llvm_pass_plugin(CompilerUtilsPlugin
Expand Down
25 changes: 25 additions & 0 deletions compilerutils/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
##
#######################################################################################################################
#
# 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.
#
#######################################################################################################################

set(COMPILERUTILS_TEST_DEPENDS cross-module-inline FileCheck count not opt)
add_custom_target(compilerutils-test-depends DEPENDS ${COMPILERUTILS_TEST_DEPENDS})
set_target_properties(compilerutils-test-depends PROPERTIES FOLDER "Tests")
Expand Down
Loading
Loading