File: DropTargetBBs.cpp

package info (click to toggle)
intel-graphics-compiler2 2.22.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 107,676 kB
  • sloc: cpp: 809,645; lisp: 288,070; ansic: 16,397; python: 4,010; yacc: 2,588; lex: 1,666; pascal: 314; sh: 186; makefile: 38
file content (153 lines) | stat: -rw-r--r-- 4,728 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
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2025 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "Compiler/CISACodeGen/DropTargetBBs.hpp"

#include "common/LLVMWarningsPush.hpp"
#include <llvm/ADT/STLExtras.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Instructions.h>
#include "common/LLVMWarningsPop.hpp"

#include "Compiler/IGCPassSupport.h"
#include "common/igc_regkeys.hpp"

#include <fstream>
#include <string>
#include <optional>
#include <filesystem>

using namespace llvm;
using namespace IGC;

#define PASS_FLAG "igc-drop-target-bbs"
#define PASS_DESCRIPTION "Drop target basic blocks"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(DropTargetBBs, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_END(DropTargetBBs, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

#define DROP_BB_DEBUG(msg)                                                                                             \
  do {                                                                                                                 \
    if (VerboseLog) {                                                                                                  \
      llvm::errs() << msg << "\n";                                                                                     \
    }                                                                                                                  \
  } while (0)

char IGC::DropTargetBBs::ID = 0;

DropTargetBBs::DropTargetBBs() : FunctionPass(ID), VerboseLog(IGC_GET_FLAG_VALUE(VerboseDropTargetBBs)) {
  initializeDropTargetBBsPass(*PassRegistry::getPassRegistry());
}

void DropTargetBBs::getAnalysisUsage(llvm::AnalysisUsage &AU) const {}

static std::optional<int> getBBIndexByName(Function &F, const std::string &BBName) {
  int bbIndex = 0;
  for (auto &BB : F) {
    if (BB.hasName() && BB.getName() == BBName) {
      return bbIndex;
    }
    bbIndex++;
  }
  return std::nullopt;
}

static std::optional<SmallVector<std::string, 8>> getBBNamesToDrop(const Function &F, const char *DropBBListBasePath) {
  SmallVector<std::string, 8> BBsToDrop;
  std::filesystem::path DropBBListPath = std::filesystem::path(DropBBListBasePath) / (F.getName().str() + ".txt");
  {
    std::ifstream DropBBFile(DropBBListPath);
    if (!DropBBFile.is_open()) {
      return std::nullopt;
    }

    std::string line;
    while (std::getline(DropBBFile, line)) {
      if (line.empty()) {
        continue;
      }

      if (line.back() == '\r') {
        line.pop_back();
      }

      BBsToDrop.push_back(line);
    }
    DropBBFile.close();
  }
  return BBsToDrop;
}

bool DropTargetBBs::runOnFunction(Function &F) {
  if (F.isDeclaration()) {
    return false;
  }

  auto BBsToDropOptional = getBBNamesToDrop(F, IGC_GET_REGKEYSTRING(DropTargetBBListPath));
  if (!BBsToDropOptional.has_value()) {
    return false;
  }

  const auto &BBsToDrop = BBsToDropOptional.value();
  SmallVector<std::uint32_t, 8> BBIndexesToDrop;
  for (const auto &BBName : BBsToDrop) {
    auto BBIndex = getBBIndexByName(F, BBName);
    if (BBIndex.has_value()) {
      BBIndexesToDrop.push_back(BBIndex.value());
    }
  }

  if (BBIndexesToDrop.empty()) {
    return false;
  }

  DROP_BB_DEBUG("DropTargetBBss: Examining function: " << F.getName());
  DROP_BB_DEBUG("DropTargetBBss: BB num: " << F.size());

  for (const auto &BBIndex : BBIndexesToDrop) {
    if (BBIndex >= F.size()) {
      continue;
    }
    auto BI = F.begin();
    std::advance(BI, BBIndex);
    BasicBlock *BB = &*BI;
    DROP_BB_DEBUG("DropTargetBBs: Dropping BB index: " << BBIndex << " Name: " << BB->getName());

    while (!BB->use_empty()) {
      auto *User = *BB->user_begin();
      if (auto *PHI = dyn_cast<PHINode>(User)) {
        PHI->removeIncomingValue(BB, true);
      } else if (auto *Br = dyn_cast<BranchInst>(User)) {
        BasicBlock *NextBB = BB->getNextNode();
        if (NextBB) {
          Br->setSuccessor(Br->getSuccessor(0) == BB ? 0 : 1, NextBB);
        } else {
          Br->eraseFromParent();
        }
      } else if (auto *SI = dyn_cast<SwitchInst>(User)) {
        if (auto *CI = SI->findCaseDest(BB)) {
          SI->removeCase(SI->findCaseValue(CI));
        }
      } else {
        User->replaceUsesOfWith(BB, PoisonValue::get(BB->getType()));
      }
    }

    for (Instruction &I : *BB) {
      if (!I.use_empty()) {
        I.replaceAllUsesWith(PoisonValue::get(I.getType()));
      }
    }

    BB->eraseFromParent();
  }

  return true;
}