You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Good(), Clang correctly performs constant propagation on the local i, even though the pointer escapes, because i is a const object that cannot be modified by ExternFunc() without invoking UB.
In Bad(), we have basically the same scenario, except in this case Clang does not perform the constant propagation. pi is not merely a const pointer, it is a pointer to a const object, and therefore the value it points to is guaranteed to be stable even across a function call where the pointer escapes. But Clang doesn't seem to realize this, and re-loads i after the function call, even though i is a const object.
The text was updated successfully, but these errors were encountered:
The harm in this example is relatively minor (one extra load), but in my actual code I was relying on constant propagation to eliminate many other branches that test the value of *pi. So this impact of this missed optimization on my code is rather large.
As a workaround in your code, you could make the constant "static const".
If you look at the LLVM IR generated by clang, in the "good" case, the constant is actually evaluated by the frontend. In the "bad" case, it isn't, and clang doesn't currently try to represent the fact that the variable is const in LLVM IR. (llvm.invariant.start might be usable for this.)
Repro:
Output from trunk (Godbolt link: https://godbolt.org/z/KT8dPM9ac):
In
Good()
, Clang correctly performs constant propagation on the locali
, even though the pointer escapes, becausei
is aconst
object that cannot be modified byExternFunc()
without invoking UB.In
Bad()
, we have basically the same scenario, except in this case Clang does not perform the constant propagation.pi
is not merely a const pointer, it is a pointer to a const object, and therefore the value it points to is guaranteed to be stable even across a function call where the pointer escapes. But Clang doesn't seem to realize this, and re-loadsi
after the function call, even thoughi
is a const object.The text was updated successfully, but these errors were encountered: