Folding global offsets to zero and replacing enqueued local offsets to their correspinding local_size X/Y/Z for DirectML kernels

This code identifies the built ins for global offsets and enqued local sizes. It folds the qneueued local offsets to the value read from metadata and folds the global offsets to zero if compilation is invoked from the Multiadapter.
Multiadapter passes a flag to IGC to enable folding global offset to zero.

Change-Id: I84aaf8aa6d316c723168903da41ac1fa363be071
This commit is contained in:
rishipal
2018-04-30 10:38:55 -07:00
committed by Aleksander Stojanowski
parent 82a2d714e2
commit efa00cb663
6 changed files with 210 additions and 1 deletions

View File

@ -34,6 +34,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "Compiler/CISACodeGen/ResolveGAS.h"
#include "Compiler/CISACodeGen/ResolvePredefinedConstant.h"
#include "Compiler/CISACodeGen/SimplifyConstant.h"
#include "Compiler/CISACodeGen/FoldKnownWorkGroupSizes.h"
#include "Compiler/Optimizer/BuiltInFuncImport.h"
#include "Compiler/Optimizer/CodeAssumption.hpp"
@ -122,6 +123,7 @@ using namespace IGC::Debug;
namespace IGC
{
int getOCLMajorVersion(const SPIRMD::SpirMetaDataUtils &spirMDUtils)
{
int oclMajor = 0, oclMinor = 0;
@ -210,6 +212,9 @@ static void CommonOCLBasedPasses(
assert((pContext->type == ShaderType::OPENCL_SHADER) && "Trying to use OCL common passes on non-OCL context");
bool shouldForceCR = static_cast<OpenCLProgramContext*>(pContext)->m_Options.CorrectlyRoundedSqrt;
pContext->getModuleMetaData()->compOpt.replaceGlobalOffsetsByZero =
static_cast<OpenCLProgramContext*>(pContext)->m_InternalOptions.replaceGlobalOffsetsByZero;
pContext->getModuleMetaData()->compOpt.SubgroupIndependentForwardProgressRequired =
(static_cast<OpenCLProgramContext*>(pContext)->m_Options.NoSubgroupIFP == false);
@ -323,6 +328,9 @@ static void CommonOCLBasedPasses(
// mpm.add(new IGILGenericAddressStaticResolution(false));
}
mpm.add(CreateFoldKnownWorkGroupSizes());
// Run the AlignmentAnalysis pass before the passes which add implicit arguments, to ensure we do not lose load/store alignment information.
// For example, ProgramScopeConstantResolution will relocate the buffer's base to an i8* typed pointer.
mpm.add(new AlignmentAnalysis());

View File

@ -23,6 +23,7 @@ set(IGC_BUILD__SRC__CISACodeGen_Common
"${CMAKE_CURRENT_SOURCE_DIR}/EstimateFunctionSize.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/FixAddrSpaceCast.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/FixupExtractValuePair.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/FoldKnownWorkGroupSizes.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/GenCodeGenModule.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/GenIRLowering.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/GenNullPointerLowering.cpp"
@ -107,6 +108,7 @@ set(IGC_BUILD__HDR__CISACodeGen_Common
"${CMAKE_CURRENT_SOURCE_DIR}/EstimateFunctionSize.h"
"${CMAKE_CURRENT_SOURCE_DIR}/FixAddrSpaceCast.h"
"${CMAKE_CURRENT_SOURCE_DIR}/FixupExtractValuePair.h"
"${CMAKE_CURRENT_SOURCE_DIR}/FoldKnownWorkGroupSizes.h"
"${CMAKE_CURRENT_SOURCE_DIR}/GenCodeGenModule.h"
"${CMAKE_CURRENT_SOURCE_DIR}/GenIRLowering.h"
"${CMAKE_CURRENT_SOURCE_DIR}/GenNullPointerLowering.h"

View File

@ -0,0 +1,152 @@
/*===================== begin_copyright_notice ==================================
Copyright (c) 2017 Intel Corporation
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.
======================= end_copyright_notice ==================================*/
#include "FoldKnownWorkGroupSizes.h"
#include "../IGCPassSupport.h"
#include "../CodeGenPublic.h"
#include "../MetaDataApi/MetaDataApi.h"
#include "LLVMWarningsPush.hpp"
#include "llvm/IR/Function.h"
#include <llvm/IR/InstVisitor.h>
#include "LLVMWarningsPop.hpp"
#include "common/igc_regkeys.hpp"
namespace IGC
{
class FoldKnownWorkGroupSizes : public llvm::FunctionPass, public llvm::InstVisitor<FoldKnownWorkGroupSizes>
{
private:
static char ID;
public:
FoldKnownWorkGroupSizes() : FunctionPass(ID) {}
bool runOnFunction(llvm::Function &F);
void visitCallInst(llvm::CallInst &I);
void getAnalysisUsage(llvm::AnalysisUsage &AU) const
{
AU.addRequired<IGC::CodeGenContextWrapper>();
}
};
bool m_changed = false;
char FoldKnownWorkGroupSizes::ID = 0;
}
using namespace llvm;
using namespace IGC;
using namespace IGCMD;
bool FoldKnownWorkGroupSizes::runOnFunction(Function &F)
{
visit(F);
return m_changed;
}
void FoldKnownWorkGroupSizes::visitCallInst(llvm::CallInst &I)
{
Function* function = I.getParent()->getParent();
//Value* callingInst = ;
Module* module = function->getParent();
StringRef funcName = I.getCalledFunction()->getName();
CodeGenContext* ctx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
if (funcName.equals("__builtin_IB_get_global_offset") && ctx->getModuleMetaData()->compOpt.replaceGlobalOffsetsByZero)
{
if (I.getCalledFunction()->getReturnType() == Type::getInt32Ty(module->getContext()))
{
ConstantInt* IntZero = ConstantInt::get(Type::getInt32Ty(module->getContext()), 0);
I.replaceAllUsesWith(IntZero);
m_changed = true;
}
return;
}
else if (funcName.equals("__builtin_IB_get_enqueued_local_size"))
{
auto itr = ctx->getMetaDataUtils()->findFunctionsInfoItem(I.getParent()->getParent());
//Check function exists in the metadata
if (itr == ctx->getMetaDataUtils()->end_FunctionsInfo())
return;
FunctionInfoMetaDataHandle funcMDHandle = itr->second;
ThreadGroupSizeMetaDataHandle tgMD = funcMDHandle->getThreadGroupSize();
//Check threadGroup has value
if (!tgMD->hasValue())
return;
unsigned int dimension = (unsigned int)static_cast<ConstantInt*>(I.getArgOperand(0))->getZExtValue();
ConstantInt *valueToReplaceWith = nullptr;
if (dimension == 0)
{
if (tgMD->isXDimHasValue())
{
valueToReplaceWith = ConstantInt::get(Type::getInt32Ty(module->getContext()), tgMD->getXDim());
I.replaceAllUsesWith(valueToReplaceWith);
m_changed = true;
}
}
else if (dimension == 1)
{
if (tgMD->isYDimHasValue())
{
valueToReplaceWith = ConstantInt::get(Type::getInt32Ty(module->getContext()), tgMD->getYDim());
I.replaceAllUsesWith(valueToReplaceWith);
m_changed = true;
}
}
else if (dimension == 2)
{
if (tgMD->isZDimHasValue())
{
valueToReplaceWith = ConstantInt::get(Type::getInt32Ty(module->getContext()), tgMD->getZDim());
I.replaceAllUsesWith(valueToReplaceWith);
m_changed = true;
}
}
else
{
assert("Invalid thread group dimension");
}
return;
}
}
namespace IGC
{
llvm::FunctionPass* CreateFoldKnownWorkGroupSizes()
{
return new FoldKnownWorkGroupSizes();
}
}

View File

@ -0,0 +1,35 @@
#pragma once
/*===================== begin_copyright_notice ==================================
Copyright (c) 2017 Intel Corporation
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.
======================= end_copyright_notice ==================================*/
#pragma once
#include "common/LLVMWarningsPush.hpp"
#include "llvm/Pass.h"
#include "common/LLVMWarningsPop.hpp"
namespace IGC
{
llvm::FunctionPass* CreateFoldKnownWorkGroupSizes();
}

View File

@ -805,6 +805,7 @@ namespace IGC
m_pMdUtils = new IGC::IGCMD::MetaDataUtils(m);
modMD = new IGC::ModuleMetaData();
initCompOptionFromRegkey();
}
// Several clients explicitly delete module without resetting module to null.
@ -879,6 +880,7 @@ namespace IGC
return getModuleMetaData()->compOpt;
}
virtual void resetOnRetry()
{
m_tempCount = 0;
@ -1118,6 +1120,12 @@ namespace IGC
return;
const char *options = pInputArgs->pInternalOptions;
if (strstr(options, "-cl-replace-global-offsets-by-zero"))
{
replaceGlobalOffsetsByZero = true;
}
if (strstr(options, "-cl-kernel-debug-enable"))
{
KernelDebugEnable = true;
@ -1168,9 +1176,11 @@ namespace IGC
bool IncludeSIPKernelDebugWithLocalMemory;
bool DoReRA;
bool IntelHasBufferOffsetArg;
bool replaceGlobalOffsetsByZero = false;
bool IntelEnablePreRAScheduling = true;
bool PromoteStatelessToBindless = false;
};
class Options
@ -1282,7 +1292,7 @@ namespace IGC
void ConstantFolder(char* bitcode, uint bitcodeSize, void* CBptr[15], uint* pNewCB);
void LinkOptIR(CodeGenContext* ctxs[]);
inline llvm::LLVMContext* toLLVMContext(CodeGenContext* p) {
return p->getLLVMContext();
}

View File

@ -91,8 +91,10 @@ namespace IGC
bool GreaterThan4GBBufferRequired = true;
bool PushConstantsEnable = true;
bool HasBufferOffsetArg = false;
bool replaceGlobalOffsetsByZero = false;
unsigned forcePixelShaderSIMDMode = 0;
bool pixelShaderDoNotAbortOnSpill = false;
};
struct ComputeShaderInfo