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

Arvind Mukund via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 4 22:29:13 PDT 2023


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

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.

>From edb04da9e4db6043f0905ad887a1fe5ffad48fca 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] 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";



More information about the cfe-commits mailing list