Skip to content

Commit 38d8a5a

Browse files
Robertorosmaninhorv-auditorrv-jenkins
authored
Don't emit branch unnecessary check for injection (#1029)
Fix Pi-Squared-Inc/pi2#1323 The llvm backend currently emits a branch each time a new injection is created to check whether the term that is its child is itself an injection. We skip over some circumstances where such a check would not be necessary, but not all. If the sort being injected has no subsort, then the child cannot be an injection. This PR adds a condition to check if the current sort has no subsorts. If that's the case, we also skip the branch/new blocks emit. --------- Co-authored-by: devops <[email protected]> Co-authored-by: rv-jenkins <[email protected]>
1 parent c4eb54b commit 38d8a5a

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

include/kllvm/ast/AST.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,9 @@ class kore_definition {
922922

923923
kore_symbol *inj_symbol_{};
924924

925+
std::optional<SubsortMap> subsorts_;
926+
std::optional<SubsortMap> supersorts_;
927+
925928
/*
926929
* Insert symbols into this definition that have knowable labels, but cannot
927930
* be directly referenced in user code:

lib/ast/definition.cpp

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,37 +105,43 @@ void kore_definition::insert_reserved_symbols() {
105105
}
106106

107107
SubsortMap kore_definition::get_subsorts() {
108-
auto subsorts = SubsortMap{};
109-
110-
for (auto *axiom : axioms_) {
111-
if (axiom->attributes().contains(attribute_set::key::Subsort)) {
112-
auto const &att = axiom->attributes().get(attribute_set::key::Subsort);
113-
auto const &inner_sort
114-
= att->get_constructor()->get_formal_arguments()[0];
115-
auto const &outer_sort
116-
= att->get_constructor()->get_formal_arguments()[1];
117-
subsorts[inner_sort.get()].insert(outer_sort.get());
108+
if (!subsorts_) {
109+
auto subsorts = SubsortMap{};
110+
111+
for (auto *axiom : axioms_) {
112+
if (axiom->attributes().contains(attribute_set::key::Subsort)) {
113+
auto const &att = axiom->attributes().get(attribute_set::key::Subsort);
114+
auto const &inner_sort
115+
= att->get_constructor()->get_formal_arguments()[0];
116+
auto const &outer_sort
117+
= att->get_constructor()->get_formal_arguments()[1];
118+
subsorts[inner_sort.get()].insert(outer_sort.get());
119+
}
118120
}
121+
subsorts_ = transitive_closure(subsorts);
119122
}
120123

121-
return transitive_closure(subsorts);
124+
return *subsorts_;
122125
}
123126

124127
SubsortMap kore_definition::get_supersorts() {
125-
auto supersorts = SubsortMap{};
126-
127-
for (auto *axiom : axioms_) {
128-
if (axiom->attributes().contains(attribute_set::key::Subsort)) {
129-
auto const &att = axiom->attributes().get(attribute_set::key::Subsort);
130-
auto const &inner_sort
131-
= att->get_constructor()->get_formal_arguments()[0];
132-
auto const &outer_sort
133-
= att->get_constructor()->get_formal_arguments()[1];
134-
supersorts[outer_sort.get()].insert(inner_sort.get());
128+
if (!supersorts_) {
129+
auto supersorts = SubsortMap{};
130+
131+
for (auto *axiom : axioms_) {
132+
if (axiom->attributes().contains(attribute_set::key::Subsort)) {
133+
auto const &att = axiom->attributes().get(attribute_set::key::Subsort);
134+
auto const &inner_sort
135+
= att->get_constructor()->get_formal_arguments()[0];
136+
auto const &outer_sort
137+
= att->get_constructor()->get_formal_arguments()[1];
138+
supersorts[outer_sort.get()].insert(inner_sort.get());
139+
}
135140
}
141+
supersorts_ = transitive_closure(supersorts);
136142
}
137143

138-
return transitive_closure(supersorts);
144+
return *supersorts_;
139145
}
140146

141147
SymbolMap kore_definition::get_overloads() const {
@@ -160,6 +166,8 @@ SymbolMap kore_definition::get_overloads() const {
160166

161167
// NOLINTNEXTLINE(*-function-cognitive-complexity)
162168
void kore_definition::preprocess() {
169+
get_subsorts();
170+
get_supersorts();
163171
for (auto *axiom : axioms_) {
164172
axiom->pattern_ = axiom->pattern_->expand_aliases(this);
165173
}

lib/codegen/CreateTerm.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -952,15 +952,13 @@ std::pair<llvm::Value *, bool> create_term::create_allocation(
952952
fn_name, constructor, false, true, false, location_stack),
953953
true);
954954
}
955-
if (auto cat
956-
= dynamic_cast<kore_composite_sort *>(symbol->get_arguments()[0].get())
957-
->get_category(definition_)
958-
.cat;
955+
if (auto *sort
956+
= dynamic_cast<kore_composite_sort *>(symbol->get_arguments()[0].get());
959957
symbol_decl->attributes().contains(attribute_set::key::SortInjection)
960-
&& (cat == sort_category::Symbol)) {
958+
&& (sort->get_category(definition_).cat == sort_category::Symbol)) {
961959
std::pair<llvm::Value *, bool> val = create_allocation(
962960
constructor->get_arguments()[0].get(), location_stack);
963-
if (val.second) {
961+
if (val.second && !definition_->get_supersorts()[sort].empty()) {
964962
llvm::Instruction *tag = llvm::CallInst::Create(
965963
get_or_insert_function(
966964
module_, "get_tag", llvm::Type::getInt32Ty(ctx_),

0 commit comments

Comments
 (0)