Skip to content

Assertion failed: ((CGF.CurFuncDecl == nullptr || CGF.Builder.getIsFPConstrained() || isa<CXXConstructorDecl>(CGF.CurFuncDecl) || isa<CXXDestructorDecl>(CGF.CurFuncDecl) || (NewExceptionBehavior == llvm::fp::ebIgnore && NewRoundingBehavior == llvm::RoundingMode::NearestTiesToEven)) && "FPConstrained should be enabled on entire function"), function ConstructorHelper, file /usr/src/contrib/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp, line 165. #63542

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

Closed
pkubaj opened this issue Jun 27, 2023 · 14 comments
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. crash Prefer [crash-on-valid] or [crash-on-invalid] floating-point Floating-point math

Comments

@pkubaj
Copy link
Contributor

pkubaj commented Jun 27, 2023

FreeBSD 14.0-CURRENT
LLVM 16.0.6
powerpc64le, powerpc64, powerpc

Reproducer:

template <class Compare> void stable_sort(int last, Compare) {
  stable_sort(last, int());
}
#pragma STDC FENV_ACCESS ON
struct cluster_result {
  int operator[](int);
};
template <int> void generate_SciPy_dendrogram() {
  cluster_result Z2;
  int trans_tmp_1(0);
  int trans_tmp_2 = Z2[1];
  stable_sort(trans_tmp_1, trans_tmp_2);
}
void linkage_wrap() { generate_SciPy_dendrogram<false>; }

Build with:

/usr/bin/c++ -cc1 -triple powerpc-unknown-freebsd14.0 -emit-obj fastcluster_python-684654.cpp

Output:

Assertion failed: ((CGF.CurFuncDecl == nullptr || CGF.Builder.getIsFPConstrained() || isa<CXXConstructorDecl>(CGF.CurFuncDecl) || isa<CXXDestructorDecl>(CGF.CurFuncDecl) || (NewExceptionBehavior == llvm::fp::ebIgnore && NewRoundingBehavior == llvm::RoundingMode::NearestTiesToEven)) && "FPConstrained should be enabled on entire function"), function ConstructorHelper, file /usr/src/contrib/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp, line 165.
@VoxSciurorum
Copy link

I confirmed this behavior with the system compiler on FreeBSD 14.0-CURRENT on amd64 and aarch64 as well. The ports build of llvm16 does not crash, despite being based on a very similar version of llvm.

@pkubaj
Copy link
Contributor Author

pkubaj commented Jun 27, 2023

LLVM in ports has assertions disabled, which is probably the reason.

@VoxSciurorum
Copy link

LLVM in ports has assertions disabled, which is probably the reason.

I think you are right about that. I also reproduced the crash on amd64 Linux using a build with assertions enabled.

@EugeneZelenko EugeneZelenko added llvm:codegen crash Prefer [crash-on-valid] or [crash-on-invalid] and removed new issue labels Jun 27, 2023
@pkubaj pkubaj changed the title [PowerPC] Assertion failed: ((CGF.CurFuncDecl == nullptr || CGF.Builder.getIsFPConstrained() || isa<CXXConstructorDecl>(CGF.CurFuncDecl) || isa<CXXDestructorDecl>(CGF.CurFuncDecl) || (NewExceptionBehavior == llvm::fp::ebIgnore && NewRoundingBehavior == llvm::RoundingMode::NearestTiesToEven)) && "FPConstrained should be enabled on entire function"), function ConstructorHelper, file /usr/src/contrib/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp, line 165. Assertion failed: ((CGF.CurFuncDecl == nullptr || CGF.Builder.getIsFPConstrained() || isa<CXXConstructorDecl>(CGF.CurFuncDecl) || isa<CXXDestructorDecl>(CGF.CurFuncDecl) || (NewExceptionBehavior == llvm::fp::ebIgnore && NewRoundingBehavior == llvm::RoundingMode::NearestTiesToEven)) && "FPConstrained should be enabled on entire function"), function ConstructorHelper, file /usr/src/contrib/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp, line 165. Jun 27, 2023
@DimitryAndric DimitryAndric added the floating-point Floating-point math label Jun 28, 2023
@DimitryAndric
Copy link
Collaborator

This regressed with 65cf77d by @spavloff (cc @rjmccall).

spavloff added a commit that referenced this issue Jun 28, 2023
@spavloff
Copy link
Collaborator

spavloff commented Jul 5, 2023

The reproducer provided here is not related to 65cf77d, because it causes compiler crash with that commit reverted.

The problem reported here is fixed by the patch https://reviews.llvm.org/D154359.

spavloff added a commit that referenced this issue Jul 5, 2023
This is recommit of 98390cc, reverted
in 82a3969, because it caused
#63542. Although the problem
described in the issue is independent of the reverted patch, fail of
PCH/late-parsed-instantiations.cpp indeed obseved on PowerPC and is
likely to be caused by wrong serialization of `LateParsedTemplate`
objects. In this patch the serialization is fixed.

Original commit message is below.

Previously function template instantiations occurred with FP options
that were in effect at the end of translation unit. It was a problem
for late template parsing as these FP options were used as attributes of
AST nodes and may result in crash. To fix it FP options are set to the
state of the point of template definition.

Differential Revision: https://reviews.llvm.org/D143241
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed llvm:codegen labels Jul 13, 2023
@llvmbot
Copy link
Member

llvmbot commented Jul 13, 2023

@llvm/issue-subscribers-clang-frontend

@pkubaj
Copy link
Contributor Author

pkubaj commented Jul 13, 2023

Would it be possible to merge it to 16?

@pkubaj
Copy link
Contributor Author

pkubaj commented Jul 13, 2023

@DimitryAndric
Also, a backport to FreeBSD would be well appreciated :)

freebsd-git pushed a commit to freebsd/freebsd-src that referenced this issue Jul 13, 2023
  [clang] Reset FP options before template instantiation

  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 llvm/llvm-project#63542.

  Differential Revision: https://reviews.llvm.org/D154359

Requested by:	pkubaj
PR:		265755, 265758
MFC after:	1 month
@DimitryAndric
Copy link
Collaborator

I think clang 17 is going to be branched soon, so 16 is a bit "old" already... In any case I have merged it into FreeBSD in freebsd/freebsd-src@1cd9788.

freebsd-git pushed a commit to freebsd/freebsd-ports that referenced this issue Jul 14, 2023
The upstream commit fde5924dcc69 has been merged to llvm in the FreeBSD
main branch so merge it here as well.

Issue: llvm/llvm-project#63542
freebsd-git pushed a commit to freebsd/freebsd-ports that referenced this issue Jul 20, 2023
The upstream commit fde5924dcc69 has been merged to llvm in the FreeBSD
main branch so merge it here as well.

Issue: llvm/llvm-project#63542
(cherry picked from commit 2a9d785)
freebsd-git pushed a commit to freebsd/freebsd-src that referenced this issue Jul 23, 2023
  [clang] Reset FP options before template instantiation

  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 llvm/llvm-project#63542.

  Differential Revision: https://reviews.llvm.org/D154359

Requested by:	pkubaj
PR:		265755, 265758
MFC after:	1 month

(cherry picked from commit 1cd9788)
@hubert-reinterpretcast
Copy link
Collaborator

The bad codegen reported in #63063 (comment) still occurs with this fix.

@hubert-reinterpretcast
Copy link
Collaborator

@spavloff, I have opened a regression defect against the fix: #64605
@DimitryAndric, fyi, in case you want to revert this out of FreeBSD.

@EugeneZelenko EugeneZelenko added clang:codegen IR generation bugs: mangling, exceptions, etc. and removed clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 10, 2023
@llvmbot
Copy link
Member

llvmbot commented Aug 10, 2023

@llvm/issue-subscribers-clang-codegen

@spavloff
Copy link
Collaborator

As I commented in #64605, this problem is not caused by fde5924, and revert of this commit does not fix the issue.

@hubert-reinterpretcast
Copy link
Collaborator

I opened #64823 for a different case that hits the same assertion.

bsdjhb pushed a commit to bsdjhb/cheribsd that referenced this issue Sep 6, 2023
  [clang] Reset FP options before template instantiation

  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 llvm/llvm-project#63542.

  Differential Revision: https://reviews.llvm.org/D154359

Requested by:	pkubaj
PR:		265755, 265758
MFC after:	1 month
veselypeta pushed a commit to veselypeta/cherillvm that referenced this issue Sep 3, 2024
This is recommit of 98390cc, reverted
in 82a3969, because it caused
llvm/llvm-project#63542. Although the problem
described in the issue is independent of the reverted patch, fail of
PCH/late-parsed-instantiations.cpp indeed obseved on PowerPC and is
likely to be caused by wrong serialization of `LateParsedTemplate`
objects. In this patch the serialization is fixed.

Original commit message is below.

Previously function template instantiations occurred with FP options
that were in effect at the end of translation unit. It was a problem
for late template parsing as these FP options were used as attributes of
AST nodes and may result in crash. To fix it FP options are set to the
state of the point of template definition.

Differential Revision: https://reviews.llvm.org/D143241
veselypeta pushed a commit to veselypeta/cherillvm that referenced this issue Sep 4, 2024
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 llvm/llvm-project#63542.

Differential Revision: https://reviews.llvm.org/D154359
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. crash Prefer [crash-on-valid] or [crash-on-invalid] floating-point Floating-point math
Projects
None yet
Development

No branches or pull requests

7 participants