Skip to content

Commit bebee99

Browse files
mralephCommit Queue
authored and
Commit Queue
committed
[vm/compiler] Move reorder_blocks onto the graph.
Make FlowGraph constructor compute whether we are planning to reorder blocks before code generation or not. This makes the state of the graph at the end of the compilation more clear. This is just a refactoring without any functional changes. TEST=ci Change-Id: Ieefb02237cc1ebd69d5d2b217bdc8ebbfdbf15c7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/358446 Commit-Queue: Slava Egorov <[email protected]> Reviewed-by: Alexander Markov <[email protected]>
1 parent 2b8c2f6 commit bebee99

14 files changed

+104
-77
lines changed

runtime/vm/compiler/aot/precompiler.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3516,8 +3516,6 @@ bool PrecompileParsedFunctionHelper::Compile(CompilationPipeline* pipeline) {
35163516

35173517
CompilerPassState pass_state(thread(), flow_graph, &speculative_policy,
35183518
precompiler_);
3519-
pass_state.reorder_blocks =
3520-
FlowGraph::ShouldReorderBlocks(function, optimized());
35213519

35223520
if (function.ForceOptimize()) {
35233521
ASSERT(optimized());

runtime/vm/compiler/backend/block_scheduler.cc

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ static void Union(GrowableArray<Chain*>* chains,
156156
}
157157

158158
void BlockScheduler::ReorderBlocks(FlowGraph* flow_graph) {
159+
if (!flow_graph->should_reorder_blocks()) {
160+
return;
161+
}
162+
159163
if (CompilerState::Current().is_aot()) {
160164
ReorderBlocksAOT(flow_graph);
161165
} else {
@@ -164,10 +168,6 @@ void BlockScheduler::ReorderBlocks(FlowGraph* flow_graph) {
164168
}
165169

166170
void BlockScheduler::ReorderBlocksJIT(FlowGraph* flow_graph) {
167-
if (!FLAG_reorder_basic_blocks) {
168-
return;
169-
}
170-
171171
// Add every block to a chain of length 1 and compute a list of edges
172172
// sorted by weight.
173173
intptr_t block_count = flow_graph->preorder().length();
@@ -219,10 +219,10 @@ void BlockScheduler::ReorderBlocksJIT(FlowGraph* flow_graph) {
219219
// Ensure the checked entry remains first to avoid needing another offset on
220220
// Instructions, compare Code::EntryPointOf.
221221
GraphEntryInstr* graph_entry = flow_graph->graph_entry();
222-
flow_graph->CodegenBlockOrder(true)->Add(graph_entry);
222+
flow_graph->CodegenBlockOrder()->Add(graph_entry);
223223
FunctionEntryInstr* checked_entry = graph_entry->normal_entry();
224224
if (checked_entry != nullptr) {
225-
flow_graph->CodegenBlockOrder(true)->Add(checked_entry);
225+
flow_graph->CodegenBlockOrder()->Add(checked_entry);
226226
}
227227
// Build a new block order. Emit each chain when its first block occurs
228228
// in the original reverse postorder ordering (which gives a topological
@@ -231,7 +231,7 @@ void BlockScheduler::ReorderBlocksJIT(FlowGraph* flow_graph) {
231231
if (chains[i]->first->block == flow_graph->postorder()[i]) {
232232
for (Link* link = chains[i]->first; link != nullptr; link = link->next) {
233233
if ((link->block != checked_entry) && (link->block != graph_entry)) {
234-
flow_graph->CodegenBlockOrder(true)->Add(link->block);
234+
flow_graph->CodegenBlockOrder()->Add(link->block);
235235
}
236236
}
237237
}
@@ -241,10 +241,6 @@ void BlockScheduler::ReorderBlocksJIT(FlowGraph* flow_graph) {
241241
// Moves blocks ending in a throw/rethrow, as well as any block post-dominated
242242
// by such a throwing block, to the end.
243243
void BlockScheduler::ReorderBlocksAOT(FlowGraph* flow_graph) {
244-
if (!FLAG_reorder_basic_blocks) {
245-
return;
246-
}
247-
248244
auto& reverse_postorder = flow_graph->reverse_postorder();
249245
const intptr_t block_count = reverse_postorder.length();
250246
GrowableArray<bool> is_terminating(block_count);
@@ -283,7 +279,7 @@ void BlockScheduler::ReorderBlocksAOT(FlowGraph* flow_graph) {
283279

284280
// Emit code in reverse postorder but move any throwing blocks (except the
285281
// function entry, which needs to come first) to the very end.
286-
auto codegen_order = flow_graph->CodegenBlockOrder(true);
282+
auto codegen_order = flow_graph->CodegenBlockOrder();
287283
for (intptr_t i = 0; i < block_count; ++i) {
288284
auto block = reverse_postorder[i];
289285
const intptr_t preorder_nr = block->preorder_number();

runtime/vm/compiler/backend/flow_graph.cc

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@ DEFINE_FLAG(bool, prune_dead_locals, true, "optimize dead locals away");
3434
// Quick access to the current zone.
3535
#define Z (zone())
3636

37+
static bool ShouldReorderBlocks(const Function& function,
38+
FlowGraph::CompilationMode mode) {
39+
return (mode == FlowGraph::CompilationMode::kOptimized) &&
40+
FLAG_reorder_basic_blocks && !function.IsFfiCallbackTrampoline();
41+
}
42+
3743
FlowGraph::FlowGraph(const ParsedFunction& parsed_function,
3844
GraphEntryInstr* graph_entry,
3945
intptr_t max_block_id,
40-
PrologueInfo prologue_info)
46+
PrologueInfo prologue_info,
47+
CompilationMode compilation_mode)
4148
: thread_(Thread::Current()),
4249
parent_(),
4350
current_ssa_temp_index_(0),
@@ -56,6 +63,8 @@ FlowGraph::FlowGraph(const ParsedFunction& parsed_function,
5663
constant_null_(nullptr),
5764
constant_dead_(nullptr),
5865
licm_allowed_(true),
66+
should_reorder_blocks_(
67+
ShouldReorderBlocks(parsed_function.function(), compilation_mode)),
5968
prologue_info_(prologue_info),
6069
loop_hierarchy_(nullptr),
6170
loop_invariant_loads_(nullptr),
@@ -152,16 +161,14 @@ void FlowGraph::ReplaceCurrentInstruction(ForwardInstructionIterator* iterator,
152161
iterator->RemoveCurrentFromGraph();
153162
}
154163

155-
bool FlowGraph::ShouldReorderBlocks(const Function& function,
156-
bool is_optimized) {
157-
return is_optimized && FLAG_reorder_basic_blocks &&
158-
!function.is_intrinsic() && !function.IsFfiCallbackTrampoline();
164+
GrowableArray<BlockEntryInstr*>* FlowGraph::CodegenBlockOrder() {
165+
return should_reorder_blocks() ? &optimized_block_order_
166+
: &reverse_postorder_;
159167
}
160168

161-
GrowableArray<BlockEntryInstr*>* FlowGraph::CodegenBlockOrder(
162-
bool is_optimized) {
163-
return ShouldReorderBlocks(function(), is_optimized) ? &optimized_block_order_
164-
: &reverse_postorder_;
169+
const GrowableArray<BlockEntryInstr*>* FlowGraph::CodegenBlockOrder() const {
170+
return should_reorder_blocks() ? &optimized_block_order_
171+
: &reverse_postorder_;
165172
}
166173

167174
ConstantInstr* FlowGraph::GetExistingConstant(

runtime/vm/compiler/backend/flow_graph.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,17 @@ struct PrologueInfo {
113113
// Class to encapsulate the construction and manipulation of the flow graph.
114114
class FlowGraph : public ZoneAllocated {
115115
public:
116+
enum class CompilationMode {
117+
kUnoptimized,
118+
kOptimized,
119+
kIntrinsic,
120+
};
121+
116122
FlowGraph(const ParsedFunction& parsed_function,
117123
GraphEntryInstr* graph_entry,
118124
intptr_t max_block_id,
119-
PrologueInfo prologue_info);
125+
PrologueInfo prologue_info,
126+
CompilationMode compilation_mode);
120127

121128
// Function properties.
122129
const ParsedFunction& parsed_function() const { return parsed_function_; }
@@ -203,8 +210,8 @@ class FlowGraph : public ZoneAllocated {
203210
const GrowableArray<BlockEntryInstr*>& optimized_block_order() const {
204211
return optimized_block_order_;
205212
}
206-
static bool ShouldReorderBlocks(const Function& function, bool is_optimized);
207-
GrowableArray<BlockEntryInstr*>* CodegenBlockOrder(bool is_optimized);
213+
GrowableArray<BlockEntryInstr*>* CodegenBlockOrder();
214+
const GrowableArray<BlockEntryInstr*>* CodegenBlockOrder() const;
208215

209216
// Iterators.
210217
BlockIterator reverse_postorder_iterator() const {
@@ -491,6 +498,8 @@ class FlowGraph : public ZoneAllocated {
491498
return compiler_pass_filters_;
492499
}
493500

501+
bool should_reorder_blocks() const { return should_reorder_blocks_; }
502+
494503
//
495504
// High-level utilities.
496505
//
@@ -562,6 +571,11 @@ class FlowGraph : public ZoneAllocated {
562571
static intptr_t ComputeArgumentsSizeInWords(const Function& function,
563572
intptr_t arguments_count);
564573

574+
static constexpr CompilationMode CompilationModeFrom(bool is_optimizing) {
575+
return is_optimizing ? CompilationMode::kOptimized
576+
: CompilationMode::kUnoptimized;
577+
}
578+
565579
private:
566580
friend class FlowGraphCompiler; // TODO(ajcbik): restructure
567581
friend class FlowGraphChecker;
@@ -678,6 +692,7 @@ class FlowGraph : public ZoneAllocated {
678692
bool licm_allowed_;
679693
bool unmatched_representations_allowed_ = true;
680694
bool huge_method_ = false;
695+
const bool should_reorder_blocks_;
681696

682697
const PrologueInfo prologue_info_;
683698

runtime/vm/compiler/backend/flow_graph_compiler.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ FlowGraphCompiler::FlowGraphCompiler(
142142
assembler_(assembler),
143143
parsed_function_(parsed_function),
144144
flow_graph_(*flow_graph),
145-
block_order_(*flow_graph->CodegenBlockOrder(is_optimizing)),
145+
block_order_(*flow_graph->CodegenBlockOrder()),
146146
current_block_(nullptr),
147147
exception_handlers_list_(nullptr),
148148
pc_descriptors_list_(nullptr),

runtime/vm/compiler/backend/il_serializer.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,9 @@ FlowGraph* FlowGraphDeserializer::ReadFlowGraph() {
743743
instr->ReadExtra(this);
744744
}
745745

746-
FlowGraph* flow_graph = new (Z)
747-
FlowGraph(parsed_function(), graph_entry_, max_block_id, prologue_info);
746+
FlowGraph* flow_graph =
747+
new (Z) FlowGraph(parsed_function(), graph_entry_, max_block_id,
748+
prologue_info, FlowGraph::CompilationMode::kOptimized);
748749
flow_graph->set_current_ssa_temp_index(current_ssa_temp_index);
749750
flow_graph->CreateCommonConstants();
750751
flow_graph->disallow_licm();
@@ -754,7 +755,7 @@ FlowGraph* FlowGraphDeserializer::ReadFlowGraph() {
754755
{
755756
const intptr_t num_blocks = Read<intptr_t>();
756757
if (num_blocks != 0) {
757-
auto* codegen_block_order = flow_graph->CodegenBlockOrder(true);
758+
auto* codegen_block_order = flow_graph->CodegenBlockOrder();
758759
ASSERT(codegen_block_order == &flow_graph->optimized_block_order());
759760
for (intptr_t i = 0; i < num_blocks; ++i) {
760761
codegen_block_order->Add(ReadRef<BlockEntryInstr*>());

runtime/vm/compiler/backend/il_test_helper.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,12 @@ FlowGraph* TestPipeline::RunPasses(
140140
flow_graph_->PopulateWithICData(function_);
141141
}
142142

143-
const bool reorder_blocks =
144-
FlowGraph::ShouldReorderBlocks(function_, optimized);
145-
if (mode_ == CompilerPass::kJIT && reorder_blocks) {
143+
if (mode_ == CompilerPass::kJIT && flow_graph_->should_reorder_blocks()) {
146144
BlockScheduler::AssignEdgeWeights(flow_graph_);
147145
}
148146

149147
pass_state_ =
150148
new CompilerPassState(thread, flow_graph_, speculative_policy_.get());
151-
pass_state_->reorder_blocks = reorder_blocks;
152149

153150
if (optimized) {
154151
JitCallSpecializer jit_call_specializer(flow_graph_,

runtime/vm/compiler/backend/il_test_helper.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,9 @@ class FlowGraphBuilderHelper {
272272
public:
273273
explicit FlowGraphBuilderHelper(intptr_t num_parameters = 0)
274274
: state_(CompilerState::Current()),
275-
flow_graph_(MakeDummyGraph(Thread::Current(), num_parameters)) {
275+
flow_graph_(MakeDummyGraph(Thread::Current(),
276+
num_parameters,
277+
state_.is_optimizing())) {
276278
flow_graph_.CreateCommonConstants();
277279
}
278280

@@ -375,7 +377,9 @@ class FlowGraphBuilderHelper {
375377
FlowGraph* flow_graph() { return &flow_graph_; }
376378

377379
private:
378-
static FlowGraph& MakeDummyGraph(Thread* thread, intptr_t num_parameters) {
380+
static FlowGraph& MakeDummyGraph(Thread* thread,
381+
intptr_t num_parameters,
382+
bool is_optimizing) {
379383
const FunctionType& signature =
380384
FunctionType::ZoneHandle(FunctionType::New());
381385
signature.set_num_fixed_parameters(num_parameters);
@@ -403,7 +407,8 @@ class FlowGraphBuilderHelper {
403407
new FunctionEntryInstr(graph_entry, block_id, kInvalidTryIndex,
404408
CompilerState::Current().GetNextDeoptId()));
405409
return *new FlowGraph(*parsed_function, graph_entry, block_id,
406-
PrologueInfo{-1, -1});
410+
PrologueInfo{-1, -1},
411+
FlowGraph::CompilationModeFrom(is_optimizing));
407412
}
408413

409414
CompilerState& state_;

runtime/vm/compiler/compiler_pass.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ CompilerPassState::CompilerPassState(
6060
sinking(nullptr),
6161
call_specializer(nullptr),
6262
speculative_policy(speculative_policy),
63-
reorder_blocks(false),
6463
sticky_flags(0),
6564
flow_graph_(flow_graph) {
6665
// Top scope function is at inlining id 0.
@@ -571,9 +570,7 @@ COMPILER_PASS(AllocateRegistersForGraphIntrinsic, {
571570
});
572571

573572
COMPILER_PASS(ReorderBlocks, {
574-
if (state->reorder_blocks) {
575-
BlockScheduler::ReorderBlocks(flow_graph);
576-
}
573+
BlockScheduler::ReorderBlocks(flow_graph);
577574

578575
// This is the last compiler pass.
579576
// Test that round-trip IL serialization works before generating code.

runtime/vm/compiler/compiler_pass.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ struct CompilerPassState {
9494

9595
SpeculativeInliningPolicy* speculative_policy;
9696

97-
bool reorder_blocks;
98-
9997
intptr_t sticky_flags;
10098

10199
FlowGraphCompiler* graph_compiler = nullptr;

runtime/vm/compiler/frontend/kernel_binary_flowgraph.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ FlowGraph* StreamingFlowGraphBuilder::BuildGraphOfFieldInitializer() {
6969
if (B->IsCompiledForOsr()) {
7070
B->graph_entry_->RelinkToOsrEntry(Z, B->last_used_block_id_ + 1);
7171
}
72-
return new (Z) FlowGraph(*parsed_function(), B->graph_entry_,
73-
B->last_used_block_id_, prologue_info);
72+
return new (Z) FlowGraph(
73+
*parsed_function(), B->graph_entry_, B->last_used_block_id_,
74+
prologue_info,
75+
FlowGraph::CompilationModeFrom(flow_graph_builder_->optimizing_));
7476
}
7577

7678
void StreamingFlowGraphBuilder::SetupDefaultParameterValues() {
@@ -892,9 +894,10 @@ FlowGraph* StreamingFlowGraphBuilder::BuildGraphOfFunction(
892894
graph_entry->RelinkToOsrEntry(Z,
893895
flow_graph_builder_->last_used_block_id_ + 1);
894896
}
895-
return new (Z)
896-
FlowGraph(*parsed_function(), graph_entry,
897-
flow_graph_builder_->last_used_block_id_, prologue_info);
897+
return new (Z) FlowGraph(
898+
*parsed_function(), graph_entry, flow_graph_builder_->last_used_block_id_,
899+
prologue_info,
900+
FlowGraph::CompilationModeFrom(flow_graph_builder_->optimizing_));
898901
}
899902

900903
FlowGraph* StreamingFlowGraphBuilder::BuildGraph() {

0 commit comments

Comments
 (0)