@@ -36,9 +36,11 @@ SPDX-License-Identifier: MIT
3636#include < llvm/IR/Instruction.h>
3737#include < llvm/IR/Intrinsics.h>
3838#include < llvm/IR/Module.h>
39+ #include < llvm/IR/ValueSymbolTable.h>
3940#include < llvm/Linker/Linker.h>
4041#include < llvm/Support/Error.h>
4142#include < llvm/Support/ErrorHandling.h>
43+ #include < llvm/Transforms/IPO/Internalize.h>
4244#include < llvm/Transforms/Utils/Cloning.h>
4345#include < llvm/Transforms/Utils/ValueMapper.h>
4446#include < llvmWrapper/IR/Instructions.h>
@@ -492,23 +494,37 @@ static bool OCLBuiltinsRequired(const Module &M) {
492494 [](const Function &F) { return isOCLBuiltinDecl (F); });
493495}
494496
497+ static void forceInlining (Module &M, const StringSet<> &GVS) {
498+ for (auto &Entry : GVS) {
499+ StringRef Name = Entry.getKey ();
500+ Value *GV = M.getValueSymbolTable ().lookup (Name);
501+ if (!isa<Function>(GV))
502+ continue ;
503+ cast<Function>(GV)->addFnAttr (Attribute::AlwaysInline);
504+ }
505+ }
506+
495507bool GenXImportOCLBiF::runOnModule (Module &M) {
496- auto OCLBuiltins = vc::collectFunctionNamesIf (
497- M, [](const Function &F) { return isOCLBuiltinDecl (F); });
498- if (OCLBuiltins.empty ())
508+ if (llvm::none_of (M, [](const Function &F) { return isOCLBuiltinDecl (F); }))
499509 return false ;
500510 std::unique_ptr<Module> GenericBiFModule =
501511 getBiFModule (BiFKind::OCLGeneric, M.getContext ());
502512 GenericBiFModule->setDataLayout (M.getDataLayout ());
503513 GenericBiFModule->setTargetTriple (M.getTargetTriple ());
514+ auto LinkerCallback = [](Module &M, const StringSet<> &GVS) {
515+ internalizeModule (M, [&GVS](const GlobalValue &GV) {
516+ return !GV.hasName () || (GVS.count (GV.getName ()) == 0 );
517+ });
518+ // FIXME: workaround to solve several issues in the backend, remove it
519+ forceInlining (M, GVS);
520+ };
504521 if (Linker::linkModules (M, std::move (GenericBiFModule),
505- Linker::Flags::LinkOnlyNeeded)) {
522+ Linker::Flags::LinkOnlyNeeded, LinkerCallback )) {
506523 IGC_ASSERT_MESSAGE (0 , " Error OCL builtin implementation module" );
507524 }
508525 removeFunctionBitcasts (M);
509526 InitializeBIFlags (M);
510527 BIConvert{}.runOnModule (M);
511- vc::internalizeImportedFunctions (M, OCLBuiltins, /* SetAlwaysInline=*/ true );
512528 return true ;
513529}
514530
0 commit comments