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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#ifndef VC_UTILS_GENX_INTRINSICS_H
#define VC_UTILS_GENX_INTRINSICS_H
#include "vc/InternalIntrinsics/InternalIntrinsics.h"
#include "vc/Utils/GenX/TypeSize.h"
#include "Probe/Assertion.h"
#include <llvm/GenXIntrinsics/GenXIntrinsics.h>
#include <llvm/ADT/ArrayRef.h>
#include <llvm/ADT/STLExtras.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Value.h>
#include <type_traits>
#include <utility>
namespace llvm {
class Function;
class Module;
class Type;
} // namespace llvm
namespace vc {
namespace detail {
// Returns zero extended value of a call inst \p CI constant operand with
// \p OpIdx index.
inline int getConstIntOperand(const llvm::CallInst &CI, int OpIdx) {
return llvm::cast<llvm::ConstantInt>(CI.getOperand(OpIdx))->getZExtValue();
}
template <bool IsConst> class ReadVariableRegionImpl {
using MaybeConstCallInst =
std::conditional_t<IsConst, const llvm::CallInst, llvm::CallInst>;
using MaybeConstGlobalVariable =
std::conditional_t<IsConst, const llvm::GlobalVariable,
llvm::GlobalVariable>;
MaybeConstCallInst *CI;
public:
enum Operand {
VariableIdx,
VStrideIdx,
WidthIdx,
StrideIdx,
OffsetIdx,
NumOperands
};
ReadVariableRegionImpl(MaybeConstCallInst &CIIn) : CI{&CIIn} {
IGC_ASSERT_MESSAGE(vc::InternalIntrinsic::getInternalIntrinsicID(CI) ==
vc::InternalIntrinsic::read_variable_region,
"expected intrinsic wasn't provided to the constructor");
IGC_ASSERT_MESSAGE(getVariable().getValueType() ==
CI->getType()->getScalarType(),
"Variable operand and return types don't match");
IGC_ASSERT_MESSAGE(llvm::isa<llvm::ConstantInt>(CI->getOperand(VStrideIdx)),
"VStride operand must be a constant");
IGC_ASSERT_MESSAGE(llvm::isa<llvm::ConstantInt>(CI->getOperand(WidthIdx)),
"Width operand must be a constant");
IGC_ASSERT_MESSAGE(llvm::isa<llvm::ConstantInt>(CI->getOperand(StrideIdx)),
"Stride operand must be a constant");
IGC_ASSERT_MESSAGE(llvm::isa<llvm::ConstantInt>(CI->getOperand(OffsetIdx)),
"Offset operand must be a constant");
}
// Casting back to llvm::CallInst.
MaybeConstCallInst &getCallInst() { return *CI; }
const llvm::CallInst &getCallInst() const { return *CI; }
operator MaybeConstCallInst &() { return getCallInst(); }
operator const llvm::CallInst &() const { return getCallInst(); }
// Gets predefined vISA variable represented as llvm::GlobalVariable.
MaybeConstGlobalVariable &getVariable() {
return *llvm::cast<llvm::GlobalVariable>(CI->getOperand(VariableIdx));
}
// Gets predefined vISA variable represented as llvm::GlobalVariable.
const llvm::GlobalVariable &getVariable() const {
return *llvm::cast<llvm::GlobalVariable>(CI->getOperand(VariableIdx));
}
// Region properties accessors.
int getVStride() const { return getConstIntOperand(*CI, VStrideIdx); }
int getWidth() const { return getConstIntOperand(*CI, WidthIdx); }
int getStride() const { return getConstIntOperand(*CI, StrideIdx); }
int getOffsetInElements() const { return getConstIntOperand(*CI, OffsetIdx); }
vc::TypeSizeWrapper getOffset(llvm::DataLayout *DL = nullptr) const {
return getOffsetInElements() * vc::getTypeSize(getElementType(), DL);
}
// Gets predefined variable element type.
llvm::Type *getElementType() const { return CI->getType()->getScalarType(); }
};
template <bool IsConst> class WriteVariableRegionImpl {
using MaybeConstCallInst =
std::conditional_t<IsConst, const llvm::CallInst, llvm::CallInst>;
using MaybeConstGlobalVariable =
std::conditional_t<IsConst, const llvm::GlobalVariable,
llvm::GlobalVariable>;
using MaybeConstValue =
std::conditional_t<IsConst, const llvm::Value, llvm::Value>;
MaybeConstCallInst *CI;
public:
enum Operand {
VariableIdx,
InputIdx,
StrideIdx,
OffsetIdx,
MaskIdx,
NumOperands
};
WriteVariableRegionImpl(MaybeConstCallInst &CIIn) : CI{&CIIn} {
IGC_ASSERT_MESSAGE(vc::InternalIntrinsic::getInternalIntrinsicID(CI) ==
vc::InternalIntrinsic::write_variable_region,
"expected intrinsic wasn't provided to the constructor");
IGC_ASSERT_MESSAGE(getVariable().getValueType() ==
getInput().getType()->getScalarType(),
"Variable and input operand types don't match");
IGC_ASSERT_MESSAGE(llvm::isa<llvm::ConstantInt>(CI->getOperand(StrideIdx)),
"Stride operand must be a constant");
IGC_ASSERT_MESSAGE(llvm::isa<llvm::ConstantInt>(CI->getOperand(OffsetIdx)),
"Offset operand must be a constant");
}
// Casting back to llvm::CallInst.
MaybeConstCallInst &getCallInst() { return *CI; }
const llvm::CallInst &getCallInst() const { return *CI; }
operator MaybeConstCallInst &() { return getCallInst(); }
operator const llvm::CallInst &() const { return getCallInst(); }
// Gets predefined vISA variable represented as llvm::GlobalVariable.
MaybeConstGlobalVariable &getVariable() {
return *llvm::cast<llvm::GlobalVariable>(CI->getOperand(VariableIdx));
}
// Gets predefined vISA variable represented as llvm::GlobalVariable.
const llvm::GlobalVariable &getVariable() const {
return *llvm::cast<llvm::GlobalVariable>(CI->getOperand(VariableIdx));
}
// Gets value that is being written to the predefined variable.
MaybeConstValue &getInput() { return *CI->getOperand(InputIdx); }
const llvm::Value &getInput() const { return *CI->getOperand(InputIdx); }
// Gets predecation mask.
MaybeConstValue &getMask() { return *CI->getOperand(MaskIdx); }
const llvm::Value &getMask() const { return *CI->getOperand(MaskIdx); }
// Region properties accessors.
int getStride() const { return getConstIntOperand(*CI, StrideIdx); }
int getOffsetInElements() const { return getConstIntOperand(*CI, OffsetIdx); }
vc::TypeSizeWrapper getOffset(llvm::DataLayout *DL = nullptr) const {
return getOffsetInElements() * vc::getTypeSize(getElementType(), DL);
}
// Gets predefined variable element type.
llvm::Type *getElementType() const {
return getInput().getType()->getScalarType();
}
};
template <typename Range, typename IsIntrinsicFunc,
typename IsOverloadedRetFunc, typename IsOverloadedArgFunc,
typename GetDeclarationFunc>
llvm::Function *getDeclarationForIdFromArgs(llvm::Type *RetTy, Range &&Args,
unsigned Id, llvm::Module &M,
IsIntrinsicFunc IsIntrinsic,
IsOverloadedRetFunc IsOverloadedRet,
IsOverloadedArgFunc IsOverloadedArg,
GetDeclarationFunc GetDeclaration) {
using namespace llvm;
IGC_ASSERT_MESSAGE(IsIntrinsic(Id), "Expected genx intrinsic id");
SmallVector<Type *, 4> Types;
if (IsOverloadedRet(Id)) {
IGC_ASSERT_MESSAGE(RetTy, "Expected return type because it is overloaded");
Types.push_back(RetTy);
}
for (auto &&EnumArg : llvm::enumerate(Args)) {
if (IsOverloadedArg(Id, EnumArg.index()))
Types.push_back(EnumArg.value()->getType());
}
return GetDeclaration(M, Id, Types);
}
} // namespace detail
// A wrapper class for CallInst with @llvm.vc.internal.read.variable.region
// intrinsic. Provides convenient accessors for the intrinsic specific operands
// and properties.
using ReadVariableRegion = detail::ReadVariableRegionImpl</* IsConst=*/false>;
// The same as above but for const CallInst case.
using ReadVariableRegionConst =
detail::ReadVariableRegionImpl</* IsConst=*/true>;
// A wrapper class for CallInst with @llvm.vc.internal.write.variable.region
// intrinsic. Provides convenient accessors for the intrinsic specific operands
// and properties.
using WriteVariableRegion = detail::WriteVariableRegionImpl</* IsConst=*/false>;
// The same as above but for const CallInst case.
using WriteVariableRegionConst =
detail::WriteVariableRegionImpl</* IsConst=*/true>;
// Return declaration for intrinsics with provided parameters.
// This is helper function to get genx intrinsic declaration for given
// intrinsic ID, return type and arguments.
// RetTy -- return type of new intrinsic, may be nullptr if not overloaded.
// Args -- range of Value * representing new intrinsic arguments. Each value
// must be non-null.
// Id -- new genx intrinsic ID.
// M -- module where to insert function declaration.
template <typename Range>
llvm::Function *getGenXDeclarationForIdFromArgs(llvm::Type *RetTy, Range &&Args,
llvm::GenXIntrinsic::ID Id,
llvm::Module &M) {
using namespace llvm;
return detail::getDeclarationForIdFromArgs(
RetTy, std::forward<Range>(Args), Id, M,
[](unsigned Id) { return GenXIntrinsic::isGenXIntrinsic(Id); },
[](unsigned Id) { return GenXIntrinsic::isOverloadedRet(Id); },
[](unsigned Id, unsigned ArgIdx) {
return GenXIntrinsic::isOverloadedArg(Id, ArgIdx);
},
[](Module &M, unsigned Id, ArrayRef<Type *> Types) {
return GenXIntrinsic::getGenXDeclaration(
&M, static_cast<GenXIntrinsic::ID>(Id), Types);
});
}
// The same as \p getGenXDeclarationForIdFromArgs but for internal intrinisics.
template <typename Range>
llvm::Function *
getInternalDeclarationForIdFromArgs(llvm::Type *RetTy, Range &&Args,
vc::InternalIntrinsic::ID Id,
llvm::Module &M) {
using namespace llvm;
return detail::getDeclarationForIdFromArgs(
RetTy, std::forward<Range>(Args), Id, M,
[](unsigned Id) {
return vc::InternalIntrinsic::isInternalIntrinsic(Id);
},
[](unsigned Id) { return vc::InternalIntrinsic::isOverloadedRet(Id); },
[](unsigned Id, unsigned ArgIdx) {
return vc::InternalIntrinsic::isOverloadedArg(Id, ArgIdx);
},
[](Module &M, unsigned Id, ArrayRef<Type *> Types) {
return vc::InternalIntrinsic::getInternalDeclaration(
&M, static_cast<vc::InternalIntrinsic::ID>(Id), Types);
});
}
// Routine for creating a new llvm::Intrinsic, InternalIntrinsic or
// GenXIntrinsic.
// Arguments:
// \p Builder - IRBuilder, need to create a new GenXIntrinsic;
// \p IID - Intrinsic::ID, GenXIntrinsic::ID or vc::IntrenalIntrinsic::ID - the
// ID of intrinsic;
// \p Types - additional input/output types for choosing right
// GenXIntrinsic/InternalIntrinsic/Intrinsic from several with the same ID;
// \p Operands - the expected arguments of intrinsic;
// Return:
// A pointer to created Intrinsic/GenXIntrinsic/InternalIntrinsic, with
// ID == IID and arguments from \p Operands;
llvm::CallInst *createAnyIntrinsic(llvm::IRBuilder<> &Builder,
llvm::ArrayRef<llvm::Value *> Operands,
unsigned IID,
llvm::ArrayRef<llvm::Type *> Types = {},
const llvm::Twine &Name = "");
} // namespace vc
#endif // VC_UTILS_GENX_INTRINSICS_H
|