Skip to content

Commit d3884d8

Browse files
authored
Move SPIRVLowerConstExprPass to header file (#1373)
So that it could be used in downstream.
1 parent 7452422 commit d3884d8

File tree

2 files changed

+55
-27
lines changed

2 files changed

+55
-27
lines changed

lib/SPIRV/SPIRVLowerConstExpr.cpp

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
//===----------------------------------------------------------------------===//
3838
#define DEBUG_TYPE "spv-lower-const-expr"
3939

40+
#include "SPIRVLowerConstExpr.h"
4041
#include "OCLUtil.h"
4142
#include "SPIRVInternal.h"
4243
#include "SPIRVMDBuilder.h"
@@ -65,29 +66,6 @@ cl::opt<bool> SPIRVLowerConst(
6566
"spirv-lower-const-expr", cl::init(true),
6667
cl::desc("LLVM/SPIR-V translation enable lowering constant expression"));
6768

68-
class SPIRVLowerConstExprBase {
69-
public:
70-
SPIRVLowerConstExprBase() : M(nullptr), Ctx(nullptr) {}
71-
72-
bool runLowerConstExpr(Module &M);
73-
void visit(Module *M);
74-
75-
private:
76-
Module *M;
77-
LLVMContext *Ctx;
78-
};
79-
80-
class SPIRVLowerConstExprPass
81-
: public llvm::PassInfoMixin<SPIRVLowerConstExprPass>,
82-
public SPIRVLowerConstExprBase {
83-
public:
84-
llvm::PreservedAnalyses run(llvm::Module &M,
85-
llvm::ModuleAnalysisManager &MAM) {
86-
return runLowerConstExpr(M) ? llvm::PreservedAnalyses::none()
87-
: llvm::PreservedAnalyses::all();
88-
}
89-
};
90-
9169
class SPIRVLowerConstExprLegacy : public ModulePass,
9270
public SPIRVLowerConstExprBase {
9371
public:
@@ -110,11 +88,11 @@ bool SPIRVLowerConstExprBase::runLowerConstExpr(Module &Module) {
11088
Ctx = &M->getContext();
11189

11290
LLVM_DEBUG(dbgs() << "Enter SPIRVLowerConstExpr:\n");
113-
visit(M);
91+
bool Changed = visit(M);
11492

11593
verifyRegularizationPass(*M, "SPIRVLowerConstExpr");
11694

117-
return true;
95+
return Changed;
11896
}
11997

12098
/// Since SPIR-V cannot represent constant expression, constant expressions
@@ -126,7 +104,8 @@ bool SPIRVLowerConstExprBase::runLowerConstExpr(Module &Module) {
126104
/// is replaced by one instruction.
127105
/// ToDo: remove redundant instructions for common subexpression
128106

129-
void SPIRVLowerConstExprBase::visit(Module *M) {
107+
bool SPIRVLowerConstExprBase::visit(Module *M) {
108+
bool Changed = false;
130109
for (auto &I : M->functions()) {
131110
std::list<Instruction *> WorkList;
132111
for (auto &BI : I) {
@@ -138,7 +117,7 @@ void SPIRVLowerConstExprBase::visit(Module *M) {
138117
while (!WorkList.empty()) {
139118
auto II = WorkList.front();
140119

141-
auto LowerOp = [&II, &FBegin, &I](Value *V) -> Value * {
120+
auto LowerOp = [&II, &FBegin, &I, &Changed](Value *V) -> Value * {
142121
if (isa<Function>(V))
143122
return V;
144123
auto *CE = cast<ConstantExpr>(V);
@@ -163,6 +142,7 @@ void SPIRVLowerConstExprBase::visit(Module *M) {
163142
ReplInst->moveBefore(User);
164143
User->replaceUsesOfWith(CE, ReplInst);
165144
}
145+
Changed = true;
166146
return ReplInst;
167147
};
168148

@@ -190,6 +170,7 @@ void SPIRVLowerConstExprBase::visit(Module *M) {
190170
}
191171
}
192172
}
173+
return Changed;
193174
}
194175

195176
} // namespace SPIRV

lib/SPIRV/SPIRVLowerConstExpr.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===- SPIRVLowerConstExpr.h - Lower constant expression --------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
/// \file SPIRVLowerConstExpr.h
10+
///
11+
/// This file declares SPIRVLowerConstExprPass that lowers constant expression.
12+
///
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef SPIRV_LOWERCONSTEXPR_H
16+
#define SPIRV_LOWERCONSTEXPR_H
17+
18+
#include "llvm/IR/PassManager.h"
19+
20+
namespace SPIRV {
21+
22+
class SPIRVLowerConstExprBase {
23+
public:
24+
SPIRVLowerConstExprBase() : M(nullptr), Ctx(nullptr) {}
25+
26+
bool runLowerConstExpr(llvm::Module &M);
27+
bool visit(llvm::Module *M);
28+
29+
private:
30+
llvm::Module *M;
31+
llvm::LLVMContext *Ctx;
32+
};
33+
34+
class SPIRVLowerConstExprPass
35+
: public llvm::PassInfoMixin<SPIRVLowerConstExprPass>,
36+
public SPIRVLowerConstExprBase {
37+
public:
38+
llvm::PreservedAnalyses run(llvm::Module &M,
39+
llvm::ModuleAnalysisManager &MAM) {
40+
return runLowerConstExpr(M) ? llvm::PreservedAnalyses::none()
41+
: llvm::PreservedAnalyses::all();
42+
}
43+
};
44+
45+
} // namespace SPIRV
46+
47+
#endif // SPIRV_LOWERCONSTEXPR_H

0 commit comments

Comments
 (0)