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

Alexandre Ganea via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 9 13:44:59 PDT 2024


https://github.com/aganea created https://github.com/llvm/llvm-project/pull/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 seems to have been introduced by https://github.com/llvm/llvm-project/commit/c9e5af3944e85c5f1272c48522b4e9eda398b462 however further inspection shows that commit only triggers a bug in the MSVC compiler.

It seems that the symptom is bad alignement generated in one of the load instructions:
```
huge alignment values are unsupported
  %2 = load i64, ptr %1, align 9223372036854775808
```
When `Builder.CreateLoad` is called [here](https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CGObjCGNU.cpp#L2096), somehow [this call](https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CGBuilder.h#L110) to `Addr.getAlignment().getAsAlign()` returns a bad alignement. The problem occurs at the highlighted line in the screenshot (`sub bh,cl`):

![Screenshot 2024-08-09 154835](https://github.com/user-attachments/assets/48a9a0a9-39f0-4d8e-bc14-77fe1de13e59)

The code line on the right is translated to the assembly on the right. `llvm::count_zero` returns a proper value (as seen in `rcx`), however `sub bh, cl` uses a bad constant in `bh` (it is not 63 as expected). I think the optimizer meant to use `dil` not `bh`. A few lines below it does `mov byte ptr [rsp + 40h], dil`. If after `sub` is executed I manually set 6 in `rdi`, as it should have been, the test passes.

I'll fix a bug with Microsoft will cross post it here.

>From 16efda65bb3d30b67b3215d7e8d69a105e2d771c Mon Sep 17 00:00:00 2001
From: Alexandre Ganea <aganea at havenstudios.com>
Date: Fri, 9 Aug 2024 16:32:09 -0400
Subject: [PATCH] [Clang][CodeGen] Fix a bad codegen when building Clang with
 latest MSVC

---
 clang/lib/CodeGen/CGObjCGNU.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 948b10954ebbed..56a3ed1b87b35f 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -2092,10 +2092,10 @@ 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))});
+        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 =



More information about the cfe-commits mailing list