From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 3D62A3AA9008; Wed, 8 Jun 2022 11:59:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3D62A3AA9008 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Merge #870 X-Act-Checkin: gcc X-Git-Author: bors[bot] <26634292+bors[bot]@users.noreply.github.com> X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 03e56b5181506a4e6cdb9fe86c543a57840e54c3 X-Git-Newrev: b21caeb3af4313016afeb94a91956e8fc4c2656d Message-Id: <20220608115947.3D62A3AA9008@sourceware.org> Date: Wed, 8 Jun 2022 11:59:47 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 11:59:47 -0000 https://gcc.gnu.org/g:b21caeb3af4313016afeb94a91956e8fc4c2656d commit b21caeb3af4313016afeb94a91956e8fc4c2656d Merge: 03e56b51815 93554f3bce5 Author: bors[bot] <26634292+bors[bot]@users.noreply.github.com> Date: Fri Jan 14 15:49:07 2022 +0000 Merge #870 870: Add constant folding to const functions r=philberty a=philberty In Rust the ArrayType has a constant capacity constraint, which means it allows for bounds checking at compile time as no variable-length arrays are allowed. In order to typecheck this case we had a constant folding pass as part of the type checking system which generated gcc tree's for the IR and enforced the constant checking along the way. GCC with optimizations turned on is capable of constant folding/propogating the compilation unit fully, but we need a method that works regardless of middlle-end optimizations to fold constant expressions at the front-end, turns out the CPP front-end already does this via its constexpr mechanism to ensure that these _do_ fold correctly. Another major reason to do this change is that the original const fold pass was a striped down copy of what the backend is _already_ doing which is creating a duplication of the code generation pass. With this all unified into the code generation pass all we need to do is port over gcc/cp/constexpr.c to enforce the const rules fully but at the GCC tree level not at the typed HIR level. Now that we have unified the pass when we hit a const function we can simply emit a normal GCC function and outside of const expressions GCC will simply emit a normal CallExpr and depending on optimization level fully optimize this. If we are in a ConstDecl we will follow the rust-constexpr.cc and fold the values or error_mark_node with an apropriate error. By reusing the CPP constexpr code we _know_ it works so reusing it as much as possible is a good idea in general for this front-end. Fixes #799 Co-authored-by: Philip Herron Diff: gcc/rust/Make-lang.in | 4 +- gcc/rust/ast/rust-item.h | 7 +- gcc/rust/backend/rust-compile-base.h | 11 +- gcc/rust/backend/rust-compile-context.h | 6 +- gcc/rust/backend/rust-compile-expr.cc | 235 +++++++++- gcc/rust/backend/rust-compile-expr.h | 81 +--- gcc/rust/backend/rust-compile-implitem.h | 4 +- gcc/rust/backend/rust-compile-item.h | 89 +++- gcc/rust/backend/rust-compile-pattern.cc | 33 +- gcc/rust/backend/rust-compile-resolve-path.cc | 23 +- gcc/rust/backend/rust-compile-stmt.h | 4 +- gcc/rust/backend/rust-compile-type.cc | 14 +- gcc/rust/backend/rust-compile-type.h | 5 +- gcc/rust/backend/rust-compile.cc | 90 +++- gcc/rust/backend/rust-constexpr.cc | 428 ++++++++++++++++++ .../rust-constexpr.h} | 39 +- gcc/rust/hir/rust-ast-lower-base.h | 3 + gcc/rust/hir/rust-ast-lower-item.h | 5 +- gcc/rust/hir/rust-ast-lower.cc | 28 ++ gcc/rust/hir/tree/rust-hir-expr.h | 170 +++++-- gcc/rust/hir/tree/rust-hir-item.h | 6 +- gcc/rust/hir/tree/rust-hir.h | 69 ++- gcc/rust/rust-backend.h | 5 - gcc/rust/rust-gcc.cc | 42 +- gcc/rust/rust-session-manager.cc | 4 - gcc/rust/typecheck/rust-hir-const-fold-base.h | 54 --- gcc/rust/typecheck/rust-hir-const-fold.cc | 138 ------ gcc/rust/typecheck/rust-hir-const-fold.h | 499 --------------------- gcc/rust/typecheck/rust-hir-type-check-enumitem.h | 20 +- gcc/rust/typecheck/rust-hir-type-check-expr.h | 149 +++--- gcc/rust/typecheck/rust-hir-type-check-implitem.h | 3 - gcc/rust/typecheck/rust-hir-type-check-stmt.h | 5 +- gcc/rust/typecheck/rust-hir-type-check-toplevel.h | 3 - gcc/rust/typecheck/rust-hir-type-check-type.cc | 23 + gcc/rust/typecheck/rust-hir-type-check.cc | 23 - gcc/rust/typecheck/rust-tyty-cast.h | 16 +- gcc/rust/typecheck/rust-tyty-cmp.h | 7 - gcc/rust/typecheck/rust-tyty-coercion.h | 16 +- gcc/rust/typecheck/rust-tyty-rules.h | 16 +- gcc/rust/typecheck/rust-tyty.cc | 15 +- gcc/rust/typecheck/rust-tyty.h | 54 ++- gcc/testsuite/rust/compile/arrays2.rs | 3 +- gcc/testsuite/rust/execute/torture/const_fold1.rs | 13 + gcc/testsuite/rust/execute/torture/const_fold2.rs | 16 + 44 files changed, 1350 insertions(+), 1128 deletions(-)