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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "TypeLegalizer.h"
#include "InstScalarizer.h"
#include "common/LLVMWarningsPush.hpp"
#include "llvm/Support/Debug.h"
#include "llvmWrapper/IR/DerivedTypes.h"
#include "llvm/Support/raw_ostream.h"
#include "common/LLVMWarningsPop.hpp"
#include "common/Types.hpp"
#include "Probe/Assertion.h"
#define DEBUG_TYPE "type-legalizer"
using namespace llvm;
using namespace IGC::Legalizer;
bool InstScalarizer::scalarize(Instruction* I) {
IRB->SetInsertPoint(&(*std::next(BasicBlock::iterator(I))));
IRB->SetCurrentDebugLocation(I->getDebugLoc());
Scalarized.clear();
if (!visit(*I))
return false;
if (!Scalarized.empty())
TL->setLegalizedValues(I, Scalarized);
return true;
}
// By default, capture all missing instructions!
bool InstScalarizer::visitInstruction(Instruction& I) {
LLVM_DEBUG(dbgs() << "SCALARIZE: " << I << '\n');
IGC_ASSERT_EXIT_MESSAGE(0, "UNKNOWN INSTRUCTION IS BEING SCALARIZED!");
return false;
}
/// Terminator instructions
///
bool InstScalarizer::visitTerminatorInst(IGCLLVM::TerminatorInst& I) {
// All terminators are handled specially.
return false;
}
/// Standard binary operators
///
bool InstScalarizer::visitBinaryOperator(BinaryOperator& I) {
ValueSeq* Ops0, * Ops1;
std::tie(Ops0, std::ignore) = TL->getLegalizedValues(I.getOperand(0));
// we should get copy of Ops0 here, as next getLegalizedValues call may grow ValueMap object
// when inserting new pair with ValueMap.insert(e.g.when ValueMap.NumBuckets grows from 64 to 128)
// and previously received ValueSeq objects will become invalid.
ValueSeq Ops0Copy(*Ops0);
std::tie(Ops1, std::ignore) = TL->getLegalizedValues(I.getOperand(1));
IGC_ASSERT(Ops0Copy.size() == Ops1->size());
for (unsigned i = 0, e = Ops0Copy.size(); i != e; ++i) {
Value* LHS = (Ops0Copy)[i];
Value* RHS = (*Ops1)[i];
Scalarized.push_back(
TL->createBinOpAsGiven(&I, LHS, RHS,
Twine(I.getName(), getSuffix()) + Twine(i)));
}
return true;
}
/// Memory operators
///
bool InstScalarizer::visitLoadInst(LoadInst& I) {
Type* OrigTy = I.getType();
TypeSeq* TySeq;
std::tie(TySeq, std::ignore) = TL->getLegalizedTypes(OrigTy);
StringRef Name = I.getName();
unsigned AS = I.getPointerAddressSpace();
Type* EltTy = cast<VectorType>(OrigTy)->getElementType();
if (TL->preferVectorLoad(OrigTy)) {
// Skip if this load is already legalized.
if (TL->hasLegalizedValues(&I))
return false;
for (unsigned i = 0, e = TySeq->size(); i != e; ++i) {
Value* Idx = IRB->getInt32(i);
Scalarized.push_back(
IRB->CreateExtractElement(&I, Idx,
Twine(Name, getSuffix()) + Twine(i)));
}
return true;
}
const auto& ProfitLengths = TL->getProfitLoadVectorLength(EltTy);
if (!ProfitLengths.empty()) {
// Break vector loads into profitable vector loads and extract all
// elements.
// NOTE: It's assumed the element in this case is byte-addressable;
// otherwise, it's broken.
IGC_ASSERT(TL->getTypeSizeInBits(EltTy) == TL->getTypeStoreSizeInBits(EltTy));
unsigned NumElts = (unsigned)cast<IGCLLVM::FixedVectorType>(OrigTy)->getNumElements();
unsigned Elt = 0;
Type* NewPtrTy = PointerType::get(EltTy, AS);
Value* OldPtr = I.getPointerOperand();
Value* NewBasePtr =
IRB->CreatePointerCast(OldPtr, NewPtrTy,
Twine(OldPtr->getName(), getSuffix()) + Twine(Elt));
// Try all possible profitable lengths.
unsigned Off = 0;
for (auto PLI = ProfitLengths.rbegin(),
PLE = ProfitLengths.rend(); PLI != PLE; ++PLI) {
unsigned PL = *PLI;
IGC_ASSERT(PL > 0);
for (; NumElts >= PL; NumElts -= PL) {
Value* NewPtr =
TL->getPointerToElt(NewBasePtr, Elt, NewPtrTy,
Twine(OldPtr->getName(), getSuffix()) + Twine(Elt));
if (PL != 1) {
// Load <PL x EltTy>
Type* VecTy = IGCLLVM::FixedVectorType::get(EltTy, PL);
Type* VecPtrTy = PointerType::get(VecTy, AS);
NewPtr =
IRB->CreatePointerCast(NewPtr, VecPtrTy,
Twine(NewPtr->getName(), ".ptrcast"));
LoadInst* NewLd =
IRB->CreateLoad(NewPtr, Twine(Name, ".vec") + Twine(Elt));
TL->dupMemoryAttribute(NewLd, &I, Off);
for (unsigned i = 0; i != PL; ++i) {
Value* Idx = IRB->getInt32(i);
Scalarized.push_back(
IRB->CreateExtractElement(NewLd, Idx,
Twine(Name, getSuffix()) + Twine(Elt + i)));
}
Off += TL->getTypeStoreSizeInBits(VecTy);
}
else {
LoadInst* NewLd =
IRB->CreateLoad(NewPtr, Twine(Name, getSuffix()) + Twine(Elt));
TL->dupMemoryAttribute(NewLd, &I, Off);
Scalarized.push_back(NewLd);
Off += TL->getTypeStoreSizeInBits(EltTy);
}
Elt += PL;
}
// Early out if all elements are extracted.
if (NumElts == 0)
break;
}
return true;
}
unsigned Width = TL->getTypeStoreSizeInBits(OrigTy);
Type* NewTy = TL->getIntNTy(Width);
Type* NewPtrTy = PointerType::get(NewTy, AS);
// Load all bits.
Value* OldPtr = I.getPointerOperand();
Value* NewPtr =
IRB->CreatePointerCast(OldPtr, NewPtrTy,
Twine(OldPtr->getName(), ".ptrcast"));
LoadInst* Chunk = IRB->CreateLoad(NewPtr, Twine(Name, ".chunk"));
TL->dupMemoryAttribute(Chunk, &I, 0);
Type* EltITy = TL->getIntNTy(TL->getTypeSizeInBits(EltTy));
unsigned Elt = 0;
Scalarized.push_back(
IRB->CreateBitCast(
IRB->CreateTrunc(Chunk, EltITy,
Twine(Name, ".trunc") + Twine(Elt)), EltTy,
Twine(Name, getSuffix()) + Twine(Elt)));
unsigned ShAmt = 0;
for (++Elt; Elt != TySeq->size(); ++Elt) {
ShAmt += TL->getTypeSizeInBits(EltTy);
Scalarized.push_back(
IRB->CreateBitCast(
IRB->CreateTrunc(
IRB->CreateLShr(Chunk, TL->getIntN(Width, ShAmt),
Twine(Name, ".lshr") + Twine(Elt)), EltITy,
Twine(Name, ".trunc") + Twine(Elt)), EltTy,
Twine(Name, getSuffix()) + Twine(Elt)));
}
return true;
}
bool InstScalarizer::visitStoreInst(StoreInst& I) {
Value* OrigVal = I.getValueOperand();
Type* OrigTy = OrigVal->getType();
ValueSeq* ValSeq;
std::tie(ValSeq, std::ignore) = TL->getLegalizedValues(OrigVal);
IGC_ASSERT(ValSeq->size() ==
cast<IGCLLVM::FixedVectorType>(OrigTy)->getNumElements());
StringRef Name = OrigVal->getName();
unsigned AS = I.getPointerAddressSpace();
Type* EltTy = cast<VectorType>(OrigTy)->getElementType();
if (TL->preferVectorStore(OrigTy)) {
// If the type of store value prefer to be vector, prepare its
// legalized into vector.
// Reset insert position as we don't create a new store from the
// original store.
IRB->SetInsertPoint(&I);
Value* Vec = UndefValue::get(OrigTy);
for (unsigned i = 0, e = ValSeq->size(); i != e; ++i) {
Value* Idx = IRB->getInt32(i);
Vec =
IRB->CreateInsertElement(Vec, (*ValSeq)[i], Idx,
Twine(Name, ".vec") + Twine(i));
}
I.setOperand(0, Vec);
return true;
}
const auto& ProfitLengths = TL->getProfitStoreVectorLength(EltTy);
if (!ProfitLengths.empty()) {
// Otherwise, if the type of store value prefer to be stored in
// vectors, break vector stores into profitable vector stores.
// NOTE: It's assumed the element in this case is byte-addressable;
// otherwise, it's broken.
IGC_ASSERT(TL->getTypeSizeInBits(EltTy) == TL->getTypeStoreSizeInBits(EltTy));
unsigned NumElts = (unsigned)cast<IGCLLVM::FixedVectorType>(OrigTy)->getNumElements();
unsigned Elt = 0;
Type* NewPtrTy = PointerType::get(EltTy, AS);
Value* OldPtr = I.getPointerOperand();
Value* NewBasePtr =
IRB->CreatePointerCast(OldPtr, NewPtrTy,
Twine(OldPtr->getName(), getSuffix()) + Twine(Elt));
// Try all possible profitable lengths.
unsigned Off = 0;
for (auto PLI = ProfitLengths.rbegin(),
PLE = ProfitLengths.rend(); PLI != PLE; ++PLI) {
unsigned PL = *PLI;
IGC_ASSERT(PL > 0);
for (; NumElts >= PL; NumElts -= PL) {
Value* NewPtr =
TL->getPointerToElt(NewBasePtr, Elt, NewPtrTy,
Twine(OldPtr->getName(), getSuffix()) + Twine(Elt));
if (PL != 1) {
// Store <PL x EltTy>
Type* VecTy = IGCLLVM::FixedVectorType::get(EltTy, PL);
// Prepare the shorter vector.
Value* Vec = UndefValue::get(VecTy);
for (unsigned i = 0; i != PL; ++i) {
Value* Idx = IRB->getInt32(i);
Vec =
IRB->CreateInsertElement(Vec, (*ValSeq)[Elt + i], Idx,
Twine(Name, ".vec") + Twine(Elt + i));
}
Type* VecPtrTy = PointerType::get(VecTy, AS);
NewPtr =
IRB->CreatePointerCast(NewPtr, VecPtrTy,
Twine(NewPtr->getName(), ".ptrcast"));
StoreInst* NewSt = IRB->CreateStore(Vec, NewPtr);
TL->dupMemoryAttribute(NewSt, &I, Off);
Off += TL->getTypeStoreSizeInBits(VecTy);
}
else {
StoreInst* NewSt = IRB->CreateStore((*ValSeq)[Elt], NewPtr);
TL->dupMemoryAttribute(NewSt, &I, Off);
Off += TL->getTypeStoreSizeInBits(EltTy);
}
Elt += PL;
}
// Early out if all elements are extracted.
if (NumElts == 0)
break;
}
return true;
}
unsigned Width = TL->getTypeStoreSizeInBits(OrigTy);
Type* NewTy = TL->getIntNTy(Width);
Type* EltITy = TL->getIntNTy(TL->getTypeSizeInBits(EltTy));
auto VI = ValSeq->begin();
auto VE = ValSeq->end();
unsigned Elt = 0;
Value* Chunk =
IRB->CreateZExt(
IRB->CreateBitCast(*VI, EltITy,
Twine((*VI)->getName(), ".bicast")), NewTy,
Twine(Name, ".chunk") + Twine(Elt));
unsigned ShAmt = 0;
for (++VI, ++Elt; VI != VE; ++VI, ++Elt) {
ShAmt += TL->getTypeSizeInBits(EltTy);
Value* V =
IRB->CreateZExt(
IRB->CreateBitCast(*VI, EltITy,
Twine((*VI)->getName(), "bitcast")), NewTy,
Twine((*VI)->getName(), ".zext"));
Chunk =
IRB->CreateOr(
Chunk, IRB->CreateShl(V, TL->getIntN(Width, ShAmt),
Twine(V->getName(), ".shl")),
Twine(Name, ".chunk") + Twine(Elt));
}
Type* NewPtrTy = PointerType::get(NewTy, AS);
Value* OldPtr = I.getPointerOperand();
Value* NewPtr =
IRB->CreatePointerCast(OldPtr, NewPtrTy,
Twine(OldPtr->getName(), ".ptrcast"));
StoreInst* NewSt = IRB->CreateStore(Chunk, NewPtr);
TL->dupMemoryAttribute(NewSt, &I, 0);
return true;
}
bool InstScalarizer::visitGetElementPtrInst(GetElementPtrInst& I) {
return false;
}
/// Cast operators
///
bool InstScalarizer::visitBitCastInst(BitCastInst& I) {
ValueSeq* ValSeq; LegalizeAction ValAct;
std::tie(ValSeq, ValAct) = TL->getLegalizedValues(I.getOperand(0));
ValueSeq LegalVal;
if (ValAct == Legal) {
LegalVal.push_back(I.getOperand(0));
ValSeq = &LegalVal;
}
TypeSeq* TySeq; LegalizeAction Act;
std::tie(TySeq, Act) = TL->getLegalizedTypes(I.getDestTy());
TypeSeq LegalTy;
if (Act == Legal) {
LegalTy.push_back(I.getDestTy());
TySeq = &LegalTy;
}
ValueSeq Repacked;
TL->repack(&Repacked, *TySeq, *ValSeq, I.getName() + getSuffix());
if (Act == Legal) {
IGC_ASSERT(Repacked.size() == 1);
I.replaceAllUsesWith(Repacked.front());
return true;
}
std::swap(Scalarized, Repacked);
return true;
}
/// Special instructions
///
bool InstScalarizer::visitExtractElementInst(ExtractElementInst& I) {
if (!isa<Constant>(I.getIndexOperand())) {
// TODO: Add support for non-constant index. If we don't add special
// intrinsics, we have to fall back to memory loads/stores, i.e.,
//
// %elt = extractelement <N x eT> %vec, <iN> %idx
//
// is translated into
//
// %stk = alloca <N x eT>
// store <N x eT> %vec, <N x eT>* %stk
// %ptr = bitcast <N x eT>* %stk to eT*
// %eptr = getelementptr eT* %ptr, <iN> %idx
// %elt = load eT* %eptr
//
// It would be much more complicated if eT is not byte addressable.
IGC_ASSERT_EXIT_MESSAGE(0, "NON-CONSTANT IDX IN EXTRACT-ELEMENT IS NOT SUPPORTED YET!");
}
ConstantInt* CI = cast<ConstantInt>(I.getIndexOperand());
uint64_t Idx = CI->getValue().getZExtValue();
ValueSeq* ValSeq;
std::tie(ValSeq, std::ignore) =
TL->getLegalizedValues(I.getVectorOperand());
if (Idx >= ValSeq->size()) {
// Undef if idx exceeds the length of the vector operand.
Scalarized.push_back(UndefValue::get(I.getType()));
return true;
}
Value* Val = (*ValSeq)[int_cast<unsigned>(Idx)];
// Skip if it's itself, i.e. the extractelement insts added during
// legalization of vector loads.
if (Val == &I)
return false;
I.replaceAllUsesWith(Val);
return true;
}
bool InstScalarizer::visitInsertElementInst(InsertElementInst& I) {
if (!isa<Constant>(I.getOperand(2))) {
// TODO: Add support for non-constant index. If we don't add special
// intrinsics, we have to fall back to memory loads/stores, i.e.,
//
// %vec1 = extractelement <N x eT> %vec0, eT %elt, <iN> %idx
//
// is translated into
//
// %stk = allca <N x eT>
// store <N x eT> %vec, <N x eT>* %stk
// %ptr = bitcast <N x eT>* %stk to eT*
// %eptr = getelementptr eT* %ptr, <iN> %idx
// store eT %elt, eT* %eptr
// %vec1 = load <N x eT>* %stk
//
// It would be much more complicated if eT is not byte addressable.
IGC_ASSERT_EXIT_MESSAGE(0, "NON-CONSTANT IDX IN INSERT-ELEMENT IS NOT SUPPORTED YET!");
}
ConstantInt* CI = cast<ConstantInt>(I.getOperand(2));
uint64_t Idx = CI->getValue().getZExtValue();
ValueSeq* VecSeq;
std::tie(VecSeq, std::ignore) =
TL->getLegalizedValues(I.getOperand(0));
// we should get copy of VecSeq here, as next getLegalizedValues call may grow ValueMap object
// when inserting new pair with ValueMap.insert(e.g.when ValueMap.NumBuckets grows from 64 to 128)
// and previously received ValueSeq objects will become invalid.
ValueSeq VecSeqCopy(*VecSeq);
ValueSeq* EltSeq = nullptr;
LegalizeAction Act;
std::tie(EltSeq, Act) = TL->getLegalizedValues(I.getOperand(1));
ValueSeq LegalVal;
if (Act == Legal) {
LegalVal.push_back(I.getOperand(1));
EltSeq = &LegalVal;
}
IGC_ASSERT(nullptr != EltSeq);
IGC_ASSERT(EltSeq->size());
IGC_ASSERT(VecSeqCopy.size() % EltSeq->size() == 0);
unsigned NumElts = (unsigned)cast<IGCLLVM::FixedVectorType>(I.getOperand(0)->getType())->getNumElements();
unsigned i = 0;
for (unsigned Elt = 0; Elt != NumElts; ++Elt) {
if (Elt == Idx) {
for (auto* Val : *EltSeq) {
Scalarized.push_back(Val);
++i;
}
continue;
}
for (unsigned j = 0, e = EltSeq->size(); j != e; ++i, ++j)
Scalarized.push_back((VecSeqCopy)[i]);
}
return true;
}
|