Skip to content
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

[clang] Correct behavior of LLVM_UNREACHABLE_OPTIMIZE=OFF for Release builds #68284

Merged
merged 4 commits into from
Oct 5, 2023
Merged

[clang] Correct behavior of LLVM_UNREACHABLE_OPTIMIZE=OFF for Release builds #68284

merged 4 commits into from
Oct 5, 2023

Conversation

MasterAwesome
Copy link
Contributor

@MasterAwesome MasterAwesome commented Oct 5, 2023

Codegen

Before

AArch64SVEPcsAttr *AArch64SVEPcsAttr::CreateImplicit(ASTContext &Ctx, SourceRange Range, Spelling S) {
  AttributeCommonInfo I(Range, NoSemaHandlerAttribute, (
    S == GNU_aarch64_sve_pcs ? AttributeCommonInfo::Form{AttributeCommonInfo::AS_GNU, GNU_aarch64_sve_pcs, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/} :
    S == CXX11_clang_aarch64_sve_pcs ? AttributeCommonInfo::Form{AttributeCommonInfo::AS_CXX11, CXX11_clang_aarch64_sve_pcs, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/} :
    S == C23_clang_aarch64_sve_pcs ? AttributeCommonInfo::Form{AttributeCommonInfo::AS_C23, C23_clang_aarch64_sve_pcs, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/} :
    (llvm_unreachable("Unknown attribute spelling!"),  AttributeCommonInfo::Form{AttributeCommonInfo::AS_GNU, 0, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/})));
  return CreateImplicit(Ctx, I);
}

After

AArch64SVEPcsAttr *AArch64SVEPcsAttr::CreateImplicit(ASTContext &Ctx, SourceRange Range, Spelling S) {
  AttributeCommonInfo I(Range, NoSemaHandlerAttribute, [&]() {
    switch (S) {
    case GNU_aarch64_sve_pcs:
      return AttributeCommonInfo::Form{AttributeCommonInfo::AS_GNU, GNU_aarch64_sve_pcs, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/};
    case CXX11_clang_aarch64_sve_pcs:
      return AttributeCommonInfo::Form{AttributeCommonInfo::AS_CXX11, CXX11_clang_aarch64_sve_pcs, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/};
    case C23_clang_aarch64_sve_pcs:
      return AttributeCommonInfo::Form{AttributeCommonInfo::AS_C23, C23_clang_aarch64_sve_pcs, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/};
    default:
      llvm_unreachable("Unknown attribute spelling!");
      return AttributeCommonInfo::Form{AttributeCommonInfo::AS_GNU, 0, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/};
    }
  }());
  return CreateImplicit(Ctx, I);
}

Fixes #68237

@MasterAwesome MasterAwesome marked this pull request as ready for review October 5, 2023 06:31
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Oct 5, 2023
@MasterAwesome MasterAwesome changed the title Correct behavior of LLVM_UNREACHABLE_OPTIMIZE=OFF for Release builds [clang] Correct behavior of LLVM_UNREACHABLE_OPTIMIZE=OFF for Release builds Oct 5, 2023
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, that bug has been around for quite a while. I like your solution to it!

The code LGTM, no tests required because there isn't really a decent way to test this from lit. However, it would be nice to add a release note to clang/docs/ReleateNotes.rst to tell users that the bug was fixed. I'd add the note to the Bug Fixes in This Version section in the document.

@erichkeane
Copy link
Collaborator

Agree with Aaron on all points!

When LLVM_UNREACHABLE_OPTIMIZE is turned off during release mode, a
`do { BUILTIN_TRAP; BUILTIN_UNREACHABLE; } while(0)` is emitted this
causes the ternary operator to not work as expected. Correct this
behavior such that it works in all modes.

Tests:
 * LLVM_UNREACHABLE_OPTIMIZE=[ON|OFF] works in both `Release` and
   `Debug` modes
 * Lambdas on release mode are inlined / similar lambdas are merged.

Signed-off-by: Arvind Mukund <[email protected]>
Signed-off-by: Arvind Mukund <[email protected]>
@MasterAwesome
Copy link
Contributor Author

MasterAwesome commented Oct 5, 2023

@erichkeane @AaronBallman, I've added the release notes and rebased the change on main. Does this need to be backported to 17.x? 16.x doesn't have this issue

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo Erich's comment on the release notes.

@MasterAwesome
Copy link
Contributor Author

MasterAwesome commented Oct 5, 2023

@AaronBallman, @erichkeane; whenever ya'll are done with the review, can you merge this in? I don't think I have access. And about the backporting, do we need it for 17.0.x bugfix releases?

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AaronBallman
Copy link
Collaborator

@erichkeane @AaronBallman, I've added the release notes and rebased the change on main. Does this need to be backported to 17.x? 16.x doesn't have this issue

Yeah, I think we should probably backport this to 17.x. We have instructions on how to kick that off here: https://www.llvm.org/docs/GitHub.html#backporting-fixes-to-the-release-branches but I can get the ball rolling by adding the magic comment to the issue.

@AaronBallman AaronBallman merged commit bffbe9a into llvm:main Oct 5, 2023
@MasterAwesome
Copy link
Contributor Author

@erichkeane @AaronBallman, I've added the release notes and rebased the change on main. Does this need to be backported to 17.x? 16.x doesn't have this issue

Yeah, I think we should probably backport this to 17.x. We have instructions on how to kick that off here: https://www.llvm.org/docs/GitHub.html#backporting-fixes-to-the-release-branches but I can get the ball rolling by adding the magic comment to the issue.

Thanks for doing it! I'll work on cherry-picking this to Rust's fork of llvm :)

@AaronBallman
Copy link
Collaborator

@erichkeane @AaronBallman, I've added the release notes and rebased the change on main. Does this need to be backported to 17.x? 16.x doesn't have this issue

Yeah, I think we should probably backport this to 17.x. We have instructions on how to kick that off here: https://www.llvm.org/docs/GitHub.html#backporting-fixes-to-the-release-branches but I can get the ball rolling by adding the magic comment to the issue.

Thanks for doing it! I'll work on cherry-picking this to Rust's fork of llvm :)

You're welcome, but it seems there's a problem with trying to cherry-pick this over to 17.x and the pick will be more involved because of merge conflicts with the release notes. Would you mind taking over the pick to 17.x? If not, that's okay! (I'm mostly asking because I'm pretty swamped with reviews and I don't want the backport to fall through the cracks by accident.)

@MasterAwesome
Copy link
Contributor Author

@erichkeane @AaronBallman, I've added the release notes and rebased the change on main. Does this need to be backported to 17.x? 16.x doesn't have this issue

Yeah, I think we should probably backport this to 17.x. We have instructions on how to kick that off here: https://www.llvm.org/docs/GitHub.html#backporting-fixes-to-the-release-branches but I can get the ball rolling by adding the magic comment to the issue.

Thanks for doing it! I'll work on cherry-picking this to Rust's fork of llvm :)

You're welcome, but it seems there's a problem with trying to cherry-pick this over to 17.x and the pick will be more involved because of merge conflicts with the release notes. Would you mind taking over the pick to 17.x? If not, that's okay! (I'm mostly asking because I'm pretty swamped with reviews and I don't want the backport to fall through the cracks by accident.)

I gotchu! The PR created by llvmbot is at llvm/llvm-project-release-prs#726

tru pushed a commit that referenced this pull request Oct 10, 2023
…ase` builds (#68284)

```c++
AArch64SVEPcsAttr *AArch64SVEPcsAttr::CreateImplicit(ASTContext &Ctx, SourceRange Range, Spelling S) {
  AttributeCommonInfo I(Range, NoSemaHandlerAttribute, (
    S == GNU_aarch64_sve_pcs ? AttributeCommonInfo::Form{AttributeCommonInfo::AS_GNU, GNU_aarch64_sve_pcs, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/} :
    S == CXX11_clang_aarch64_sve_pcs ? AttributeCommonInfo::Form{AttributeCommonInfo::AS_CXX11, CXX11_clang_aarch64_sve_pcs, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/} :
    S == C23_clang_aarch64_sve_pcs ? AttributeCommonInfo::Form{AttributeCommonInfo::AS_C23, C23_clang_aarch64_sve_pcs, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/} :
    (llvm_unreachable("Unknown attribute spelling!"),  AttributeCommonInfo::Form{AttributeCommonInfo::AS_GNU, 0, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/})));
  return CreateImplicit(Ctx, I);
}
```

```c++
AArch64SVEPcsAttr *AArch64SVEPcsAttr::CreateImplicit(ASTContext &Ctx, SourceRange Range, Spelling S) {
  AttributeCommonInfo I(Range, NoSemaHandlerAttribute, [&]() {
    switch (S) {
    case GNU_aarch64_sve_pcs:
      return AttributeCommonInfo::Form{AttributeCommonInfo::AS_GNU, GNU_aarch64_sve_pcs, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/};
    case CXX11_clang_aarch64_sve_pcs:
      return AttributeCommonInfo::Form{AttributeCommonInfo::AS_CXX11, CXX11_clang_aarch64_sve_pcs, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/};
    case C23_clang_aarch64_sve_pcs:
      return AttributeCommonInfo::Form{AttributeCommonInfo::AS_C23, C23_clang_aarch64_sve_pcs, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/};
    default:
      llvm_unreachable("Unknown attribute spelling!");
      return AttributeCommonInfo::Form{AttributeCommonInfo::AS_GNU, 0, false /*IsAlignas*/, false /*IsRegularKeywordAttribute*/};
    }
  }());
  return CreateImplicit(Ctx, I);
}
```

Fixes #68237
Conflicts:
	clang/docs/ReleaseNotes.rst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot build obj.clangAST when LLVM_UNREACHABLE_OPTIMIZATIONS are turned off
4 participants