Skip to content

Commit d36ba0a

Browse files
michalpaszkowskiigcbot
authored andcommitted
Legacy Pass Manager wrapper for the ADCE pass
This change introduces an initial wrapper for the ADCE pass. The wrapper allows the New Pass Manager's implementation of the ADCE pass to be executed using the Legacy Pass Manager infrastructure.
1 parent b3e92c3 commit d36ba0a

File tree

6 files changed

+134
-5
lines changed

6 files changed

+134
-5
lines changed

IGC/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,7 @@ set(IGC_BUILD__SRC__IGC__igc_common
16601660
set(IGC_BUILD__SRC__IGC__igc_dll
16611661
${IGC_BUILD__SRC__IGC__igc_common}
16621662
${IGC_BUILD__SRC__IGC_DriverInterface__igc_dll}
1663+
${IGC_WrapperLLVM_SRC}
16631664
)
16641665

16651666

IGC/Compiler/CISACodeGen/ShaderCodeGen.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ SPDX-License-Identifier: MIT
156156
#include <llvm/Transforms/Utils.h>
157157
#include <llvm/Transforms/Scalar.h>
158158
#include <llvm/Bitcode/BitcodeWriter.h>
159+
#include "llvmWrapper/Transforms/Scalar/LegacyADCE.h"
159160
#include "common/LLVMWarningsPop.hpp"
160161
#include "Compiler/CISACodeGen/PatternMatchPass.hpp"
161162
#include "Compiler/CISACodeGen/EmitVISAPass.hpp"
@@ -557,7 +558,7 @@ void AddLegalizationPasses(CodeGenContext &ctx, IGCPassManager &mpm, PSSignature
557558
// DCE doesn't remove dead control flow; ADCE does (currently)
558559
// otherwise you'd have to call createCFGSimplificationPass and DCE
559560
// iteratively e.g..
560-
mpm.add(llvm::createAggressiveDCEPass());
561+
mpm.add(IGCLLVM::createLegacyADCEPass());
561562
// TODO: we probably should be running other passes on the result
562563

563564
if (!IGC::ForceAlwaysInline(&ctx)) {
@@ -811,7 +812,7 @@ void AddLegalizationPasses(CodeGenContext &ctx, IGCPassManager &mpm, PSSignature
811812
mpm.add(llvm::createLICMPass(100, 500, true));
812813
mpm.add(llvm::createEarlyCSEPass());
813814
}
814-
mpm.add(createAggressiveDCEPass());
815+
mpm.add(IGCLLVM::createLegacyADCEPass());
815816
// As DPC++ FE apply LICM we cannot reduce register pressure just
816817
// by turning off LICM at IGC in some cases so apply sinking address arithmetic
817818
if ((IGC_IS_FLAG_ENABLED(ForceAddressArithSinking) ||
@@ -1477,7 +1478,7 @@ void OptimizeIR(CodeGenContext *const pContext) {
14771478

14781479
mpm.add(llvm::createDeadCodeEliminationPass());
14791480
if (!extensiveShader(pContext))
1480-
mpm.add(llvm::createAggressiveDCEPass());
1481+
mpm.add(IGCLLVM::createLegacyADCEPass());
14811482

14821483
mpm.add(new BreakConstantExpr());
14831484
mpm.add(new IGCConstProp(IGC_IS_FLAG_ENABLED(EnableSimplifyGEP)));
@@ -1726,7 +1727,7 @@ void OptimizeIR(CodeGenContext *const pContext) {
17261727
}
17271728
if (IGC_IS_FLAG_ENABLED(EnableVectorizer)) {
17281729
mpm.add(new IGCVectorizer());
1729-
mpm.add(llvm::createAggressiveDCEPass());
1730+
mpm.add(IGCLLVM::createLegacyADCEPass());
17301731
if (IGC_IS_FLAG_ENABLED(VectorizerCheckScalarizer))
17311732
mpm.add(createScalarizerPass(SelectiveScalarizer::Auto));
17321733
}

IGC/WrapperLLVM/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ set(IGC_WrapperLLVM_HDR
6868
"${CMAKE_CURRENT_SOURCE_DIR}/include/llvmWrapper/Transforms/Utils/ScalarEvolutionExpander.h"
6969
"${CMAKE_CURRENT_SOURCE_DIR}/include/llvmWrapper/Transforms/Utils/ValueMapper.h"
7070
"${CMAKE_CURRENT_SOURCE_DIR}/include/llvmWrapper/Transforms/Utils/BasicBlockUtils.h"
71-
71+
"${CMAKE_CURRENT_SOURCE_DIR}/include/llvmWrapper/Transforms/InitializePasses.h"
72+
"${CMAKE_CURRENT_SOURCE_DIR}/include/llvmWrapper/Transforms/Scalar/LegacyADCE.h"
7273
"${CMAKE_CURRENT_SOURCE_DIR}/include/lldWrapper/Common/Driver.h"
7374
)
7475

76+
set(IGC_WrapperLLVM_SRC
77+
"${CMAKE_CURRENT_SOURCE_DIR}/lib/llvmWrapper/Transforms/Scalar/LegacyADCE.cpp"
78+
)
79+
7580
include_directories(
7681
"${CMAKE_CURRENT_SOURCE_DIR}/include"
7782
"${CMAKE_CURRENT_SOURCE_DIR}"
@@ -90,6 +95,8 @@ igc_sg_register(
9095
"WrapperLLVM"
9196
FILES
9297
${IGC_WrapperLLVM_HDR}
98+
${IGC_WrapperLLVM_SRC}
9399
)
94100

95101
set(IGC_WrapperLLVM_HDR ${IGC_WrapperLLVM_HDR} PARENT_SCOPE)
102+
set(IGC_WrapperLLVM_SRC ${IGC_WrapperLLVM_SRC} PARENT_SCOPE)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2025 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
#ifndef IGCLLVM_TRANSFORMS_INITIALIZE_PASSES_H
10+
#define IGCLLVM_TRANSFORMS_INITIALIZE_PASSES_H
11+
12+
namespace llvm {
13+
class PassRegistry;
14+
}
15+
16+
void initializeADCELegacyPassWrapperPass(llvm::PassRegistry &);
17+
18+
#endif // IGCLLVM_TRANSFORMS_INITIALIZE_PASSES_H
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2025 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
#ifndef IGCLLVM_TRANSFORMS_SCALAR_LEGACY_ADCE_H
10+
#define IGCLLVM_TRANSFORMS_SCALAR_LEGACY_ADCE_H
11+
12+
#include "llvm/IR/LegacyPassManager.h"
13+
#include "llvm/Pass.h"
14+
#include "llvm/IR/PassManager.h"
15+
#include "llvm/Passes/PassBuilder.h"
16+
17+
using namespace llvm;
18+
19+
namespace IGCLLVM {
20+
21+
struct ADCELegacyPassWrapper : public FunctionPass {
22+
ADCELegacyPassWrapper();
23+
static char ID;
24+
25+
bool runOnFunction(llvm::Function &F);
26+
void getAnalysisUsage(AnalysisUsage &AU) const;
27+
28+
private:
29+
FunctionAnalysisManager FAM;
30+
PassBuilder PB;
31+
};
32+
33+
FunctionPass *createLegacyADCEPass();
34+
35+
} // end namespace IGCLLVM
36+
37+
#endif // IGCLLVM_TRANSFORMS_SCALAR_LEGACY_ADCE_H
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2025 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
#include "llvmWrapper/Transforms/Scalar/LegacyADCE.h"
10+
11+
#include "llvm/Transforms/Scalar/ADCE.h"
12+
#include "llvm/Analysis/GlobalsModRef.h"
13+
#include "llvm/Analysis/PostDominators.h"
14+
#include "llvm/IR/Function.h"
15+
#include "llvm/IR/PassManager.h"
16+
#include "llvm/IR/Value.h"
17+
#include "llvm/Pass.h"
18+
#include "llvm/Passes/PassBuilder.h"
19+
20+
#include "llvmWrapper/Transforms/InitializePasses.h"
21+
#include "Compiler/IGCPassSupport.h"
22+
23+
using namespace llvm;
24+
25+
namespace IGCLLVM {
26+
27+
ADCELegacyPassWrapper::ADCELegacyPassWrapper() : FunctionPass(ID) {
28+
initializeADCELegacyPassPass(*PassRegistry::getPassRegistry());
29+
PB.registerFunctionAnalyses(FAM);
30+
}
31+
32+
bool ADCELegacyPassWrapper::runOnFunction(Function &F) {
33+
// The legacy pass manager implementation of the pass used to skip some functions. In the new pass manager
34+
// implementation this is done globally through the pass manager. Check and skip explicitly here to preserve the old
35+
// behavior.
36+
if (skipFunction(F))
37+
return false;
38+
39+
// Run the New Pass Manager implementation of the pass. Note, there is no need to inject any analyses for
40+
// PostDominatorTree as ADCE does not actually use it but only "requires" it to mark it as preserved.
41+
ADCEPass Implementation;
42+
Implementation.run(F, FAM);
43+
return true;
44+
}
45+
46+
void ADCELegacyPassWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
47+
AU.addRequired<PostDominatorTreeWrapperPass>();
48+
AU.addPreserved<DominatorTreeWrapperPass>();
49+
AU.addPreserved<PostDominatorTreeWrapperPass>();
50+
AU.addPreserved<GlobalsAAWrapperPass>();
51+
}
52+
53+
char ADCELegacyPassWrapper::ID = 0;
54+
FunctionPass *createLegacyADCEPass() { return new ADCELegacyPassWrapper(); }
55+
56+
} // namespace IGCLLVM
57+
58+
using namespace IGCLLVM;
59+
#define PASS_FLAG "adce-legacy-wrapped"
60+
#define PASS_DESCRIPTION "Aggressive Dead Code Elimination LPM Wrapped"
61+
#define PASS_CFG_ONLY false
62+
#define PASS_ANALYSIS false
63+
IGC_INITIALIZE_PASS_BEGIN(ADCELegacyPassWrapper, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
64+
IGC_INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
65+
IGC_INITIALIZE_PASS_END(ADCELegacyPassWrapper, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

0 commit comments

Comments
 (0)