Comment by pizlonator

1 day ago

Inlining doesn’t require UB

I didn't claim that. What I mean is that if a pointer escapes into an inlined function and no further, it will still prevent further optimizations if we apply your rule that only non-escaping locals don't get addresses. The main benefit of inlining is that it is effectively a simple way to do interprocedurally optimizations. I.e.

  inline void add(int* to, int what) { *to += what; }
  void foo();
  void bar() {
      int x = 0;
      add(&x, 1);
      foo();
      return x;
  }

By your rules, optimizing bar to return the constant 1 would not be allowed.

  • I think you’re applying a very strange strawman definition to “nonescaping”. It’s certainly not the definition I would pick.

    The right definition is probably something like:

    - pointers that come out of the outside world (syscalls) are escaped. They are just integers.

    - pointers to locals have provenance. They point to an abstract location. It is up to the implementation to decide when the location gets an integer value (is in an actual address) and what that value is. The implementation must do this no later than when the pointer to the local escapes.

    - pointer values passed to the outside world (syscalls) escape.

    - pointer values stored in escaped memory also escape, transitively

    That’s one possible definition that turns the UB into implementation defined behavior. I’m sure there are others

    • I think you have a non-standard definition. An escaping pointer is an address that the compiler cannot fully track (directly or indirectly). It could be to a syscall, it could be a separately compiled function (without LTO), it could even be to a function in the same translation unit if the compiler cannot inline that function nor do sufficient intraprocedural analysis.

      Again, I'm not a compiler writer, but my understanding is that non escaping variables can be optimized in SSA form, escaped variables are otherwise treated as memory and the compiler must be significantly more conservative.

      In any case, whether a pointer escapes or not depends purely on the compiler capabilities and optimization level, so it would not be sane making a code well defined or UB depending on the compiler or optimization level.

      edit: to be more concrete, do you think that in my example the constant folding of the return into return 1 should be allowed? And if so, which variant of this code would prevent the optimization and why?

      1 reply →