Skip to content

Commit 10b1caf

Browse files
[SPIRV][OPT] Adding flag to run spirv structurizer (#119301)
This PR adds a new flag into OPT to run SPIRV structurizer, this is being added improving testing of such pass. This change is required to implement a test request that come #116331. --------- Co-authored-by: Joao Saffran <[email protected]>
1 parent 0d59fc2 commit 10b1caf

File tree

6 files changed

+77
-4
lines changed

6 files changed

+77
-4
lines changed

llvm/lib/Target/SPIRV/SPIRV.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void initializeSPIRVModuleAnalysisPass(PassRegistry &);
3737
void initializeSPIRVConvergenceRegionAnalysisWrapperPassPass(PassRegistry &);
3838
void initializeSPIRVPreLegalizerPass(PassRegistry &);
3939
void initializeSPIRVPostLegalizerPass(PassRegistry &);
40+
void initializeSPIRVStructurizerPass(PassRegistry &);
4041
void initializeSPIRVEmitIntrinsicsPass(PassRegistry &);
4142
void initializeSPIRVEmitNonSemanticDIPass(PassRegistry &);
4243
} // namespace llvm
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===- SPIRVPassRegistry.def - Registry of SPIRV passes -----*- C++--*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file is used as the registry of passes that are part of the
10+
// SPIRV backend.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
// NOTE: NO INCLUDE GUARD DESIRED!
15+
16+
17+
#ifndef FUNCTION_PASS
18+
#define FUNCTION_PASS(NAME, CREATE_PASS)
19+
#endif
20+
FUNCTION_PASS("spirv-structurizer", SPIRVStructurizerWrapper())
21+
#undef FUNCTION_PASS

llvm/lib/Target/SPIRV/SPIRVStructurizer.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010

1111
#include "Analysis/SPIRVConvergenceRegionAnalysis.h"
1212
#include "SPIRV.h"
13+
#include "SPIRVStructurizerWrapper.h"
1314
#include "SPIRVSubtarget.h"
1415
#include "SPIRVTargetMachine.h"
1516
#include "SPIRVUtils.h"
1617
#include "llvm/ADT/DenseMap.h"
1718
#include "llvm/ADT/SmallPtrSet.h"
1819
#include "llvm/Analysis/LoopInfo.h"
1920
#include "llvm/CodeGen/IntrinsicLowering.h"
21+
#include "llvm/IR/Analysis.h"
2022
#include "llvm/IR/CFG.h"
2123
#include "llvm/IR/Dominators.h"
2224
#include "llvm/IR/IRBuilder.h"
@@ -1211,16 +1213,26 @@ class SPIRVStructurizer : public FunctionPass {
12111213

12121214
char SPIRVStructurizer::ID = 0;
12131215

1214-
INITIALIZE_PASS_BEGIN(SPIRVStructurizer, "structurizer", "structurize SPIRV",
1215-
false, false)
1216+
INITIALIZE_PASS_BEGIN(SPIRVStructurizer, "spirv-structurizer",
1217+
"structurize SPIRV", false, false)
12161218
INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
12171219
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
12181220
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
12191221
INITIALIZE_PASS_DEPENDENCY(SPIRVConvergenceRegionAnalysisWrapperPass)
12201222

1221-
INITIALIZE_PASS_END(SPIRVStructurizer, "structurize", "structurize SPIRV",
1222-
false, false)
1223+
INITIALIZE_PASS_END(SPIRVStructurizer, "spirv-structurizer",
1224+
"structurize SPIRV", false, false)
12231225

12241226
FunctionPass *llvm::createSPIRVStructurizerPass() {
12251227
return new SPIRVStructurizer();
12261228
}
1229+
1230+
PreservedAnalyses SPIRVStructurizerWrapper::run(Function &F,
1231+
FunctionAnalysisManager &AF) {
1232+
FunctionPass *StructurizerPass = createSPIRVStructurizerPass();
1233+
if (!StructurizerPass->runOnFunction(F))
1234+
return PreservedAnalyses::all();
1235+
PreservedAnalyses PA;
1236+
PA.preserveSet<CFGAnalyses>();
1237+
return PA;
1238+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===- SPIRVStructurizerWrapper.h - New pass manager wrapper from SPIRV
2+
// Structurizer -----------*- C++ -*-===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// \file New pass manager wrapper from SPIRV Structurizer.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_LIB_TARGET_DIRECTX_SPIRVSTRUCTURIZER_H
15+
#define LLVM_LIB_TARGET_DIRECTX_SPIRVSTRUCTURIZER_H
16+
17+
#include "llvm/IR/PassManager.h"
18+
19+
namespace llvm {
20+
21+
class SPIRVStructurizerWrapper
22+
: public PassInfoMixin<SPIRVStructurizerWrapper> {
23+
public:
24+
PreservedAnalyses run(Function &M, FunctionAnalysisManager &AM);
25+
};
26+
27+
} // namespace llvm
28+
29+
#endif // LLVM_LIB_TARGET_DIRECTX_SPIRVSTRUCTURIZER_H

llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "SPIRVCallLowering.h"
1616
#include "SPIRVGlobalRegistry.h"
1717
#include "SPIRVLegalizerInfo.h"
18+
#include "SPIRVStructurizerWrapper.h"
1819
#include "SPIRVTargetObjectFile.h"
1920
#include "SPIRVTargetTransformInfo.h"
2021
#include "TargetInfo/SPIRVTargetInfo.h"
@@ -28,6 +29,7 @@
2829
#include "llvm/InitializePasses.h"
2930
#include "llvm/MC/TargetRegistry.h"
3031
#include "llvm/Pass.h"
32+
#include "llvm/Passes/PassBuilder.h"
3133
#include "llvm/Target/TargetOptions.h"
3234
#include "llvm/Transforms/Scalar/Reg2Mem.h"
3335
#include "llvm/Transforms/Utils.h"
@@ -45,6 +47,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTarget() {
4547
initializeGlobalISel(PR);
4648
initializeSPIRVModuleAnalysisPass(PR);
4749
initializeSPIRVConvergenceRegionAnalysisWrapperPassPass(PR);
50+
initializeSPIRVStructurizerPass(PR);
4851
}
4952

5053
static std::string computeDataLayout(const Triple &TT) {
@@ -92,6 +95,11 @@ SPIRVTargetMachine::SPIRVTargetMachine(const Target &T, const Triple &TT,
9295
setRequiresStructuredCFG(false);
9396
}
9497

98+
void SPIRVTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
99+
#define GET_PASS_REGISTRY "SPIRVPassRegistry.def"
100+
#include "llvm/Passes/TargetPassRegistry.inc"
101+
}
102+
95103
namespace {
96104
// SPIR-V Code Generator Pass Configuration Options.
97105
class SPIRVPassConfig : public TargetPassConfig {

llvm/lib/Target/SPIRV/SPIRVTargetMachine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class SPIRVTargetMachine : public CodeGenTargetMachineImpl {
4343
TargetLoweringObjectFile *getObjFileLowering() const override {
4444
return TLOF.get();
4545
}
46+
47+
void registerPassBuilderCallbacks(PassBuilder &PB) override;
4648
};
4749
} // namespace llvm
4850

0 commit comments

Comments
 (0)