Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][CIRGen][NFC] Return scope result in compound stmt builders #313

Merged
merged 7 commits into from
Jan 10, 2024
3 changes: 2 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "clang/Frontend/FrontendDiagnostic.h"

#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Support/LogicalResult.h"

using namespace cir;
using namespace clang;
Expand Down Expand Up @@ -1111,7 +1112,7 @@ mlir::LogicalResult CIRGenFunction::buildFunctionBody(const clang::Stmt *Body) {

auto result = mlir::LogicalResult::success();
if (const CompoundStmt *S = dyn_cast<CompoundStmt>(Body))
result = buildCompoundStmtWithoutScope(*S);
buildCompoundStmtWithoutScope(*S);
else
result = buildStmt(Body, /*useCurrentScope*/ true);

Expand Down
10 changes: 6 additions & 4 deletions clang/lib/CIR/CodeGen/CIRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -834,11 +834,13 @@ class CIRGenFunction : public CIRGenTypeCache {
bool IsFnTryBlock = false);
void exitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);

mlir::LogicalResult buildCompoundStmt(const clang::CompoundStmt &S);

mlir::LogicalResult
buildCompoundStmtWithoutScope(const clang::CompoundStmt &S);
Address buildCompoundStmt(const clang::CompoundStmt &S, bool getLast = false,
AggValueSlot slot = AggValueSlot::ignored());

Address
buildCompoundStmtWithoutScope(const clang::CompoundStmt &S,
bool getLast = false,
AggValueSlot slot = AggValueSlot::ignored());
GlobalDecl CurSEHParent;
bool currentFunctionUsesSEHTry() const { return !!CurSEHParent; }

Expand Down
33 changes: 15 additions & 18 deletions clang/lib/CIR/CodeGen/CIRGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,27 @@
//
//===----------------------------------------------------------------------===//

#include "Address.h"
#include "CIRGenFunction.h"
#include "mlir/IR/Value.h"

using namespace cir;
using namespace clang;
using namespace mlir::cir;

mlir::LogicalResult
CIRGenFunction::buildCompoundStmtWithoutScope(const CompoundStmt &S) {
Address CIRGenFunction::buildCompoundStmtWithoutScope(const CompoundStmt &S,
bool getLast,
AggValueSlot slot) {
for (auto *CurStmt : S.body())
if (buildStmt(CurStmt, /*useCurrentScope=*/false).failed())
return mlir::failure();
return Address::invalid();

return mlir::success();
return Address::invalid();
}

mlir::LogicalResult CIRGenFunction::buildCompoundStmt(const CompoundStmt &S) {
mlir::LogicalResult res = mlir::success();

auto compoundStmtBuilder = [&]() -> mlir::LogicalResult {
if (buildCompoundStmtWithoutScope(S).failed())
return mlir::failure();

return mlir::success();
};
Address CIRGenFunction::buildCompoundStmt(const CompoundStmt &S, bool getLast,
AggValueSlot slot) {
Address retAlloca = Address::invalid();

// Add local scope to track new declared variables.
SymTableScopeTy varScope(symbolTable);
Expand All @@ -42,10 +39,10 @@ mlir::LogicalResult CIRGenFunction::buildCompoundStmt(const CompoundStmt &S) {
scopeLoc, /*scopeBuilder=*/
[&](mlir::OpBuilder &b, mlir::Location loc) {
LexicalScope lexScope{*this, loc, builder.getInsertionBlock()};
res = compoundStmtBuilder();
retAlloca = buildCompoundStmtWithoutScope(S);
});

return res;
return retAlloca;
}

void CIRGenFunction::buildStopPoint(const Stmt *S) {
Expand Down Expand Up @@ -258,9 +255,9 @@ mlir::LogicalResult CIRGenFunction::buildSimpleStmt(const Stmt *S,
case Stmt::DeclStmtClass:
return buildDeclStmt(cast<DeclStmt>(*S));
case Stmt::CompoundStmtClass:
return useCurrentScope
? buildCompoundStmtWithoutScope(cast<CompoundStmt>(*S))
: buildCompoundStmt(cast<CompoundStmt>(*S));
useCurrentScope ? buildCompoundStmtWithoutScope(cast<CompoundStmt>(*S))
: buildCompoundStmt(cast<CompoundStmt>(*S));
break;
case Stmt::ReturnStmtClass:
return buildReturnStmt(cast<ReturnStmt>(*S));
case Stmt::GotoStmtClass:
Expand Down