[clang] [CIR] Upstream support for switch statements case kinds (PR #138003)

via cfe-commits cfe-commits at lists.llvm.org
Mon May 5 10:53:36 PDT 2025


================
@@ -515,19 +517,44 @@ CIRGenFunction::emitCaseDefaultCascade(const T *stmt, mlir::Type condType,
 mlir::LogicalResult CIRGenFunction::emitCaseStmt(const CaseStmt &s,
                                                  mlir::Type condType,
                                                  bool buildingTopLevelCase) {
-  llvm::APSInt intVal = s.getLHS()->EvaluateKnownConstInt(getContext());
+  cir::CaseOpKind kind;
+  mlir::ArrayAttr value;
+
   SmallVector<mlir::Attribute, 1> caseEltValueListAttr;
-  caseEltValueListAttr.push_back(cir::IntAttr::get(condType, intVal));
-  mlir::ArrayAttr value = builder.getArrayAttr(caseEltValueListAttr);
-  if (s.getRHS()) {
-    getCIRGenModule().errorNYI(s.getSourceRange(), "SwitchOp range kind");
-    return mlir::failure();
+  llvm::APSInt intVal = s.getLHS()->EvaluateKnownConstInt(getContext());
+
+  // If the case statement has an RHS value, it is representing a GNU
+  // case range statement, where LHS is the beginning of the range
+  // and RHS is the end of the range.
+  if (const Expr *rhs = s.getRHS()) {
+
+    llvm::APSInt endVal = rhs->EvaluateKnownConstInt(getContext());
+    SmallVector<mlir::Attribute, 4> rangeCaseAttr = {
+        cir::IntAttr::get(condType, intVal),
+        cir::IntAttr::get(condType, endVal)};
+    value = builder.getArrayAttr(rangeCaseAttr);
+    kind = cir::CaseOpKind::Range;
+
+    // We don't currently fold case range statements with other case statements.
+    // TODO(cir): Add this capability.
+    assert(!cir::MissingFeatures::foldRangeCase());
----------------
Andres-Salamanca wrote:

I updated the comment to mention implementing this in CIRSimplify.

https://github.com/llvm/llvm-project/pull/138003


More information about the cfe-commits mailing list