1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#define DEBUG_TYPE "type-demote"
#include "Compiler/CISACodeGen/TypeDemote.h"
#include "Compiler/CISACodeGen/ShaderCodeGen.hpp"
#include "Compiler/CodeGenContextWrapper.hpp"
#include "Compiler/MetaDataUtilsWrapper.h"
#include "Compiler/IGCPassSupport.h"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/ADT/PostOrderIterator.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/Pass.h>
#include <llvmWrapper/IR/DerivedTypes.h>
#include "common/LLVMWarningsPop.hpp"
#include "GenISAIntrinsics/GenIntrinsics.h"
#include "Probe/Assertion.h"
using namespace llvm;
using namespace IGC;
using namespace IGC::IGCMD;
namespace {
typedef IRBuilder<> BuilderType;
class TypeDemote : public FunctionPass {
BuilderType* IRB;
public:
static char ID;
TypeDemote() : FunctionPass(ID), IRB(nullptr) {
initializeTypeDemotePass(*PassRegistry::getPassRegistry());
}
bool runOnFunction(Function&) override;
void getAnalysisUsage(AnalysisUsage& AU) const override {
AU.setPreservesCFG();
}
private:
bool demoteOnFunction(Function*) const;
bool demoteOnBasicBlock(BasicBlock*) const;
Value* getDemotedValue(Value* V, Type* DemotedTy, bool Unsigned) const;
};
} // End anonymous namespace
FunctionPass* IGC::createTypeDemotePass() {
return new TypeDemote();
}
char TypeDemote::ID = 0;
#define PASS_FLAG "igc-type-demote"
#define PASS_DESC "Demote type safely"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
namespace IGC {
IGC_INITIALIZE_PASS_BEGIN(TypeDemote, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_END(TypeDemote, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
}
bool TypeDemote::runOnFunction(Function& F) {
BuilderType TheBuilder(F.getContext());
IRB = &TheBuilder;
bool Changed = false;
bool LocalChanged = false;
do {
LocalChanged = demoteOnFunction(&F);
Changed |= LocalChanged;
} while (LocalChanged);
return Changed;
}
bool TypeDemote::demoteOnFunction(Function* F) const {
bool Changed = false;
ReversePostOrderTraversal<Function*> RPOT(F);
for (auto& BB : RPOT)
Changed |= demoteOnBasicBlock(BB);
return Changed;
}
bool TypeDemote::demoteOnBasicBlock(BasicBlock* BB) const {
bool Changed = false;
Type* DemotedTy = IRB->getInt8Ty();
for (auto BI = BB->begin(), BE = BB->end(); BI != BE; /* EMPTY */) {
Instruction* I = &(*BI++);
// So far, we only demote unsigned i32 to i8.
// TODO: Consider demote to i16 as well as i8 may not be supported natively
// on future Gen.
if (PHINode * PN = dyn_cast<PHINode>(I)) {
// Skip non-i32 so far.
if (!PN->getType()->isIntegerTy(32))
continue;
unsigned N = PN->getNumIncomingValues();
SmallVector<Value*, 8> DemotedValues(N, nullptr);
bool SafeToDemote = true;
for (unsigned i = 0; i != N; ++i) {
Value* DemotedVal = getDemotedValue(PN->getIncomingValue(i), DemotedTy, true);
if (!DemotedVal) {
SafeToDemote = false;
break;
}
DemotedValues[i] = DemotedVal;
}
if (!SafeToDemote)
continue;
Type* OriginTy = PN->getType();
// Demote the original `phi` to i8 one followed a `zext`.
PN->mutateType(DemotedTy);
for (unsigned i = 0; i != N; ++i)
PN->setIncomingValue(i, DemotedValues[i]);
// Create the unsigned extension.
BuilderType::InsertPointGuard Guard(*IRB);
IRB->SetInsertPoint(BB->getFirstNonPHI());
Value* V = IRB->CreateZExt(PN, OriginTy, ".demoted.zext");
for (auto UI = PN->use_begin(), UE = PN->use_end(); UI != UE; /* EMPTY */) {
Use& U = *UI++;
if (U.getUser() == V)
continue;
U.set(V);
}
Changed = true;
BI = std::next(BasicBlock::iterator(PN));
continue;
}
if (ICmpInst * Cmp = dyn_cast<ICmpInst>(I)) {
if (!Cmp->getOperand(0)->getType()->isIntegerTy(32))
continue;
if (!Cmp->isUnsigned())
continue;
Value* DemotedLHS = getDemotedValue(Cmp->getOperand(0), DemotedTy, true);
if (!DemotedLHS)
continue;
Value* DemotedRHS = getDemotedValue(Cmp->getOperand(1), DemotedTy, true);
if (!DemotedRHS)
continue;
Cmp->setOperand(0, DemotedLHS);
Cmp->setOperand(1, DemotedRHS);
Changed = true;
continue;
}
if (SelectInst * SI = dyn_cast<SelectInst>(I)) {
// Check for min or max before doing this
if (ICmpInst * cmp = dyn_cast<ICmpInst>(SI->getOperand(0)))
{
if ((cmp->getOperand(0) == SI->getOperand(1) && cmp->getOperand(1) == SI->getOperand(2)) ||
(cmp->getOperand(0) == SI->getOperand(2) && cmp->getOperand(1) == SI->getOperand(1)))
{
continue;
}
}
if (!SI->getType()->isIntegerTy(32))
continue;
Value* DemotedTrueVal = getDemotedValue(SI->getTrueValue(), DemotedTy, true);
if (!DemotedTrueVal)
continue;
Value* DemotedFalseVal = getDemotedValue(SI->getFalseValue(), DemotedTy, true);
if (!DemotedFalseVal)
continue;
Type* OriginTy = SI->getType();
SI->setOperand(1, DemotedTrueVal);
SI->setOperand(2, DemotedFalseVal);
SI->mutateType(DemotedTy);
// Create the unsigned extension.
BuilderType::InsertPointGuard Guard(*IRB);
IRB->SetInsertPoint(&(*std::next(BasicBlock::iterator(I))));
Value* V = IRB->CreateZExt(SI, OriginTy, ".demoted.zext");
for (auto UI = SI->use_begin(), UE = SI->use_end(); UI != UE; /* EMPTY */) {
Use& U = *UI++;
if (U.getUser() == V)
continue;
U.set(V);
}
Changed = true;
BI = std::next(BasicBlock::iterator(SI));
continue;
}
if (BinaryOperator * BO = dyn_cast<BinaryOperator>(I)) {
// Skip if it's already demoted.
if (BO->getType() == DemotedTy)
continue;
switch (BO->getOpcode()) {
default:
continue;
// TODO: Only handle 'and', 'or', 'xor' so far.
case Instruction::And:
case Instruction::Or:
case Instruction::Xor:
break;
}
unsigned src0BitSize = BO->getOperand( 0 )->getType()->getScalarSizeInBits();
unsigned src1BitSize = BO->getOperand( 1 )->getType()->getScalarSizeInBits();
unsigned demotedBitSize = DemotedTy->getScalarSizeInBits();
if( src0BitSize <= demotedBitSize || src1BitSize <= demotedBitSize )
continue;
Value* DemotedLHS = getDemotedValue(BO->getOperand(0), DemotedTy, true);
if (!DemotedLHS)
continue;
Value* DemotedRHS = getDemotedValue(BO->getOperand(1), DemotedTy, true);
if (!DemotedRHS)
continue;
Type* OriginTy = BO->getType();
BO->setOperand(0, DemotedLHS);
BO->setOperand(1, DemotedRHS);
BO->mutateType(DemotedTy);
// Create the unsigned extension.
BuilderType::InsertPointGuard Guard(*IRB);
IRB->SetInsertPoint(&(*std::next(BasicBlock::iterator(I))));
Value* V = IRB->CreateZExt(BO, OriginTy, ".demoted.zext");
for (auto UI = BO->use_begin(), UE = BO->use_end(); UI != UE; /* EMPTY */) {
Use& U = *UI++;
if (U.getUser() == V)
continue;
U.set(V);
}
BI = std::next(BasicBlock::iterator(BO));
if (BO->getOpcode() == Instruction::And) {
if (ConstantInt * CI = dyn_cast<ConstantInt>(DemotedRHS))
if (CI->isAllOnesValue()) {
BO->replaceAllUsesWith(DemotedLHS);
BO->eraseFromParent();
}
}
Changed = true;
continue;
}
if (TruncInst * TI = dyn_cast<TruncInst>(I)) {
if (!TI->getType()->isIntegerTy(8))
continue;
if (!TI->getSrcTy()->isIntegerTy(32))
continue;
Value* DemotedVal = getDemotedValue(TI->getOperand(0), DemotedTy, true);
if (!DemotedVal)
continue;
TI->replaceAllUsesWith(DemotedVal);
TI->eraseFromParent();
Changed = true;
continue;
}
if (ZExtInst * ZEI = dyn_cast<ZExtInst>(I)) {
if (!ZEI->getSrcTy()->isIntegerTy(8))
continue;
// TODO
}
// j.0163 = phi i16 [ 0, %._crit_edge155 ], [ %337, %..lr.ph_crit_edge ]
// %57 = zext i16 %j.0163 to i64
// %58 = extractelement <32 x i16> <i16 3, ..., i16 44, i16 50>, i64 %57
//
if (auto EEI = dyn_cast<ExtractElementInst>(I)) {
Value* Index = EEI->getIndexOperand();
CastInst* CI = dyn_cast<CastInst>(Index);
if (CI && (CI->getOpcode() == Instruction::ZExt ||
CI->getOpcode() == Instruction::SExt)) {
unsigned VS = (unsigned)cast<IGCLLVM::FixedVectorType>(EEI->getVectorOperandType())->getNumElements();
unsigned N = (unsigned int)CI->getSrcTy()->getPrimitiveSizeInBits();
unsigned Bound = (N < 32) ? (1U << N) : UINT32_MAX;
if (VS <= Bound) {
EEI->setOperand(1, CI->getOperand(0));
if (CI->use_empty())
CI->eraseFromParent();
Changed = true;
}
}
continue;
}
}
return Changed;
}
Value* TypeDemote::getDemotedValue(Value* V, Type* DemotedTy, bool Unsigned) const {
IGC_ASSERT(nullptr != DemotedTy);
IGC_ASSERT_MESSAGE(DemotedTy->isIntegerTy(8), "Only support demotion to i8!");
IGC_ASSERT_MESSAGE(Unsigned, "Only support demotion to i8!");
if (ConstantInt * CI = dyn_cast<ConstantInt>(V)) {
if (!CI->getValue().isIntN(8))
return nullptr;
V = IRB->CreateTrunc(V, DemotedTy);
IGC_ASSERT_MESSAGE(isa<ConstantInt>(V), "Constant folding is failed!");
return V;
}
if (ZExtInst * ZEI = dyn_cast<ZExtInst>(V)) {
if (!ZEI->getSrcTy()->isIntegerTy(8))
return nullptr;
return ZEI->getOperand(0);
}
return nullptr;
}
|