Skip to content

Commit 8ca9e82

Browse files
wenju-heigcbot
authored andcommitted
Purge MetaDataUtils at the end of SubroutineInliner pass
SubroutineInliner removes Inlined functions from module. The functions should be removed from MetaDataUtils and ModuleMetaData as well. This PR re-use purgeMetaDataUtils to update them.
1 parent 21f7225 commit 8ca9e82

File tree

4 files changed

+83
-26
lines changed

4 files changed

+83
-26
lines changed

IGC/Compiler/CISACodeGen/GenCodeGenModule.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ SPDX-License-Identifier: MIT
1313
#include "Compiler/DebugInfo/ScalarVISAModule.h"
1414
#include "Compiler/CodeGenContextWrapper.hpp"
1515
#include "Compiler/IGCPassSupport.h"
16+
#include "Compiler/MetaDataApi/PurgeMetaDataUtils.hpp"
1617
#include "Compiler/MetaDataUtilsWrapper.h"
1718
#include "common/igc_regkeys.hpp"
1819
#include "common/LLVMWarningsPush.hpp"
@@ -1259,6 +1260,7 @@ namespace {
12591260
class SubroutineInliner : public LegacyInlinerBase, public llvm::InstVisitor<SubroutineInliner>
12601261
{
12611262
EstimateFunctionSize* FSA;
1263+
MetaDataUtilsWrapper* MDUW;
12621264
public:
12631265
static char ID; // Pass identification, replacement for typeid
12641266

@@ -1277,7 +1279,9 @@ namespace {
12771279

12781280
using llvm::Pass::doFinalization;
12791281
bool doFinalization(CallGraph& CG) override {
1280-
return removeDeadFunctions(CG);
1282+
bool Changed = removeDeadFunctions(CG);
1283+
Changed |= purgeMetaDataUtils(CG.getModule(), MDUW);
1284+
return Changed;
12811285
}
12821286
};
12831287

@@ -1286,6 +1290,7 @@ namespace {
12861290
IGC_INITIALIZE_PASS_BEGIN(SubroutineInliner, "SubroutineInliner", "SubroutineInliner", false, false)
12871291
IGC_INITIALIZE_PASS_DEPENDENCY(EstimateFunctionSize)
12881292
IGC_INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1293+
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
12891294
IGC_INITIALIZE_PASS_END(SubroutineInliner, "SubroutineInliner", "SubroutineInliner", false, false)
12901295

12911296
char SubroutineInliner::ID = 0;
@@ -1294,6 +1299,7 @@ void SubroutineInliner::getAnalysisUsage(AnalysisUsage& AU) const
12941299
{
12951300
AU.addRequired<EstimateFunctionSize>();
12961301
AU.addRequired<CodeGenContextWrapper>();
1302+
AU.addRequired<MetaDataUtilsWrapper>();
12971303
LegacyInlinerBase::getAnalysisUsage(AU);
12981304
}
12991305

@@ -1374,6 +1380,7 @@ void SubroutineInliner::verifyAddrSpaceMismatch(CallGraphSCC& SCC)
13741380
bool SubroutineInliner::runOnSCC(CallGraphSCC& SCC)
13751381
{
13761382
FSA = &getAnalysis<EstimateFunctionSize>();
1383+
MDUW = &getAnalysis<MetaDataUtilsWrapper>();
13771384
bool changed = LegacyInlinerBase::runOnSCC(SCC);
13781385
if (changed) verifyAddrSpaceMismatch(SCC);
13791386

IGC/Compiler/MetaDataApi/PurgeMetaDataUtils.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,17 @@ PurgeMetaDataUtils::PurgeMetaDataUtils() : ModulePass(ID)
3636
// metadata util object.
3737
bool PurgeMetaDataUtils::runOnModule(Module& M)
3838
{
39-
m_pModule = &M;
40-
m_changed = false;
41-
42-
CodeGenContext* pContext = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
43-
IGCMD::MetaDataUtils* pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
39+
return purgeMetaDataUtils(M, &getAnalysis<MetaDataUtilsWrapper>());
40+
}
4441

45-
auto shouldRemoveFunction = [=](llvm::Module* M, void* ptr)
42+
bool IGC::purgeMetaDataUtils(Module& M, MetaDataUtilsWrapper* MDUW) {
43+
IGCMD::MetaDataUtils* MDUtils = MDUW->getMetaDataUtils();
44+
auto shouldRemoveFunction = [=](llvm::Module& M, void* ptr)
4645
{
4746
llvm::Function* F = nullptr;
4847

4948
// Function may have been removed already.
50-
for (auto& G : M->getFunctionList())
49+
for (auto& G : M.getFunctionList())
5150
{
5251
if (&G == ptr)
5352
{
@@ -62,7 +61,7 @@ bool PurgeMetaDataUtils::runOnModule(Module& M)
6261
return true;
6362
}
6463

65-
if (F->use_empty() && !isEntryFunc(pMdUtils, F))
64+
if (F->use_empty() && !isEntryFunc(MDUtils, F))
6665
{
6766
if (F->hasFnAttribute("referenced-indirectly") &&
6867
GlobalValue::isExternalLinkage(F->getLinkage()))
@@ -96,24 +95,24 @@ bool PurgeMetaDataUtils::runOnModule(Module& M)
9695
auto checkFuncRange = [&](auto beginIt, auto endIt) {
9796
for (auto it = beginIt, e = endIt; it != e; ++it)
9897
{
99-
if (shouldRemoveFunction(pContext->getModule(), it->first))
98+
if (shouldRemoveFunction(M, it->first))
10099
{
101100
ToBeDeleted.insert(it->first);
102101
}
103102
}
104103
};
105104

106-
auto& FuncMD = pContext->getModuleMetaData()->FuncMD;
107-
checkFuncRange(pMdUtils->begin_FunctionsInfo(), pMdUtils->end_FunctionsInfo());
105+
auto& FuncMD = MDUW->getModuleMetaData()->FuncMD;
106+
checkFuncRange(MDUtils->begin_FunctionsInfo(), MDUtils->end_FunctionsInfo());
108107
checkFuncRange(FuncMD.begin(), FuncMD.end());
109108

110109
// Remove them.
111110
for (auto F : ToBeDeleted)
112111
{
113-
auto Iter = pMdUtils->findFunctionsInfoItem(F);
114-
if (Iter != pMdUtils->end_FunctionsInfo())
112+
auto Iter = MDUtils->findFunctionsInfoItem(F);
113+
if (Iter != MDUtils->end_FunctionsInfo())
115114
{
116-
pMdUtils->eraseFunctionsInfoItem(Iter);
115+
MDUtils->eraseFunctionsInfoItem(Iter);
117116
}
118117

119118
if (FuncMD.find(F) != FuncMD.end())
@@ -125,9 +124,8 @@ bool PurgeMetaDataUtils::runOnModule(Module& M)
125124
// Update when there is any change.
126125
if (!ToBeDeleted.empty())
127126
{
128-
pMdUtils->save(*pContext->getLLVMContext());
129-
m_changed = true;
127+
MDUtils->save(M.getContext());
130128
}
131129

132-
return m_changed;
130+
return !ToBeDeleted.empty();
133131
}

IGC/Compiler/MetaDataApi/PurgeMetaDataUtils.hpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ SPDX-License-Identifier: MIT
1212
#include <llvm/Pass.h>
1313
#include "common/LLVMWarningsPop.hpp"
1414
#include "Compiler/MetaDataUtilsWrapper.h"
15-
#include "Compiler/CodeGenContextWrapper.hpp"
1615

1716
namespace IGC
1817
{
19-
class MetaDataUtilsWrapper;
2018
class PurgeMetaDataUtils : public llvm::ModulePass
2119
{
2220
public:
@@ -33,16 +31,11 @@ namespace IGC
3331

3432
virtual void getAnalysisUsage(llvm::AnalysisUsage& AU) const override {
3533
AU.addRequired<MetaDataUtilsWrapper>();
36-
AU.addRequired<CodeGenContextWrapper>();
3734
}
3835

3936
// Pass identification, replacement for typeid
4037
static char ID;
41-
protected:
42-
llvm::Module* m_pModule = nullptr;
43-
44-
/// @brief Indicates if the pass changed the processed function
45-
bool m_changed{};
4638
};
4739

40+
bool purgeMetaDataUtils(llvm::Module& M, MetaDataUtilsWrapper* MDUW);
4841
} // namespace IGC
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2024 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
9+
; Check inlined __kmpc_barrier is removed from igc.functions and FuncMD metadata.
10+
11+
; RUN: igc_opt -SubroutineInliner -S < %s 2>&1 | FileCheck %s
12+
13+
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32"
14+
target triple = "spir64-unknown-unknown"
15+
16+
define spir_kernel void @__omp_offloading_MAIN(i8 addrspace(2)* %constBase, i8 addrspace(1)* %globalBase) {
17+
DIR.OMP.END.LOOP.4:
18+
call spir_func void @__kmpc_barrier(i8 addrspace(2)* %constBase, i8 addrspace(1)* %globalBase)
19+
ret void
20+
}
21+
22+
define internal spir_func void @__kmpc_barrier(i8 addrspace(2)* nocapture readnone %constBase, i8 addrspace(1)* nocapture readnone %globalBase) {
23+
entry:
24+
ret void
25+
}
26+
27+
; CHECK: !igc.functions = !{[[F:![0-9]+]]}
28+
; CHECK: [[F]] = !{{{.*}} @__omp_offloading_MAIN
29+
; CHECK-NOT: = !{{{.*}} @__kmpc_barrier
30+
; CHECK: !{!"FuncMDMap[0]", {{.*}} @__omp_offloading_MAIN}
31+
; CHECK: !{!"FuncMDMap[1]", null}
32+
33+
!spirv.MemoryModel = !{!0}
34+
!spirv.Source = !{!1}
35+
!spirv.Generator = !{!2}
36+
!igc.functions = !{!3, !9}
37+
!IGCMetadata = !{!12}
38+
!opencl.ocl.version = !{!18}
39+
!opencl.spir.version = !{!18}
40+
41+
!0 = !{i32 2, i32 2}
42+
!1 = !{i32 4, i32 200000}
43+
!2 = !{i16 6, i16 14}
44+
!3 = !{void (i8 addrspace(2)*, i8 addrspace(1)*)* @__omp_offloading_MAIN, !4}
45+
!4 = !{!5, !6}
46+
!5 = !{!"function_type", i32 0}
47+
!6 = !{!"implicit_arg_desc", !7, !8}
48+
!7 = !{i32 10}
49+
!8 = !{i32 11}
50+
!9 = !{void (i8 addrspace(2)*, i8 addrspace(1)*)* @__kmpc_barrier, !10}
51+
!10 = !{!11, !6}
52+
!11 = !{!"function_type", i32 2}
53+
!12 = !{!"ModuleMD", !13}
54+
!13 = !{!"FuncMD", !14, !15, !16, !17}
55+
!14 = !{!"FuncMDMap[0]", void (i8 addrspace(2)*, i8 addrspace(1)*)* @__omp_offloading_MAIN}
56+
!15 = !{!"FuncMDValue[0]"}
57+
!16 = !{!"FuncMDMap[1]", void (i8 addrspace(2)*, i8 addrspace(1)*)* @__kmpc_barrier}
58+
!17 = !{!"FuncMDValue[1]"}
59+
!18 = !{i32 2, i32 0}

0 commit comments

Comments
 (0)