197411 Commits

Author SHA1 Message Date
Martin Jambor
e3a5cc3259
ipa-sra: Be optimistic about Fortran descriptors
Fortran descriptors are structures which are often constructed just
for a particular argument of a particular call where it is passed by
reference.  When the called function is under compiler's control, it
can be beneficial to split up the descriptor and pass it in individual
parameters.  Unfortunately, currently we allow IPA-SRA to replace a
pointer with a set of replacements which are at most twice as big in
total and for descriptors we'd need to bump that factor to seven.

This patch looks for parameters which are ADDR_EXPRs of local
variables which are written to and passed as arguments by reference
but are never loaded from and marks them with a flag in the call edge
summary.  The IPA analysis phase then identifies formal parameters
which are always fed such arguments and then is more lenient when it
comoes to size.

In order not to store to maximums per formal parameter, I calculate
the more lenient one by multiplying the existing one with a new
parameter.  If it is preferable to keep the maximums independent, we
can do that.  Documentation for the new parameter is missing as I
still need to re-base the patch on a version which has sphinx.  I will
write it before committing.

I have disable IPA-SRA in pr48636-2.f90 in order to be able to keep
using its dump-scan expressions.  The new testcase is basically a copy
of it with different options and IPA-SRA dump scans.

gcc/ChangeLog:

2022-11-11  Martin Jambor  <mjambor@suse.cz>

	* ipa-sra.cc (isra_param_desc): New field not_specially_constructed.
	(struct isra_param_flow): New field constructed_for_calls.
	(isra_call_summary::dump): Dump the new flag.
	(loaded_decls): New variable.
	(dump_isra_param_descriptor): New parameter hints, dump
	not_specially_constructed if it is true.
	(dump_isra_param_descriptors): New parameter hints, pass it to
	dump_isra_param_descriptor.
	(ipa_sra_function_summaries::duplicate): Duplicate new flag.
	(create_parameter_descriptors): Adjust comment.
	(get_gensum_param_desc): Bail out when decl2desc is NULL.
	(scan_expr_access): Add loaded local variables to loaded_decls.
	(scan_function): Survive if final_bbs is NULL.
	(isra_analyze_call): Compute constructed_for_calls flag.
	(process_scan_results): Be optimistic about size limits.  Do not dump
	computed param hints when dumpint IPA-SRA structures.
	(isra_write_edge_summary): Stream constructed_for_calls.
	(isra_read_edge_summary): Likewise.
	(ipa_sra_dump_all_summaries): New parameter hints, pass it to
	dump_isra_param_descriptor.
	(flip_all_hints_pessimistic): New function.
	(flip_all_param_hints_pessimistic): Likewise.
	(propagate_param_hints): Likewise.
	(disable_unavailable_parameters): Renamed to
	adjust_parameter_descriptions.  Expand size limits for parameters
	which are specially contstructed by all callers.  Check limits again.p
	(ipa_sra_analysis): Pass required hints to ipa_sra_dump_all_summaries.
	Add hint propagation.
	(ipa_sra_summarize_function): Initialize and destory loaded_decls,
	rearrange so that scan_function is called even when there are no
	candidates.
	* params.opt (ipa-sra-ptrwrap-growth-factor): New parameter.

gcc/testsuite/ChangeLog:

2021-11-11  Martin Jambor  <mjambor@suse.cz>

	* gfortran.dg/pr48636-2.f90: Disable IPA-SRA.
	* gfortran.dg/ipa-sra-1.f90: New test.
2022-12-14 00:58:24 +01:00
Martin Jambor
803a91330b
ipa-sra: Move caller->callee propagation before callee->caller one
This patch does not do any functional changes, it merely moves
top-down propagation in the IPA-SRA WPA phase before bottom-up one.
This also meant moving some preliminary checks from the latter to the
former - where they need to be in their own loop over each SCC because
the subsequent one looks at callers.

Currently the propagations are independent (top-down is used for
return value rermoval, bottom-up for parameter removal and splitting)
but subsequent patches will introduce flags about parameters which
should be propagated from callers first and used in splitting.  I
separated this change to test ir independently and make those
subsequent patches cleaner.

While at it, I also replaced couple of FOR_EACH_VEC_ELT macros with
C++11 style iteration.

gcc/ChangeLog:

2022-11-11  Martin Jambor  <mjambor@suse.cz>

	* ipa-sra.cc (ipa_sra_analysis): Move top-down analysis before
	bottom-up analysis.  Replace FOR_EACH_VEC_ELT with C++11 iteration.

gcc/testsuite/ChangeLog:

2021-12-14  Martin Jambor  <mjambor@suse.cz>

	* gcc.dg/ipa/ipa-sra-25.c: New test
2022-12-14 00:58:18 +01:00
Martin Jambor
10478270fe
ipa-sra: Treat REFERENCE_TYPES as always dereferencable
C++ and especially Fortran pass data by references which are not
pointers potentially pointing anywhere and so can be assumed to be
safely dereferencable.  This patch teaches IPA-SRA to treat them as
such and avoid the dance we do to prove that we can move loads from
them to the caller.

When we do not know that a dereference will happen all the time, we
need a heuristics so that we do not force memory accesses that normally
happen only rarely.  The patch simply uses the (possibly guessed)
profile and checks whether the (expected) number of loads is at least
half of function invocations invocations - the half is now
configurable with a param as requested by Honza.

gcc/ChangeLog:

2022-12-13  Martin Jambor  <mjambor@suse.cz>

	PR ipa/103585
	* params.opt (ipa-sra-deref-prob-threshold): New parameter.
	* doc/invoke.texi (ipa-sra-deref-prob-threshold): Document it.
	* ipa-sra.cc (struct gensum_param_access): New field load_count.
	(struct gensum_param_desc): New field safe_ref, adjusted comments.
	(by_ref_count): Renamed to unsafe_by_ref_count, adjusted all uses.
	(dump_gensum_access): Dump the new field.
	(dump_gensum_param_descriptor): Likewise.
	(create_parameter_descriptors): Set safe_ref field, move setting
	by_ref forward.  Only increment unsafe_by_ref_count for unsafe
	by_ref parameters.
	(allocate_access): Initialize new field.
	(mark_param_dereference): Adjust indentation.  Only add data to
	bb_dereferences for unsafe by_ref parameters.
	(scan_expr_access): For loads, accumulate BB counts.
	(dereference_probable_p): New function.
	(check_gensum_access): Fix leading comment, add parameter FUN.
	Check cumulative counts of loads for safe by_ref accesses instead
	of dereferences.
	(process_scan_results): Do not propagate dereference distances for
	safe by_ref parameters.  Pass fun to check_gensum_access.  Safe
	by_ref params do not need the postdominance check.

gcc/testsuite/ChangeLog:

2022-11-11  Martin Jambor  <mjambor@suse.cz>

	* g++.dg/ipa/ipa-sra-5.C: New test
2022-12-14 00:58:10 +01:00
Martin Jambor
095a13eda2
ipa-cp: Leave removal of unused parameters to IPA-SRA
Looking at some benchmarks I have noticed many cases when IPA-CP
cloned a function for all contexts just because it knew that some
parameters were not used at all.  Then IPA-SRA looked at the function
and cloned it again to split another parameter or two.  The latter
pass is better equipped to detect when parameters can be altogether
removed and so the IPA-CP cloning was for no good reason.

This patch simply alters the IPA-CP not to do that in the situations
where IPA-SRA can (for nodes which can be made local) with additional
dumping requested by Honza.

gcc/ChangeLog:

2022-12-13  Martin Jambor  <mjambor@suse.cz>

	* ipa-cp.cc (clone_for_param_removal_p): New function.
	(estimate_local_effects): Call it before considering cloning
	just to remove unused parameters.
2022-12-14 00:58:04 +01:00
Martin Jambor
4834e9360f
ipa: Better way of applying both IPA-CP and IPA-SRA (PR 103227)
This is basically a better fix for PR 103227.  The one currently in
use, rushed in late at stage3, which means that IPA-CP transformation
simply does a replacement of default-definition of IPA-SRA-created
scalar parameters with a constant, meant that IPA-SRA actually often
led to creation of a bunch of unused parameters, which was rather
ironic and sub-optimal.

This patch rips that old way out and makes sure the clash is resolved
at clone-materialization time.  What happens is that:

1) IPA-SRA IPA analysis (decision) stage recognizes the clash and does
   not create a param adjustment entry for such a scalar component.

2) Clone materialization code checks the IPA-CP transformation
   summary and when it realizes that it is removing a parameter that
   is a base for a discovered IPA-CP aggregate constant, and:

   a) the value is passed by reference, it internally records that any
      load of the value is replaced directly with the known constant
      value.  IPA-SRA will not attempt to split values passed by
      reference when there is a write to it so we know such a load
      won't be on a a LHS.

   b) the value is passed by value, there can be stores to the
      corresponding bit of the aggregate and so all accesses are
      replaced with a new decl and an assignment of the constant to
      this decl is generated at the beginning of the function.

The new testcase contains an xfail as the patch does not fix PR 107640
but it is one that ICEs when one is not careful about remapping
indices of parameters, so I'd like to have it in testsuite/gcc.gd/ipa/
even now.

I don't think that PR 107640 should be attempted through
ipa-param-manipulation replacements because the information is not
really there any more and we'd either need to do the replacements
earlier or dig deep into the clone parent info.  Instead, we should
record somewhere that at the beginning of the function the bits of the
global decl have known values and use that in the value numbering.
That way we could one day encode also known constants in globals that
do not come through parameters.

gcc/ChangeLog:

2022-11-11  Martin Jambor  <mjambor@suse.cz>

	PR ipa/103227
	* ipa-param-manipulation.h (class ipa_param_adjustments): Removed
	member function get_updated_index_or_split.
	(class ipa_param_body_adjustments): New overload of
	register_replacement, new member function append_init_stmts, new
	member m_split_agg_csts_inits.
	* ipa-param-manipulation.cc: Include ipa-prop.h.
	(ipa_param_adjustments::get_updated_index_or_split): Removed.
	(ipa_param_body_adjustments::register_replacement): New overload, use
	it from the older one.
	(ipa_param_body_adjustments::common_initialization): Added the
	capability to create replacements for conflicting IPA-CP discovered
	constants.
	(ipa_param_body_adjustments::ipa_param_body_adjustments): Construct
	the new member.
	(ipa_param_body_adjustments::append_init_stmts): New function.
	* ipa-sra.cc: Include ipa-prop.h.
	(push_param_adjustments_for_index): Require IPA-CP transformation
	summary as a parameter, do not create replacements which are known to
	have constant values.
	(process_isra_node_results): Find and pass to the above function the
	IPA-CP transformation summary.
	* ipa-prop.cc (adjust_agg_replacement_values): Remove the
	functionality replacing IPA-SRA created scalar parameters with
	constants.  Simplify, do not require parameter descriptors, do not
	return anything.
	(ipcp_transform_function): Simplify now that
	adjust_agg_replacement_values does not change cfg.  Move definition
	and initialization of descriptors lower.
	* tree-inline.cc (tree_function_versioning): Call append_init_stmts of
	param_body_adjs, if there are any.

gcc/testsuite/ChangeLog:

2022-11-11  Martin Jambor  <mjambor@suse.cz>

	PR ipa/103227
	PR ipa/107640
	* gcc.dg/ipa/pr107640-2.c: New test.
2022-12-14 00:57:56 +01:00
Martin Jambor
7450b25566
ipa-cp: Write transformation summaries of all functions
IPA-CP transformation summary streaming code currently won't stream
out transformations necessary for clones which are only necessary for
materialization of other clones (such as an IPA-CP clone which is then
cloned again by IPA-SRA).  However, a follow-up patch for bettor
reconciling IPA-SRA and IPA-CP modifications requires to have that
information at its disposal and so this one reworks the streaming to
write out all non-empty transformation summaries.

In order not to stream transformation summaies into partitions where
the node itself nor any of its clones are materialized, I had to make
sure that clones also get encode_body flag in the encoder (so that it
could be tested) and therefore in turn lto_output understands it needs
to skip clones.

This should actually mean less streaming in typical case because
previously we streamed three zeros for all nodes in a partition with
no useful information associated with them.  Currently we don't stream
anything for those.

When reworking the streaming, I also simplified it a little a
converted it writing to nicer C++ vector iterations.

gcc/ChangeLog:

2022-11-25  Martin Jambor  <mjambor@suse.cz>

	* ipa-prop.cc (useful_ipcp_transformation_info_p): New function.
	(write_ipcp_transformation_info): Added a parameter, simplified
	given that is known not to be NULL.
	(ipcp_write_transformation_summaries): Write out all useful
	transformation summaries.
	(read_ipcp_transformation_info): Simplify given that some info
	will be read.
	(read_replacements_section): Remove assert.
	* lto-cgraph.cc (add_node_to): Also set encode_body for clones.
	* lto-streamer-out.cc (lto_output): Do not output virtual clones.
2022-12-14 00:57:32 +01:00
Jakub Jelinek
37c2d99f3f i386: Fix up *concat*_{5,6,7} patterns [PR108044]
The following patch fixes 2 issues with the *concat<half><mode>3_5 and
*concat<mode><dwi>3_{6,7} patterns.
One is that if the destination is memory rather than register, then
we can't use movabsq and so can't support all the possible immediates.
I see 3 possibilities to fix that.  One would be to use
x86_64_hilo_int_operand predicate instead of const_scalar_int_operand
and thus not match it at all during combine in such cases, but that
unnecessarily pessimizes also the case when it is loaded into register
where we can use movabsq.
Another one is what is implemented in the patch, use Wd constraint
for the integer on 64-bit if destination is memory and n otherwise.
Yet another option would be to add match_scratch to the pattern and use
it with =X constraints except for the =o case for 64-bit non-Wd where it
would give a single DImode register (rather than 2).

Another thing is that if one half of the constant is
ix86_endbr_immediate_operand, then for -fcf-protection=branch we
force those constants into memory and that might not work properly
with -fpic.  So we should refuse to match with such constants.
OT, seems for movabsq we don't check that and happily allow the endbr
pattern in the immediate.

2022-12-13  Jakub Jelinek  <jakub@redhat.com>

	PR target/108044
	* config/i386/i386.md (*concat<half><mode>3_5, *concat<mode><dwi>3_6,
	*concat<mode><dwi>3_7): Split alternative with =ro output constraint
	into =r,o,o and use Wd input constraint for the last alternative which
	is enabled for TARGET_64BIT.  Reject ix86_endbr_immediate_operand
	in the input constant.

	* gcc.target/i386/pr108044-1.c: New test.
	* gcc.target/i386/pr108044-2.c: New test.
	* gcc.target/i386/pr108044-3.c: New test.
	* gcc.target/i386/pr108044-4.c: New test.
2022-12-13 22:16:34 +01:00
Jakub Jelinek
f82afb6976 libstdc++: Update backtrace-rename.h
When writing the r13-4629 commit log I've realized that libsanitizer
isn't the only place which nowadays renames libbacktrace symbols,
libstdc++ does that too.

2022-12-13  Jakub Jelinek  <jakub@redhat.com>

	* src/libbacktrace/backtrace-rename.h (backtrace_uncompress_zstd):
	Define.
2022-12-13 22:15:29 +01:00
Steve Kargl
531ca06c00 Fortran: NULL pointer dereference while parsing a function [PR107423]
gcc/fortran/ChangeLog:

	PR fortran/107423
	* parse.cc (parse_spec): Avoid NULL pointer dereference when parsing
	a function and an error occured.

gcc/testsuite/ChangeLog:

	PR fortran/107423
	* gfortran.dg/pr107423.f90: New test.
2022-12-13 19:08:23 +01:00
Tamar Christina
69ec1e2065 AArch64: Fix ILP32 tbranch
the baremetal builds are currently broken because the shift ends up in the wrong
representation if the mode is SImode and the shift amount if 31.   To fix this
create the rtx constant with an explicit mode so the backend passes know which
representation it needs to take.

gcc/ChangeLog:

	* config/aarch64/aarch64.md (tbranch_<code><mode>3): Use gen_int_mode.
2022-12-13 18:01:15 +00:00
Jakub Jelinek
33be3ee36a vect-patterns: Fix up vect_recog_rotate_pattern [PR108064]
Since vect_recog_rotate_pattern has been extended to work also
on signed types in r13-1100 we miscompile the testcase below.
vect_recog_rotate_pattern actually emits correct scalar code into
the pattern def sequence (in particular cast to utype, doing the
2 shifts in utype so that the right shift is logical and not arithmetic,
or and then cast back to the signed type), but it didn't supply vectype
for most of those pattern statements, which means that the generic handling
fills it up later with the vectype provided by vect_recog_rotate_pattern.
The problem is that it is vectype of the result of the whole pattern,
i.e. vector of signed values in this case, while the conversion to utype,
2 shifts and or (everything with utype lhs in scalar code) should have
uvectype as STMT_VINFO_VECTYPE.

2022-12-13  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/108064
	* tree-vect-patterns.cc (vect_recog_rotate_pattern): Pass uvectype
	as 4th argument to append_pattern_def_seq for statements with lhs
	with utype type.

	* gcc.c-torture/execute/pr108064.c: New test.
2022-12-13 16:55:21 +01:00
Richard Biener
8f4634fb82 tree-optimization/105801 - CCP and .DEFERRED_INIT
This makes sure we treat .DEFERRED_INIT as producing UNDEFINED so
we can continue optimizing uninitialized uses the same as without
-ftrivial-auto-var-init=zero.  For the testcase this means we
catch the return 1 optimization opportunity at CCP rather than
only at FRE which already does the right thing here.

	PR tree-optimization/105801
	* tree-ssa-ccp.cc (likely_value): .DEFERRED_INIT produces
	UNDEFINED.
	* doc/invoke.texi (ftrivial-auto-var-init): Explicitely
	mention we treat variables without an initializer as
	undefined also for optimization purposes.

	* gcc.dg/tree-ssa/ssa-ccp-43.c: New testcase.
2022-12-13 14:36:02 +01:00
Philip Herron
edc676cfe8 gccrs: Add README, CONTRIBUTING and compiler logo
We still need to write out a documentation section, but these READMEs will
help in the meantime.

	gcc/rust/
	* CONTRIBUTING.md: New.
	* README.md: New.
	* logo.png: New.
2022-12-13 14:00:08 +01:00
Philip Herron
a75f038c06 gccrs: Add config-lang.in
This was a copy/paste from gccgo front-end. We do not use any of the
target_libs yet, but we will need these when we support the libpanic crate.

	gcc/rust/
	* config-lang.in: New.
2022-12-13 14:00:08 +01:00
Arthur Cohen
b07ef39ffb gccrs: Add fatal_error when experimental flag is not present
This commits ports over the flag/env variable checking from #1540 which only
allows compilation using gccrs if a certain flag is specified

	gcc/rust/
	* lang.opt (-frust-incomplete-and-experimental-compiler-do-not-use):
	New.
	* rust-session-manager.cc (Session::compile_crate): Check it.
	* Make-lang.in (RUST_SELFTEST_FLAGS): Add it.
	gcc/testsuite/
	* lib/rust.exp (rust_init): Add
	'-frust-incomplete-and-experimental-compiler-do-not-use'.
2022-12-13 14:00:08 +01:00
Philip Herron
88415d33bb gccrs: Add GCC Rust front-end Make-lang.in
This is the Makefile for our front-end.

	gcc/rust/
	* Make-lang.in: New.
2022-12-13 14:00:07 +01:00
Philip Herron
5e7d199739 gccrs: Add lang.opt
We have some Rust-specific language options. Note that -fwrapv is
enabled by default in the code. We are trying to respect options such as
-Wunused-result which we get by porting over C++'s no-discard for Rust's
must-use attribute, so we have enabled these by default directly here.

	gcc/rust/
	* lang.opt: New.
2022-12-13 14:00:07 +01:00
Philip Herron
ab1e0db43c gccrs: Add lang-spec.h
This specifies the extensions of the Rust language.

	gcc/rust/
	* lang-specs.h: New.
2022-12-13 14:00:07 +01:00
Philip Herron
ea34614225 gccrs: Compiler proper interface kicks off the pipeline
This is a wrapper to get out of C land in the rust-lang.cc and into our
class hierarchy for the rust front-end. We expect that the front-end only
support one source file input as the expansion pass will attempt to resolve
that relative pass and parse accordingly.

The main missing piece here is that we are using saw_errors() to return
early which is unnecessary but as our error handling has been improving
over time we will start to take advantage of error node in our type system
as well as error_mark_node from GCC. The caveat being that our lints/checks
expect no errors and will throw an assertion.

	gcc/rust/
	* rust-lang.cc: New.
	* rust-session-manager.cc: New.
	* rust-session-manager.h: New.
2022-12-13 14:00:07 +01:00
Philip Herron
bba14a0790 gccrs: Add compiler driver
Our compiler driver is fairly simple so far. The key piece to enforce is
that a compilation unit in Rust is the whole crate, so the process for
compiling Rust means pointing the compiler at the main entry point such as
src/lib.rs or src/main.rs where the expansion pass takes over loading the
other source files to include them in the crate.

	gcc/rust/
	* rustspec.cc: New.
2022-12-13 14:00:07 +01:00
Philip Herron
fe6264fa28 gccrs: These are wrappers ported from reusing gccgo
The wrappers over linemap and location will eventually disappear here but
served as a useful starting point for us. We have wrappers over the
diagnostics system which we might be able to get rid of as well.

	gcc/rust/
	* rust-diagnostics.cc: New.
	* rust-diagnostics.h: New.
	* rust-gcc-diagnostics.cc: New.
	* rust-linemap.cc: New.
	* rust-linemap.h: New.
	* rust-location.h: New.
	* rust-system.h: New.
2022-12-13 14:00:07 +01:00
Philip Herron
cfbda2f78b gccrs: Add HIR to GCC GENERIC lowering entry point
This patch contains the entry point and utilities used for the lowering
of HIR nodes to `tree`s. It also contains a constant evaluator, ported
over from the C++ frontend.

	gcc/rust/
	* backend/rust-compile-context.cc: New.
	* backend/rust-compile-context.h: New.
	* backend/rust-compile.cc: New.
	* backend/rust-compile.h: New.
	* backend/rust-constexpr.cc: New.
	* backend/rust-constexpr.h: New.

Co-authored-by: David Faust <david.faust@oracle.com>
Co-authored-by: Faisal Abbas <90.abbasfaisal@gmail.com>
Signed-off-by: Faisal Abbas <90.abbasfaisal@gmail.com>
2022-12-13 14:00:07 +01:00
Philip Herron
019b2f1558 gccrs: Add HIR to GCC GENERIC lowering for all nodes
This patch implements the lowering mentioned in the previous patch for all HIR nodes.

	gcc/rust/
	* backend/rust-compile-block.cc: New.
	* backend/rust-compile-block.h: New.
	* backend/rust-compile-expr.cc: New.
	* backend/rust-compile-expr.h: New.
	* backend/rust-compile-extern.h: New.
	* backend/rust-compile-fnparam.cc: New.
	* backend/rust-compile-fnparam.h: New.
	* backend/rust-compile-implitem.cc: New.
	* backend/rust-compile-implitem.h: New.
	* backend/rust-compile-intrinsic.cc: New.
	* backend/rust-compile-intrinsic.h: New.
	* backend/rust-compile-item.cc: New.
	* backend/rust-compile-item.h: New.
	* backend/rust-compile-pattern.cc: New.
	* backend/rust-compile-pattern.h: New.
	* backend/rust-compile-resolve-path.cc: New.
	* backend/rust-compile-resolve-path.h: New.
	* backend/rust-compile-stmt.cc: New.
	* backend/rust-compile-stmt.h: New.
	* backend/rust-compile-struct-field-expr.cc: New.
	* backend/rust-compile-struct-field-expr.h: New.
	* backend/rust-compile-type.cc: New.
	* backend/rust-compile-type.h: New.
	* backend/rust-compile-var-decl.h: New.

Co-authored-by: David Faust <david.faust@oracle.com>
2022-12-13 14:00:07 +01:00
Philip Herron
15f04af347 gccrs: Add base for HIR to GCC GENERIC lowering
This pass walks the HIR crate and turns them into GCC `tree`s. We do not have
any Rust specific tree's. We are slowly removing the backend abstraction
which was ported over from gccgo in favour of using `tree`s directly.

	gcc/rust/
	* backend/rust-builtins.h: New.
	* backend/rust-compile-base.cc: New.
	* backend/rust-compile-base.h: New.
	* backend/rust-mangle.cc: New.
	* backend/rust-mangle.h: New.
	* backend/rust-tree.cc: New.
	* backend/rust-tree.h: New.
	* rust-backend.h: New.
	* rust-gcc.cc: New.

Co-authored-by: David Faust <david.faust@oracle.com>
2022-12-13 14:00:07 +01:00
Philip Herron
509e4c32c6 gccrs: Add metadata output pass
Extern crates statements to tell the front-end to look for another library.
The mechanism here is heavily inspired from gccgo, so when we compile a
library for example we invoke:

  gccrs -g -O2 -frust-crate=mylib -c src/lib.rs -o src/mylib.o

All going well this object file will now contain extra data inside
.rust-export section inside the object file which will be preserved inside
archives and shared objects. When we have another application which uses
this library 'mylib'.

  extern crate mylib;
  use mylib::foo;

  fn main() {
    foo();
  }

We compile using:

  gcc -g -O2 -frust-crate=test -c src/main.rs -o src/main.o

When the extern crate line is hit the front-end will look for mylib.o,
libmylib.a, mylib.rox. If it finds a raw object file it will read the
.rust-export section directly from the object for the public metadata
such as public functions, types constants etc. If it fails to find an
object it might find .rox which is the objdump of the .rust-export to a
raw file, it might even find libmylib.a and read the export directly out
of the archive file reusing code from gccgo to do so.

The full compiler pipeline is reused here, so the metatadata is actually
just real rust code. The benifit here is that Rust supports exporting,
macros and generics so this requires the name-resolution and type info
all to be generated and inserted into the apropriate context classes. Since
the metadata is real rust code it means we can reuse the full pipeline to
generate the code as necessary. So for the simple case of a public struct
we simply emit the AST dump of this struct directly into the metadata. If
its a non-generic public function we emit and extern rust abi block for
that function. If its a trait we can simply emit the trait with the public
memebers. Generics are more complicated since we need to emit the function
fully for it to be compiled correctly this still needs tests to be added.
The hardest part is non generic impl blocks which is still a WIP.

To finally link the two crates together you run:

  gcc -g -O2 -o rust-program.exe src/main.o src/mylib.o

	gcc/rust/
	* metadata/rust-export-metadata.cc: New.
	* metadata/rust-export-metadata.h: New.
	* metadata/rust-extern-crate.cc: New.
	* metadata/rust-extern-crate.h: New.
	* metadata/rust-import-archive.cc: New.
	* metadata/rust-imports.cc: New.
	* metadata/rust-imports.h: New.
	* rust-object-export.cc: New.
	* rust-object-export.h: New.
2022-12-13 14:00:06 +01:00
Philip Herron
4d67468d1d gccrs: Add unused variable scan
This is a simple walk_tree which acts on the monomorphized code by walking
the compiled translation unit of functions.

	gcc/rust/
	* checks/lints/rust-lint-unused-var.cc: New.
	* checks/lints/rust-lint-unused-var.h: New.
2022-12-13 14:00:06 +01:00
Thomas Young
520b52b24e gccrs: Add dead code scan on HIR
In order to find dead code we use a depth first search and keep liveness
variables, after type resolution. In this case, if a function is unused
and it calls another function the 2nd function is now unused since the
caller is not used etc. The algorithm is a depth first search.

	gcc/rust/
	* checks/lints/rust-lint-marklive-base.h: New.
	* checks/lints/rust-lint-marklive.cc: New.
	* checks/lints/rust-lint-marklive.h: New.
	* checks/lints/rust-lint-scan-deadcode.h: New.
2022-12-13 14:00:06 +01:00
Arthur Cohen
ca246e573f gccrs: Add privacy checks
This pass is responsible for resolving the privacy of items and verifying
that access to these items is performed within the limits of that privacy.
By default, items in Rust are private and only public to the current
module and its submodules. However, the user can annotate an item with
various qualifiers such as `pub` to publicly expose an item. Furthermore,
a module path can be given to `pub` to restrict an item's privacy to a
certain module: These paths need to be resolved and later on checked by
the privacy error reporter.

	gcc/rust/
	* checks/errors/privacy/rust-privacy-check.cc: New.
	* checks/errors/privacy/rust-privacy-check.h: New.
	* checks/errors/privacy/rust-privacy-common.h: New.
	* checks/errors/privacy/rust-privacy-ctx.cc: New.
	* checks/errors/privacy/rust-privacy-ctx.h: New.
	* checks/errors/privacy/rust-privacy-reporter.cc: New.
	* checks/errors/privacy/rust-privacy-reporter.h: New.
	* checks/errors/privacy/rust-pub-restricted-visitor.cc: New.
	* checks/errors/privacy/rust-pub-restricted-visitor.h: New.
	* checks/errors/privacy/rust-reachability.cc: New.
	* checks/errors/privacy/rust-reachability.h: New.
	* checks/errors/privacy/rust-visibility-resolver.cc: New.
	* checks/errors/privacy/rust-visibility-resolver.h: New.
2022-12-13 14:00:06 +01:00
Arthur Cohen
5215235f01 gccrs: Add const checker
Similarly to the unsafe checker, constant evaluation can only be performed
in a few contexts and include restrictions on the Rust language. Should
the user fail to uphold those conditions, errors will be reported and the
compilation pipeline interrupted.

These contexts are as follow:

- Array type length expressions
- Array repeat length expressions
- Constants
- Statics
- Enum discriminants
- Const generic arguments

In these contexts, the user is restricted to calling only functions marked
as `const` or perform arithmetic operations only on certain types, among
other restrictions.

	gcc/rust/
	* checks/errors/rust-const-checker.cc: New.
	* checks/errors/rust-const-checker.h: New.
2022-12-13 14:00:06 +01:00
Arthur Cohen
b1b35204d8 gccrs: Add unsafe checks for Rust
The UnsafeChecker visitor verifies that unsafe actions are only performed
in unsafe contexts. Otherwise, an error should be reported to the user and
the compilation pipeline should be halted. These contexts, which include
unsafe blocks or unsafe functions, are allowed to perform more actions
than regular safe Rust code. These actions currently include:

- Dereferencing raw pointers
- Calls to unsafe functions
- Use of inline assembly
- Use of mutable static
- Use of extern static
- Access to a union's field
- Call to functions with #[target(feature)] attribute
- Initializing type with rustc_layout_scalar_valid_range attribute
- Mutation of layout constrained field
- Borrow of layout constrained field

	gcc/rust/
	* checks/errors/rust-unsafe-checker.cc: New.
	* checks/errors/rust-unsafe-checker.h: New.
2022-12-13 14:00:06 +01:00
Philip Herron
06688fe40a gccrs: Add remaining type system transformations
This patch implements multiple transformation performed on the HIR
during type-resolution such as type coercion, casts, auto-dereferencement.

	gcc/rust/
	* typecheck/rust-autoderef.cc: New.
	* typecheck/rust-autoderef.h: New.
	* typecheck/rust-casts.cc: New.
	* typecheck/rust-casts.h: New.
	* typecheck/rust-coercion.cc: New.
	* typecheck/rust-coercion.h: New.
	* typecheck/rust-hir-dot-operator.cc: New.
	* typecheck/rust-hir-dot-operator.h: New.
	* typecheck/rust-hir-inherent-impl-overlap.h: New.
	* typecheck/rust-hir-path-probe.h: New.
	* typecheck/rust-hir-trait-ref.h: New.
	* typecheck/rust-hir-type-bounds.h: New.
	* typecheck/rust-substitution-mapper.cc: New.
	* typecheck/rust-substitution-mapper.h: New.
	* typecheck/rust-tycheck-dump.h: New.
	* typecheck/rust-tyctx.cc: New.
	* typecheck/rust-tyty-bounds.cc: New.
	* typecheck/rust-tyty-call.cc: New.
	* typecheck/rust-tyty-call.h: New.
	* typecheck/rust-tyty-cmp.h: New.
	* typecheck/rust-tyty-rules.h: New.
2022-12-13 14:00:06 +01:00
Philip Herron
24393cb68f gccrs: Add Rust type information
Contains abstractions over Rust's types, used when performing the
HIR's type-resolution.

	gcc/rust/
	* typecheck/rust-tyty.cc: New.
	* typecheck/rust-tyty.h: New.
2022-12-13 14:00:06 +01:00
Philip Herron
c6c3db2176 gccrs: Add type resolution and trait solving pass
This serves to handle parts of the Rust type-system. Namely, the type
resolution (similar to type-checking) and the trait solving algorithms
(which ensure Rust's type contracts are upheld throughout the codebase).

	gcc/rust/
	* typecheck/rust-hir-trait-resolve.cc: New.
	* typecheck/rust-hir-trait-resolve.h: New.
	* typecheck/rust-hir-type-check-base.cc: New.
	* typecheck/rust-hir-type-check-base.h: New.
	* typecheck/rust-hir-type-check-enumitem.cc: New.
	* typecheck/rust-hir-type-check-enumitem.h: New.
	* typecheck/rust-hir-type-check-expr.cc: New.
	* typecheck/rust-hir-type-check-expr.h: New.
	* typecheck/rust-hir-type-check-implitem.cc: New.
	* typecheck/rust-hir-type-check-implitem.h: New.
	* typecheck/rust-hir-type-check-item.cc: New.
	* typecheck/rust-hir-type-check-item.h: New.
	* typecheck/rust-hir-type-check-path.cc: New.
	* typecheck/rust-hir-type-check-pattern.cc: New.
	* typecheck/rust-hir-type-check-pattern.h: New.
	* typecheck/rust-hir-type-check-stmt.cc: New.
	* typecheck/rust-hir-type-check-stmt.h: New.
	* typecheck/rust-hir-type-check-struct-field.h: New.
	* typecheck/rust-hir-type-check-struct.cc: New.
	* typecheck/rust-hir-type-check-toplevel.cc: New.
	* typecheck/rust-hir-type-check-toplevel.h: New.
	* typecheck/rust-hir-type-check-type.cc: New.
	* typecheck/rust-hir-type-check-type.h: New.
	* typecheck/rust-hir-type-check-util.cc: New.
	* typecheck/rust-hir-type-check-util.h: New.
	* typecheck/rust-hir-type-check.cc: New.
	* typecheck/rust-hir-type-check.h: New.
	* typecheck/rust-tyty-visitor.h: New.
2022-12-13 14:00:06 +01:00
Philip Herron
9ce37e7206 gccrs: Add helpers mappings canonical path and lang items
These are various helper classes used in the compiler pipeline.

	gcc/rust/
	* util/rust-canonical-path.h: New.
	* util/rust-common.h: New.
	* util/rust-hir-map.cc: New.
	* util/rust-hir-map.h: New.
	* util/rust-identifier.h: New.
	* util/rust-lang-item.h: New.
	* util/rust-mapping-common.h: New.
	* util/rust-stacked-contexts.h: New.
2022-12-13 14:00:05 +01:00
Arthur Cohen
2e7fc8780e gccrs: Add attributes checker
The attribute checker is responsible for checking the validity of various
attributes including built-in ones. It is currently unfinished and will
receive some modifications, as well as become the host of some existing
code in the compiler which needs to be refactored. One of its
responsibilities is to make sure that arguments given to built-in
attributes are correct, or contain the correct type of information. This
visitor also checks that an attribute is allowed to be used in the current
particular context.

	gcc/rust/
	* util/rust-attributes.cc: New.
	* util/rust-attributes.h: New.
2022-12-13 14:00:05 +01:00
Arthur Cohen
9a4fee5f57 gccrs: Add implementation of Optional
This adds an Optional<T> class to improve error handling.

	gcc/rust/
	* util/rust-optional-test.cc: New.
	* util/rust-optional.h: New.
2022-12-13 14:00:05 +01:00
Arthur Cohen
eb10bc5225 gccrs: Add Base62 implementation
Used for V0 symbol mangling scheme which is still in development.

	gcc/rust/
	* util/rust-base62.cc: New.
	* util/rust-base62.h: New.
2022-12-13 14:00:05 +01:00
Philip Herron
15b0278905 gccrs: Add Rust ABI enum helpers
This is a simple helper over an enum of possible ABI options in Rust.

	gcc/rust/
	* util/rust-abi.cc: New.
	* util/rust-abi.h: New.
2022-12-13 14:00:05 +01:00
Philip Herron
c7f8347e83 gccrs: Add port of FNV hash used during legacy symbol mangling
This hash was ported from the Go runtime, as we needed a hash for the legacy
symbol mangling system. All symbols in Rust contain a hash of some
metadata for uniqueness on generic functions.

	gcc/rust/
	* util/fnv-hash.h: New.
2022-12-13 14:00:05 +01:00
Philip Herron
b32b1b1576 gccrs: Add wrapper for make_unique
This is a wrapper for make_unique. We can likely get rid of this, as there
are other implementations available, or simply keep using the unique_ptr
constructor.

	gcc/rust/
	* util/rust-make-unique.h: New.
2022-12-13 14:00:05 +01:00
Philip Herron
7999cf327d gccrs: Add AST to HIR lowering pass
This performs the lowering of the AST to HIR. The interesting piece here is
that we desugar much of the AST as mentioned in the previous pass, but
crucially, we also strip out all code that is "marked-for-strip" which failed
cfg-expansion from the expansion pass. After this, the HIR includes all code
required to compile for this crate.

	gcc/rust/
	* hir/rust-ast-lower-base.cc: New.
	* hir/rust-ast-lower-base.h: New.
	* hir/rust-ast-lower-block.h: New.
	* hir/rust-ast-lower-enumitem.h: New.
	* hir/rust-ast-lower-expr.h: New.
	* hir/rust-ast-lower-extern.h: New.
	* hir/rust-ast-lower-implitem.h: New.
	* hir/rust-ast-lower-item.cc: New.
	* hir/rust-ast-lower-item.h: New.
	* hir/rust-ast-lower-pattern.cc: New.
	* hir/rust-ast-lower-pattern.h: New.
	* hir/rust-ast-lower-stmt.h: New.
	* hir/rust-ast-lower-struct-field-expr.h: New.
	* hir/rust-ast-lower-type.h: New.
	* hir/rust-ast-lower.cc: New.
	* hir/rust-ast-lower.h: New.
	* hir/rust-hir-dump.cc: New.
	* hir/rust-hir-dump.h: New.
2022-12-13 14:00:05 +01:00
Philip Herron
7641eaead4 gccrs: Add HIR definitions and visitor framework
This patch implements the classes mentioned in the previous HIR patch,
as well as a set of visitor frameworks used in handling that HIR.

	gcc/rust/
	* hir/tree/rust-hir-full-decls.h: New.
	* hir/tree/rust-hir-full-test.cc: New.
	* hir/tree/rust-hir-full.h: New.
	* hir/tree/rust-hir-visitor.h: New.
	* hir/tree/rust-hir.h: New.
2022-12-13 14:00:04 +01:00
Philip Herron
8ad1d56d68 gccrs: Add declarations for Rust HIR
This patch contains the declarations needed for our second intermediate
representation, which we will refer to as an HIR.

This gives the front-end a chance to desugar much of the AST, such as:
- Removing distinction between functions and methods
- Removing Macros
- Removing IdentifierExprs
- Removing duplicate attribute structures

	gcc/rust/
	* hir/tree/rust-hir-expr.h: New.
	* hir/tree/rust-hir-item.h: New.
	* hir/tree/rust-hir-path.h: New.
	* hir/tree/rust-hir-pattern.h: New.
	* hir/tree/rust-hir-stmt.h: New.
	* hir/tree/rust-hir-type.h: New.
2022-12-13 14:00:04 +01:00
Philip Herron
85a8fe00f8 gccrs: Add name resolution pass to the Rust front-end
The name resolution is split into two phases, one toplevel pass which scans
the whole "Crate" which iterates all items and nested items in modules to
generate a context class full of CanonicalPath items. It also generates
a hierarchy of parent->child and child->parent relationships using the AST
NodeId for PathResolution in the second phase.

The second phase drills into each item like functions and creates a stack
of canonical paths for variables etc so that we can store information in
a side table of usage variable 'a' resolves to NodeId '123' which refers
to the NodeId of the "let a;" statement.

	gcc/rust/
	* resolve/rust-ast-resolve-base.cc: New.
	* resolve/rust-ast-resolve-base.h: New.
	* resolve/rust-ast-resolve-expr.cc: New.
	* resolve/rust-ast-resolve-expr.h: New.
	* resolve/rust-ast-resolve-implitem.h: New.
	* resolve/rust-ast-resolve-item.cc: New.
	* resolve/rust-ast-resolve-item.h: New.
	* resolve/rust-ast-resolve-path.cc: New.
	* resolve/rust-ast-resolve-path.h: New.
	* resolve/rust-ast-resolve-pattern.cc: New.
	* resolve/rust-ast-resolve-pattern.h: New.
	* resolve/rust-ast-resolve-stmt.cc: New.
	* resolve/rust-ast-resolve-stmt.h: New.
	* resolve/rust-ast-resolve-struct-expr-field.cc: New.
	* resolve/rust-ast-resolve-struct-expr-field.h: New.
	* resolve/rust-ast-resolve-toplevel.h: New.
	* resolve/rust-ast-resolve-type.cc: New.
	* resolve/rust-ast-resolve-type.h: New.
	* resolve/rust-ast-resolve.cc: New.
	* resolve/rust-ast-resolve.h: New.
	* resolve/rust-ast-verify-assignee.h: New.
	* resolve/rust-name-resolver.cc: New.
	* resolve/rust-name-resolver.h: New.
2022-12-13 14:00:04 +01:00
Arthur Cohen
1841081a8a gccrs: Add expansion pass for the Rust front-end
The expansion pass is responsible for two actions on our AST:

1. Expanding macro calls
2. Performing conditional compilation

Calls to macros should be checked and expanded into an AST fragment based on
the context they've been called in. This is similar to token substitution, with
a lot of intricacies and checks being performed. A single invocation can result
in an AST fragment containing multiple statements or multiple expressions,
which need to be handled as well. Furthermore, Rust macros can contain
repetitions relying on Kleine operators, similar to regular expression
patterns, that also need to be expanded properly.

Finally, Rust code can be hidden behind `cfg` directives, which allow the user
to perform conditional compilation. If a `cfg` predicate is not met, the
expression or statement it refers to should be marked for strip and removed
from the AST.

	gcc/rust/
	* expand/rust-attribute-visitor.cc: New.
	* expand/rust-attribute-visitor.h: New.
	* expand/rust-macro-builtins.cc: New.
	* expand/rust-macro-builtins.h: New.
	* expand/rust-macro-expand.cc: New.
	* expand/rust-macro-expand.h: New.
	* expand/rust-macro-invoc-lexer.cc: New.
	* expand/rust-macro-invoc-lexer.h: New.
	* expand/rust-macro-substitute-ctx.cc: New.
	* expand/rust-macro-substitute-ctx.h: New.

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Co-authored-by: Joel Phillips <simplytheother@gmail.com>
Signed-off-by: Joel Phillips <simplytheother@gmail.com>
2022-12-13 14:00:04 +01:00
Joel Phillips
32c8fb0eea gccrs: Add Parser for Rust front-end pt.2
This patch contains the second half of the templated Rust parser.

	gcc/rust/
	* parse/rust-parse-impl.h: New, second half.

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>

Signed-off-by: Joel Phillips <simplytheother@gmail.com>
2022-12-13 14:00:04 +01:00
Joel Phillips
35e4f3b4af gccrs: Add Parser for Rust front-end pt.1
This is a Pratt-style parser for Rust that implements all of the AST. The
rust-parser-impl.h is the implementation of the parser as a template,
allowing it to be given ManagedTokenSource and avoid virtual calls. The
downside is it takes time to compile when used.

see: https://en.wikipedia.org/wiki/Operator-precedence_parser#Pratt_parsing

This patch contains the first half of the templated parser, so as to not
lose patches in the mailing list archives.

	gcc/rust/
	* parse/rust-cfg-parser.cc: New.
	* parse/rust-cfg-parser.h: New.
	* parse/rust-parse-impl.h: New.
	* parse/rust-parse.cc: New.
	* parse/rust-parse.h: New.

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>

Signed-off-by: Joel Phillips <simplytheother@gmail.com>
2022-12-13 14:00:04 +01:00
Joel Phillips
18f6990f84 gccrs: Add Lexer for Rust front-end
The lexer is referred to as a ManagedTokenSource within the parser. This
lexer does not currently support Unicode, but serves as a starting point
to do so.

	gcc/rust/
	* lex/rust-codepoint.h: New.
	* lex/rust-lex.cc: New.
	* lex/rust-lex.h: New.
	* lex/rust-token.cc: New.
	* lex/rust-token.h: New.
	* rust-buffered-queue.h: New.

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Co-authored-by: Mark Wielaard <mark@klomp.org>

Signed-off-by: Joel Phillips <simplytheother@gmail.com>
2022-12-13 14:00:04 +01:00
Joel Phillips
5b981e9c74 gccrs: Add Rust AST visitors
This patch contains the basic framework of our AST visitors, as well as
one aimed at pretty-printing and exporting these AST nodes.

	gcc/rust/
	* ast/rust-ast-dump.cc: New.
	* ast/rust-ast-dump.h: New.
	* ast/rust-ast-visitor.h: New.
	* ast/rust-cond-compilation.h: New.

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>

Signed-off-by: Joel Phillips <simplytheother@gmail.com>
2022-12-13 14:00:04 +01:00
Joel Phillips
d588754c82 gccrs: Add full definitions of Rust AST data structures
This adds the proper definitions of our AST nodes split across multiple
files for clarity

	gcc/rust/
	* ast/rust-expr.h: New.
	* ast/rust-macro.h: New.
	* ast/rust-path.h: New.
	* ast/rust-pattern.h: New.
	* ast/rust-stmt.h: New.
	* ast/rust-type.h: New.

Co-authored-by: Philip Herron <philip.herron@embecosm.com>

Signed-off-by: Joel Phillips <simplytheother@gmail.com>
2022-12-13 14:00:04 +01:00