8000 fix simplifycfg: block argument forwarding needs iterated substitution by cfbolz · Pull Request #166 · can-lehmann/metajit.cpp · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions jitir.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,13 @@ def run(self, ir):
code += f"return nullptr;\n"
return {"clone": code}

def binop(name, type_checks = None):
def binop(name, type_checks = None, commutative = False):
if type_checks is None:
type_checks = ["is_int(a->type())"]
return Inst(name,
args = [Arg("a"), Arg("b")],
type = "a->type()",
commutative = commutative,
type_checks = [
"a->type() == b->type()",
*type_checks
Expand All @@ -360,10 +361,11 @@ def binop(name, type_checks = None):
def binop_f(name):
return binop(name, type_checks = ["is_float(a->type())"])

def cmp(name, type_checks):
def cmp(name, type_checks, commutative = False):
return Inst(name,
args = [Arg("a"), Arg("b")],
type = "Type::Bool",
commutative = commutative,
type_checks = [
"a->type() == b->type()",
*type_checks
Expand Down Expand Up @@ -523,25 +525,25 @@ def cmp(name, type_checks):
"offset->type() == Type::Int64"
]
),
binop("Add"),
binop("Add", commutative = True),
binop("Sub"),
binop("Mul"),
binop("MulHighU"),
binop("Mul", commutative = True),
binop("MulHighU", commutative = True),
binop("DivS"),
binop("DivU"),
binop("ModS"),
binop("ModU"),
binop("And", type_checks = ["is_int_or_bool(a->type())"]),
binop("Or", type_checks = ["is_int_or_bool(a->type())"]),
binop("Xor", type_checks = ["is_int_or_bool(a->type())"]),
binop("And", type_checks = ["is_int_or_bool(a->type())"], commutative = True),
binop("Or", type_checks = ["is_int_or_bool(a->type())"], commutative = True),
binop("Xor", type_checks = ["is_int_or_bool(a->type())"], commutative = True),
binop("ShrU"),
binop("ShrS"),
binop("Shl"),
binop_f("AddF"),
binop_f("SubF"),
binop_f("MulF"),
binop_f("DivF"),
cmp("Eq", type_checks = []),
cmp("Eq", type_checks = [], commutative = True),
cmp("LtU", type_checks = ["is_int(a->type())"]),
cmp("LtS", type_checks = ["is_int(a->type())"]),
cmp("LtFO", type_checks = ["is_float(a->type())"]),
Expand Down Expand Up @@ -608,6 +610,7 @@ def cmp(name, type_checks):
InstEqualsPlugin(),
InstHashPlugin()
]),
InstCommutativityPlugin(),
AllocatorBuilderPlugin(),
ClonePlugin(),
CAPIPlugin(
Expand Down
38 changes: 24 additions & 14 deletions jitir.tmpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ namespace metajit {

void substitute_args(NameMap<Value*>& substs);
void substitute_args_iterated(NameMap<Value*>& substs);
void substitute_args_iterated(std::map<Value*, Value*>& substs);

void substitute_blocks(BlockMap<Block*>& substs);
void substitute_blocks(const std::map<Block*, Block*>& substs);
Expand Down Expand Up @@ -964,13 +965,7 @@ namespace metajit {
/* ${insts} */

bool Inst::is_commutative() const {
return dynamic_cast<const AndInst*>(this) ||
dynamic_cast<const OrInst*>(this) ||
dynamic_cast<const XorInst*>(this) ||
dynamic_cast<const AddInst*>(this) ||
dynamic_cast<const MulInst*>(this) ||
dynamic_cast<const MulHighUInst*>(this) ||
dynamic_cast<const EqInst*>(this);
return /* ${commutative_checks} */;
}

bool Inst::has_side_effect() const {
Expand Down Expand Up @@ -2185,6 +2180,25 @@ namespace metajit {
}
}

Value* resolve_substitution(Value* value, std::map<Value*, Value*>& substs) {
Value* replacement = value;
while (substs.find(replacement) != substs.end()) {
replacement = substs.at(replacement);
}
while (value != replacement) {
Value* next = substs.at(value);
substs.at(value) = replacement;
value = next;
}
return replacement;
}

void Inst::substitute_args_iterated(std::map<Value*, Value*>& substs) {
for (size_t index = 0; index < arg_count(); index++) {
set_arg(index, resolve_substitution(arg(index), substs));
}
}

void Inst::substitute_args(NameMap<Value*>& substs) {
for (size_t it = 0; it < arg_count(); it++) {
Value* argument = arg(it);
Expand Down Expand Up @@ -5123,14 +5137,10 @@ class SimplifyTrace: public metajit::Pass<SimplifyTrace> {
externally_used_args.insert(block);
}
for (size_t i = 0; i < target->args().size(); i++) {
Value* arg = jump->arg(i);
if (substs.find(arg) != substs.end()) {
arg = substs.at(arg);
}
substs[target->args().at(i)] = arg;
substs[target->args().at(i)] = resolve_substitution(jump->arg(i), substs);
}
for (Inst* inst : *target) {
inst->substitute_args(substs);
inst->substitute_args_iterated(substs);
}

auto target_successors = target->successors();
Expand Down Expand Up @@ -5199,7 +5209,7 @@ class SimplifyTrace: public metajit::Pass<SimplifyTrace> {
if (substs.size()) {
for (Block* block : *_section) {
for (Inst* inst : *block) {
inst->substitute_args(substs);
inst->substitute_args_iterated(substs);
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions tests/test_opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2905,6 +2905,45 @@ void test_cse_pure_loads_across_guards() {

void test_fold_clz_constant_select();

void test_simplifycfg_transitive_block_arguments() {
Context context;
Allocator allocator;
std::istringstream input(R"(section {
b0(%0: Ptr, %1: Bool):
Branch %1, true_block=b1, false_block=b3
b1:
Branch 0:Bool, true_block=b7, false_block=b2
b2:
Jump 0:Int64, block=b4
b3:
Jump 0:Int64, block=b4
b4(%2: Int64):
Jump %2, block=b5
b5(%3: Int64):
Branch %1, true_block=b6, false_block=b7
b6:
Store %0, %3, flags={}, aliasing=0, offset=0
Jump block=b7
b7:
Exit
})");
Section* section = SectionReader<>::read_section(context, allocator, input);
OrderBlocks::run(section, BlockOrdering::Natural);
unittest_assert(!section->verify(std::cerr));
SimplifyCFG::run(section);
unittest_assert(!section->verify(std::cerr));
for (uint64_t condition : {0, 1}) {
uint64_t output = 42;
Interpreter interpreter(section, {
Interpreter::Bits::constant(Type::Ptr, (uint64_t) &output),
Interpreter::Bits::constant(Type::Bool, condition)
});
interpreter.run_until(Interpreter::Event::Exit);
unittest_assert(output == (condition ? 0 : 42));
}
delete section;
}

int main(int argc, char** argv) {
metajit::LLVMCodeGen::initilize_llvm_jit();

Expand Down Expand Up @@ -4220,6 +4259,7 @@ b0(%0: Ptr):
});

suite.test("fold clz constant select").run(test_fold_clz_constant_select);
suite.test("simplifycfg transitive block arguments").run(test_simplifycfg_transitive_block_arguments);

return suite.finish();
}
Expand Down
Loading
0