Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Commit 54451c3

Browse files
committed
Run folding pass first without global demotion
1 parent f15c070 commit 54451c3

File tree

7 files changed

+54
-16
lines changed

7 files changed

+54
-16
lines changed

compiler/sir/analyze/module/side_effect.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,19 @@ struct SideEfectAnalyzer : public util::ConstVisitor {
2626
static const std::string NON_PURE_ATTR;
2727

2828
VarUseAnalyzer &vua;
29+
bool globalAssignmentHasSideEffects;
2930
std::unordered_map<id_t, bool> result;
3031
bool exprSE;
3132
bool funcSE;
3233

33-
SideEfectAnalyzer(VarUseAnalyzer &vua)
34-
: util::ConstVisitor(), vua(vua), result(), exprSE(true), funcSE(false) {}
34+
// We have to sometimes be careful with globals since future
35+
// IR passes might introduce globals that we've eliminated
36+
// or demoted earlier. Hence the distinction with whether
37+
// global assignments are considered to have side effects.
38+
SideEfectAnalyzer(VarUseAnalyzer &vua, bool globalAssignmentHasSideEffects)
39+
: util::ConstVisitor(), vua(vua),
40+
globalAssignmentHasSideEffects(globalAssignmentHasSideEffects), result(),
41+
exprSE(true), funcSE(false) {}
3542

3643
template <typename T> bool has(const T *v) {
3744
return result.find(v->getId()) != result.end();
@@ -174,8 +181,13 @@ struct SideEfectAnalyzer : public util::ConstVisitor {
174181
auto count1 = (it1 != vua.varCounts.end()) ? it1->second : 0;
175182
auto count2 = (it2 != vua.varAssignCounts.end()) ? it2->second : 0;
176183

184+
bool g = v->getLhs()->isGlobal();
177185
bool s = (count1 != count2);
178-
set(v, s | process(v->getRhs()), s & v->getLhs()->isGlobal());
186+
if (globalAssignmentHasSideEffects) {
187+
set(v, s | g | process(v->getRhs()), g);
188+
} else {
189+
set(v, s | process(v->getRhs()), s & g);
190+
}
179191
}
180192

181193
void visit(const ExtractInstr *v) override { set(v, process(v->getVal())); }
@@ -257,7 +269,7 @@ bool SideEffectResult::hasSideEffect(Value *v) const {
257269
std::unique_ptr<Result> SideEffectAnalysis::run(const Module *m) {
258270
VarUseAnalyzer vua;
259271
const_cast<Module *>(m)->accept(vua);
260-
SideEfectAnalyzer sea(vua);
272+
SideEfectAnalyzer sea(vua, globalAssignmentHasSideEffects);
261273
m->accept(sea);
262274
return std::make_unique<SideEffectResult>(sea.result);
263275
}

compiler/sir/analyze/module/side_effect.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,19 @@ struct SideEffectResult : public Result {
2121
};
2222

2323
class SideEffectAnalysis : public Analysis {
24+
private:
25+
/// true if assigning to a global variable automatically has side effects
26+
bool globalAssignmentHasSideEffects;
27+
28+
public:
2429
static const std::string KEY;
30+
31+
/// Constructs a side effect analysis.
32+
/// @param globalAssignmentHasSideEffects true if global variable assignment
33+
/// automatically has side effects
34+
explicit SideEffectAnalysis(bool globalAssignmentHasSideEffects = true)
35+
: Analysis(), globalAssignmentHasSideEffects(globalAssignmentHasSideEffects){};
36+
2537
std::string getKey() const override { return KEY; }
2638

2739
std::unique_ptr<Result> run(const Module *m) override;

compiler/sir/llvm/llvisitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ void LLVMVisitor::visit(const Module *x) {
728728
} else {
729729
auto *storage = new llvm::GlobalVariable(
730730
*module, llvmType, /*isConstant=*/false,
731-
llvm::GlobalVariable::InternalLinkage,
731+
llvm::GlobalVariable::PrivateLinkage,
732732
llvm::Constant::getNullValue(llvmType), var->getName());
733733
vars.insert(var, storage);
734734

compiler/sir/transform/folding/folding.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const std::string FoldingPassGroup::KEY = "core-folding-pass-group";
1212

1313
FoldingPassGroup::FoldingPassGroup(const std::string &sideEffectsPass,
1414
const std::string &reachingDefPass,
15-
const std::string &globalVarPass) {
15+
const std::string &globalVarPass,
16+
bool runGlobalDemotion) {
1617
auto gdUnique = std::make_unique<cleanup::GlobalDemotionPass>();
1718
auto canonUnique = std::make_unique<cleanup::CanonicalizationPass>(sideEffectsPass);
1819
auto fpUnique = std::make_unique<FoldingPass>();
@@ -23,7 +24,8 @@ FoldingPassGroup::FoldingPassGroup(const std::string &sideEffectsPass,
2324
fp = fpUnique.get();
2425
dce = dceUnique.get();
2526

26-
push_back(std::move(gdUnique));
27+
if (runGlobalDemotion)
28+
push_back(std::move(gdUnique));
2729
push_back(std::make_unique<ConstPropPass>(reachingDefPass, globalVarPass));
2830
push_back(std::move(canonUnique));
2931
push_back(std::move(fpUnique));

compiler/sir/transform/folding/folding.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class FoldingPassGroup : public PassGroup {
2929
/// @param reachingDefPass the key of the reaching definitions pass
3030
/// @param globalVarPass the key of the global variables pass
3131
FoldingPassGroup(const std::string &sideEffectsPass,
32-
const std::string &reachingDefPass,
33-
const std::string &globalVarPass);
32+
const std::string &reachingDefPass, const std::string &globalVarPass,
33+
bool runGlobalDemotion = true);
3434

3535
bool shouldRepeat() const override;
3636
};

compiler/sir/transform/manager.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ void PassManager::invalidate(const std::string &key) {
145145
void PassManager::registerStandardPasses(bool debug) {
146146
if (debug) {
147147
registerPass(std::make_unique<lowering::PipelineLowering>());
148+
registerPass(std::make_unique<lowering::ImperativeForFlowLowering>());
148149
registerPass(std::make_unique<parallel::OpenMPPass>());
149150
} else {
150151
// Pythonic
@@ -157,19 +158,29 @@ void PassManager::registerStandardPasses(bool debug) {
157158
registerPass(std::make_unique<lowering::ImperativeForFlowLowering>());
158159

159160
// folding
160-
auto seKey =
161-
registerAnalysis(std::make_unique<analyze::module::SideEffectAnalysis>());
161+
auto seKey1 =
162+
registerAnalysis(std::make_unique<analyze::module::SideEffectAnalysis>(
163+
/*globalAssignmentHasSideEffects=*/true));
164+
auto seKey2 =
165+
registerAnalysis(std::make_unique<analyze::module::SideEffectAnalysis>(
166+
/*globalAssignmentHasSideEffects=*/false));
162167
auto cfgKey = registerAnalysis(std::make_unique<analyze::dataflow::CFAnalysis>());
163168
auto rdKey = registerAnalysis(
164169
std::make_unique<analyze::dataflow::RDAnalysis>(cfgKey), {cfgKey});
165170
auto globalKey =
166171
registerAnalysis(std::make_unique<analyze::module::GlobalVarsAnalyses>());
167-
registerPass(std::make_unique<folding::FoldingPassGroup>(seKey, rdKey, globalKey),
168-
/*insertBefore=*/"", {seKey, rdKey, globalKey},
169-
{seKey, rdKey, cfgKey, globalKey});
172+
registerPass(std::make_unique<folding::FoldingPassGroup>(
173+
seKey1, rdKey, globalKey, /*runGlobalDemoton=*/false),
174+
/*insertBefore=*/"", {seKey1, rdKey, globalKey},
175+
{seKey1, rdKey, cfgKey, globalKey});
170176

171177
// parallel
172178
registerPass(std::make_unique<parallel::OpenMPPass>());
179+
180+
registerPass(std::make_unique<folding::FoldingPassGroup>(seKey2, rdKey, globalKey,
181+
/*runGlobalDemoton=*/true),
182+
/*insertBefore=*/"", {seKey2, rdKey, globalKey},
183+
{seKey2, rdKey, cfgKey, globalKey});
173184
}
174185
}
175186

stdlib/internal/dlopen.seq

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from C import seq_is_macos() -> bool
1+
@pure
2+
@C
3+
def seq_is_macos() -> bool: pass
24

35
# <dlfcn.h>
46
from C import dlerror() -> cobj as c_dlerror
@@ -8,7 +10,6 @@ from C import dlclose(cobj) -> i32 as c_dlclose
810
RTLD_NOW = 2
911
RTLD_GLOBAL = 8 if seq_is_macos() else 256
1012

11-
@pure
1213
def dlext():
1314
if seq_is_macos():
1415
return 'dylib'

0 commit comments

Comments
 (0)