File: ModuleSplitter.cpp

package info (click to toggle)
intel-graphics-compiler2 2.28.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 792,744 kB
  • sloc: cpp: 5,761,745; ansic: 466,928; lisp: 312,143; python: 114,790; asm: 44,736; pascal: 10,930; sh: 8,033; perl: 7,914; ml: 3,625; awk: 3,523; yacc: 2,747; javascript: 2,667; lex: 1,898; f90: 1,028; cs: 573; xml: 474; makefile: 344; objc: 162
file content (103 lines) | stat: -rw-r--r-- 3,370 bytes parent folder | download
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "common/LLVMWarningsPush.hpp"
#include <llvm/ADT/SetVector.h>
#include <llvm/IR/InstIterator.h>
#include <llvm/Transforms/Utils/Cloning.h>
#include <llvm/Transforms/IPO.h>
#include "llvmWrapper/Transforms/IPO/GlobalDCE.h"
#include "llvmWrapper/Transforms/IPO/StripSymbols.h"
#include "llvmWrapper/Transforms/IPO/StripDeadPrototypes.h"

#include "common/LLVMWarningsPop.hpp"

#include <common/LLVMUtils.h>
#include <common/ModuleSplitter.h>
#include <Compiler/CodeGenPublic.h>
#include "Compiler/CISACodeGen/OpenCLKernelCodeGen.hpp"

namespace IGC {
KernelModuleSplitter::KernelModuleSplitter(IGC::OpenCLProgramContext &oclContext, llvm::Module &module)
    : _oclContext(oclContext), _originalModule(module), _splittedModule(nullptr) {}

KernelModuleSplitter::~KernelModuleSplitter() { restoreOclContextModule(); }

void KernelModuleSplitter::splitModuleForKernel(const llvm::Function *kernelF) {
  using namespace llvm;
  IGC_ASSERT_EXIT_MESSAGE(kernelF != nullptr, "Cannot split for null function!");

  std::vector<const Function *> workqueue;
  SetVector<const GlobalValue *> GVs;

  // add all functions called by the kernel, recursively
  // start with the kernel...
  GVs.insert(kernelF);
  workqueue.push_back(kernelF);

  // and for all called functions...
  while (!workqueue.empty()) {
    const Function *F = workqueue.back();
    workqueue.pop_back();

    for (const auto &I : instructions(F)) {
      if (const CallBase *CB = dyn_cast<CallBase>(&I)) {
        if (const Function *CF = CB->getCalledFunction()) {
          if (CF->isDeclaration() || GVs.count(CF))
            continue;

          // add only defined ones and rerun for their's calls ...
          GVs.insert(CF);
          workqueue.push_back(CF);
        }
      }
    }
  }

  // add all globals - it's easier to let them be removed later than search for them here
  for (auto &GV : _originalModule.globals()) {
    GVs.insert(&GV);
  }

  // create new module with selected globals and functions
  ValueToValueMapTy VMap;
  std::unique_ptr<Module> kernelM =
      CloneModule(_originalModule, VMap, [&](const GlobalValue *GV) { return GVs.count(GV); });
  IGC_ASSERT_EXIT_MESSAGE(kernelM, "Cloning module failed!");

  // Do cleanup.
  IGC::IGCPassManager mpm(&_oclContext, "CleanupAfterModuleSplitting");
  mpm.add(IGCLLVM::createLegacyWrappedGlobalDCEPass());           // Delete unreachable globals.
  mpm.add(IGCLLVM::createLegacyWrappedStripDeadDebugInfoPass());  // Remove dead debug info.
  mpm.add(IGCLLVM::createLegacyWrappedStripDeadPrototypesPass()); // Remove dead func decls.

  mpm.run(*kernelM.get());
  _splittedModule = std::move(kernelM);
}

void KernelModuleSplitter::retry() {
  if (_splittedModule) {
    restoreOclContextModule();
    delete _splittedModule.release();
  }
}

void KernelModuleSplitter::restoreOclContextModule() {
  if (_splittedModule) {
    _oclContext.clearMD();
    _oclContext.setModule(&_originalModule);
  }
}

void KernelModuleSplitter::setSplittedModuleInOCLContext() {
  if (_splittedModule) {
    _oclContext.clearMD();
    _oclContext.setModule(_splittedModule.get());
  }
}
} // namespace IGC