-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation #64605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation #64605
Comments
@llvm/issue-subscribers-clang-frontend |
This defect is not caused by fde5924. The reason is the elimination of llvm-project/clang/lib/Sema/TreeTransform.h Lines 11629 to 11631 in a5fe6c7
ImplicitCastExpr keeps FP options, including rounding direction. Removing them results in FPOptions loss, they are taken from the current Sema state, which is incorrect behavior.
Revert of fde5924 accidentally fixed the behavior, because FP options used to generate code were taken from Sema state (and nof from AST node) and they were the same as at the point of the template definition. If, for example, I will prepare a fix soon. |
FWIW, rc3 is supposed to go out in about a week and I believe we're hoping that's one of the last rcs, so we don't have much bake time for a fix. If the fix ends up being trivial and obviously correct, then I think it may still be able to make 17.x. I'm a bit more worried about it not being a trivial change though. It sounds like fde5924 accidentally exposed an issue that is causing more miscompiles and a revert of it would get us back to the same stability as Clang 16.x, even though it's not actually a "fix" per se. Am I correct? If so, we may want to consider doing that revert from the 17.x branch so we don't introduce more miscompilations and then do the real fix for 18.x with plenty of time for testing fallout. |
I put a patch https://reviews.llvm.org/D158158, which should fix the issue.It is not a full-fledged solution (it does not support variable templates), but it should be enough for this issue. It is small and obvious, hopefully this can facilitate the review. |
It seems variable templates have another layer of problems to begin with. Clang 16 will not generate a call to #include <float.h>
struct ChangeRounding {
ChangeRounding();
~ChangeRounding();
};
int foo(), bar();
#pragma STDC FENV_ACCESS ON
template <typename T>
int var = (ChangeRounding(), (T)0xFFFFFFFF != (T)0x100000000 ? foo() : bar());
int main() {
return var<float>;
} |
Those changes do seem quite simple and I think they're safe for a backport to 17.x, but I'd appreciate a few opinions from @hubert-reinterpretcast and @jcranmer-intel to see if they agree we should backport. The alternatives are: leave the regression in Clang 16, or revert the original changes from just the 17.x branch so we're back in the (previously also bad) state we were in for 16. |
I agree, and I think we should backport. The original changes fixed an issue in Clang 16 that was encountered by multiple parties (so reverting is not my first choice). With the additional changes, the "regressions" known to me are addressed. |
Excellent, thank you @hubert-reinterpretcast @spavloff, can you handle the backport for both commits to 17.x? (I think it's fine to squash them into one commit and only put up one PR) |
When an expression is instantiated, TreeTransform skips ImplicitCastExpr nodes, assuming they are recreated when the instantiated expression is built. It breaks functions that use non-default floating-point options, because they are kept in these ImplicitCastExprs. In this case the recreated ImplicitCastExpr takes FP options from the current Sema state and not from AST node. To fix this issue the FP options in Sema object are set when a compound statement is cloned in TreeTransform. This change fixes llvm#64605 ([Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation). Differential Revision: https://reviews.llvm.org/D158158 (cherry picked from commit 0baf85c) (cherry picked from commit 73e5a70)
/branch llvm/llvm-project-release-prs/issue64605 |
When an expression is instantiated, TreeTransform skips ImplicitCastExpr nodes, assuming they are recreated when the instantiated expression is built. It breaks functions that use non-default floating-point options, because they are kept in these ImplicitCastExprs. In this case the recreated ImplicitCastExpr takes FP options from the current Sema state and not from AST node. To fix this issue the FP options in Sema object are set when a compound statement is cloned in TreeTransform. This change fixes llvm/llvm-project#64605 ([Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation). Differential Revision: https://reviews.llvm.org/D158158 (cherry picked from commit 0baf85c331090fbe2d2b42214ed0664d55feb0b5)
/pull-request llvm/llvm-project-release-prs#627 |
Never mind, I thought about another issue. Yes this seems like a problem. @spavloff can you have a look? |
Hi, apologies in advance if this has already been addressed/mentioned elsewhere/is obvious, but in case it is helpful: I ran into this downstream and I think the failing test might just need the "implicit_instantiation" bit removed (or another commit brought in). IIUC, the ability to output this was added in 3bfafc4 which is present in main but not the release_17.x branch--template-64605.cpp was added after 3bfafc4 in main, so it expectedly has the "implicit_instantation" bit, but we don't know how to generate this in the release branch. Again though, apologies in advance if I'm misunderstanding something/this has already been handled! |
I think you nailed the problem! |
@jpenix-quic Thank you for your analysis! |
A check pattern in clang/test/SemaCXX/template-64605.cpp contains template specialization kind (the text "implicit_instantiation"). It does not need to be checked and can be safely removed. Presence of this text in the check pattern prevents from backporting some commits to the release branch: #64605. It has only recently been printed and the relevant commit is not present in the release/17.x branch.
/branch llvm/llvm-project-release-prs/issue64605 |
When an expression is instantiated, TreeTransform skips ImplicitCastExpr nodes, assuming they are recreated when the instantiated expression is built. It breaks functions that use non-default floating-point options, because they are kept in these ImplicitCastExprs. In this case the recreated ImplicitCastExpr takes FP options from the current Sema state and not from AST node. To fix this issue the FP options in Sema object are set when a compound statement is cloned in TreeTransform. This change fixes llvm/llvm-project#64605 ([Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation). Differential Revision: https://reviews.llvm.org/D158158 (cherry picked from commit 0baf85c331090fbe2d2b42214ed0664d55feb0b5)
A check pattern in clang/test/SemaCXX/template-64605.cpp contains template specialization kind (the text "implicit_instantiation"). It does not need to be checked and can be safely removed. Presence of this text in the check pattern prevents from backporting some commits to the release branch: llvm/llvm-project#64605. It has only recently been printed and the relevant commit is not present in the release/17.x branch. (cherry picked from commit 8859c644ede4898f90f77dcad2286de08a9ba62e)
/pull-request llvm/llvm-project-release-prs#666 |
When an expression is instantiated, TreeTransform skips ImplicitCastExpr nodes, assuming they are recreated when the instantiated expression is built. It breaks functions that use non-default floating-point options, because they are kept in these ImplicitCastExprs. In this case the recreated ImplicitCastExpr takes FP options from the current Sema state and not from AST node. To fix this issue the FP options in Sema object are set when a compound statement is cloned in TreeTransform. This change fixes llvm/llvm-project#64605 ([Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation). Differential Revision: https://reviews.llvm.org/D158158 (cherry picked from commit 0baf85c331090fbe2d2b42214ed0664d55feb0b5)
A check pattern in clang/test/SemaCXX/template-64605.cpp contains template specialization kind (the text "implicit_instantiation"). It does not need to be checked and can be safely removed. Presence of this text in the check pattern prevents from backporting some commits to the release branch: llvm/llvm-project#64605. It has only recently been printed and the relevant commit is not present in the release/17.x branch. (cherry picked from commit 8859c644ede4898f90f77dcad2286de08a9ba62e)
When an expression is instantiated, TreeTransform skips ImplicitCastExpr nodes, assuming they are recreated when the instantiated expression is built. It breaks functions that use non-default floating-point options, because they are kept in these ImplicitCastExprs. In this case the recreated ImplicitCastExpr takes FP options from the current Sema state and not from AST node. To fix this issue the FP options in Sema object are set when a compound statement is cloned in TreeTransform. This change fixes llvm#64605 ([Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation). Differential Revision: https://reviews.llvm.org/D158158
When an expression is instantiated, TreeTransform skips ImplicitCastExpr nodes, assuming they are recreated when the instantiated expression is built. It breaks functions that use non-default floating-point options, because they are kept in these ImplicitCastExprs. In this case the recreated ImplicitCastExpr takes FP options from the current Sema state and not from AST node. To fix this issue the FP options in Sema object are set when a compound statement is cloned in TreeTransform. This change fixes llvm#64605 ([Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation). Differential Revision: https://reviews.llvm.org/D158158
When an expression is instantiated, TreeTransform skips ImplicitCastExpr nodes, assuming they are recreated when the instantiated expression is built. It breaks functions that use non-default floating-point options, because they are kept in these ImplicitCastExprs. In this case the recreated ImplicitCastExpr takes FP options from the current Sema state and not from AST node. To fix this issue the FP options in Sema object are set when a compound statement is cloned in TreeTransform. This change fixes llvm#64605 ([Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation). Differential Revision: https://reviews.llvm.org/D158158
When an expression is instantiated, TreeTransform skips ImplicitCastExpr nodes, assuming they are recreated when the instantiated expression is built. It breaks functions that use non-default floating-point options, because they are kept in these ImplicitCastExprs. In this case the recreated ImplicitCastExpr takes FP options from the current Sema state and not from AST node. To fix this issue the FP options in Sema object are set when a compound statement is cloned in TreeTransform. This change fixes llvm#64605 ([Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation). Differential Revision: https://reviews.llvm.org/D158158
When an expression is instantiated, TreeTransform skips ImplicitCastExpr nodes, assuming they are recreated when the instantiated expression is built. It breaks functions that use non-default floating-point options, because they are kept in these ImplicitCastExprs. In this case the recreated ImplicitCastExpr takes FP options from the current Sema state and not from AST node. To fix this issue the FP options in Sema object are set when a compound statement is cloned in TreeTransform. This change fixes llvm#64605 ([Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation). Differential Revision: https://reviews.llvm.org/D158158
When an expression is instantiated, TreeTransform skips ImplicitCastExpr nodes, assuming they are recreated when the instantiated expression is built. It breaks functions that use non-default floating-point options, because they are kept in these ImplicitCastExprs. In this case the recreated ImplicitCastExpr takes FP options from the current Sema state and not from AST node. To fix this issue the FP options in Sema object are set when a compound statement is cloned in TreeTransform. This change fixes llvm#64605 ([Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation). Differential Revision: https://reviews.llvm.org/D158158
When an expression is instantiated, TreeTransform skips ImplicitCastExpr nodes, assuming they are recreated when the instantiated expression is built. It breaks functions that use non-default floating-point options, because they are kept in these ImplicitCastExprs. In this case the recreated ImplicitCastExpr takes FP options from the current Sema state and not from AST node. To fix this issue the FP options in Sema object are set when a compound statement is cloned in TreeTransform. This change fixes llvm#64605 ([Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation). Differential Revision: https://reviews.llvm.org/D158158
Consider:
Clang 16 generates the floating-point operations (as is appropriate for
FENV_ACCESS
beingON
):Clang "17" currently generates no floating-point operations:
Compiler Explorer: https://godbolt.org/z/PcoaaxzKn
Reverting fde5924 restores the previous behaviour.
The text was updated successfully, but these errors were encountered: