From d56481799fc569788e63432b82b71504ff43087c Mon Sep 17 00:00:00 2001 From: cjsrxzdyzds Date: Mon, 30 Nov 2026 16:22:34 -0500 Subject: [PATCH] AssumeVal: a well-defined assumption has side effects A range attribute on a noundef parameter becomes a well-defined AssumeVal, which triggers UB when the value is out of range. But AssumeVal::hasSideEffects() returned false, so the transform deleted it when the parameter was unused. A function that ignores such a parameter was then treated as defined for every argument, which gives a false counterexample when the target uses the parameter and misses a bug when the target narrows the range. Report side effects for well-defined assumptions, as for division. Assisted-by: Claude Code (Claude Opus 5.5) Co-Authored-By: Claude Opus 5.5 --- ir/instr.cpp | 4 +++- .../attrs/range-param-unused-narrow.srctgt.ll | 10 ++++++++++ tests/alive-tv/attrs/range-param-unused.srctgt.ll | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/alive-tv/attrs/range-param-unused-narrow.srctgt.ll create mode 100644 tests/alive-tv/attrs/range-param-unused.srctgt.ll diff --git a/ir/instr.cpp b/ir/instr.cpp index 27b036d02..b0d288c6a 100644 --- a/ir/instr.cpp +++ b/ir/instr.cpp @@ -3783,7 +3783,9 @@ bool AssumeVal::propagatesPoison() const { } bool AssumeVal::hasSideEffects() const { - return false; + // A well-defined assumption is UB when it fails, so it must stay even if its + // result is unused. + return is_welldefined; } DEFINE_AS_RETZERO(AssumeVal, getMaxGEPOffset) diff --git a/tests/alive-tv/attrs/range-param-unused-narrow.srctgt.ll b/tests/alive-tv/attrs/range-param-unused-narrow.srctgt.ll new file mode 100644 index 000000000..4e0846379 --- /dev/null +++ b/tests/alive-tv/attrs/range-param-unused-narrow.srctgt.ll @@ -0,0 +1,10 @@ +; ERROR: Source is more defined than target + +; Narrowing the range of an unused parameter makes tgt UB for 8 and 9. +define i8 @src(i8 noundef range(i8 4, 10) %x) { + ret i8 0 +} + +define i8 @tgt(i8 noundef range(i8 4, 8) %x) { + ret i8 0 +} diff --git a/tests/alive-tv/attrs/range-param-unused.srctgt.ll b/tests/alive-tv/attrs/range-param-unused.srctgt.ll new file mode 100644 index 000000000..b29c87098 --- /dev/null +++ b/tests/alive-tv/attrs/range-param-unused.srctgt.ll @@ -0,0 +1,14 @@ +; %x is unused in src, but a value outside the range is still UB there, so tgt +; may assume the range. +define i8 @src(i8 noundef range(i8 4, 8) %x) { + ret i8 0 +} + +define i8 @tgt(i8 noundef range(i8 4, 8) %x) { + %d = sub i8 %x, 4 + %in = icmp ult i8 %d, 4 + call void @llvm.assume(i1 %in) + ret i8 0 +} + +declare void @llvm.assume(i1)