From 4a02732fc74605c1b8cd6b0a8dd98e33d52684f8 Mon Sep 17 00:00:00 2001 From: John Regehr Date: Wed, 11 Nov 2026 08:31:45 -0700 Subject: [PATCH] work --- ir/value.cpp | 10 +++--- ir/value.h | 2 +- .../alive-tv/aggregate-input-poison.srctgt.ll | 16 ++++++++++ .../alive-tv/aggregate-input-undef.srctgt.ll | 17 ++++++++++ .../alive-tv/aggregate-input-value.srctgt.ll | 13 ++++++++ tests/unit/vector/scalablevector1.opt | 2 ++ tools/transform.cpp | 32 +++++++++++++------ 7 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 tests/alive-tv/aggregate-input-poison.srctgt.ll create mode 100644 tests/alive-tv/aggregate-input-undef.srctgt.ll create mode 100644 tests/alive-tv/aggregate-input-value.srctgt.ll diff --git a/ir/value.cpp b/ir/value.cpp index b355c0aac..a531269b4 100644 --- a/ir/value.cpp +++ b/ir/value.cpp @@ -211,18 +211,19 @@ string Input::getSMTName(unsigned child) const { return smt_name; } -StateValue Input::mkInput(State &s, const Type &ty, unsigned child) const { +StateValue Input::mkInput(State &s, const Type &ty, unsigned &next_child) const { if (auto agg = ty.getAsAggregateType()) { vector vals; for (unsigned i = 0, e = agg->numElementsConst(); i < e; ++i) { if (agg->isPadding(i)) continue; - auto name = getSMTName(child + i); - vals.emplace_back(mkInput(s, agg->getChild(i), child + i)); + vals.emplace_back(mkInput(s, agg->getChild(i), next_child)); } return agg->aggregateVals(vals); } + // Number non-padding scalar leaves in depth-first order. + unsigned child = next_child++; expr val; if (hasAttribute(ParamAttrs::ByVal)) { unsigned bid; @@ -275,7 +276,8 @@ bool Input::isUndefMask(const expr &e) { } StateValue Input::toSMT(State &s) const { - return mkInput(s, getType(), 0); + unsigned next_child = 0; + return mkInput(s, getType(), next_child); } void Input::merge(const ParamAttrs &other) { diff --git a/ir/value.h b/ir/value.h index b03bdfee6..9d191a472 100644 --- a/ir/value.h +++ b/ir/value.h @@ -119,7 +119,7 @@ class Input final : public Value { std::string smt_name; ParamAttrs attrs; std::string getSMTName(unsigned child) const; - StateValue mkInput(State &s, const Type &ty, unsigned child) const; + StateValue mkInput(State &s, const Type &ty, unsigned &next_child) const; public: Input(Type &type, std::string &&name); void setAttributes(ParamAttrs &&new_attrs); diff --git a/tests/alive-tv/aggregate-input-poison.srctgt.ll b/tests/alive-tv/aggregate-input-poison.srctgt.ll new file mode 100644 index 000000000..d43e05c86 --- /dev/null +++ b/tests/alive-tv/aggregate-input-poison.srctgt.ll @@ -0,0 +1,16 @@ +; TEST-ARGS: -disable-undef-input +; ERROR: Target is more poisonous than source + +define i32 @src({ <2 x i32>, <2 x i32> } %x) { + %a = extractvalue { <2 x i32>, <2 x i32> } %x, 0 + %v = extractelement <2 x i32> %a, i32 1 + %r = mul i32 %v, 0 + ret i32 %r +} + +define i32 @tgt({ <2 x i32>, <2 x i32> } %x) { + %a = extractvalue { <2 x i32>, <2 x i32> } %x, 1 + %v = extractelement <2 x i32> %a, i32 0 + %r = mul i32 %v, 0 + ret i32 %r +} diff --git a/tests/alive-tv/aggregate-input-undef.srctgt.ll b/tests/alive-tv/aggregate-input-undef.srctgt.ll new file mode 100644 index 000000000..b20900a12 --- /dev/null +++ b/tests/alive-tv/aggregate-input-undef.srctgt.ll @@ -0,0 +1,17 @@ +; TEST-ARGS: -disable-poison-input +; ERROR: Target's return value is more undefined + +; Include an empty aggregate and alignment padding before the nested array. +%T = type { {}, i8, [2 x <2 x i32>] } + +define i32 @src(%T %x) { + %a = extractvalue %T %x, 2, 0 + %v = extractelement <2 x i32> %a, i32 1 + ret i32 %v +} + +define i32 @tgt(%T %x) { + %a = extractvalue %T %x, 2, 1 + %v = extractelement <2 x i32> %a, i32 0 + ret i32 %v +} diff --git a/tests/alive-tv/aggregate-input-value.srctgt.ll b/tests/alive-tv/aggregate-input-value.srctgt.ll new file mode 100644 index 000000000..b7874fc27 --- /dev/null +++ b/tests/alive-tv/aggregate-input-value.srctgt.ll @@ -0,0 +1,13 @@ +; ERROR: Value mismatch + +define i32 @src({ <2 x i32>, <2 x i32> } noundef %x) { + %a = extractvalue { <2 x i32>, <2 x i32> } %x, 0 + %v = extractelement <2 x i32> %a, i32 1 + ret i32 %v +} + +define i32 @tgt({ <2 x i32>, <2 x i32> } noundef %x) { + %a = extractvalue { <2 x i32>, <2 x i32> } %x, 1 + %v = extractelement <2 x i32> %a, i32 0 + ret i32 %v +} diff --git a/tests/unit/vector/scalablevector1.opt b/tests/unit/vector/scalablevector1.opt index fdfa22135..c9e69f7a0 100644 --- a/tests/unit/vector/scalablevector1.opt +++ b/tests/unit/vector/scalablevector1.opt @@ -1,3 +1,5 @@ +; TEST-ARGS: -disable-undef-input + Name: scalable vector add - identity %r = add %a, %b => diff --git a/tools/transform.cpp b/tools/transform.cpp index 52e8493ea..390e16e5e 100644 --- a/tools/transform.cpp +++ b/tools/transform.cpp @@ -90,11 +90,11 @@ static void print_single_varval(ostream &os, State &st, const Model &m, } } -void tools::print_model_val(ostream &os, State &st, const Model &m, - const Value *var, const Type &type, - const StateValue &val, unsigned child) { +static void print_model_val_impl(ostream &os, State &st, const Model &m, + const Value *var, const Type &type, + const StateValue &val, unsigned &next_child) { if (!type.isAggregateType()) { - print_single_varval(os, st, m, var, type, val, child); + print_single_varval(os, st, m, var, type, val, next_child++); return; } @@ -103,12 +103,20 @@ void tools::print_model_val(ostream &os, State &st, const Model &m, for (unsigned i = 0, e = agg->numElementsConst(); i < e; ++i) { if (i != 0) os << ", "; - tools::print_model_val(os, st, m, var, agg->getChild(i), - agg->extract(val, i), child + i); + unsigned padding_child = 0; + print_model_val_impl(os, st, m, agg->isPadding(i) ? nullptr : var, + agg->getChild(i), agg->extract(val, i), + agg->isPadding(i) ? padding_child : next_child); } os << (type.isStructType() ? " }" : " >"); } +void tools::print_model_val(ostream &os, State &st, const Model &m, + const Value *var, const Type &type, + const StateValue &val, unsigned child) { + print_model_val_impl(os, st, m, var, type, val, child); +} + using print_var_val_ty = function; @@ -363,15 +371,17 @@ static bool error(Errors &errs, State &src_state, State &tgt_state, static void instantiate_undef(const Input *in, map &instances, - const Type &ty, unsigned child) { + const Type &ty, unsigned &next_child) { if (auto agg = ty.getAsAggregateType()) { for (unsigned i = 0, e = agg->numElementsConst(); i < e; ++i) { if (!agg->isPadding(i)) - instantiate_undef(in, instances, agg->getChild(i), child + i); + instantiate_undef(in, instances, agg->getChild(i), next_child); } return; } + unsigned child = next_child++; + // Bail out if it gets too big. It's unlikely we can solve it anyway. if (instances.size() >= 128 || hit_half_memory_limit()) return; @@ -450,8 +460,10 @@ static expr preprocess(const Transform &t, const set &qvars0, map instances({ { std::move(e), true } }); for (auto &i : t.src.getInputs()) { - if (auto in = dynamic_cast(&i)) - instantiate_undef(in, instances, i.getType(), 0); + if (auto in = dynamic_cast(&i)) { + unsigned next_child = 0; + instantiate_undef(in, instances, i.getType(), next_child); + } } expr insts(false);