From 21ecde9c85c49bfdea39fa6c2d1f24506674437f Mon Sep 17 00:00:00 2001 From: bokrzesi <99400121+bokrzesi@users.noreply.github.com> Date: Fri, 23 Aug 2024 07:44:12 +0000 Subject: [PATCH] Enablement of GenISAIntrinsics target on LLVM16 Enablement of GenISAIntrinsics target on LLVM16 * Updated ShiftAmount of UnsignedDivisionByConstantInfo calculation based on https://reviews.llvm.org/D141014 * Used newer API (isKillLocation) in getVariableLocation * Updated LLVMWrapper for InlineFunction and slightly refactored it --- IGC/AdaptorCommon/DivergentBarrierPass.cpp | 2 +- IGC/Compiler/LegalizationPass.cpp | 3 +- .../TransformUnmaskedFunctionsPass.cpp | 2 +- .../lib/GenXCodeGen/GenXSimdCFConformance.cpp | 2 +- .../lib/GenXCodeGen/GenXTidyControlFlow.cpp | 2 +- .../include/llvmWrapper/ADT/APInt.h | 5 +++- .../include/llvmWrapper/IR/IntrinsicInst.h | 28 +++++++++++++++---- .../llvmWrapper/Transforms/Utils/Cloning.h | 20 ++++++------- IGC/common/LLVMUtils.cpp | 2 +- 9 files changed, 43 insertions(+), 23 deletions(-) diff --git a/IGC/AdaptorCommon/DivergentBarrierPass.cpp b/IGC/AdaptorCommon/DivergentBarrierPass.cpp index 281c9f5eeac1..4467a7db3f8f 100644 --- a/IGC/AdaptorCommon/DivergentBarrierPass.cpp +++ b/IGC/AdaptorCommon/DivergentBarrierPass.cpp @@ -424,7 +424,7 @@ void DivergentBarrierPass::generateBody( for (auto* CI : ContCalls) { InlineFunctionInfo IFI; - bool CanInline = IGCLLVM::InlineFunction(CI, IFI, nullptr, false); + bool CanInline = IGCLLVM::InlineFunction(*CI, IFI, nullptr, false); IGC_ASSERT_MESSAGE(CanInline, "failed to inline?"); } diff --git a/IGC/Compiler/LegalizationPass.cpp b/IGC/Compiler/LegalizationPass.cpp index dd4ffa099b4b..b159e6b43237 100644 --- a/IGC/Compiler/LegalizationPass.cpp +++ b/IGC/Compiler/LegalizationPass.cpp @@ -28,6 +28,7 @@ SPDX-License-Identifier: MIT #include "GenISAIntrinsics/GenIntrinsicInst.h" #include "Probe/Assertion.h" #include "LLVM3DBuilder/BuiltinsFrontend.hpp" +#include "llvm/Support/Casting.h" using namespace llvm; @@ -2200,7 +2201,7 @@ void Legalization::visitIntrinsicInst(llvm::IntrinsicInst& I) Value* result = (llvmBuilder.*replacementFunc)(argument); InlineFunctionInfo IFI; I.replaceAllUsesWith(result); - IGCLLVM::InlineFunction(static_cast(result), IFI, nullptr, false); + IGCLLVM::InlineFunction(*cast(result), IFI, nullptr, false); I.eraseFromParent(); } }; diff --git a/IGC/Compiler/Optimizer/OpenCLPasses/TransformUnmaskedFunctionsPass/TransformUnmaskedFunctionsPass.cpp b/IGC/Compiler/Optimizer/OpenCLPasses/TransformUnmaskedFunctionsPass/TransformUnmaskedFunctionsPass.cpp index 13bd6973a110..8a9eb5cde92a 100644 --- a/IGC/Compiler/Optimizer/OpenCLPasses/TransformUnmaskedFunctionsPass/TransformUnmaskedFunctionsPass.cpp +++ b/IGC/Compiler/Optimizer/OpenCLPasses/TransformUnmaskedFunctionsPass/TransformUnmaskedFunctionsPass.cpp @@ -345,7 +345,7 @@ bool InlineUnmaskedFunctionsPass::runOnModule(llvm::Module& M) llvm::InlineFunctionInfo IFI; for (auto *CB : Calls) - IGCLLVM::InlineFunction(CB, IFI); + IGCLLVM::InlineFunction(*CB, IFI); for (Function *F : Funcs) { F->removeDeadConstantUsers(); diff --git a/IGC/VectorCompiler/lib/GenXCodeGen/GenXSimdCFConformance.cpp b/IGC/VectorCompiler/lib/GenXCodeGen/GenXSimdCFConformance.cpp index ea304507c5f3..64671351082d 100644 --- a/IGC/VectorCompiler/lib/GenXCodeGen/GenXSimdCFConformance.cpp +++ b/IGC/VectorCompiler/lib/GenXCodeGen/GenXSimdCFConformance.cpp @@ -3918,7 +3918,7 @@ static void fixBlockDataBeforeRemoval(BasicBlock *BB, BasicBlock *SuccBB) { while (auto *DBG = dyn_cast(BB->begin())) { DBG->moveBefore(InsertBefore); if (!HasOnePred) - IGCLLVM::setDbgVariableLocationToUndef(DBG); + IGCLLVM::setKillLocation(DBG); } } diff --git a/IGC/VectorCompiler/lib/GenXCodeGen/GenXTidyControlFlow.cpp b/IGC/VectorCompiler/lib/GenXCodeGen/GenXTidyControlFlow.cpp index 6f3388c7a397..99f3cf94d1e6 100644 --- a/IGC/VectorCompiler/lib/GenXCodeGen/GenXTidyControlFlow.cpp +++ b/IGC/VectorCompiler/lib/GenXCodeGen/GenXTidyControlFlow.cpp @@ -152,7 +152,7 @@ static void moveDbgBeforeBlockRemoval(BasicBlock *BB, Instruction *InsertBefore, while (auto *DBG = dyn_cast(BB->begin())) { DBG->moveBefore(InsertBefore); if (MakeUndef) - IGCLLVM::setDbgVariableLocationToUndef(DBG); + IGCLLVM::setKillLocation(DBG); } IGC_ASSERT_MESSAGE(BB->front().isTerminator(), "Expected that only terminator instruction remains"); } diff --git a/IGC/WrapperLLVM/include/llvmWrapper/ADT/APInt.h b/IGC/WrapperLLVM/include/llvmWrapper/ADT/APInt.h index bf3bc9b35c13..d787fe2b0c25 100644 --- a/IGC/WrapperLLVM/include/llvmWrapper/ADT/APInt.h +++ b/IGC/WrapperLLVM/include/llvmWrapper/ADT/APInt.h @@ -71,7 +71,10 @@ inline bool IsAddition(const UnsignedDivisionByConstantInfo &mu) { } inline unsigned ShiftAmount(const UnsignedDivisionByConstantInfo &mu) { -#if LLVM_VERSION_MAJOR >= 14 +#if LLVM_VERSION_MAJOR >= 16 + // Basing on this: https://reviews.llvm.org/D141014 + return IsAddition(mu) ? mu.PostShift + 1 : mu.PostShift; +#elif LLVM_VERSION_MAJOR >= 14 return mu.ShiftAmount; #else return mu.s; diff --git a/IGC/WrapperLLVM/include/llvmWrapper/IR/IntrinsicInst.h b/IGC/WrapperLLVM/include/llvmWrapper/IR/IntrinsicInst.h index 2fecc99f2d2a..f5f3fb489cc0 100644 --- a/IGC/WrapperLLVM/include/llvmWrapper/IR/IntrinsicInst.h +++ b/IGC/WrapperLLVM/include/llvmWrapper/IR/IntrinsicInst.h @@ -13,35 +13,51 @@ SPDX-License-Identifier: MIT #include #include #include "llvm/IR/DebugInfoMetadata.h" +#include "llvm/Support/Casting.h" #include "Probe/Assertion.h" namespace IGCLLVM { + inline bool isKillLocation(const llvm::DbgVariableIntrinsic* DbgInst) + { + IGC_ASSERT(DbgInst); +#if LLVM_VERSION_MAJOR <= 12 + return llvm::dyn_cast(DbgInst->getVariableLocation()); +#elif LLVM_VERSION_MAJOR <= 15 + return DbgInst->isUndef(); +#else // LLVM_VERSION_MAJOR >= 16 + return DbgInst->isKillLocation(); +#endif + } + inline llvm::Value* getVariableLocation(const llvm::DbgVariableIntrinsic* DbgInst) { IGC_ASSERT(DbgInst); #if LLVM_VERSION_MAJOR <= 12 return DbgInst->getVariableLocation(); #else - IGC_ASSERT_MESSAGE(((DbgInst->getNumVariableLocationOps() == 1) || DbgInst->isUndef()), + IGC_ASSERT_MESSAGE((DbgInst->getNumVariableLocationOps() == 1) || isKillLocation(DbgInst), "unsupported number of location ops"); return DbgInst->getVariableLocationOp(0); #endif } - inline void setDbgVariableLocationToUndef(llvm::DbgVariableIntrinsic *DbgInst) + inline void setKillLocation(llvm::DbgVariableIntrinsic *DbgInst) { IGC_ASSERT(DbgInst); + #if LLVM_VERSION_MAJOR <= 12 auto *OP = DbgInst->getVariableLocation(); IGC_ASSERT_MESSAGE(OP != nullptr, "Empty dbg var not supported"); + auto *Undef = llvm::UndefValue::get(OP->getType()); - DbgInst->setOperand( - 0, llvm::MetadataAsValue::get(DbgInst->getContext(), - llvm::ValueAsMetadata::get(Undef))); -#else + DbgInst->setOperand(0, llvm::MetadataAsValue::get(DbgInst->getContext(), + llvm::ValueAsMetadata::get(Undef))); +#elif LLVM_VERSION_MAJOR <= 15 DbgInst->setUndef(); +#else // LLVM_VERSION_MAJOR >= 16 + DbgInst->setKillLocation(); #endif } diff --git a/IGC/WrapperLLVM/include/llvmWrapper/Transforms/Utils/Cloning.h b/IGC/WrapperLLVM/include/llvmWrapper/Transforms/Utils/Cloning.h index 2452b3e8190e..97f3de7141ea 100644 --- a/IGC/WrapperLLVM/include/llvmWrapper/Transforms/Utils/Cloning.h +++ b/IGC/WrapperLLVM/include/llvmWrapper/Transforms/Utils/Cloning.h @@ -11,25 +11,25 @@ SPDX-License-Identifier: MIT #include "llvm/Config/llvm-config.h" #include "llvm/Transforms/Utils/Cloning.h" +#include "llvm/Support/Casting.h" +#include "Probe/Assertion.h" namespace IGCLLVM { - inline bool InlineFunction(llvm::CallInst* CB, llvm::InlineFunctionInfo& IFI, + inline bool InlineFunction(llvm::CallBase& CB, llvm::InlineFunctionInfo& IFI, llvm::AAResults* CalleeAAR = nullptr, bool InsertLifetime = true, llvm::Function* ForwardVarArgsTo = nullptr) { - return llvm::InlineFunction( #if LLVM_VERSION_MAJOR < 11 - CB -#else - *llvm::dyn_cast(CB) -#endif - , IFI, CalleeAAR, InsertLifetime, ForwardVarArgsTo) -#if LLVM_VERSION_MAJOR >= 11 - .isSuccess() + auto* CI = llvm::dyn_cast(&CB); + IGC_ASSERT(CI); + return llvm::InlineFunction(CI, IFI, CalleeAAR, InsertLifetime, ForwardVarArgsTo); +#elif LLVM_VERSION_MAJOR <= 15 + return llvm::InlineFunction(CB, IFI, CalleeAAR, InsertLifetime, ForwardVarArgsTo).isSuccess(); +#else // LLVM_VERSION_MAJOR >= 16 + return llvm::InlineFunction(CB, IFI, true, CalleeAAR, InsertLifetime, ForwardVarArgsTo).isSuccess(); #endif - ; } #if LLVM_VERSION_MAJOR < 13 diff --git a/IGC/common/LLVMUtils.cpp b/IGC/common/LLVMUtils.cpp index c6acfd05ead9..417302449995 100644 --- a/IGC/common/LLVMUtils.cpp +++ b/IGC/common/LLVMUtils.cpp @@ -945,7 +945,7 @@ void InlineHelper::InlineAndOptimize(CallInst* callInst) auto* fn = callInst->getFunction(); InlineFunctionInfo IFI; - bool CanInline = IGCLLVM::InlineFunction(callInst, IFI); + bool CanInline = IGCLLVM::InlineFunction(*callInst, IFI); IGC_ASSERT_MESSAGE(CanInline, "failed to inline?"); auto& perFnAllocas = m_InlinedStaticArrayAllocas[fn];