Skip to content

Commit bae47b7

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 9aabce6 commit bae47b7

File tree

6 files changed

+143
-5
lines changed

6 files changed

+143
-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/ADCE.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::createLegacyWrappedADCEPass());
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::createLegacyWrappedADCEPass());
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::createLegacyWrappedADCEPass());
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::createLegacyWrappedADCEPass());
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/ADCE.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/ADCE.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 *createLegacyWrappedADCEPass();
34+
35+
} // end namespace IGCLLVM
36+
37+
#endif // IGCLLVM_TRANSFORMS_SCALAR_LEGACY_ADCE_H
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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/ADCE.h"
10+
11+
#include "llvm/Transforms/Scalar.h"
12+
#include "llvm/Transforms/Scalar/ADCE.h"
13+
#include "llvm/Analysis/GlobalsModRef.h"
14+
#include "llvm/Analysis/PostDominators.h"
15+
#include "llvm/IR/Function.h"
16+
#include "llvm/IR/PassManager.h"
17+
#include "llvm/IR/Value.h"
18+
#include "llvm/Pass.h"
19+
#include "llvm/Passes/PassBuilder.h"
20+
21+
#include "llvmWrapper/Transforms/InitializePasses.h"
22+
#include "Compiler/IGCPassSupport.h"
23+
24+
using namespace llvm;
25+
26+
namespace IGCLLVM {
27+
28+
ADCELegacyPassWrapper::ADCELegacyPassWrapper() : FunctionPass(ID) {
29+
initializeADCELegacyPassPass(*PassRegistry::getPassRegistry());
30+
PB.registerFunctionAnalyses(FAM);
31+
}
32+
33+
bool ADCELegacyPassWrapper::runOnFunction(Function &F) {
34+
// The legacy pass manager implementation of the pass used to skip some functions. In the new pass manager
35+
// implementation this is done globally through the pass manager. Check and skip explicitly here to preserve the old
36+
// behavior.
37+
if (skipFunction(F))
38+
return false;
39+
40+
// Run the New Pass Manager implementation of the pass. Note, there is no need to inject any analyses for
41+
// PostDominatorTree as ADCE does not actually use it but only "requires" it to mark it as preserved.
42+
ADCEPass Implementation;
43+
Implementation.run(F, FAM);
44+
return true;
45+
}
46+
47+
void ADCELegacyPassWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
48+
AU.addRequired<PostDominatorTreeWrapperPass>();
49+
AU.addPreserved<DominatorTreeWrapperPass>();
50+
AU.addPreserved<PostDominatorTreeWrapperPass>();
51+
AU.addPreserved<GlobalsAAWrapperPass>();
52+
}
53+
54+
char ADCELegacyPassWrapper::ID = 0;
55+
FunctionPass *createLegacyWrappedADCEPass() {
56+
// For LLVM versions older than 16, use the legacy ADCE pass implementation. The new pass manager implementation has a
57+
// bit different handling of CFG analyses preservation.
58+
#if LLVM_VERSION_MAJOR < 16
59+
return llvm::createAggressiveDCEPass();
60+
#endif
61+
62+
return new ADCELegacyPassWrapper();
63+
}
64+
65+
} // namespace IGCLLVM
66+
67+
using namespace IGCLLVM;
68+
#define PASS_FLAG "adce-legacy-wrapped"
69+
#define PASS_DESCRIPTION "Aggressive Dead Code Elimination LPM Wrapped"
70+
#define PASS_CFG_ONLY false
71+
#define PASS_ANALYSIS false
72+
IGC_INITIALIZE_PASS_BEGIN(ADCELegacyPassWrapper, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
73+
IGC_INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
74+
IGC_INITIALIZE_PASS_END(ADCELegacyPassWrapper, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

0 commit comments

Comments
 (0)