Skip to content

Commit 8630cde

Browse files
sitio-coutolanza
authored andcommitted
[CIR][CIRGen][NFC] Return scope result in compound stmt builders
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
1 parent b62f666 commit 8630cde

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "clang/Frontend/FrontendDiagnostic.h"
2525

2626
#include "mlir/Dialect/Func/IR/FuncOps.h"
27+
#include "mlir/Support/LogicalResult.h"
2728

2829
using namespace cir;
2930
using namespace clang;
@@ -1114,7 +1115,7 @@ mlir::LogicalResult CIRGenFunction::buildFunctionBody(const clang::Stmt *Body) {
11141115

11151116
auto result = mlir::LogicalResult::success();
11161117
if (const CompoundStmt *S = dyn_cast<CompoundStmt>(Body))
1117-
result = buildCompoundStmtWithoutScope(*S);
1118+
buildCompoundStmtWithoutScope(*S);
11181119
else
11191120
result = buildStmt(Body, /*useCurrentScope*/ true);
11201121

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -834,11 +834,13 @@ class CIRGenFunction : public CIRGenTypeCache {
834834
bool IsFnTryBlock = false);
835835
void exitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
836836

837-
mlir::LogicalResult buildCompoundStmt(const clang::CompoundStmt &S);
838-
839-
mlir::LogicalResult
840-
buildCompoundStmtWithoutScope(const clang::CompoundStmt &S);
837+
Address buildCompoundStmt(const clang::CompoundStmt &S, bool getLast = false,
838+
AggValueSlot slot = AggValueSlot::ignored());
841839

840+
Address
841+
buildCompoundStmtWithoutScope(const clang::CompoundStmt &S,
842+
bool getLast = false,
843+
AggValueSlot slot = AggValueSlot::ignored());
842844
GlobalDecl CurSEHParent;
843845
bool currentFunctionUsesSEHTry() const { return !!CurSEHParent; }
844846

clang/lib/CIR/CodeGen/CIRGenStmt.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,27 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "Address.h"
1314
#include "CIRGenFunction.h"
15+
#include "mlir/IR/Value.h"
1416

1517
using namespace cir;
1618
using namespace clang;
1719
using namespace mlir::cir;
1820

19-
mlir::LogicalResult
20-
CIRGenFunction::buildCompoundStmtWithoutScope(const CompoundStmt &S) {
21+
Address CIRGenFunction::buildCompoundStmtWithoutScope(const CompoundStmt &S,
22+
bool getLast,
23+
AggValueSlot slot) {
2124
for (auto *CurStmt : S.body())
2225
if (buildStmt(CurStmt, /*useCurrentScope=*/false).failed())
23-
return mlir::failure();
26+
return Address::invalid();
2427

25-
return mlir::success();
28+
return Address::invalid();
2629
}
2730

28-
mlir::LogicalResult CIRGenFunction::buildCompoundStmt(const CompoundStmt &S) {
29-
mlir::LogicalResult res = mlir::success();
30-
31-
auto compoundStmtBuilder = [&]() -> mlir::LogicalResult {
32-
if (buildCompoundStmtWithoutScope(S).failed())
33-
return mlir::failure();
34-
35-
return mlir::success();
36-
};
31+
Address CIRGenFunction::buildCompoundStmt(const CompoundStmt &S, bool getLast,
32+
AggValueSlot slot) {
33+
Address retAlloca = Address::invalid();
3734

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

48-
return res;
45+
return retAlloca;
4946
}
5047

5148
void CIRGenFunction::buildStopPoint(const Stmt *S) {
@@ -260,9 +257,9 @@ mlir::LogicalResult CIRGenFunction::buildSimpleStmt(const Stmt *S,
260257
case Stmt::DeclStmtClass:
261258
return buildDeclStmt(cast<DeclStmt>(*S));
262259
case Stmt::CompoundStmtClass:
263-
return useCurrentScope
264-
? buildCompoundStmtWithoutScope(cast<CompoundStmt>(*S))
265-
: buildCompoundStmt(cast<CompoundStmt>(*S));
260+
useCurrentScope ? buildCompoundStmtWithoutScope(cast<CompoundStmt>(*S))
261+
: buildCompoundStmt(cast<CompoundStmt>(*S));
262+
break;
266263
case Stmt::ReturnStmtClass:
267264
return buildReturnStmt(cast<ReturnStmt>(*S));
268265
case Stmt::GotoStmtClass:

0 commit comments

Comments
 (0)