[Clang][CodeGen] Fix bad codegen when building Clang with latest MSVC (#102681)

Before this PR, when using the latest MSVC `Microsoft (R) C/C++
Optimizing Compiler Version 19.40.33813 for x64` one of the Clang unit
test used to fail: `CodeGenObjC/gnustep2-direct-method.m`, see full
failure log:
[here](https://github.com/llvm/llvm-project/pull/100517#issuecomment-2266269490).

This PR temporarily shuffles around the code to make the MSVC inliner/
optimizer happy and avoid the bug.

MSVC bug report:
https://developercommunity.visualstudio.com/t/Bad-code-generation-when-building-LLVM-w/10719589?port=1025&fsid=e572244a-cde7-4d75-a73d-9b8cd94204dd
This commit is contained in:
Alexandre Ganea 2024-08-10 16:26:10 -04:00 committed by GitHub
parent fe31363a68
commit 2ba1cc8ea5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2092,10 +2092,15 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
auto *classStart =
llvm::StructType::get(PtrTy, PtrTy, PtrTy, LongTy, LongTy);
auto &astContext = CGM.getContext();
auto flags = Builder.CreateLoad(
Address{Builder.CreateStructGEP(classStart, selfValue, 4), LongTy,
CharUnits::fromQuantity(
astContext.getTypeAlign(astContext.UnsignedLongTy))});
// FIXME: The following few lines up to and including the call to
// `CreateLoad` were known to miscompile when MSVC 19.40.33813 is used
// to build Clang. When the bug is fixed in future MSVC releases, we
// should revert these lines to their previous state. See discussion in
// https://github.com/llvm/llvm-project/pull/102681
llvm::Value *Val = Builder.CreateStructGEP(classStart, selfValue, 4);
auto Align = CharUnits::fromQuantity(
astContext.getTypeAlign(astContext.UnsignedLongTy));
auto flags = Builder.CreateLoad(Address{Val, LongTy, Align});
auto isInitialized =
Builder.CreateAnd(flags, ClassFlags::ClassFlagInitialized);
llvm::BasicBlock *notInitializedBlock =