Skip to content

Commit 727eaf1

Browse files
sstricklcommit-bot@chromium.org
authored andcommitted
[vm/compiler] Fix type propagation for Redefinitions.
In addition to the underlying type (stored as either a cid or an AbstractType), CompileTypes also store two flags: whether a value of the type can be null and whether it can be a sentinel. When constraining the type of a definition via a RedefinitionInstr, the resulting type should be nullable only if both the original and constrained type are. Similarly, the resulting type should only allow the sentinel value if both the original and constrained type do. When the underlying type is represented by a cid, this was already the case. When it is represented by an AbstractType, only nullability was appropriately handled. This CL fixes it so that the possibility of being a sentinel is also handled correctly in the latter case. TEST=vm/cc/TypePropagator_RedefineCanBeSentinelWithCannotBe Bug: #47739 Change-Id: I9d51b1c14ff385d522309f9c984a25dc6bdfbbf4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/220767 Commit-Queue: Tess Strickland <[email protected]> Reviewed-by: Daco Harkes <[email protected]>
1 parent 8b4dea2 commit 727eaf1

File tree

2 files changed

+133
-6
lines changed

2 files changed

+133
-6
lines changed

runtime/vm/compiler/backend/type_propagator.cc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ CompileType RedefinitionInstr::ComputeType() const {
10251025
// If either type is non-nullable, the resulting type is non-nullable.
10261026
const bool is_nullable =
10271027
value()->Type()->is_nullable() && constrained_type_->is_nullable();
1028+
// The resulting type can be the sentinel value only if both types can be.
10281029
const bool can_be_sentinel = value()->Type()->can_be_sentinel() &&
10291030
constrained_type_->can_be_sentinel();
10301031

@@ -1037,13 +1038,18 @@ CompileType RedefinitionInstr::ComputeType() const {
10371038
return CompileType(is_nullable, can_be_sentinel,
10381039
constrained_type_->ToNullableCid(), nullptr);
10391040
}
1040-
if (value()->Type()->IsSubtypeOf(*constrained_type_->ToAbstractType())) {
1041-
return is_nullable ? *value()->Type()
1042-
: value()->Type()->CopyNonNullable();
1043-
} else {
1044-
return is_nullable ? *constrained_type_
1045-
: constrained_type_->CopyNonNullable();
1041+
1042+
CompileType result(
1043+
value()->Type()->IsSubtypeOf(*constrained_type_->ToAbstractType())
1044+
? *value()->Type()
1045+
: *constrained_type_);
1046+
if (!is_nullable) {
1047+
result = result.CopyNonNullable();
1048+
}
1049+
if (!can_be_sentinel) {
1050+
result = result.CopyNonSentinel();
10461051
}
1052+
return result;
10471053
}
10481054
return *value()->Type();
10491055
}

runtime/vm/compiler/backend/type_propagator_test.cc

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,127 @@ ISOLATE_UNIT_TEST_CASE(TypePropagator_NonNullableLoadStaticField) {
590590
EXPECT_PROPERTY(load->AsLoadStaticField()->Type(), !it.is_nullable());
591591
}
592592

593+
ISOLATE_UNIT_TEST_CASE(TypePropagator_RedefineCanBeSentinelWithCannotBe) {
594+
const char* kScript = R"(
595+
late final int x;
596+
)";
597+
Zone* const Z = Thread::Current()->zone();
598+
const auto& root_library = Library::CheckedHandle(Z, LoadTestScript(kScript));
599+
const auto& toplevel = Class::Handle(Z, root_library.toplevel_class());
600+
const auto& field_x = Field::Handle(
601+
Z, toplevel.LookupStaticField(String::Handle(Z, String::New("x"))));
602+
603+
using compiler::BlockBuilder;
604+
CompilerState S(thread, /*is_aot=*/false, /*is_optimizing=*/true);
605+
FlowGraphBuilderHelper H;
606+
607+
// We are going to build the following graph:
608+
//
609+
// B0[graph]:0 {
610+
// v2 <- Constant(#3)
611+
// }
612+
// B1[function entry]:2
613+
// v3 <- LoadStaticField:10(x, ThrowIfSentinel)
614+
// v5 <- Constant(#sentinel)
615+
// Branch if StrictCompare:12(===, v3, v5) goto (2, 3)
616+
// B2[target]:4
617+
// goto:16 B4
618+
// B3[target]:6
619+
// v7 <- Redefinition(v3 ^ T{int?})
620+
// goto:18 B4
621+
// B4[join]:8 pred(B2, B3) {
622+
// v9 <- phi(v2, v7) alive
623+
// }
624+
// Return:20(v9)
625+
626+
Definition* v2 = H.IntConstant(3);
627+
Definition* v3;
628+
Definition* v7;
629+
PhiInstr* v9;
630+
auto b1 = H.flow_graph()->graph_entry()->normal_entry();
631+
auto b2 = H.TargetEntry();
632+
auto b3 = H.TargetEntry();
633+
auto b4 = H.JoinEntry();
634+
635+
{
636+
BlockBuilder builder(H.flow_graph(), b1);
637+
v3 = builder.AddDefinition(new LoadStaticFieldInstr(
638+
field_x, {},
639+
/*calls_initializer=*/false, S.GetNextDeoptId()));
640+
auto v5 = builder.AddDefinition(new ConstantInstr(Object::sentinel()));
641+
builder.AddBranch(new StrictCompareInstr(
642+
{}, Token::kEQ_STRICT, new Value(v3), new Value(v5),
643+
/*needs_number_check=*/false, S.GetNextDeoptId()),
644+
b2, b3);
645+
}
646+
647+
{
648+
BlockBuilder builder(H.flow_graph(), b2);
649+
builder.AddInstruction(new GotoInstr(b4, S.GetNextDeoptId()));
650+
}
651+
652+
{
653+
BlockBuilder builder(H.flow_graph(), b3);
654+
v7 = builder.AddDefinition(new RedefinitionInstr(new Value(v3)));
655+
CompileType int_type = CompileType::FromAbstractType(
656+
Type::Handle(Type::IntType()),
657+
/*can_be_null=*/
658+
!IsolateGroup::Current()->use_strict_null_safety_checks(),
659+
/*can_be_sentinel=*/false);
660+
v7->AsRedefinition()->set_constrained_type(new CompileType(int_type));
661+
builder.AddInstruction(new GotoInstr(b4, S.GetNextDeoptId()));
662+
}
663+
664+
{
665+
BlockBuilder builder(H.flow_graph(), b4);
666+
v9 = H.Phi(b4, {{b2, v2}, {b3, v7}});
667+
builder.AddPhi(v9);
668+
builder.AddReturn(new Value(v9));
669+
}
670+
671+
H.FinishGraph();
672+
673+
FlowGraphPrinter::PrintGraph("Before TypePropagator", H.flow_graph());
674+
FlowGraphTypePropagator::Propagate(H.flow_graph());
675+
FlowGraphPrinter::PrintGraph("After TypePropagator", H.flow_graph());
676+
677+
auto& blocks = H.flow_graph()->reverse_postorder();
678+
EXPECT_EQ(5, blocks.length());
679+
EXPECT_PROPERTY(blocks[0], it.IsGraphEntry());
680+
681+
// We expect the following types:
682+
//
683+
// B1[function entry]:2
684+
// v3 <- LoadStaticField:10(x) T{int?~} // T{int~} in null safe mode
685+
// v5 <- Constant(#sentinel) T{Sentinel~}
686+
// Branch if StrictCompare:12(===, v3, v5) goto (2, 3)
687+
688+
EXPECT_PROPERTY(blocks[1], it.IsFunctionEntry());
689+
EXPECT_PROPERTY(blocks[1]->next(), it.IsLoadStaticField());
690+
EXPECT_PROPERTY(blocks[1]->next()->AsLoadStaticField(), it.HasType());
691+
EXPECT_PROPERTY(blocks[1]->next()->AsLoadStaticField()->Type(),
692+
it.can_be_sentinel());
693+
694+
// B3[target]:6
695+
// v7 <- Redefinition(v3 ^ T{int?}) T{int?} // T{int} in null safe mode
696+
// goto:18 B4
697+
EXPECT_PROPERTY(blocks[3], it.IsTargetEntry());
698+
EXPECT_PROPERTY(blocks[3]->next(), it.IsRedefinition());
699+
EXPECT_PROPERTY(blocks[3]->next()->AsRedefinition(), it.HasType());
700+
EXPECT_PROPERTY(blocks[3]->next()->AsRedefinition()->Type(),
701+
!it.can_be_sentinel());
702+
703+
// B4[join]:8 pred(B2, B3) {
704+
// v9 <- phi(v2, v7) alive T{int?} // T{int} in null safe mode
705+
// }
706+
// Return:20(v9)
707+
EXPECT_PROPERTY(blocks[4], it.IsJoinEntry());
708+
EXPECT_PROPERTY(blocks[4], it.AsJoinEntry()->phis() != nullptr);
709+
EXPECT_PROPERTY(blocks[4]->AsJoinEntry()->phis()->At(0), it.HasType());
710+
EXPECT_PROPERTY(blocks[4]->AsJoinEntry()->phis()->At(0)->Type(),
711+
!it.can_be_sentinel());
712+
}
713+
593714
#endif // defined(DART_PRECOMPILER)
594715

595716
} // namespace dart

0 commit comments

Comments
 (0)