Skip to content

Commit

Permalink
[CIR][CIRGen] Support for __attribute__((fallthrough)) statement (#517)
Browse files Browse the repository at this point in the history
This PR adds handling of AttributedStmt to support fallthrough
attribute.
  • Loading branch information
YazZz1k authored and lanza committed Mar 23, 2024
1 parent 38ca68d commit 53ccc79
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,8 @@ class CIRGenFunction : public CIRGenTypeCache {
mlir::LogicalResult buildLabel(const clang::LabelDecl *D);
mlir::LogicalResult buildLabelStmt(const clang::LabelStmt &S);

mlir::LogicalResult buildAttributedStmt(const AttributedStmt &S);

mlir::LogicalResult buildBreakStmt(const clang::BreakStmt &S);
mlir::LogicalResult buildContinueStmt(const clang::ContinueStmt &S);

Expand Down
19 changes: 19 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ mlir::LogicalResult CIRGenFunction::buildSimpleStmt(const Stmt *S,
return buildBreakStmt(cast<BreakStmt>(*S));

case Stmt::AttributedStmtClass:
return buildAttributedStmt(cast<AttributedStmt>(*S));

case Stmt::SEHLeaveStmtClass:
llvm::errs() << "CIR codegen for '" << S->getStmtClassName()
<< "' not implemented\n";
Expand All @@ -325,6 +327,23 @@ mlir::LogicalResult CIRGenFunction::buildLabelStmt(const clang::LabelStmt &S) {
return buildStmt(S.getSubStmt(), /* useCurrentScope */ true);
}

mlir::LogicalResult
CIRGenFunction::buildAttributedStmt(const AttributedStmt &S) {
for (const auto *A : S.getAttrs()) {
switch (A->getKind()) {
case attr::NoMerge:
case attr::NoInline:
case attr::AlwaysInline:
case attr::MustTail:
llvm_unreachable("NIY attributes");
default:
break;
}
}

return buildStmt(S.getSubStmt(), true, S.getAttrs());
}

// Add terminating yield on body regions (loops, ...) in case there are
// not other terminators used.
// FIXME: make terminateCaseRegion use this too.
Expand Down
26 changes: 26 additions & 0 deletions clang/test/CIR/CodeGen/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,29 @@ void sw12(int a) {
// CHECK-NEXT: ^bb1: // no predecessors
// CHECK-NEXT: cir.break
// CHECK-NEXT: }

void fallthrough(int x) {
switch (x) {
case 1:
__attribute__((fallthrough));
case 2:
break;
default:
break;
}
}

// CHECK: cir.func @_Z11fallthroughi
// CHECK: cir.scope {
// CHECK: cir.switch (%1 : !s32i) [
// CHECK-NEXT: case (equal, 1) {
// CHECK-NEXT: cir.yield
// CHECK-NEXT: },
// CHECK-NEXT: case (equal, 2) {
// CHECK-NEXT: cir.break
// CHECK-NEXT: },
// CHECK-NEXT: case (default) {
// CHECK-NEXT: cir.break
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }

0 comments on commit 53ccc79

Please sign in to comment.