Skip to content

Commit

Permalink
[CIR][CIRGen][NFC] Return scope result in compound stmt builders
Browse files Browse the repository at this point in the history
Instead of returning a boolean indicating whether the statement was
handled, returns the ReturnExpr of the statement if there is one. It
also adds some extra bookkeeping to ensure that the result is returned
when needed. This allows for better support of GCC's `ExprStmt`
extension.

The logical result was not used: it was handled but it would never fail.
Any errors within builders should likely be handled with asserts and
unreachables since they imply a programmer's error in the code.

ghstack-source-id: 2319cf3f12e56374a52aaafa4304e74de3ee6453
Pull Request resolved: llvm/clangir#313
  • Loading branch information
sitio-couto authored and lanza committed Jun 20, 2024
1 parent b62f666 commit 8630cde
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
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 @@ -1114,7 +1115,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 @@ -260,9 +257,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

0 comments on commit 8630cde

Please sign in to comment.