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.Q3.1 #2903

Merged
merged 1 commit into from
Aug 13, 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
  •  
  •  
  •  
16 changes: 13 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,12 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")

set_property(TARGET all-targets PROPERTY FOLDER Misc)
set_property(TARGET AMDGPU PROPERTY FOLDER Misc)
set_property(TARGET benchmark PROPERTY FOLDER Misc)
set_property(TARGET benchmark_main PROPERTY FOLDER Misc)
if (TARGET benchmark)
set_property(TARGET benchmark PROPERTY FOLDER Misc)
endif()
if (TARGET benchmark_main)
set_property(TARGET benchmark_main PROPERTY FOLDER Misc)
endif()
set_property(TARGET distribution PROPERTY FOLDER Misc)
set_property(TARGET Engine PROPERTY FOLDER Misc)
set_property(TARGET install-distribution PROPERTY FOLDER Misc)
Expand All @@ -210,8 +214,14 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set_property(TARGET CONFIGURE_LLVM_NATIVE PROPERTY FOLDER Misc)
set_property(TARGET CREATE_LLVM_NATIVE PROPERTY FOLDER Misc)
endif()
#if _WIN32
if (MSVC)
# We can't use LLVM_OPTIMIZED_TABLEGEN on Windows, and the 32-bit llvm-tblgen can easily
# to run out of memory. Tell the linker to allow addresses larger than 2GB.
set_property(TARGET llvm-tblgen PROPERTY LINK_FLAGS "/LARGEADDRESSAWARE")
endif()
#endif
if (LLPC_BUILD_TESTS)
set_property(TARGET check-all PROPERTY FOLDER Tests)
if(NOT LLPC_IS_STANDALONE)
set_property(TARGET check-amber PROPERTY FOLDER "LLPC Tests")
endif()
Expand Down
2 changes: 2 additions & 0 deletions compilerutils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ function(set_compiler_options PROJECT_NAME)
endfunction()

add_llvm_library(LLVMCompilerUtils
lib/ArgPromotion.cpp
lib/CompilerUtils.cpp
lib/TypeLowering.cpp
lib/TypesMetadata.cpp

DEPENDS
intrinsics_gen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* 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 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,
Expand All @@ -23,27 +23,25 @@
*
**********************************************************************************************************************/

//===- TypesMetadata.h - Pointee type metadata for processing DXIL ---------==//
// Promotion of pointer args to by-value.

#pragma once

#include "llvm/Bitcode/BitcodeReader.h"

namespace llvm {
class Function;
class SmallBitVector;
} // namespace llvm

/// Return element type of a function argument resolving opaque pointers
/// via !types metadata where appropriate.
/// Returns nullptr for non-pointers.
Type *getFuncArgPtrElementType(const Argument *Arg);
namespace CompilerUtils {

/// Return element type of a function argument resolving opaque pointers
/// via !types metadata where appropriate.
/// Returns nullptr for non-pointers.
Type *getFuncArgPtrElementType(const Function *F, int ArgNo);
// Replace struct return type with its first element type.
llvm::Function *unpackStructReturnType(llvm::Function *Fn);

/// LLVM parser callback which adds !types metadata during DXIL parsing
void DXILValueTypeMetadataCallback(Value *V, unsigned TypeID,
GetTypeByIDTy GetTypeByID,
GetContainedTypeIDTy GetContainedTypeID);
// Turn StructRet argument into return type, modifying pointee type metadata as appropriate.
llvm::Function *lowerStructRetArgument(llvm::Function *Fn);

} // namespace llvm
// Promote pointer (by-ref) arguments to by-value, according to PromotionMask
// and using pointee type metadata.
llvm::Function *promotePointerArguments(llvm::Function *Fn, const llvm::SmallBitVector &PromotionMask);

} // namespace CompilerUtils
26 changes: 25 additions & 1 deletion compilerutils/include/compilerutils/CompilerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ class CrossModuleInliner {
// The caller has to handle the erasure afterwards.
void replaceAllPointerUses(llvm::IRBuilder<> *builder, llvm::Value *oldPointerValue, llvm::Value *newPointerValue,
llvm::SmallVectorImpl<llvm::Instruction *> &toBeRemoved);

} // namespace CompilerUtils

namespace llvm {
Expand All @@ -141,6 +140,31 @@ namespace llvm {
// fixed.
PointerType *getWithSamePointeeType(PointerType *ptrTy, unsigned addressSpace);

/// Free-standing helpers.

// Helper to visit all calls of a function.
// Expected type for Callback:
// void(CallInst &)
template <typename CallbackTy> void forEachCall(Function &F, CallbackTy Callback) {
static_assert(std::is_invocable_v<CallbackTy, CallInst &>);
for (auto &Use : make_early_inc_range(F.uses())) {
if (auto *CInst = dyn_cast<CallInst>(Use.getUser()))
if (CInst->isCallee(&Use))
Callback(*CInst);
}
}

// For each basic block in Func, find the terminator. If it is contained in
// TerminatorOpcodes, then apply the callback on the terminator.
template <typename CallbackTy, typename = std::enable_if<std::is_invocable_v<CallbackTy, llvm::Instruction &>>>
void forEachTerminator(Function *Func, ArrayRef<unsigned> TerminatorOpcodes, CallbackTy Callback) {
for (auto &BB : *Func) {
auto *Terminator = BB.getTerminator();
if (llvm::find(TerminatorOpcodes, Terminator->getOpcode()) != TerminatorOpcodes.end())
Callback(*Terminator);
}
}

} // namespace llvm

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <type_traits>
#include <vector>

namespace compilerutils {
namespace CompilerUtils {

/// @brief A key-value map from pointer keys to tuples of pointers that is optimized for value and type lowering uses
///
Expand Down Expand Up @@ -252,4 +252,4 @@ template <typename KeyT, typename ValueT, bool TrackReverse> class LoweringPoint
}
};

} // namespace compilerutils
} // namespace CompilerUtils
8 changes: 6 additions & 2 deletions compilerutils/include/compilerutils/TypeLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/IRBuilder.h"

namespace CompilerUtils {

class TypeLowering;

/// Given a type, check if it should be replaced.
Expand Down Expand Up @@ -169,14 +171,16 @@ class TypeLowering {
llvm::SmallVector<std::function<ConstantTypeLoweringFn>> m_constantRules;

/// Cache mappings of types (including no-op mappings).
compilerutils::LoweringPointerTupleMap<llvm::Type *, llvm::Type *, false> m_typeConversions;
CompilerUtils::LoweringPointerTupleMap<llvm::Type *, llvm::Type *, false> m_typeConversions;

llvm::IRBuilder<> m_builder;

/// Map original values to type-converted values.
compilerutils::LoweringPointerTupleMap<llvm::Value *, llvm::Value *, true> m_valueMap;
CompilerUtils::LoweringPointerTupleMap<llvm::Value *, llvm::Value *, true> m_valueMap;

std::vector<std::pair<llvm::PHINode *, llvm::SmallVector<llvm::PHINode *>>> m_phis;
std::vector<llvm::Instruction *> m_instructionsToErase;
llvm::SmallVector<llvm::Function *> m_functionsToErase;
};

} // namespace CompilerUtils
117 changes: 117 additions & 0 deletions compilerutils/include/compilerutils/TypesMetadata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
***********************************************************************************************************************
*
* 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.
*
**********************************************************************************************************************/

//===- TypesMetadata.h - Pointee type metadata for processing DXIL ---------==//

#pragma once

#include "llvm/Bitcode/BitcodeReader.h"

namespace llvm {

class MDTuple;

// A function argument type and pointee type.
class TypedArgTy {
private:
Type *ArgTy = nullptr;
Type *ElemTy = nullptr;

public:
TypedArgTy() {}
TypedArgTy(Type *Arg);
TypedArgTy(Type *Arg, Type *Elem);

static TypedArgTy get(const Argument *Arg);
static TypedArgTy get(const Function *F, const unsigned ArgNo);

Type *asType() const { return ArgTy; }
Type *getPointerElementType() const;

bool isPointerTy() const;
bool isVoidTy() const;
Metadata *getTypeMetadata(LLVMContext &Context);

bool operator==(const TypedArgTy &RHS) const { return (ArgTy == RHS.ArgTy) && (ElemTy == RHS.ElemTy); }
};

// A wrapper round FunctionType and metadata for the pointee type(s) of any pointer return type and parameters.
class TypedFuncTy {
public:
TypedFuncTy() {}

// Construct a TypedFuncTy for the given result type and arg types.
// This constructs the !pointeetys metadata; that can then be attached to a function
// using writeMetadata().
TypedFuncTy(TypedArgTy ResultTy, ArrayRef<TypedArgTy> ArgTys);

// Get a TypedFuncTy for the given Function, looking up the !pointeetys metadata.
static TypedFuncTy get(const Function *F);

// Get the IR FunctionType.
FunctionType *asFunctionType() const { return FuncTy; }

// Get a TypedArgTy for the return type.
TypedArgTy getReturnType() const;

// Get a TypedArgTy for a parameter type.
TypedArgTy getParamType(unsigned Idx) const;

// Push a TypedArgTy for each parameter onto the supplied vector.
void getParamTypes(SmallVectorImpl<TypedArgTy> &ArgTys) const;

// Write the metadata (if any) onto the specified function. Typically used when creating a new function
// and using our constructor that takes TypedArgTy for return type and arg types.
void writeMetadata(Function *F) const;

static constexpr const char *MDTypesName = "pointeetys";

private:
// Shared code for getReturnType and getParamType. This decodes the !pointeetys metadata.
Type *getPointeeType(Type *Ty, unsigned Idx) const;

FunctionType *FuncTy = nullptr;
MDTuple *Meta = nullptr;
};

/// Return element type of a function argument resolving opaque pointers
/// via !pointeetys metadata where appropriate.
/// Returns nullptr for non-pointers.
Type *getFuncArgPtrElementType(const Argument *Arg);

/// Return element type of a function argument resolving opaque pointers
/// via !pointeetys metadata where appropriate.
/// Returns nullptr for non-pointers.
Type *getFuncArgPtrElementType(const Function *F, int ArgNo);

/// Get element type of function return type resolving opaque pointers
/// via !pointeetys metadata where appropriate.
Type *getFuncReturnPtrElementType(const Function *F);

/// LLVM parser callback which adds !pointeetys metadata during DXIL parsing
void DXILValueTypeMetadataCallback(Value *V, unsigned TypeID, GetTypeByIDTy GetTypeByID,
GetContainedTypeIDTy GetContainedTypeID);

} // namespace llvm
Loading
Loading