This is an archive of the discontinued LLVM Phabricator instance.

[clang] Reset FP options before template instantiation
ClosedPublic

Authored by sepavloff on Jul 3 2023, 8:43 AM.

Details

Summary

AST nodes that may depend on FP options keep them as a difference
relative to the options outside the AST node. At the moment of
instantiation the FP options may be different from the default values,
defined by command-line option. In such case FP attributes would have
unexpected values. For example, the code:

template <class C> void func_01(int last, C) {
  func_01(last, int());
}
void func_02() { func_01(0, 1); }
#pragma STDC FENV_ACCESS ON

caused compiler crash, because template instantiation takes place at the
end of translation unit, where pragma STDC FENV_ACCESS is in effect. As
a result, code in the template instantiation would use constrained
intrinsics while the function does not have StrictFP attribute.

To solve this problem, FP attributes in Sema must be set to default
values, defined by command line options.

This change resolves https://github.com/llvm/llvm-project/issues/63542.

Diff Detail

Event Timeline

sepavloff created this revision.Jul 3 2023, 8:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 3 2023, 8:43 AM
sepavloff requested review of this revision.Jul 3 2023, 8:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 3 2023, 8:43 AM
zahiraam added inline comments.
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
5093

This seems to be fixing the crash.

clang/test/CodeGen/fp-template.cpp
28

Shouldn't we be expecting a constraint add here?

sepavloff added inline comments.Jul 12 2023, 9:19 AM
clang/test/CodeGen/fp-template.cpp
28

Yes, you are right. I incorrectly merged the fix to main branch.

Thank you!

LGTM. Thanks for the patch.

zahiraam accepted this revision.Jul 12 2023, 10:44 AM
This revision is now accepted and ready to land.Jul 12 2023, 10:44 AM
This revision was automatically updated to reflect the committed changes.

This patch seems to be the direct cause of a regression: https://github.com/llvm/llvm-project/issues/64605.