[clang] [CIR] Handle NullStmt (PR #134889)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 8 11:18:02 PDT 2025
https://github.com/andykaylor updated https://github.com/llvm/llvm-project/pull/134889
>From efbec15d42a83cd2f4980418a939a88af7c7b92a Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Tue, 8 Apr 2025 10:01:21 -0700
Subject: [PATCH 1/2] [CIR] Handle NullStmt
The handling for NullStmt was going to an error saying the statement
handling wasn't implemented. It doesn't need any implementation. It is
sufficient for emitSimpleStmt to just return success for that statement
class. This change does that.
---
clang/lib/CIR/CodeGen/CIRGenStmt.cpp | 7 ++++++-
clang/test/CIR/CodeGen/basic.c | 15 +++++++++++++++
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmt.cpp b/clang/lib/CIR/CodeGen/CIRGenStmt.cpp
index 00d33e7feddff..072370ffeb4c8 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmt.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmt.cpp
@@ -57,6 +57,7 @@ mlir::LogicalResult CIRGenFunction::emitStmt(const Stmt *s,
switch (s->getStmtClass()) {
case Stmt::BreakStmtClass:
+ case Stmt::NullStmtClass:
case Stmt::CompoundStmtClass:
case Stmt::ContinueStmtClass:
case Stmt::DeclStmtClass:
@@ -93,7 +94,6 @@ mlir::LogicalResult CIRGenFunction::emitStmt(const Stmt *s,
case Stmt::SEHExceptStmtClass:
case Stmt::SEHFinallyStmtClass:
case Stmt::MSDependentExistsStmtClass:
- case Stmt::NullStmtClass:
case Stmt::LabelStmtClass:
case Stmt::AttributedStmtClass:
case Stmt::GotoStmtClass:
@@ -231,6 +231,11 @@ mlir::LogicalResult CIRGenFunction::emitSimpleStmt(const Stmt *s,
break;
case Stmt::ContinueStmtClass:
return emitContinueStmt(cast<ContinueStmt>(*s));
+
+ // NullStmt doesn't need any handling, but we need to say we handled it.
+ case Stmt::NullStmtClass:
+ break;
+
case Stmt::BreakStmtClass:
return emitBreakStmt(cast<BreakStmt>(*s));
case Stmt::ReturnStmtClass:
diff --git a/clang/test/CIR/CodeGen/basic.c b/clang/test/CIR/CodeGen/basic.c
index 673ff256c22af..6365dc2158138 100644
--- a/clang/test/CIR/CodeGen/basic.c
+++ b/clang/test/CIR/CodeGen/basic.c
@@ -90,3 +90,18 @@ int f3(void) {
// OGCG-NEXT: store i32 3, ptr %[[I_PTR]], align 4
// OGCG-NEXT: %[[I:.*]] = load i32, ptr %[[I_PTR]], align 4
// OGCG-NEXT: ret i32 %[[I]]
+
+// Verify null statement handling.
+void f4(void) {
+ ;
+}
+
+// CIR: cir.func @f4()
+// CIR-NEXT: cir.return
+
+// LLVM: define void @f4()
+// LLVM-NEXT: ret void
+
+// OGCG: define{{.*}} void @f4()
+// OGCG-NEXT: entry:
+// OGCG-NEXT: ret void
>From c4568c76c5b1f98d44cc3cf25e991b8ed61c375f Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Tue, 8 Apr 2025 11:17:06 -0700
Subject: [PATCH 2/2] Added test case, moved unreachable statement classes
---
clang/lib/CIR/CodeGen/CIRGenStmt.cpp | 11 ++++----
clang/test/CIR/CodeGen/basic.c | 39 ++++++++++++++++++++++++++++
2 files changed, 45 insertions(+), 5 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmt.cpp b/clang/lib/CIR/CodeGen/CIRGenStmt.cpp
index 072370ffeb4c8..55022cbd53bc2 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmt.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmt.cpp
@@ -56,6 +56,12 @@ mlir::LogicalResult CIRGenFunction::emitStmt(const Stmt *s,
return mlir::success();
switch (s->getStmtClass()) {
+ case Stmt::NoStmtClass:
+ case Stmt::CXXCatchStmtClass:
+ case Stmt::SEHExceptStmtClass:
+ case Stmt::SEHFinallyStmtClass:
+ case Stmt::MSDependentExistsStmtClass:
+ llvm_unreachable("invalid statement class to emit generically");
case Stmt::BreakStmtClass:
case Stmt::NullStmtClass:
case Stmt::CompoundStmtClass:
@@ -89,11 +95,6 @@ mlir::LogicalResult CIRGenFunction::emitStmt(const Stmt *s,
case Stmt::OMPScopeDirectiveClass:
case Stmt::OMPErrorDirectiveClass:
- case Stmt::NoStmtClass:
- case Stmt::CXXCatchStmtClass:
- case Stmt::SEHExceptStmtClass:
- case Stmt::SEHFinallyStmtClass:
- case Stmt::MSDependentExistsStmtClass:
case Stmt::LabelStmtClass:
case Stmt::AttributedStmtClass:
case Stmt::GotoStmtClass:
diff --git a/clang/test/CIR/CodeGen/basic.c b/clang/test/CIR/CodeGen/basic.c
index 6365dc2158138..7184e395ce386 100644
--- a/clang/test/CIR/CodeGen/basic.c
+++ b/clang/test/CIR/CodeGen/basic.c
@@ -105,3 +105,42 @@ void f4(void) {
// OGCG: define{{.*}} void @f4()
// OGCG-NEXT: entry:
// OGCG-NEXT: ret void
+
+// Verify null statement as for-loop body.
+void f5(void) {
+ for (;;)
+ ;
+}
+
+// CIR: cir.func @f5()
+// CIR-NEXT: cir.scope {
+// CIR-NEXT: cir.for : cond {
+// CIR-NEXT: %0 = cir.const #true
+// CIR-NEXT: cir.condition(%0)
+// CIR-NEXT: } body {
+// CIR-NEXT: cir.yield
+// CIR-NEXT: } step {
+// CIR-NEXT: cir.yield
+// CIR-NEXT: }
+// CIR-NEXT: }
+// CIR-NEXT: cir.return
+// CIR-NEXT: }
+
+// LLVM: define void @f5()
+// LLVM: br label %[[SCOPE:.*]]
+// LLVM: [[SCOPE]]:
+// LLVM: br label %[[LOOP:.*]]
+// LLVM: [[LOOP]]:
+// LLVM: br i1 true, label %[[LOOP_STEP:.*]], label %[[LOOP_EXIT:.*]]
+// LLVM: [[LOOP_STEP]]:
+// LLVM: br label %[[LOOP_BODY:.*]]
+// LLVM: [[LOOP_BODY]]:
+// LLVM: br label %[[LOOP]]
+// LLVM: [[LOOP_EXIT]]:
+// LLVM: ret void
+
+// OGCG: define{{.*}} void @f5()
+// OGCG: entry:
+// OGCG: br label %[[LOOP:.*]]
+// OGCG: [[LOOP]]:
+// OGCG: br label %[[LOOP]]
More information about the cfe-commits
mailing list