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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "vc/Utils/General/InstRebuilder.h"
#include "vc/Utils/General/Types.h"
#include "Probe/Assertion.h"
#include "llvmWrapper/Support/Alignment.h"
#include <llvm/ADT/ArrayRef.h>
#include <llvm/IR/InstVisitor.h>
#include <llvm/IR/Instruction.h>
#include <llvm/IR/IntrinsicInst.h>
#include <algorithm>
#include <iterator>
using namespace llvm;
using namespace vc;
// Define intrinsic return type based on its arguments types.
static Type *getIntrinsicRetTypeBasedOnArgs(Intrinsic::ID IID,
ArrayRef<Type *> ArgTys,
LLVMContext &C) {
switch (IID) {
case Intrinsic::masked_gather:
// "Pass through" operand.
return ArgTys[3];
case Intrinsic::masked_scatter:
return Type::getVoidTy(C);
default:
IGC_ASSERT_MESSAGE(0, "unsupported intrinsic");
return nullptr;
}
}
// Emits list of overloaded types for the provided intrinsic. Takes all the
// types that may take part in overloading: return type, types of arguments.
static std::vector<Type *>
getIntrinsicOverloadedTypes(Intrinsic::ID IID, Type *RetTy,
ArrayRef<Type *> ArgTys) {
IGC_ASSERT_MESSAGE(RetTy, "wrong argument");
// FIXME: generalize for any ID using IntrinsicInfoTable.
switch (IID) {
case Intrinsic::masked_gather:
// Loaded value type and pointer operand type.
return {RetTy, ArgTys[0]};
case Intrinsic::masked_scatter:
// Value and pointer types.
return {ArgTys[0], ArgTys[1]};
default:
IGC_ASSERT_MESSAGE(0, "unsupported intrinsic");
return {};
}
}
namespace {
class cloneInstWithNewOpsImpl
: public InstVisitor<cloneInstWithNewOpsImpl, Instruction *> {
ArrayRef<Value *> NewOperands;
public:
cloneInstWithNewOpsImpl(ArrayRef<Value *> NewOperandsIn)
: NewOperands{NewOperandsIn} {}
Instruction *visitInstruction(Instruction &I) const {
IGC_ASSERT_MESSAGE(0, "yet unsupported instruction");
return nullptr;
}
Instruction *visitBinaryOperator(BinaryOperator &OrigBO) {
IGC_ASSERT_MESSAGE(NewOperands.size() == 2, "binary operator has 2 operands");
return BinaryOperator::Create(OrigBO.getOpcode(), NewOperands[0],
NewOperands[1]);
}
Instruction *visitGetElementPtrInst(GetElementPtrInst &OrigGEP) const {
return GetElementPtrInst::Create(OrigGEP.getSourceElementType(),
NewOperands.front(),
NewOperands.drop_front());
}
Instruction *visitTrunc(TruncInst &Trunc) {
Value &NewOp = getSingleNewOperand();
Type *NewOutType = vc::getNewTypeForCast(
Trunc.getType(), Trunc.getOperand(0)->getType(), NewOp.getType());
return new TruncInst{&NewOp, NewOutType};
}
Instruction *visitLoadInst(LoadInst &OrigLoad) {
Value &Ptr = getSingleNewOperand();
auto *NewLoad =
new LoadInst{cast<PointerType>(Ptr.getType())->getPointerElementType(),
&Ptr,
"",
OrigLoad.isVolatile(),
IGCLLVM::getAlign(OrigLoad),
OrigLoad.getOrdering(),
OrigLoad.getSyncScopeID()};
return NewLoad;
}
StoreInst *visitStoreInst(StoreInst &OrigStore) {
IGC_ASSERT_MESSAGE(NewOperands.size() == 2, "store has 2 operands");
return new StoreInst{NewOperands[0], NewOperands[1],
OrigStore.isVolatile(), IGCLLVM::getAlign(OrigStore),
OrigStore.getOrdering(), OrigStore.getSyncScopeID()};
}
// Rebuilds bitcast \p OrigInst so it now has \p NewOp as operand and result
// type addrspace corresponds with this operand.
CastInst *visitBitCastInst(BitCastInst &OrigCast) {
Value &NewOp = getSingleNewOperand();
Type *NewOutType = vc::getNewTypeForCast(
OrigCast.getType(), OrigCast.getOperand(0)->getType(), NewOp.getType());
return new BitCastInst{&NewOp, NewOutType};
}
CastInst *visitAddrSpaceCastInst(AddrSpaceCastInst &OrigCast) {
Value &NewOp = getSingleNewOperand();
auto *NewOpTy = cast<PointerType>(NewOp.getType());
auto *CastTy = cast<PointerType>(OrigCast.getType());
if (NewOpTy->getAddressSpace() == CastTy->getAddressSpace())
return nullptr;
return new AddrSpaceCastInst{&NewOp, CastTy};
}
SelectInst *visitSelectInst(SelectInst &OrigSelect) {
IGC_ASSERT_MESSAGE(NewOperands.size() == 3, "select has 3 operands");
return SelectInst::Create(NewOperands[0], NewOperands[1], NewOperands[2]);
}
IntrinsicInst *visitIntrinsicInst(IntrinsicInst &OrigIntrinsic) {
auto IID = OrigIntrinsic.getIntrinsicID();
if (IID != Intrinsic::masked_gather && IID != Intrinsic::masked_scatter) {
IGC_ASSERT_MESSAGE(0, "yet unsupported instruction");
return nullptr;
}
std::vector<Type *> ArgTys;
std::transform(NewOperands.begin(), NewOperands.end(),
std::back_inserter(ArgTys),
[](Value *Operand) { return Operand->getType(); });
auto *RetTy =
getIntrinsicRetTypeBasedOnArgs(IID, ArgTys, OrigIntrinsic.getContext());
auto OverloadedTys = getIntrinsicOverloadedTypes(IID, RetTy, ArgTys);
auto *Decl = Intrinsic::getDeclaration(OrigIntrinsic.getModule(), IID,
OverloadedTys);
return cast<IntrinsicInst>(CallInst::Create(Decl, NewOperands));
}
private:
Value &getSingleNewOperand() {
IGC_ASSERT_MESSAGE(
NewOperands.size() == 1,
"it should've been called only for instructions with a single operand");
return *NewOperands.front();
}
};
} // anonymous namespace
// Creates new instruction with all the properties taken from the \p OrigInst
// except for operands that are taken from \p NewOps.
// nullptr is returned when clonning is imposible.
Instruction *vc::cloneInstWithNewOps(Instruction &OrigInst,
ArrayRef<Value *> NewOps) {
Instruction *NewInst = cloneInstWithNewOpsImpl{NewOps}.visit(OrigInst);
if (NewInst) {
NewInst->copyIRFlags(&OrigInst);
NewInst->copyMetadata(OrigInst);
}
return NewInst;
}
|