Skip to content

Commit eafae43

Browse files
committed
---
yaml --- r: 327539 b: refs/heads/tensorflow c: 8e11010 h: refs/heads/master i: 327537: e3a530a 327535: 38da48d
1 parent f4bdb70 commit eafae43

File tree

11 files changed

+204
-252
lines changed

11 files changed

+204
-252
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-04-25-a: 22f738a831d43aff2b9c9773bcb65
816816
refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-05-08-a: 7d98cc16689baba5c8a3b90a9329bdcc1a12b4e9
817817
refs/heads/cherr42: a566ad54b073c2c56ac0a705d0a5bed9743135a5
818818
"refs/heads/codable_test_comment_fix": fc8f6824f7f347e1e8db55bff62db385c5728b5a
819-
refs/heads/tensorflow: 0673530b5e6485aefb9c622bfd7a50c39b988d2e
819+
refs/heads/tensorflow: 8e110108d33c8eb24c974d180791c1e57f6d15ed
820820
refs/tags/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-05-11-a: 8126fd7a652e2f70ad6d76505239e34fb2ef3e1a
821821
refs/tags/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-05-12-a: b3fd3dd84df6717f2e2e9df58c6d7e99fed57086
822822
refs/tags/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-05-13-a: 71135119579039dc321c5f65d870050fe36efda2

branches/tensorflow/include/swift/Basic/BlotSetVector.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class BlotSetVector {
6464
iterator end() { return vector.end(); }
6565
const_iterator begin() const { return vector.begin(); }
6666
const_iterator end() const { return vector.end(); }
67+
68+
ArrayRef<Optional<ValueT>> getArray() const { return vector; }
69+
6770
llvm::iterator_range<const_iterator> getRange() const {
6871
return {begin(), end()};
6972
}

branches/tensorflow/include/swift/SIL/SILInstructionWorklist.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,17 @@ class SILInstructionWorklist : SILInstructionWorklistBase {
151151
/// \return whether the instruction was deleted or modified.
152152
bool replaceInstructionWithInstruction(SILInstruction *instruction,
153153
SILInstruction *result
154-
#ifndef NDEBUG
154+
#ifndef NDNEBUG
155155
,
156156
std::string instructionDescription
157157
#endif
158158
) {
159159
if (result == instruction) {
160-
#ifndef NDEBUG
161160
withDebugStream([&](llvm::raw_ostream &stream, StringRef loggingName) {
162161
stream << loggingName << ": Mod = " << instructionDescription << '\n'
163162
<< " "
164163
<< " New = " << *instruction << '\n';
165164
});
166-
#endif
167165

168166
// If the instruction was modified, it's possible that it is now dead.
169167
// if so, remove it.

branches/tensorflow/lib/SILOptimizer/Mandatory/MandatoryCombine.cpp

Lines changed: 139 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,6 @@
3939

4040
using namespace swift;
4141

42-
//===----------------------------------------------------------------------===//
43-
// Utility
44-
//===----------------------------------------------------------------------===//
45-
46-
/// \returns whether all the values are of trivial type in the provided
47-
/// function.
48-
template <typename Values>
49-
static bool areAllValuesTrivial(Values values, SILFunction &function) {
50-
return llvm::all_of(values, [&](SILValue value) -> bool {
51-
return value->getType().isTrivial(function);
52-
});
53-
}
54-
55-
//===----------------------------------------------------------------------===//
56-
// MandatoryCombiner Interface
57-
//===----------------------------------------------------------------------===//
58-
5942
namespace {
6043

6144
class MandatoryCombiner final
@@ -88,186 +71,182 @@ class MandatoryCombiner final
8871
[&](SILInstruction *instruction) { worklist.add(instruction); }),
8972
createdInstructions(createdInstructions){};
9073

91-
void addReachableCodeToWorklist(SILFunction &function);
92-
93-
/// \return whether a change was made.
94-
bool doOneIteration(SILFunction &function, unsigned iteration);
74+
/// Base visitor that does not do anything.
75+
SILInstruction *visitSILInstruction(SILInstruction *) { return nullptr; }
9576

96-
void clear() {
97-
iteration = 0;
98-
worklist.resetChecked();
99-
madeChange = false;
77+
/// \returns whether all the values are of trivial type in the provided
78+
/// function.
79+
template <typename Values>
80+
static bool areAllValuesTrivial(Values values, SILFunction &function) {
81+
return llvm::all_of(values, [&](SILValue value) -> bool {
82+
return value->getType().isTrivial(function);
83+
});
10084
}
10185

102-
/// Applies the MandatoryCombiner to the provided function.
103-
///
104-
/// \param function the function to which to apply the MandatoryCombiner.
105-
///
106-
/// \return whether a change was made.
107-
bool runOnFunction(SILFunction &function) {
108-
bool changed = false;
109-
110-
while (doOneIteration(function, iteration)) {
111-
changed = true;
112-
++iteration;
86+
SILInstruction *visitApplyInst(ApplyInst *instruction) {
87+
// Apply this pass only to partial applies all of whose arguments are
88+
// trivial.
89+
auto calledValue = instruction->getCallee();
90+
if (calledValue == nullptr) {
91+
return nullptr;
92+
}
93+
auto fullApplyCallee = calledValue->getDefiningInstruction();
94+
if (fullApplyCallee == nullptr) {
95+
return nullptr;
96+
}
97+
auto partialApply = dyn_cast<PartialApplyInst>(fullApplyCallee);
98+
if (partialApply == nullptr) {
99+
return nullptr;
100+
}
101+
auto *function = partialApply->getCalleeFunction();
102+
if (function == nullptr) {
103+
return nullptr;
104+
}
105+
ApplySite fullApplySite(instruction);
106+
auto fullApplyArguments = fullApplySite.getArguments();
107+
if (!areAllValuesTrivial(fullApplyArguments, *function)) {
108+
return nullptr;
109+
}
110+
auto partialApplyArguments = ApplySite(partialApply).getArguments();
111+
if (!areAllValuesTrivial(partialApplyArguments, *function)) {
112+
return nullptr;
113113
}
114114

115-
return changed;
116-
}
115+
auto callee = partialApply->getCallee();
117116

118-
/// Base visitor that does not do anything.
119-
SILInstruction *visitSILInstruction(SILInstruction *) { return nullptr; }
120-
SILInstruction *visitApplyInst(ApplyInst *instruction);
121-
};
117+
ApplySite partialApplySite(partialApply);
122118

123-
} // end anonymous namespace
119+
SmallVector<SILValue, 8> argsVec;
120+
llvm::copy(fullApplyArguments, std::back_inserter(argsVec));
121+
llvm::copy(partialApplyArguments, std::back_inserter(argsVec));
124122

125-
//===----------------------------------------------------------------------===//
126-
// MandatoryCombiner Non-Visitor Utility Methods
127-
//===----------------------------------------------------------------------===//
123+
SILBuilderWithScope builder(instruction, &createdInstructions);
124+
ApplyInst *replacement = builder.createApply(
125+
/*Loc=*/instruction->getDebugLocation().getLocation(), /*Fn=*/callee,
126+
/*Subs=*/partialApply->getSubstitutionMap(),
127+
/*Args*/ argsVec,
128+
/*isNonThrowing=*/instruction->isNonThrowing(),
129+
/*SpecializationInfo=*/partialApply->getSpecializationInfo());
128130

129-
void MandatoryCombiner::addReachableCodeToWorklist(SILFunction &function) {
130-
SmallBlotSetVector<SILBasicBlock *, 32> blockWorklist;
131-
SmallBlotSetVector<SILBasicBlock *, 32> blocksVisited;
132-
SmallVector<SILInstruction *, 128> instructions;
133-
134-
blockWorklist.insert(&*function.begin());
135-
while (!blockWorklist.empty()) {
136-
auto *block = blockWorklist.pop_back_val().getValueOr(nullptr);
137-
if (block == nullptr) {
138-
continue;
139-
}
131+
worklist.replaceInstructionWithInstruction(instruction, replacement
132+
#ifndef NDEBUG
133+
,
134+
/*instructionDescription=*/""
135+
#endif
136+
);
137+
tryDeleteDeadClosure(partialApply, instModCallbacks);
138+
return nullptr;
139+
}
140140

141-
if (!blocksVisited.insert(block).second) {
142-
continue;
143-
}
141+
void addReachableCodeToWorklist(SILFunction &function) {
142+
SmallBlotSetVector<SILBasicBlock *, 32> blockWorklist;
143+
SmallBlotSetVector<SILBasicBlock *, 32> blocksVisited;
144+
SmallVector<SILInstruction *, 128> instructions;
144145

145-
for (auto iterator = block->begin(), end = block->end(); iterator != end;) {
146-
auto *instruction = &*iterator;
147-
++iterator;
146+
blockWorklist.insert(&*function.begin());
147+
while (!blockWorklist.empty()) {
148+
auto *block = blockWorklist.pop_back_val().getValueOr(nullptr);
149+
if (block == nullptr) {
150+
continue;
151+
}
148152

149-
if (isInstructionTriviallyDead(instruction)) {
153+
if (!blocksVisited.insert(block).second) {
150154
continue;
151155
}
152156

153-
instructions.push_back(instruction);
157+
for (auto iterator = block->begin(), end = block->end(); iterator != end;) {
158+
auto *instruction = &*iterator;
159+
++iterator;
160+
161+
if (isInstructionTriviallyDead(instruction)) {
162+
continue;
163+
}
164+
165+
instructions.push_back(instruction);
166+
}
167+
168+
for_each(block->getSuccessorBlocks(), [&](SILBasicBlock *block) {
169+
blockWorklist.insert(block);
170+
});
154171
}
155172

156-
for_each(block->getSuccessorBlocks(),
157-
[&](SILBasicBlock *block) { blockWorklist.insert(block); });
173+
worklist.addInitialGroup(instructions);
158174
}
159175

160-
worklist.addInitialGroup(instructions);
161-
}
162-
163-
bool MandatoryCombiner::doOneIteration(SILFunction &function,
164-
unsigned iteration) {
165-
madeChange = false;
176+
/// \return whether a change was made.
177+
bool doOneIteration(SILFunction &function, unsigned iteration) {
178+
madeChange = false;
166179

167-
addReachableCodeToWorklist(function);
180+
addReachableCodeToWorklist(function);
168181

169-
while (!worklist.isEmpty()) {
170-
auto *instruction = worklist.pop_back_val();
171-
if (instruction == nullptr) {
172-
continue;
173-
}
182+
while (!worklist.isEmpty()) {
183+
auto *instruction = worklist.pop_back_val();
184+
if (instruction == nullptr) {
185+
continue;
186+
}
174187

175188
#ifndef NDEBUG
176-
std::string instructionDescription;
189+
std::string instructionDescription;
177190
#endif
178-
LLVM_DEBUG(llvm::raw_string_ostream SS(instructionDescription);
179-
instruction->print(SS); instructionDescription = SS.str(););
180-
LLVM_DEBUG(llvm::dbgs()
181-
<< "MC: Visiting: " << instructionDescription << '\n');
191+
LLVM_DEBUG(llvm::raw_string_ostream SS(instructionDescription);
192+
instruction->print(SS); instructionDescription = SS.str(););
193+
LLVM_DEBUG(llvm::dbgs()
194+
<< "MC: Visiting: " << instructionDescription << '\n');
182195

183-
if (auto replacement = visit(instruction)) {
184-
worklist.replaceInstructionWithInstruction(instruction, replacement
196+
if (auto replacement = visit(instruction)) {
197+
worklist.replaceInstructionWithInstruction(instruction, replacement
185198
#ifndef NDEBUG
186-
,
187-
instructionDescription
199+
, instructionDescription
188200
#endif
189-
);
190-
}
201+
);
202+
}
191203

192-
for (SILInstruction *instruction : instructionsPendingDeletion) {
193-
worklist.eraseInstFromFunction(*instruction);
194-
}
195-
instructionsPendingDeletion.clear();
196-
197-
// Our tracking list has been accumulating instructions created by the
198-
// SILBuilder during this iteration. Go through the tracking list and add
199-
// its contents to the worklist and then clear said list in preparation
200-
// for the next iteration.
201-
for (SILInstruction *instruction : createdInstructions) {
202-
LLVM_DEBUG(llvm::dbgs() << "MC: add " << *instruction
203-
<< " from tracking list to worklist\n");
204-
worklist.add(instruction);
204+
for (SILInstruction *instruction : instructionsPendingDeletion) {
205+
worklist.eraseInstFromFunction(*instruction);
206+
}
207+
instructionsPendingDeletion.clear();
208+
209+
// Our tracking list has been accumulating instructions created by the
210+
// SILBuilder during this iteration. Go through the tracking list and add
211+
// its contents to the worklist and then clear said list in preparation
212+
// for the next iteration.
213+
for (SILInstruction *instruction : createdInstructions) {
214+
LLVM_DEBUG(llvm::dbgs() << "MC: add " << *instruction
215+
<< " from tracking list to worklist\n");
216+
worklist.add(instruction);
217+
}
218+
createdInstructions.clear();
205219
}
206-
createdInstructions.clear();
207-
}
208-
209-
worklist.resetChecked();
210-
return madeChange;
211-
}
212220

213-
//===----------------------------------------------------------------------===//
214-
// MandatoryCombiner Visitor Methods
215-
//===----------------------------------------------------------------------===//
216-
217-
SILInstruction *MandatoryCombiner::visitApplyInst(ApplyInst *instruction) {
218-
// Apply this pass only to partial applies all of whose arguments are
219-
// trivial.
220-
auto calledValue = instruction->getCallee();
221-
if (calledValue == nullptr) {
222-
return nullptr;
223-
}
224-
auto fullApplyCallee = calledValue->getDefiningInstruction();
225-
if (fullApplyCallee == nullptr) {
226-
return nullptr;
227-
}
228-
auto partialApply = dyn_cast<PartialApplyInst>(fullApplyCallee);
229-
if (partialApply == nullptr) {
230-
return nullptr;
231-
}
232-
auto *function = partialApply->getCalleeFunction();
233-
if (function == nullptr) {
234-
return nullptr;
235-
}
236-
ApplySite fullApplySite(instruction);
237-
auto fullApplyArguments = fullApplySite.getArguments();
238-
if (!areAllValuesTrivial(fullApplyArguments, *function)) {
239-
return nullptr;
221+
worklist.resetChecked();
222+
return madeChange;
240223
}
241-
auto partialApplyArguments = ApplySite(partialApply).getArguments();
242-
if (!areAllValuesTrivial(partialApplyArguments, *function)) {
243-
return nullptr;
224+
225+
void clear() {
226+
iteration = 0;
227+
worklist.resetChecked();
228+
madeChange = false;
244229
}
245230

246-
auto callee = partialApply->getCallee();
247231

248-
ApplySite partialApplySite(partialApply);
232+
/// Applies the MandatoryCombiner to the provided function.
233+
///
234+
/// \param function the function to which to apply the MandatoryCombiner.
235+
///
236+
/// \return whether a change was made.
237+
bool runOnFunction(SILFunction &function) {
238+
bool changed = false;
249239

250-
SmallVector<SILValue, 8> argsVec;
251-
llvm::copy(fullApplyArguments, std::back_inserter(argsVec));
252-
llvm::copy(partialApplyArguments, std::back_inserter(argsVec));
240+
while (doOneIteration(function, iteration)) {
241+
changed = true;
242+
++iteration;
243+
}
253244

254-
SILBuilderWithScope builder(instruction, &createdInstructions);
255-
ApplyInst *replacement = builder.createApply(
256-
/*Loc=*/instruction->getDebugLocation().getLocation(), /*Fn=*/callee,
257-
/*Subs=*/partialApply->getSubstitutionMap(),
258-
/*Args*/ argsVec,
259-
/*isNonThrowing=*/instruction->isNonThrowing(),
260-
/*SpecializationInfo=*/partialApply->getSpecializationInfo());
245+
return changed;
246+
}
247+
};
261248

262-
worklist.replaceInstructionWithInstruction(instruction, replacement
263-
#ifndef NDEBUG
264-
,
265-
/*instructionDescription=*/""
266-
#endif
267-
);
268-
tryDeleteDeadClosure(partialApply, instModCallbacks);
269-
return nullptr;
270-
}
249+
} // end anonymous namespace
271250

272251
//===----------------------------------------------------------------------===//
273252
// Top Level Entrypoint

0 commit comments

Comments
 (0)