[clang] [clang] Correct behavior of `LLVM_UNREACHABLE_OPTIMIZE=OFF` for `Release` builds (PR #68284)

Arvind Mukund via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 5 09:55:10 PDT 2023


https://github.com/MasterAwesome updated https://github.com/llvm/llvm-project/pull/68284

>From 8598d5f4cfad4f3887ec40b7c3d21e385f731d9d Mon Sep 17 00:00:00 2001
From: Arvind Mukund <armu30 at gmail.com>
Date: Wed, 4 Oct 2023 22:25:08 -0700
Subject: [PATCH 1/3] Correct unreachable hint in release mode

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 <armu30 at gmail.com>
---
 clang/utils/TableGen/ClangAttrEmitter.cpp | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp
index ffada02ac4d30a5..6a821463aa9f0fe 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -2690,7 +2690,8 @@ static void emitAttributes(RecordKeeper &Records, raw_ostream &OS,
         OS << ", ";
         emitFormInitializer(OS, Spellings[0], "0");
       } else {
-        OS << ", (\n";
+        OS << ", [&]() {\n";
+        OS << "    switch (S) {\n";
         std::set<std::string> Uniques;
         unsigned Idx = 0;
         for (auto I = Spellings.begin(), E = Spellings.end(); I != E;
@@ -2698,15 +2699,19 @@ static void emitAttributes(RecordKeeper &Records, raw_ostream &OS,
           const FlattenedSpelling &S = *I;
           const auto &Name = SemanticToSyntacticMap[Idx];
           if (Uniques.insert(Name).second) {
-            OS << "    S == " << Name << " ? AttributeCommonInfo::Form";
+            OS << "    case " << Name << ":\n";
+            OS << "      return  AttributeCommonInfo::Form";
             emitFormInitializer(OS, S, Name);
-            OS << " :\n";
+            OS << ";\n";
           }
         }
-        OS << "    (llvm_unreachable(\"Unknown attribute spelling!\"), "
-           << " AttributeCommonInfo::Form";
+        OS << "    default:\n";
+        OS << "      llvm_unreachable(\"Unknown attribute spelling!\");\n"
+           << "      return AttributeCommonInfo::Form";
         emitFormInitializer(OS, Spellings[0], "0");
-        OS << "))";
+        OS << ";\n"
+           << "    }\n"
+           << "  }()";
       }
 
       OS << ");\n";

>From 0a4442b0a3baac993b8e3ca98fe4389b0e0e577d Mon Sep 17 00:00:00 2001
From: Arvind Mukund <armu30 at gmail.com>
Date: Wed, 4 Oct 2023 22:34:32 -0700
Subject: [PATCH 2/3] Remove extra space after `return`

Signed-off-by: Arvind Mukund <armu30 at gmail.com>
---
 clang/utils/TableGen/ClangAttrEmitter.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp
index 6a821463aa9f0fe..f2a6c1dfcf2fcc4 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -2700,7 +2700,7 @@ static void emitAttributes(RecordKeeper &Records, raw_ostream &OS,
           const auto &Name = SemanticToSyntacticMap[Idx];
           if (Uniques.insert(Name).second) {
             OS << "    case " << Name << ":\n";
-            OS << "      return  AttributeCommonInfo::Form";
+            OS << "      return AttributeCommonInfo::Form";
             emitFormInitializer(OS, S, Name);
             OS << ";\n";
           }

>From df0287c44856a08badc6ce3051c4cd6963662db1 Mon Sep 17 00:00:00 2001
From: Arvind Mukund <armu30 at gmail.com>
Date: Thu, 5 Oct 2023 09:53:51 -0700
Subject: [PATCH 3/3] Add this bug fix to clang's release notes

Signed-off-by: Arvind Mukund <armu30 at gmail.com>
---
 clang/docs/ReleaseNotes.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7b22f3c6842c9f8..29f3e18fc59fc2b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -343,6 +343,8 @@ Bug Fixes in This Version
 - Fix a crash when evaluating value-dependent structured binding
   variables at compile time.
   Fixes (`#67690 <https://github.com/llvm/llvm-project/issues/67690>`_)
+- Fixes a regression where ``LLVM_UNREACHABLE_OPTIMIZE=OFF`` cannot be used
+  with ``Release`` mode builds. (`#68237 <https://github.com/llvm/llvm-project/issues/68237>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



More information about the cfe-commits mailing list