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 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2019-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
//
/// GenXImportOCLBiF
/// -----------
///
/// This pass import Builtin Function library compiled into bitcode
///
/// - analysis functions called by the main module
///
/// - import used function, and remove unused functions
///
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "cmimportbif"
#include "vc/GenXOpts/GenXOpts.h"
#include "vc/Support/BackendConfig.h"
#include "vc/Utils/GenX/Intrinsics.h"
#include "vc/Utils/General/BiF.h"
#include <llvm/ADT/STLExtras.h>
#include <llvm/ADT/SmallPtrSet.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/ADT/Twine.h>
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/IR/Attributes.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/InstIterator.h>
#include <llvm/IR/Instruction.h>
#include <llvm/IR/Intrinsics.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/ValueSymbolTable.h>
#include <llvm/Linker/Linker.h>
#include <llvm/Support/Error.h>
#include <llvm/Support/ErrorHandling.h>
#include <llvm/Transforms/IPO/Internalize.h>
#include <llvm/Transforms/Utils/Cloning.h>
#include <llvm/Transforms/Utils/ValueMapper.h>
#include <llvmWrapper/IR/Instructions.h>
#include "llvmWrapper/Transforms/Utils/Cloning.h"
#include <algorithm>
#include <iterator>
#include <sstream>
#include <unordered_set>
#include <vector>
#include "Probe/Assertion.h"
#include "llvm/GenXIntrinsics/GenXIntrinsicInst.h"
using namespace llvm;
class BIConvert {
// builtins that maps to one intrinsic
std::map<StringRef, unsigned> OneMap;
// builtins that maps to two intrinsics
std::map<StringRef, std::pair<unsigned, unsigned>> TwoMap;
public:
BIConvert();
void runOnModule(Module &M);
};
BIConvert::BIConvert() {
// float-to-float
OneMap["__builtin_IB_frnd_ne"] = GenXIntrinsic::genx_rnde;
OneMap["__builtin_IB_ftoh_rtn"] = GenXIntrinsic::genx_rndd;
OneMap["__builtin_IB_ftoh_rtp"] = GenXIntrinsic::genx_rndu;
OneMap["__builtin_IB_ftoh_rtz"] = GenXIntrinsic::genx_rndz;
OneMap["__builtin_IB_dtoh_rtn"] = GenXIntrinsic::genx_rnde;
OneMap["__builtin_IB_dtoh_rtp"] = GenXIntrinsic::genx_rndu;
OneMap["__builtin_IB_dtoh_rtz"] = GenXIntrinsic::genx_rndz;
OneMap["__builtin_IB_dtof_rtn"] = GenXIntrinsic::genx_rnde;
OneMap["__builtin_IB_dtof_rtp"] = GenXIntrinsic::genx_rndu;
OneMap["__builtin_IB_dtof_rtz"] = GenXIntrinsic::genx_rndz;
// math
OneMap["__builtin_IB_frnd_pi"] = GenXIntrinsic::genx_rndu;
OneMap["__builtin_IB_frnd_ni"] = GenXIntrinsic::genx_rndd;
OneMap["__builtin_IB_frnd_zi"] = GenXIntrinsic::genx_rndz;
OneMap["__builtin_IB_native_cosf"] = GenXIntrinsic::genx_cos;
OneMap["__builtin_IB_native_cosh"] = GenXIntrinsic::genx_cos;
OneMap["__builtin_IB_native_sinf"] = GenXIntrinsic::genx_sin;
OneMap["__builtin_IB_native_sinh"] = GenXIntrinsic::genx_sin;
OneMap["__builtin_IB_native_exp2f"] = GenXIntrinsic::genx_exp;
OneMap["__builtin_IB_native_exp2h"] = GenXIntrinsic::genx_exp;
OneMap["__builtin_IB_native_log2f"] = GenXIntrinsic::genx_log;
OneMap["__builtin_IB_native_log2h"] = GenXIntrinsic::genx_log;
OneMap["__builtin_IB_native_sqrtf"] = GenXIntrinsic::genx_sqrt;
OneMap["__builtin_IB_native_sqrth"] = GenXIntrinsic::genx_sqrt;
OneMap["__builtin_IB_native_sqrtd"] = GenXIntrinsic::genx_sqrt;
OneMap["__builtin_IB_popcount_1u32"] = GenXIntrinsic::genx_cbit;
OneMap["__builtin_IB_popcount_1u16"] = GenXIntrinsic::genx_cbit;
OneMap["__builtin_IB_popcount_1u8"] = GenXIntrinsic::genx_cbit;
OneMap["__builtin_IB_native_powrf"] = GenXIntrinsic::genx_pow;
OneMap["__builtin_IB_fma"] = Intrinsic::fma;
OneMap["__builtin_IB_fmah"] = Intrinsic::fma;
OneMap["__builtin_IB_bfrev"] = GenXIntrinsic::genx_bfrev;
OneMap["__builtin_IB_fmax"] = GenXIntrinsic::genx_fmax;
OneMap["__builtin_IB_fmin"] = GenXIntrinsic::genx_fmin;
OneMap["__builtin_IB_HMAX"] = GenXIntrinsic::genx_fmax;
OneMap["__builtin_IB_HMIN"] = GenXIntrinsic::genx_fmin;
OneMap["__builtin_IB_dmin"] = GenXIntrinsic::genx_fmin;
OneMap["__builtin_IB_dmax"] = GenXIntrinsic::genx_fmax;
// ieee
OneMap["__builtin_IB_ieee_sqrt"] = GenXIntrinsic::genx_ieee_sqrt;
OneMap["__builtin_IB_ieee_divide"] = GenXIntrinsic::genx_ieee_div;
OneMap["__builtin_IB_ieee_divide_f64"] = GenXIntrinsic::genx_ieee_div;
TwoMap["__builtin_IB_dtoi8_rtn"] =
std::make_pair(GenXIntrinsic::genx_rndd, GenXIntrinsic::genx_fptosi_sat);
TwoMap["__builtin_IB_dtoi8_rtp"] =
std::make_pair(GenXIntrinsic::genx_rndu, GenXIntrinsic::genx_fptosi_sat);
TwoMap["__builtin_IB_dtoi8_rte"] =
std::make_pair(GenXIntrinsic::genx_rnde, GenXIntrinsic::genx_fptosi_sat);
TwoMap["__builtin_IB_dtoi16_rtn"] =
std::make_pair(GenXIntrinsic::genx_rndd, GenXIntrinsic::genx_fptosi_sat);
TwoMap["__builtin_IB_dtoi16_rtp"] =
std::make_pair(GenXIntrinsic::genx_rndu, GenXIntrinsic::genx_fptosi_sat);
TwoMap["__builtin_IB_dtoi16_rte"] =
std::make_pair(GenXIntrinsic::genx_rnde, GenXIntrinsic::genx_fptosi_sat);
TwoMap["__builtin_IB_dtoi32_rtn"] =
std::make_pair(GenXIntrinsic::genx_rndd, GenXIntrinsic::genx_fptosi_sat);
TwoMap["__builtin_IB_dtoi32_rtp"] =
std::make_pair(GenXIntrinsic::genx_rndu, GenXIntrinsic::genx_fptosi_sat);
TwoMap["__builtin_IB_dtoi32_rte"] =
std::make_pair(GenXIntrinsic::genx_rnde, GenXIntrinsic::genx_fptosi_sat);
TwoMap["__builtin_IB_dtoi64_rtn"] =
std::make_pair(GenXIntrinsic::genx_rndd, GenXIntrinsic::genx_fptosi_sat);
TwoMap["__builtin_IB_dtoi64_rtp"] =
std::make_pair(GenXIntrinsic::genx_rndu, GenXIntrinsic::genx_fptosi_sat);
TwoMap["__builtin_IB_dtoi64_rte"] =
std::make_pair(GenXIntrinsic::genx_rnde, GenXIntrinsic::genx_fptosi_sat);
TwoMap["__builtin_IB_dtoui8_rtn"] =
std::make_pair(GenXIntrinsic::genx_rndd, GenXIntrinsic::genx_fptoui_sat);
TwoMap["__builtin_IB_dtoui8_rtp"] =
std::make_pair(GenXIntrinsic::genx_rndu, GenXIntrinsic::genx_fptoui_sat);
TwoMap["__builtin_IB_dtoui8_rte"] =
std::make_pair(GenXIntrinsic::genx_rnde, GenXIntrinsic::genx_fptoui_sat);
TwoMap["__builtin_IB_dtoui16_rtn"] =
std::make_pair(GenXIntrinsic::genx_rndd, GenXIntrinsic::genx_fptoui_sat);
TwoMap["__builtin_IB_dtoui16_rtp"] =
std::make_pair(GenXIntrinsic::genx_rndu, GenXIntrinsic::genx_fptoui_sat);
TwoMap["__builtin_IB_dtoui16_rte"] =
std::make_pair(GenXIntrinsic::genx_rnde, GenXIntrinsic::genx_fptoui_sat);
TwoMap["__builtin_IB_dtoui32_rtn"] =
std::make_pair(GenXIntrinsic::genx_rndd, GenXIntrinsic::genx_fptoui_sat);
TwoMap["__builtin_IB_dtoui32_rtp"] =
std::make_pair(GenXIntrinsic::genx_rndu, GenXIntrinsic::genx_fptoui_sat);
TwoMap["__builtin_IB_dtoui32_rte"] =
std::make_pair(GenXIntrinsic::genx_rnde, GenXIntrinsic::genx_fptoui_sat);
TwoMap["__builtin_IB_dtoui64_rtn"] =
std::make_pair(GenXIntrinsic::genx_rndd, GenXIntrinsic::genx_fptoui_sat);
TwoMap["__builtin_IB_dtoui64_rtp"] =
std::make_pair(GenXIntrinsic::genx_rndu, GenXIntrinsic::genx_fptoui_sat);
TwoMap["__builtin_IB_dtoui64_rte"] =
std::make_pair(GenXIntrinsic::genx_rnde, GenXIntrinsic::genx_fptoui_sat);
TwoMap["__builtin_IB_fma_rtz_f64"] =
std::make_pair(Intrinsic::fma, GenXIntrinsic::genx_rndz);
TwoMap["__builtin_IB_fma_rtz_f32"] =
std::make_pair(Intrinsic::fma, GenXIntrinsic::genx_rndz);
TwoMap["__builtin_IB_fma_rtp_f64"] =
std::make_pair(Intrinsic::fma, GenXIntrinsic::genx_rndu);
TwoMap["__builtin_IB_fma_rtn_f64"] =
std::make_pair(Intrinsic::fma, GenXIntrinsic::genx_rndd);
}
// Get intrinsic declaration for given base call instruction and intrinsic ID.
// Specialized for OneMap IID usage -- in case IID is not genx intrinsic id,
// assume that this is intrinsic with only one overloaded type (fma).
static Function *getOneMapIntrinsicDeclaration(CallInst &CI, const unsigned IID,
Module &M) {
if (GenXIntrinsic::isGenXIntrinsic(IID))
return vc::getGenXDeclarationForIdFromArgs(
CI.getType(), CI.args(), static_cast<GenXIntrinsic::ID>(IID), M);
IGC_ASSERT_MESSAGE(IID == Intrinsic::fma, "Expected fma intrinsic");
return Intrinsic::getDeclaration(&M, static_cast<Intrinsic::ID>(IID),
{CI.getType()});
}
void BIConvert::runOnModule(Module &M) {
std::vector<Instruction *> ListDelete;
for (Function &func : M) {
for (auto &BB : func) {
for (auto I = BB.begin(), E = BB.end(); I != E; I++) {
CallInst *InstCall = dyn_cast<CallInst>(I);
if (!InstCall)
continue;
Function *callee = InstCall->getCalledFunction();
if (!callee)
continue;
// get rid of lifetime marker, avoid dealing with it in packetizer
Intrinsic::ID id = (Intrinsic::ID)callee->getIntrinsicID();
if (id == Intrinsic::lifetime_start || id == Intrinsic::lifetime_end) {
ListDelete.push_back(InstCall);
continue;
} else if (id == Intrinsic::ctlz) {
// convert this to genx_ldz, but genx_lzd only support 32-bit input
auto Src = InstCall->getOperand(0);
auto SrcTy = Src->getType();
IGC_ASSERT(SrcTy->isIntegerTy());
IGC_ASSERT(SrcTy->getPrimitiveSizeInBits() == 32);
Type *tys[1];
SmallVector<llvm::Value *, 1> args;
// build type-list for the 1st intrinsic
tys[0] = SrcTy;
// build argument list for the 1st intrinsic
args.push_back(Src);
Function *IntrinFunc = GenXIntrinsic::getAnyDeclaration(
&M, GenXIntrinsic::genx_lzd, tys);
Instruction *IntrinCall =
CallInst::Create(IntrinFunc, args, InstCall->getName(), InstCall);
IntrinCall->setDebugLoc(InstCall->getDebugLoc());
InstCall->replaceAllUsesWith(IntrinCall);
ListDelete.push_back(InstCall);
continue;
}
StringRef CalleeName = callee->getName();
// Check if it exists in the one-intrinsic map.
if (OneMap.count(CalleeName)) {
const unsigned IID = OneMap[CalleeName];
Function *const IntrinFunc =
getOneMapIntrinsicDeclaration(*InstCall, IID, M);
const SmallVector<llvm::Value *, 3> Args{InstCall->args()};
Instruction *const IntrinCall =
CallInst::Create(IntrinFunc, Args, InstCall->getName(), InstCall);
IntrinCall->setDebugLoc(InstCall->getDebugLoc());
InstCall->replaceAllUsesWith(IntrinCall);
ListDelete.push_back(InstCall);
}
// check if the builtin maps to two intrinsics
else if (TwoMap.count(CalleeName)) {
auto pair = TwoMap[CalleeName];
// create the 1st intrinsic
Type *tys0[1];
SmallVector<llvm::Value *, 3> args0;
// build type-list for the 1st intrinsic
tys0[0] = InstCall->getArgOperand(0)->getType();
// build argument list for the 1st intrinsic
args0.append(InstCall->op_begin(),
InstCall->op_begin() + IGCLLVM::getNumArgOperands(InstCall));
Function *IntrinFunc0 =
GenXIntrinsic::getAnyDeclaration(&M, pair.first, tys0);
Instruction *IntrinCall0 = CallInst::Create(
IntrinFunc0, args0, InstCall->getName(), InstCall);
IntrinCall0->setDebugLoc(InstCall->getDebugLoc());
// create the 2nd intrinsic
Type *tys1[2];
SmallVector<llvm::Value *, 3> args1;
// build type-list for the 1st intrinsic
tys1[0] = callee->getReturnType();
tys1[1] = IntrinCall0->getType();
// build argument list for the 1st intrinsic
args1.push_back(IntrinCall0);
Function *IntrinFunc1 =
GenXIntrinsic::getAnyDeclaration(&M, pair.second, tys1);
Instruction *IntrinCall1 = CallInst::Create(
IntrinFunc1, args1, InstCall->getName(), InstCall);
IntrinCall1->setDebugLoc(InstCall->getDebugLoc());
InstCall->replaceAllUsesWith(IntrinCall1);
ListDelete.push_back(InstCall);
}
// other cases
else if (CalleeName.startswith("__builtin_IB_itof")) {
Instruction *Replace = SIToFPInst::Create(
Instruction::SIToFP, InstCall->getArgOperand(0),
callee->getReturnType(), InstCall->getName(), InstCall);
Replace->setDebugLoc(InstCall->getDebugLoc());
InstCall->replaceAllUsesWith(Replace);
ListDelete.push_back(InstCall);
} else if (CalleeName.startswith("__builtin_IB_uitof")) {
Instruction *Replace = UIToFPInst::Create(
Instruction::UIToFP, InstCall->getArgOperand(0),
callee->getReturnType(), InstCall->getName(), InstCall);
Replace->setDebugLoc(InstCall->getDebugLoc());
InstCall->replaceAllUsesWith(Replace);
ListDelete.push_back(InstCall);
} else if (CalleeName.startswith("__builtin_IB_mul_rtz")) {
Instruction *Mul = BinaryOperator::Create(
Instruction::FMul, InstCall->getArgOperand(0),
InstCall->getArgOperand(1), InstCall->getName(), InstCall);
Mul->setDebugLoc(InstCall->getDebugLoc());
Type *tys[1];
SmallVector<llvm::Value *, 3> args;
// build type-list for the 1st intrinsic
tys[0] = InstCall->getArgOperand(0)->getType();
// build argument list for the 1st intrinsic
args.push_back(Mul);
Function *IntrinFunc = GenXIntrinsic::getAnyDeclaration(
&M, GenXIntrinsic::genx_rndz, tys);
Instruction *IntrinCall =
CallInst::Create(IntrinFunc, args, InstCall->getName(), InstCall);
IntrinCall->setDebugLoc(InstCall->getDebugLoc());
InstCall->replaceAllUsesWith(IntrinCall);
ListDelete.push_back(InstCall);
} else if (CalleeName.startswith("__builtin_IB_add_rtz")) {
Instruction *Add = BinaryOperator::Create(
Instruction::FAdd, InstCall->getArgOperand(0),
InstCall->getArgOperand(1), InstCall->getName(), InstCall);
Add->setDebugLoc(InstCall->getDebugLoc());
Type *tys[1];
SmallVector<llvm::Value *, 3> args;
// build type-list for the 1st intrinsic
tys[0] = InstCall->getArgOperand(0)->getType();
// build argument list for the 1st intrinsic
args.push_back(Add);
Function *IntrinFunc = GenXIntrinsic::getAnyDeclaration(
&M, GenXIntrinsic::genx_rndz, tys);
Instruction *IntrinCall =
CallInst::Create(IntrinFunc, args, InstCall->getName(), InstCall);
IntrinCall->setDebugLoc(InstCall->getDebugLoc());
InstCall->replaceAllUsesWith(IntrinCall);
ListDelete.push_back(InstCall);
}
}
}
}
// clean up the dead calls
for (auto I : ListDelete) {
I->eraseFromParent();
}
}
static void removeFunctionBitcasts(Module &M) {
std::vector<Instruction *> list_delete;
DenseMap<Function *, std::vector<Function *>> bitcastFunctionMap;
for (Function &func : M) {
for (auto &BB : func) {
for (auto I = BB.begin(), E = BB.end(); I != E; I++) {
CallInst *pInstCall = dyn_cast<CallInst>(I);
if (!pInstCall || pInstCall->getCalledFunction())
continue;
if (auto constExpr = dyn_cast<llvm::ConstantExpr>(
IGCLLVM::getCalledValue(pInstCall))) {
if (auto funcTobeChanged =
dyn_cast<llvm::Function>(constExpr->stripPointerCasts())) {
if (funcTobeChanged->isDeclaration())
continue;
// Map between values (functions) in source of bitcast
// to their counterpart values in destination
llvm::ValueToValueMapTy operandMap;
Function *pDstFunc = nullptr;
auto BCFMI = bitcastFunctionMap.find(funcTobeChanged);
bool notExists = BCFMI == bitcastFunctionMap.end();
if (!notExists) {
auto funcVec = bitcastFunctionMap[funcTobeChanged];
notExists = true;
for (Function *F : funcVec) {
if (pInstCall->getFunctionType() == F->getFunctionType()) {
notExists = false;
pDstFunc = F;
break;
}
}
}
if (notExists) {
pDstFunc = Function::Create(pInstCall->getFunctionType(),
funcTobeChanged->getLinkage(),
funcTobeChanged->getName(), &M);
if (pDstFunc->arg_size() != funcTobeChanged->arg_size())
continue;
// Need to copy the attributes over too.
auto FuncAttrs = funcTobeChanged->getAttributes();
pDstFunc->setAttributes(FuncAttrs);
// Go through and convert function arguments over, remembering the
// mapping.
Function::arg_iterator itSrcFunc = funcTobeChanged->arg_begin();
Function::arg_iterator eSrcFunc = funcTobeChanged->arg_end();
llvm::Function::arg_iterator itDest = pDstFunc->arg_begin();
for (; itSrcFunc != eSrcFunc; ++itSrcFunc, ++itDest) {
itDest->setName(itSrcFunc->getName());
operandMap[&(*itSrcFunc)] = &(*itDest);
}
// Clone the body of the function into the dest function.
SmallVector<ReturnInst *, 8> Returns; // Ignore returns.
IGCLLVM::CloneFunctionInto(pDstFunc, funcTobeChanged, operandMap,
IGCLLVM::CloneFunctionChangeType::LocalChangesOnly, Returns, "");
pDstFunc->setCallingConv(funcTobeChanged->getCallingConv());
bitcastFunctionMap[funcTobeChanged].push_back(pDstFunc);
}
std::vector<Value *> Args;
for (unsigned I = 0, E = IGCLLVM::getNumArgOperands(pInstCall); I != E;
++I) {
Args.push_back(pInstCall->getArgOperand(I));
}
auto newCI = CallInst::Create(pDstFunc, Args, "", pInstCall);
newCI->takeName(pInstCall);
newCI->setCallingConv(pInstCall->getCallingConv());
pInstCall->replaceAllUsesWith(newCI);
pInstCall->dropAllReferences();
if (constExpr->use_empty())
constExpr->dropAllReferences();
if (funcTobeChanged->use_empty())
funcTobeChanged->eraseFromParent();
list_delete.push_back(pInstCall);
}
}
}
}
}
for (auto i : list_delete) {
i->eraseFromParent();
}
}
static void InitializeBIFlags(Module &M) {
/// @brief Adds initialization to a global-var according to given value.
/// If the given global-var does not exist, does nothing.
auto initializeVarWithValue = [&M](StringRef varName, uint32_t value) {
GlobalVariable *gv = M.getGlobalVariable(varName);
if (gv == nullptr)
return;
gv->setInitializer(
ConstantInt::get(Type::getInt32Ty(M.getContext()), value));
};
initializeVarWithValue("__FlushDenormals", 1);
initializeVarWithValue("__DashGSpecified", 0);
initializeVarWithValue("__FastRelaxedMath", 0);
initializeVarWithValue("__MadEnable", 0);
initializeVarWithValue("__UseNative64BitIntSubgroupBuiltin", 1);
initializeVarWithValue("__UseNative64BitFloatSubgroupBuiltin", 1);
initializeVarWithValue("__CRMacros", 1);
initializeVarWithValue("__IsSPIRV", 0);
initializeVarWithValue("__EnableSWSrgbWrites", 0);
float profilingTimerResolution = 0.0;
initializeVarWithValue("__ProfilingTimerResolution",
*reinterpret_cast<int *>(&profilingTimerResolution));
initializeVarWithValue("__UseMathWithLUT", 0);
initializeVarWithValue("__UseHighAccuracyMath", 0);
// FIXME: target specific, but subtarget cannot be reached in middle-end.
initializeVarWithValue("__HasInt64SLMAtomicCAS", 0);
}
static bool isOCLBuiltinDecl(const Function &F) {
if (!F.isDeclaration())
return false;
if (F.isIntrinsic() || GenXIntrinsic::isGenXIntrinsic(&F))
return false;
// presuming that the only declarations left are from OCL header
return true;
}
class GenXImportOCLBiF final : public ModulePass {
public:
static char ID;
GenXImportOCLBiF() : ModulePass(ID) {}
StringRef getPassName() const override { return "GenX import OCL BiF"; }
void getAnalysisUsage(AnalysisUsage &AU) const override;
bool runOnModule(Module &M) override;
private:
std::unique_ptr<Module> getBiFModule(BiFKind Kind, LLVMContext &Ctx);
};
char GenXImportOCLBiF::ID = 0;
INITIALIZE_PASS_BEGIN(GenXImportOCLBiF, "GenXImportOCLBiF", "GenXImportOCLBiF",
false, false)
INITIALIZE_PASS_DEPENDENCY(GenXBackendConfig)
INITIALIZE_PASS_END(GenXImportOCLBiF, "GenXImportOCLBiF", "GenXImportOCLBiF",
false, false)
ModulePass *llvm::createGenXImportOCLBiFPass() {
initializeGenXImportOCLBiFPass(*PassRegistry::getPassRegistry());
return new GenXImportOCLBiF;
}
void GenXImportOCLBiF::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<GenXBackendConfig>();
}
// Whether module has uresolved calls to OpenCL builtins.
static bool OCLBuiltinsRequired(const Module &M) {
return std::any_of(M.begin(), M.end(),
[](const Function &F) { return isOCLBuiltinDecl(F); });
}
static void forceInlining(Module &M, const StringSet<> &GVS) {
for (auto &Entry : GVS) {
StringRef Name = Entry.getKey();
Value *GV = M.getValueSymbolTable().lookup(Name);
if (!isa<Function>(GV))
continue;
cast<Function>(GV)->addFnAttr(Attribute::AlwaysInline);
}
}
bool GenXImportOCLBiF::runOnModule(Module &M) {
if (llvm::none_of(M, [](const Function &F) { return isOCLBuiltinDecl(F); }))
return false;
std::unique_ptr<Module> GenericBiFModule =
getBiFModule(BiFKind::OCLGeneric, M.getContext());
GenericBiFModule->setDataLayout(M.getDataLayout());
GenericBiFModule->setTargetTriple(M.getTargetTriple());
auto LinkerCallback = [](Module &M, const StringSet<> &GVS) {
internalizeModule(M, [&GVS](const GlobalValue &GV) {
return !GV.hasName() || (GVS.count(GV.getName()) == 0);
});
// FIXME: workaround to solve several issues in the backend, remove it
forceInlining(M, GVS);
};
if (Linker::linkModules(M, std::move(GenericBiFModule),
Linker::Flags::LinkOnlyNeeded, LinkerCallback)) {
IGC_ASSERT_MESSAGE(0, "Error OCL builtin implementation module");
}
removeFunctionBitcasts(M);
InitializeBIFlags(M);
BIConvert{}.runOnModule(M);
return true;
}
std::unique_ptr<Module> GenXImportOCLBiF::getBiFModule(BiFKind Kind,
LLVMContext &Ctx) {
MemoryBufferRef BiFModuleBuffer =
getAnalysis<GenXBackendConfig>().getBiFModule(Kind);
return vc::getLazyBiFModuleOrReportError(BiFModuleBuffer, Ctx);
}
|