Skip to content

Commit

Permalink
address
Browse files Browse the repository at this point in the history
  • Loading branch information
wenpen committed May 10, 2024
1 parent 00c75fa commit 9ae5d1f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
6 changes: 6 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,8 @@ class CIRGenFunction : public CIRGenTypeCache {
buildSwitchCase(const clang::SwitchCase &S, mlir::Type condType,
SmallVector<mlir::Attribute, 4> &caseAttrs);

mlir::LogicalResult buildCaseNoneStmt(const Stmt *stmt);

mlir::LogicalResult
buildSwitchBody(const clang::Stmt *S, mlir::Type condType,
SmallVector<mlir::Attribute, 4> &caseAttrs);
Expand Down Expand Up @@ -1898,6 +1900,8 @@ class CIRGenFunction : public CIRGenTypeCache {

void setRetVal(mlir::Value v) { retVal = v; }

LexicalScope *getParentScope() { return ParentScope; }

void cleanup();
void restore() { CGF.currLexScope = ParentScope; }

Expand Down Expand Up @@ -2027,6 +2031,8 @@ class CIRGenFunction : public CIRGenTypeCache {
// Scope entry block tracking
mlir::Block *getEntryBlock() { return EntryBlock; }

bool IsInsideCaseNoneStmt = false;

mlir::Location BeginLoc, EndLoc;
};

Expand Down
53 changes: 31 additions & 22 deletions clang/lib/CIR/CodeGen/CIRGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,6 @@ using namespace cir;
using namespace clang;
using namespace mlir::cir;

namespace {

// Handle stmt that don't belong to any cases.
void checkCaseNoneStmt(const Stmt &S, bool checkCaseStmt = true) {
if (S.getStmtClass() == Stmt::LabelStmtClass)
llvm_unreachable("LabelStmtClass support NYI");

if (S.getStmtClass() == Stmt::CaseStmtClass ||
S.getStmtClass() == Stmt::DefaultStmtClass)
assert(!checkCaseStmt && "CaseStmtClass/DefaultStmtClass support NYI");

if (S.getStmtClass() == Stmt::SwitchStmtClass)
checkCaseStmt = false;

for (const Stmt *c : S.children())
checkCaseNoneStmt(*c, checkCaseStmt);
}

} // namespace

Address CIRGenFunction::buildCompoundStmtWithoutScope(const CompoundStmt &S,
bool getLast,
AggValueSlot slot) {
Expand Down Expand Up @@ -348,6 +328,14 @@ mlir::LogicalResult CIRGenFunction::buildLabelStmt(const clang::LabelStmt &S) {
// IsEHa: not implemented.
assert(!(getContext().getLangOpts().EHAsynch && S.isSideEntry()));

// TODO: After support case stmt crossing scopes, we should build LabelStmt
// and clean LexicalScope::IsInsideCaseNoneStmt.
for (auto *lexScope = currLexScope; lexScope;
lexScope = lexScope->getParentScope()) {
assert(!lexScope->IsInsideCaseNoneStmt &&
"LabelStmt inside case none stmt NYI");
}

return buildStmt(S.getSubStmt(), /* useCurrentScope */ true);
}

Expand Down Expand Up @@ -479,6 +467,11 @@ mlir::LogicalResult CIRGenFunction::buildReturnStmt(const ReturnStmt &S) {
assert(!UnimplementedFeature::requiresReturnValueCheck());
auto loc = getLoc(S.getSourceRange());

// TODO: Rewrite the logic to handle ReturnStmt inside SwitchStmt, then
// clean up the code below.
if (currLexScope->IsInsideCaseNoneStmt)
return mlir::success();

// Emit the result value, even if unused, to evaluate the side effects.
const Expr *RV = S.getRetValue();

Expand Down Expand Up @@ -724,6 +717,22 @@ CIRGenFunction::buildSwitchCase(const SwitchCase &S, mlir::Type condType,
llvm_unreachable("expect case or default stmt");
}

mlir::LogicalResult CIRGenFunction::buildCaseNoneStmt(const Stmt *S) {
// Create orphan region to skip over the case none stmts.
mlir::Region region;
auto *block = builder.createBlock(&region);
mlir::OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPointToStart(block);

currLexScope->IsInsideCaseNoneStmt = true;

auto res = buildStmt(S, /*useCurrentScope=*/isa<CompoundStmt>(S));

currLexScope->IsInsideCaseNoneStmt = false;

return res;
}

mlir::LogicalResult
CIRGenFunction::buildCXXForRangeStmt(const CXXForRangeStmt &S,
ArrayRef<const Attr *> ForAttrs) {
Expand Down Expand Up @@ -1004,7 +1013,7 @@ mlir::LogicalResult CIRGenFunction::buildSwitchBody(
builder.setInsertionPointToEnd(lastCaseBlock);
res = buildStmt(c, /*useCurrentScope=*/!isa<CompoundStmt>(c));
} else {
checkCaseNoneStmt(*c);
buildCaseNoneStmt(c);
continue;
}

Expand All @@ -1016,7 +1025,7 @@ mlir::LogicalResult CIRGenFunction::buildSwitchBody(
return res;
}

checkCaseNoneStmt(*S);
buildCaseNoneStmt(S);
return mlir::success();
}

Expand Down

0 comments on commit 9ae5d1f

Please sign in to comment.