[llvm] [llvm] Fix __builtin_object_size interaction between Negative Offset … (PR #111827)

Harald van Dijk via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 26 11:43:49 PDT 2024


hvdijk wrote:

I did write in my previous comment:

> However, if `ExactSizeFromOffset` is restored to handle that case, it then becomes important to ensure that nothing calls `ObjectSizeOffsetVisitor::compute` with `Options.EvalMode = ObjectSizeOpts::Mode::ExactSizeFromOffset` if actually it does care about the offset. Yet `ObjectSizeOffsetEvaluator::compute_` does exactly that.

The result is that this latest iteration does not handle my test case from https://github.com/llvm/llvm-project/pull/111827#issuecomment-2419260982: that now prints 0 again, but should not.

The possible approach I mentioned in my previous comment to use a different `EvalMode` is simple to do:
```diff
--- a/llvm/lib/Analysis/MemoryBuiltins.cpp
+++ b/llvm/lib/Analysis/MemoryBuiltins.cpp
@@ -1083,7 +1083,9 @@ SizeOffsetValue ObjectSizeOffsetEvaluator::compute(Value *V) {
 }
 
 SizeOffsetValue ObjectSizeOffsetEvaluator::compute_(Value *V) {
-  ObjectSizeOffsetVisitor Visitor(DL, TLI, Context, EvalOpts);
+  ObjectSizeOpts VisitorEvalOpts(EvalOpts);
+  VisitorEvalOpts.EvalMode = ObjectSizeOpts::Mode::ExactUnderlyingSizeAndOffset;
+  ObjectSizeOffsetVisitor Visitor(DL, TLI, Context, VisitorEvalOpts);
   SizeOffsetAPInt Const = Visitor.compute(V);
   if (Const.bothKnown())
     return SizeOffsetValue(ConstantInt::get(Context, Const.Size),
```
This does also need `builtin-object-size-phi.ll` to be updated, but I think the effect that it has on that is correct, I think that adequately tests that the bug is fixed.

https://github.com/llvm/llvm-project/pull/111827


More information about the llvm-commits mailing list