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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
//===----------------------------------------------------------------------===//
///
/// These functions convert load and store instructions that have 64-bit
/// elements (whether scalar or vector) to vectors of dwords.
///
/// This is used by PrepareLoadsStoresPass to run prior to MemOpt to aid
/// vectorization of values with different types.
///
/// For example:
///
/// %x = load i64, i64 addrspace(1)* %"&x"
/// ===>
/// %7 = bitcast i64 addrspace(1)* %"&x" to <2 x i32> addrspace(1)*
/// 8 = load <2 x i32>, <2 x i32> addrspace(1)* %7
/// %9 = bitcast <2 x i32> %8 to i64
///
//===----------------------------------------------------------------------===//
#include "Compiler/IGCPassSupport.h"
#include "Compiler/CodeGenPublicEnums.h"
#include "Compiler/CodeGenPublic.h"
#include "AdaptorCommon/RayTracing/RTBuilder.h"
#include "PrepareLoadsStoresUtils.h"
#include "common/LLVMWarningsPush.hpp"
#include "llvmWrapper/Support/Alignment.h"
#include "llvmWrapper/IR/DerivedTypes.h"
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/InstIterator.h>
#include <llvm/IR/NoFolder.h>
#include "common/LLVMWarningsPop.hpp"
using namespace llvm;
using namespace IGC;
using IGCLLVM::getAlign;
template <typename T>
static Value* getIntToPtr(IRBuilder<T> &IRB, Value* NewVal, Type* Ty)
{
// We must emit scalar inttoptr this late in compilation otherwise it won't
// be handled correctly (by e.g., Emu64Ops).
if (NewVal->getType()->isIntegerTy())
return IRB.CreateIntToPtr(NewVal, Ty);
// otherwise, 'NewVal' and 'Ty' are both vectors and will have the same
// length.
IGCLLVM::FixedVectorType* newVecType = dyn_cast<IGCLLVM::FixedVectorType>(NewVal->getType());
IGCLLVM::FixedVectorType* vecType = dyn_cast<IGCLLVM::FixedVectorType>(Ty);
uint64_t NumElts = newVecType->getNumElements();
IGC_ASSERT(NumElts == vecType->getNumElements());
Value* ResultVec = UndefValue::get(vecType);
auto* ResEltTy = vecType->getElementType();
for (uint32_t i = 0; i < NumElts; i++)
{
auto* Elt = IRB.CreateExtractElement(NewVal, i);
auto* Cast = IRB.CreateIntToPtr(Elt, ResEltTy);
ResultVec = IRB.CreateInsertElement(ResultVec, Cast, i);
}
return ResultVec;
}
template <typename T>
static Value* getPtrToInt(IRBuilder<T> &IRB, Value* NewVal, Type* Ty)
{
// We must emit scalar ptrtoint this late in compilation otherwise it won't
// be handled correctly (by e.g., Emu64Ops).
if (NewVal->getType()->isPointerTy())
return IRB.CreatePtrToInt(NewVal, Ty);
// otherwise, 'NewVal' and 'Ty' are both vectors and will have the same
// length.
IGCLLVM::FixedVectorType* newVecType = dyn_cast<IGCLLVM::FixedVectorType>(NewVal->getType());
IGCLLVM::FixedVectorType* vecType = dyn_cast<IGCLLVM::FixedVectorType>(Ty);
uint64_t NumElts = newVecType->getNumElements();
IGC_ASSERT(NumElts == vecType->getNumElements());
Value* ResultVec = UndefValue::get(Ty);
auto* ResEltTy = vecType->getElementType();
for (uint32_t i = 0; i < NumElts; i++)
{
auto* Elt = IRB.CreateExtractElement(NewVal, i);
auto* Cast = IRB.CreatePtrToInt(Elt, ResEltTy);
ResultVec = IRB.CreateInsertElement(ResultVec, Cast, i);
}
return ResultVec;
}
namespace IGC {
template <typename T>
std::pair<Value*, LoadInst*>
expand64BitLoad(IRBuilder<T>& IRB, const DataLayout &DL, LoadInst* LI)
{
auto* Ty = LI->getType();
if (Ty->isAggregateType())
return {};
auto* EltTy = Ty->getScalarType();
auto* OldPtrTy = LI->getPointerOperandType();
uint64_t NumEltBytes = DL.getTypeStoreSize(EltTy);
if (NumEltBytes != 8)
return {};
uint32_t NumElts = isa<IGCLLVM::FixedVectorType>(Ty) ?
(uint32_t)cast<IGCLLVM::FixedVectorType>(Ty)->getNumElements() : 1;
uint32_t NewNumElts = NumElts * 2;
auto* NewTy = IGCLLVM::FixedVectorType::get(IRB.getInt32Ty(), NewNumElts);
auto* PtrTy = PointerType::get(
NewTy, OldPtrTy->getPointerAddressSpace());
auto* NewPtr = IRB.CreateBitCast(LI->getPointerOperand(), PtrTy);
LoadInst* NewLI =
IRB.CreateAlignedLoad(NewTy, NewPtr, IGCLLVM::getCorrectAlign(LI->getAlignment()));
NewLI->copyMetadata(*LI);
Value* NewVal = NewLI;
if (Ty->isPtrOrPtrVectorTy())
{
Type* NewTy = (NumElts == 1) ?
(Type*)IRB.getInt64Ty() :
(Type*)IGCLLVM::FixedVectorType::get(IRB.getInt64Ty(), NumElts);
NewVal = IRB.CreateBitCast(NewVal, NewTy);
NewVal = getIntToPtr(IRB, NewVal, Ty);
}
else
{
NewVal = IRB.CreateBitCast(NewVal, Ty);
}
return { NewVal, NewLI };
}
template <typename T>
StoreInst* expand64BitStore(IRBuilder<T>& IRB, const DataLayout &DL, StoreInst* SI)
{
auto* Ty = SI->getValueOperand()->getType();
if (Ty->isAggregateType())
return nullptr;
auto* EltTy = Ty->getScalarType();
auto* OldPtrTy = SI->getPointerOperandType();
uint64_t NumEltBytes = DL.getTypeStoreSize(EltTy);
if (NumEltBytes != 8)
return nullptr;
uint32_t NumElts = isa<IGCLLVM::FixedVectorType>(Ty) ?
(uint32_t)cast<IGCLLVM::FixedVectorType>(Ty)->getNumElements() : 1;
uint32_t NewNumElts = NumElts * 2;
auto* NewTy = IGCLLVM::FixedVectorType::get(IRB.getInt32Ty(), NewNumElts);
Value* NewVal = SI->getValueOperand();
if (Ty->isPtrOrPtrVectorTy())
{
Type* NewTy = (NumElts == 1) ?
(Type*)IRB.getInt64Ty() :
(Type*)IGCLLVM::FixedVectorType::get(IRB.getInt64Ty(), NumElts);
NewVal = getPtrToInt(IRB, NewVal, NewTy);
}
NewVal = IRB.CreateBitCast(NewVal, NewTy);
auto* NewPtr = IRB.CreateBitCast(
SI->getPointerOperand(),
NewVal->getType()->getPointerTo(
OldPtrTy->getPointerAddressSpace()));
auto* NewST =
IRB.CreateAlignedStore(NewVal, NewPtr, IGCLLVM::getCorrectAlign(SI->getAlignment()));
NewST->copyMetadata(*SI);
return NewST;
}
template std::pair<Value*, LoadInst*>
expand64BitLoad(IRBuilder<>& IRB, const DataLayout& DL, LoadInst* LI);
template StoreInst* expand64BitStore(
IRBuilder<>& IRB, const DataLayout& DL, StoreInst* SI);
template std::pair<Value*, LoadInst*>
expand64BitLoad(IRBuilder<NoFolder>& IRB, const DataLayout& DL, LoadInst* LI);
template StoreInst* expand64BitStore(
IRBuilder<NoFolder>& IRB, const DataLayout& DL, StoreInst* SI);
}
|