From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by sourceware.org (Postfix) with ESMTPS id BBAB7394743F for ; Tue, 31 Jan 2023 13:21:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BBAB7394743F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wm1-x32d.google.com with SMTP id l8so10323063wms.3 for ; Tue, 31 Jan 2023 05:21:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=mwrLbZXm7+wNaDFaR0qLagU/1nSW+p8KD4JGnEsaBOQ=; b=UuYI21tTJgg7BS6hPT/vy64mlZiCkg0FpZOvoPh0R62w5fSNB4h8/nle3la446LJL+ WPm6gIM9IAUpDiKYmqMIVAb3bY+KxXBcGZFZyfRVcb9M5TMgFnjLJIHxwjlJUruaxYGy NDqglKhx9GP4jCYCLNqs+wLxdJYxC8qDOGB2F2pIwjfv7VNzfOX2+OOyEo+eiD4j3ZyW CmzJJM04RkTlX46vcwUTHWhY24mU6we6uhcOjRsWIBMrsPCsAQNXsLp74PSKIkytI8YB dpvrzKYngtqcD23uN2hhJajg4W4Ut+FvjSFTECleISBmOvyhPs56FGjMLCRn5pzibnR8 3LLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=mwrLbZXm7+wNaDFaR0qLagU/1nSW+p8KD4JGnEsaBOQ=; b=YfznEoTfP85jdDqLvsDjWBdJ4AH2sRFw8r1QVtxp6WdGWEho28+7TNOrfgIjnpVw0C DWBT3E+P0+J2Lv5Xpko3qvVIxvE79ztkhwW+Lz5jRwllwyOrALO/NUU9fUzv+/hzGraY SKIJfkI0oxp2MCtDgPJkCNMXSLNJXsfzQ/cybGh2XoNSBy2ENJ86rMXKj+T+8ZGBh1ji PntK2GtzXGvJJZHYZZeHz9l4OMYdvjemHtl641g2pK2QVMAjK/NtxBgHV79GsOTIJcjh 4L3V+O9s+7HIwF8PcZ6hQs38zEinVP1c7/o73WV9xBTv4NvY06RHfT+Mi0UNwB+sw/pG rrKA== X-Gm-Message-State: AO0yUKWDeCcLqZgULnVBAFqFZBQRKT5kvGOmtfBO2pvU31zuUJlOHxR4 B2lGZNxASmHR7aQWbxemIgPCuL6lqf5+Q7BEGA== X-Google-Smtp-Source: AK7set8xtrTkNMzfHe6+AtS0E9KTrLTcY7TDMOY1mLtihuVu4SHJW8bRr5iw0pnuiZZD+FDnmhsoJw== X-Received: by 2002:a05:600c:2157:b0:3dc:42d2:aedc with SMTP id v23-20020a05600c215700b003dc42d2aedcmr10538168wml.23.1675171313090; Tue, 31 Jan 2023 05:21:53 -0800 (PST) Received: from platypus.lan ([2001:861:5e4c:3bb0:6424:328a:1734:3249]) by smtp.gmail.com with ESMTPSA id r38-20020a05600c322600b003dd19baf45asm3590963wmp.40.2023.01.31.05.21.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Jan 2023 05:21:52 -0800 (PST) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Philip Herron Subject: [COMMITTED] gccrs: Create canonical process of compiling constant items Date: Tue, 31 Jan 2023 14:25:38 +0100 Message-Id: <20230131132538.662296-1-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-15.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Philip Herron In order to compile a block expression constant, the simplest way for us was to reuse what code we have and to generate an artifical function which does not get added to the translation unit. The constant then becomes a CALL_EXPR to this artifical function which we can pass to the constexpr evaluator to resolve the result of this artifical 'CALL_EXPR'. Before this patch we seperated the difference between block expressions and non block expressions in constants. So for non block expressions we simply compiled them as if it was a simple constant but this is not guaranteed to be the case in rust, for example coercion sites can generate temporaries during autoderef which we let the constant evaluator resolve for us. This makes all constants handled in the same way to simplify the logic here. gcc/rust/ChangeLog: * backend/rust-compile-base.cc: Improve compilation pipeline and simplify function. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/rust/backend/rust-compile-base.cc | 103 ++++++++++++++------------ 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index e1506b377ce..568abf9ca2c 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -652,65 +652,72 @@ HIRCompileBase::compile_constant_item ( bool is_block_expr = const_value_expr->get_expression_type () == HIR::Expr::ExprType::Block; - // compile the expression - tree folded_expr = error_mark_node; - if (!is_block_expr) - { - tree value = CompileExpr::Compile (const_value_expr, ctx); - folded_expr = fold_expr (value); - } - else - { - // in order to compile a block expr we want to reuse as much existing - // machineary that we already have. This means the best approach is to - // make a _fake_ function with a block so it can hold onto temps then - // use our constexpr code to fold it completely or error_mark_node - Backend::typed_identifier receiver; - tree compiled_fn_type = ctx->get_backend ()->function_type ( - receiver, {}, {Backend::typed_identifier ("_", const_type, locus)}, - NULL, locus); - - tree fndecl - = ctx->get_backend ()->function (compiled_fn_type, ident, "", 0, locus); - TREE_READONLY (fndecl) = 1; + // in order to compile a block expr we want to reuse as much existing + // machineary that we already have. This means the best approach is to + // make a _fake_ function with a block so it can hold onto temps then + // use our constexpr code to fold it completely or error_mark_node + Backend::typed_identifier receiver; + tree compiled_fn_type = ctx->get_backend ()->function_type ( + receiver, {}, {Backend::typed_identifier ("_", const_type, locus)}, NULL, + locus); + + tree fndecl + = ctx->get_backend ()->function (compiled_fn_type, ident, "", 0, locus); + TREE_READONLY (fndecl) = 1; + + tree enclosing_scope = NULL_TREE; - tree enclosing_scope = NULL_TREE; + Location start_location = const_value_expr->get_locus (); + Location end_location = const_value_expr->get_locus (); + if (is_block_expr) + { HIR::BlockExpr *function_body = static_cast (const_value_expr); - Location start_location = function_body->get_locus (); - Location end_location = function_body->get_end_locus (); + start_location = function_body->get_locus (); + end_location = function_body->get_end_locus (); + } - tree code_block - = ctx->get_backend ()->block (fndecl, enclosing_scope, {}, - start_location, end_location); - ctx->push_block (code_block); + tree code_block = ctx->get_backend ()->block (fndecl, enclosing_scope, {}, + start_location, end_location); + ctx->push_block (code_block); - bool address_is_taken = false; - tree ret_var_stmt = NULL_TREE; - Bvariable *return_address - = ctx->get_backend ()->temporary_variable (fndecl, code_block, - const_type, NULL, - address_is_taken, locus, - &ret_var_stmt); + bool address_is_taken = false; + tree ret_var_stmt = NULL_TREE; + Bvariable *return_address + = ctx->get_backend ()->temporary_variable (fndecl, code_block, const_type, + NULL, address_is_taken, locus, + &ret_var_stmt); - ctx->add_statement (ret_var_stmt); - ctx->push_fn (fndecl, return_address); + ctx->add_statement (ret_var_stmt); + ctx->push_fn (fndecl, return_address); + if (is_block_expr) + { + HIR::BlockExpr *function_body + = static_cast (const_value_expr); compile_function_body (ctx, fndecl, *function_body, true); - tree bind_tree = ctx->pop_block (); + } + else + { + tree value = CompileExpr::Compile (const_value_expr, ctx); + tree return_expr = ctx->get_backend ()->return_statement ( + fndecl, {value}, const_value_expr->get_locus ()); + ctx->add_statement (return_expr); + } - gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR); - DECL_SAVED_TREE (fndecl) = bind_tree; - DECL_DECLARED_CONSTEXPR_P (fndecl) = 1; - maybe_save_constexpr_fundef (fndecl); + tree bind_tree = ctx->pop_block (); + + gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR); + DECL_SAVED_TREE (fndecl) = bind_tree; + DECL_DECLARED_CONSTEXPR_P (fndecl) = 1; + maybe_save_constexpr_fundef (fndecl); - ctx->pop_fn (); + ctx->pop_fn (); - // lets fold it into a call expr - tree call = build_call_array_loc (locus.gcc_location (), const_type, - fndecl, 0, NULL); - folded_expr = fold_expr (call); - } + // lets fold it into a call expr + tree call + = build_call_array_loc (locus.gcc_location (), const_type, fndecl, 0, NULL); + tree folded_expr = fold_expr (call); return named_constant_expression (const_type, ident, folded_expr, locus); } -- 2.39.1