From 4ada6ff902396a47a4a391216937ce63890208c8 Mon Sep 17 00:00:00 2001 From: Vinicius Couto Espindola Date: Wed, 10 Jan 2024 20:08:12 -0300 Subject: [PATCH] [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: https://github.com/llvm/clangir/pull/313 --- clang/lib/CIR/CodeGen/CIRGenFunction.cpp | 3 ++- clang/lib/CIR/CodeGen/CIRGenFunction.h | 10 ++++--- clang/lib/CIR/CodeGen/CIRGenStmt.cpp | 33 +++++++++++------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp index 90509d84ad25..11f562680e08 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp @@ -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; @@ -1111,7 +1112,7 @@ mlir::LogicalResult CIRGenFunction::buildFunctionBody(const clang::Stmt *Body) { auto result = mlir::LogicalResult::success(); if (const CompoundStmt *S = dyn_cast(Body)) - result = buildCompoundStmtWithoutScope(*S); + buildCompoundStmtWithoutScope(*S); else result = buildStmt(Body, /*useCurrentScope*/ true); diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h index 9e2d1687366f..2d56b33dc823 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.h +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h @@ -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; } diff --git a/clang/lib/CIR/CodeGen/CIRGenStmt.cpp b/clang/lib/CIR/CodeGen/CIRGenStmt.cpp index 0d5be04f8ef5..6c93e9f0cd5b 100644 --- a/clang/lib/CIR/CodeGen/CIRGenStmt.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenStmt.cpp @@ -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); @@ -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) { @@ -258,9 +255,9 @@ mlir::LogicalResult CIRGenFunction::buildSimpleStmt(const Stmt *S, case Stmt::DeclStmtClass: return buildDeclStmt(cast(*S)); case Stmt::CompoundStmtClass: - return useCurrentScope - ? buildCompoundStmtWithoutScope(cast(*S)) - : buildCompoundStmt(cast(*S)); + useCurrentScope ? buildCompoundStmtWithoutScope(cast(*S)) + : buildCompoundStmt(cast(*S)); + break; case Stmt::ReturnStmtClass: return buildReturnStmt(cast(*S)); case Stmt::GotoStmtClass: